function checkfields()
{
	if (document.theform.name.value=="" || document.theform.email.value=="" || document.theform.query.value=="")
	{
		alert ("Not all fields were filled by you. Please go back and re enter all information.");
		return false;
	}
	else if (!echeck(document.theform.email.value))
	{
		  return false;
	}
	else
	{
			processcontact(document.theform.name.value,document.theform.email.value,document.theform.query.value);
	
	}
}

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e) { }
		}
	}
	if (!xmlHttp)
		alert("Error Creating the XML HttpRequest Object");
		else
			return xmlHttp;
}

function processcontact(name,email,query)
{
	document.getElementById('contactform').innerHTML='<p>Sending your request...</p>';
	if (xmlHttp)
	{
		try
		{
			
			var divelement = "contactform";
				xmlHttp.open("GET", '/site/processors/processContact.php?name='+name+'&email='+email+'&query='+query, true);
		
			xmlHttp.onreadystatechange = 
			
			function handleRequestStateChange()
{
	
	myDiv = document.getElementById(divelement);
	if (xmlHttp.readyState == 1)
	{
		myDiv.innerHTML = "<p>Sending your request...</p>";
	}
	else if (xmlHttp.readyState==2)
	{
		myDiv.innerHTML = "<p>Sending your request...</p>";

	}
	else if (xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			try
			{

				var response = xmlHttp.responseText;
				myDiv.innerHTML = ""+response+"";
				
			}
			catch(e)
			{
				alert("Error Sending Mail");
			}
		}
		else
		{
		alert("There was a problem retrieving content:\n" + xmlHttp.statusText);
		}
	}


}
			
			xmlHttp.send(null);
		}
		catch(e)
		{
			alert ("Can't connect to server:\n" + e.toString());
		}
	}
}
function echeck(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address Format")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address Format")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address Format")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address Format")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address Format")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address Format")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address Format")
		    return false
		 }

 		 return true					
}
