//write the div that will be used
document.write('<div id="contactdiv" style="display:none;position:absolute;width:400px;height:250px;top:50%;left:50%;margin-top:-125px;margin-left:-200px;background-color:#FFFFFF;border:2px solid black;">');
document.write('<table width="100%" border=0>');
document.write('<tr><td width="20%" style="font-size:12px;"><b>First Name:</b></td>');
document.write('<td width="80%"><input type=text name=ct_name id=ct_name style="width:96%;"></td>');
document.write('</tr>');
document.write('<tr><td width="20%" style="font-size:12px;"><b>Your Email:</b></td>');
document.write('<td width="80%"><input type=text name=ct_email id=ct_email style="width:96%;"></td>');
document.write('</tr>');
document.write('<tr><td width="20%" style="font-size:12px;"><b>Your Questions:</b></td>');
document.write('<td width="80%"><textarea name=ct_ques id=ct_ques rows=5 style="width:96%;"></textarea></td>');
document.write('</tr>');
document.write('<tr><td width="100%" colspan=2 align=center style="font-size:12px;"><br><br><input type=submit id="ct_sendbut" value="Send" onClick="return goSendNow();">');
document.write('&nbsp;&nbsp;&nbsp;<input type=submit value="Close" onClick="return doClose();"></td>');
document.write('</tr>');
document.write('</table>');
document.write('</div>');

//functions
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      xmlhttp.overrideMimeType('text/xml');
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject();

function sendIt(){
	//position centered in screen		
	var margin = -125;
	if (typeof window.pageYOffset != 'undefined'){
		margin += window.pageYOffset;
	}else if (document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop != 0){
	    margin += document.documentElement.scrollTop;   
	}else if (typeof document.body.scrollTop != 'undefined'){
	   margin += document.body.scrollTop;
	}
	
	var cdiv = document.getElementById("contactdiv");
		
	cdiv.style.marginTop = margin +"px";

	//display 
	cdiv.style.display = 'block';
	return false;
}

function doClose(){
	document.getElementById("contactdiv").style.display= 'none';
	return false;
}

function goSendNow(){
	var RE_email = /^[0-9A-Za-z._-]{1,}@[0-9A-Za-z._-]{1,}$/;
	var RE_txt   = /\S+/;
	var n = document.getElementById("ct_name").value;
	var e = document.getElementById("ct_email").value;
	var q = document.getElementById("ct_ques").value;
	if (! RE_txt.test(n)){
		alert("Please enter your first name");
		document.getElementById("ct_name").focus();
		return false;
	}
	if (! RE_email.test(e)){
		alert("Please enter a valid email address");
		document.getElementById("ct_email").focus();
		return false;
	}
	if (! RE_txt.test(q)){
		alert("Please enter your questions in the box");
		document.getElementById("ct_ques").focus();
		return false;
	}
	document.getElementById("ct_sendbut").value = "Sending...";	
	var rand= new Date().getTime();		
	var url = 'sendct.php';
	var params = 'rnd='+rand+'&n='+escape(n)+'&e='+escape(e)+'&q='+escape(q);
	http.open("POST", url, true);
  	http.onreadystatechange = handleHttpResponseContact;
  	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  			
  	http.send(params);
  	return false;
}

function handleHttpResponseContact(){
	if (http.readyState == 4){
		document.getElementById("ct_sendbut").value = "Send";
		if (http.responseText == "1"){
			alert("Your message has been sent!");
		}else{
			alert("An error ocurred when trying to send your message, please try again!");
		}
		doClose();
	}
}
