var xmlHttp = createXmlHttpObject();

function ContactUs()
{

	var submitstring = '';
	var returnval = 0;
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var phoneNumberPattern = /^\(?(\d{3})\)?[- \.]?(\d{3})[- \.]?(\d{4})$/;

	if (document.getElementById("Name").value == '')
	{
		document.getElementById("NameLeft").className = "error";
		returnval = 1;
	} 
	else
	{
		submitstring = "Name=" + document.getElementById("Name").value + "&";
	}	

	if (document.getElementById("Email").value == '' || !emailPattern.test(document.getElementById("Email").value))
	{
		document.getElementById("EmailLeft").className = "error";
		returnval = 1;
	} 
	else
	{
		submitstring = submitstring + "Email=" + document.getElementById("Email").value + "&";
	}	

	if (document.getElementById("Phone").value == '' || !phoneNumberPattern.test(document.getElementById("Phone").value))
	{
		document.getElementById("PhoneLeft").className = "error";
		returnval = 1;
	} 
	else
	{
		submitstring = submitstring + "Phone=" + document.getElementById("Phone").value + "&";
	}	

	if (returnval == 1) 
	{
		return;
	}

	submitstring = submitstring + "Company=" + document.getElementById("Company").value + "&";
	submitstring = submitstring + "Address1=" + document.getElementById("Address1").value + "&";
	submitstring = submitstring + "Address2=" + document.getElementById("Address2").value + "&";
	submitstring = submitstring + "City=" + document.getElementById("City").value + "&";
	submitstring = submitstring + "State=" + document.getElementById("State").value + "&";
	submitstring = submitstring + "ZipCode=" + document.getElementById("ZipCode").value + "&";
	submitstring = submitstring + "HowDidYouHearAboutUs=" + document.getElementById("HowDidYouHearAboutUs").value + "&";

	if (document.contactus.Billing.checked)
	{
		submitstring = submitstring + "Billing=" + document.getElementById("Billing").value + "&";
	}
	if (document.contactus.ResellerPrograms.checked)
	{
		submitstring = submitstring + "ResellerPrograms=" + document.getElementById("ResellerPrograms").value + "&";
	}
	if (document.contactus.Colocation.checked)
	{
		submitstring = submitstring + "Colocation=" + document.getElementById("Colocation").value + "&";
	}
	if (document.contactus.TechnicalSupport.checked)
	{
		submitstring = submitstring + "TechnicalSupport=" + document.getElementById("TechnicalSupport").value + "&";
	}
	if (document.contactus.DialupAccess.checked)
	{
		submitstring = submitstring + "DialupAccess=" + document.getElementById("DialupAccess").value + "&";
	}
	if (document.contactus.TelephoneVOIPSystems.checked)
	{
		submitstring = submitstring + "TelephoneVOIPSystems=" + document.getElementById("TelephoneVOIPSystems").value + "&";
	}
	if (document.contactus.HighSpeedAccess.checked)
	{
		submitstring = submitstring + "HighSpeedAccess=" + document.getElementById("HighSpeedAccess").value + "&";
	}
	if (document.contactus.WebHosting.checked)
	{
		submitstring = submitstring + "WebHosting=" + document.getElementById("WebHosting").value + "&";
	}
	if (document.contactus.Xecutechservices.checked)
	{
		submitstring = submitstring + "Xecutechservices=" + document.getElementById("Xecutechservices").value + "&";
	}
	if (document.contactus.RemoteBackup.checked)
	{
		submitstring = submitstring + "RemoteBackup=" + document.getElementById("RemoteBackup").value + "&";
	}
	if (document.contactus.CableManufacturing.checked)
	{
		submitstring = submitstring + "CableManufacturing=" + document.getElementById("CableManufacturing").value + "&";
	}
	if (document.contactus.Other.checked)
	{
		submitstring = submitstring + "Other=" + document.getElementById("Other").value + "&";
	}
	submitstring = submitstring + "Other_value=" + document.getElementById("Other_value").value + "&";
	submitstring = submitstring + "CommentsQuestions=" + document.getElementById("CommentsQuestions").value + "&";

	if (document.contactus.Residential.checked)
	{
		submitstring = submitstring + "EnquiryType=Residential&";
	}
	if (document.contactus.Business.checked)
	{
		submitstring = submitstring + "EnquiryType=Business&";
	}

	if (submitstring)
	{
		data = submitstring;
		if (xmlHttp)
		{
			try
			{ 
				document.body.style.cursor = "wait";
				xmlHttp.open("POST", "/proc/contactus.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleCU;
				xmlHttp.send(data);
			}
			catch(e)
			{
				alert("Failed to connect to server: " + e.toString());
			}
		}
	}
}


function createXmlHttpObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e) { }
	}
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

function handleCU()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			try
			{
				handleCUResponse();
			}
			catch(e)
			{
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			alert("There was a problem retrieving the data: " + xmlHttp.statusText);
		}
	}
}

function handleCUResponse()
{
	response = xmlHttp.responseText;

	target = document.getElementById("contactform");
	if (response == 0)
	{
		target.innerHTML = "<br><br><p>Your request has been submitted to Xecunet. You will be contacted shortly.</p>";
	} 
	document.body.style.cursor = "default";
}



