//////////////////////////////////
// getNumbers(strInput)
// function accepts a string and 
// returns a string stripped of all
// but numerical digits.
//
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
function getNumbers(strInput){
	var ValidChars = "0123456789";
	var Char;
	var strNumbers = "";
	for (i = 0; i < strInput.length; i++){ 
		Char = strInput.charAt(i); 
		if (ValidChars.indexOf(Char) != -1){
			strNumbers += Char;
		}
	}
	return strNumbers;
}//End function getNumbers()
function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function checkForm(){
	var oXMLHTTP = GetXmlHttpObject();
	var sURL;
	var txtagentemail = document.getElementById("txtAgentEmail");
	var txtmlsnbr = document.getElementById("txtmlsnbr");
	var txtName = document.getElementById("txtName");
	var txtEmail = document.getElementById("txtEmail");
	var txtPhone = document.getElementById("txtPhone");
	var txtCaptchaT = document.getElementById("txtCaptchaT");
	var txtCaptchaV = document.getElementById("txtCaptchaV");
	var selContact = document.getElementById("selContact");
	var txtagentname = document.getElementById("txtagentname");
	var txaComment = document.getElementById("txaComment");

	
	if (txtName.value == ""){
		alert("Please provide your Name.");
		txtName.focus();
		return false;
	}
	
	if (selContact.value == ""){
		alert("Please Tell me how you'd like to be contacted.");
		selContact.focus();
		return false;
	}
	if (txtCaptchaT.value != txtCaptchaV.value)
	{
		alert("The confirmation codes do not match.");
		txtCaptchaV.focus();
		return false;
	}
	if (txtEmail.value == ""){
		alert("Please provide your Email Address.");
		txtEmail.focus();
		return false;
	}
	else if (!isValidEmail(txtEmail.value) ){
		alert("Email Address appears to be invalid.");
		txtEmail.focus();
		return false;		
	}
	if (txtPhone.value == ""){
		// not required
	}
	else {
		txtPhone.value = getNumbers(txtPhone.value);
		if (txtPhone.value.length != 10){
			alert("Phone Number must be 10 digits including area code.");
			txtPhone.select();
			return false;
		}
	}
//	alert("about to do ajax");
	sURL = "http://"+window.location.host+"/ajax/contactemail.asp?an="+txtagentname.value+"&e="+txtagentemail.value+"&m="+txtmlsnbr.value+"&n="+txtName.value+"&ce="+txtEmail.value+"&p="+txtPhone.value+"&cm="+selContact.value+"&c="+txaComment.value
//		alert(sURL);
	
	oXMLHTTP.open("GET",sURL,false);
	
	// Execute the request
	oXMLHTTP.send(null);
//	alert( "back from ajax call" );
//	alert( oXMLHTTP.responseText );
	document.body.style.cursor='auto';
	alert("Your request has been submitted to "+txtagentname.value+".");
//	document.body.style.cursor='auto';
	oXMLHTTP.close;
		

	// all good
	return true;
}
