/*
FLE

-INHOUSE SOLUTIONS
-Ryan
*/
var nam = "ContactUs";
var dd;
var cName = "mandq";

var frame_delay = 10;

var target_top = 500; //the distance to travel

var pixel_travel = 5; //the distance of each frame

var ease = 0; //the easing amount (milisec)

var trace = 0;

var top_up = -375;

//DETECT BROWSER
var p = 0;
function aylert(msg)
{
	if(p == 0)
	{
		alert(msg);
		p = 1;
	}
}
function buildQuoter()
{
	dd = document.createElement("div");
	dd.id = nam;
	dd.className = cName;
	dd.style.top = top_up+"px";
	dd.style.width = "335px";
	dd.innerHTML = "<p align='center' class='"+cName+"_title'>Contact Us Online</p><p>Complete your details below and an experienced consultant will contact you personally.</p><form id='f1'><table width='100%'>	<tr>		<td>First Name:</td><td><input type='text' id='pfname' name='pfname'></td>	</tr>	<tr>		<td>Last Name:</td><td><input type='text' id='psname' name='psname'></td>	</tr>	<tr>		<td>Telephone:</td><td><input type='text' id='phone' name='phone'></td>	</tr><tr>		<td>Email Address:</td><td><input type='text' id='pemail' name='pemail'></td>	</tr>	<tr>		<td>Message:</td><td><textarea id='pmessage' name='pmessage' cols='20' rows='4'></textarea></td>	</tr>	<tr>		<td colspan='2'><input type='button' value='Submit' id='sub' onclick='doPost();'><input type='button' id='goaway' name='goaway' value='Cancel' onclick='closeMe();'></td>	</tr></table></form><p>Your complete satisfaction—all around the world - is our mission.</p>";
	
	
}

function figureOutCentre()
{
	var pagewidth = document.body.clientWidth;
	var divwidth = dd.style.width;
	x = divwidth.replace("px","");
	divwidth = parseInt(x,10);
	//var lpos = (pagewidth / 2)-divwidth;
	var lpos = (pagewidth / 2)-(divwidth/2);
	dd.style.left = lpos+"px";
}

function pixelAdd(n)
{
	x = dd.style.top;
	x = x.replace("px","");
	var t_t;
	if(x.substring(0,1)=="-")
	{
		var tuneg = x.replace("-","");
		var xt = parseInt(tuneg,10); 
		t_t = 0 - xt;
	}
	else
	{
		t_t = parseInt(x,10);
	}
	t_t += n;
	dd.style.top = t_t + "px";
}

function pixelSub(n)
{
	x = dd.style.top;
	x = x.replace("px","");
	var t_t;
	if(x.substring(0,1)=="-")
	{
		var tuneg = x.replace("-","");
		var xt = parseInt(tuneg,10); 
		t_t = 0 - xt;
	}
	else
	{
		t_t = parseInt(x,10);
	}
	t_t -= n;
	dd.style.top = t_t + "px";
}

function show()
{
	figureOutCentre();
	document.body.appendChild(dd);	
	slideIn();
}


function slideIn()
{
	if(trace < target_top)
	{
		setTimeout("f_n()",frame_delay);	
		frame_delay += ease;
	}
}

function slideOut()
{
	if(trace >= top_up)
	{
		setTimeout("f_o()",frame_delay);	
		frame_delay += ease;
	}
}


function f_n()
{
	//dd.style.pixelTop += 5;
	pixelAdd(pixel_travel);
	trace += pixel_travel;
	relay("in");
}	

function f_o()
{
	pixelSub(pixel_travel);
	trace -= pixel_travel;
	relay("out");
}	


function relay(way)
{
	if(way == "in")
	{
		if(trace < target_top)
		{
			setTimeout("slideIn()",frame_delay);	
		}
	}
	if(way == "out")
	{
		if(trace >= top_up)
		{
			setTimeout("slideOut()",frame_delay);	
		}
	}
}


function doPost()
{
	var fname = document.getElementById("pfname").value;
	var sname = document.getElementById("psname").value;
	var email = document.getElementById("pemail").value;
	var phone = document.getElementById("phone").value;
	var message = document.getElementById("pmessage").value;
	
	var posty = dojo.io.bind({
							 url		:	"/aspmailer.asp",
							 mimetype	:   "text/javascript",
							 method		:	"POST",
							 content	:	{"pfname"  :  fname,
							 				 "psname"  :  sname,
											 "pemail"  :  email,
											 "phone"   :  phone,
											 "pmessage":  message},
							 error		:   function(type, errObj){
       							 dd.innerHTML = errObj.message;
    						 },
							 load		:  function(type, data, evt){
        						eval(data);
    						 }					 
							 });
	
}

function closeMe()
{
	document.body.removeChild(dd);
	dd = null;
	trace = 0;
	makeCookie();
	buildQuoter();	
}

function clickIn()
{
	//THIS FUNCTION IS SPECIAL BECAUSE THE OFFSETS CHANGE!
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	var dsocleft=document.all? iebody.scrollLeft : pageXOffset
	var dsoctop=document.all? iebody.scrollTop : pageYOffset	
	
	
	//TEMP VARS
	t_target_top = target_top;
	t_top_up = top_up;
	t_frame_delay = frame_delay;
	t_pixel_travel = pixel_travel;
	////TEMP VARS
	
	target_top = target_top + dsoctop; // Makes sure a click brings it into view.
	//aylert(target_top);
	top_up = top_up - dsoctop; //Makes sure it moves away fully.
	frame_delay = 1;
	pixel_travel = 200;
	
	if(document.getElementById(nam))
	{
		document.body.removeChild(dd);
		trace = 0;
		dd=null;
		buildQuoter();	
	
		target_top = target_top + dsoctop; // Makes sure a click brings it into view.
		top_up = top_up - dsoctop; //Makes sure it moves away fully.
		frame_delay = 1;
		pixel_travel = 200;
		
	}
	
	show();
	
	
	//RESET VARS
	//target_top = t_target_top;
	//top_up = t_top_up;
	//frame_delay = t_frame_delay;
	////RESET VARS
}
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
  
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function cookieCheck()
{
	return getCookie(nam);
}

function makeCookie()
{
	setCookie(nam,"STOP ASKING ME FOR MY DETAILS, PLEASE.");
}
buildQuoter();
