var http = createRequestObject();
var areal = Math.random() + "";
var real = areal.substring(2,6);

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
  	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function sendRequest(url, postData) {
	try{
	    http.open('POST',  url);
	    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    http.onreadystatechange = handleResponse;
	    http.send(postData);
	}
	catch(e){}
	finally{}
}


function checkValues(url, reqFields, emailFields, dataFields) {
	var valid = document.getElementById("valid").value;
	if(real !== valid) {
		alert("Ant-Bot check failed.....Please enter the 4 digits as they appear.");
		return false;
	}
	var requiredError = false;
	for (var i=0; i<reqFields.length; i++) {
		if (trim(document.getElementById(reqFields[i]).value) == "") {
			document.getElementById(reqFields[i]).focus();
			$requiredError = true;
		}
	}
	var emailFormatError = false;
	for (var i=0; i<emailFields.length; i++) {
		if(!isEmail(document.getElementById(emailFields[i]).value)) {
			document.getElementById(emailFields[i]).focus();
			document.getElementById(emailFields[i]).select();
			emailFormatError = true;
		}
	}
	if (requiredError) {
		alert("Please complete all required fields");
	} else if (emailFormatError) {
		alert("Your email appears to be invalid. Please check.");
	} else {
		document.getElementById("submit").disabled=true;
		document.getElementById("submit").value='Please Wait..';
		document.getElementById("confirmation").style.display ="";
		var postString = 'rnd='+Math.random();
		for (var i=0; i<dataFields.length; i++) {
			var tmp = escape(document.getElementById(dataFields[i]).value);
			postString += '&'+dataFields[i]+'='+tmp;
		}
		sendRequest(url, postString);
	}	
}

function handleResponse() {
	try{
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			document.getElementById("confirmation").innerHTML = response;
			if (document.getElementById("email")) {
				document.getElementById("email").value = ''; 
			} else if (document.getElementById("sender")) {
				document.getElementById("sender").value = '';
			}
			document.getElementById("submit").value='Send it!';
			document.getElementById("submit").disabled=false;
		}
	}
	catch(e){}
	finally{}
}

function isUndefined(a) {
	return typeof a == 'undefined';
}

function isObject(a) {
	return (typeof a == 'object' && !!a) || isFunction(a);
}

function isFunction(a) {
	return typeof a == 'function';
}

function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function isEmail(a) {
	return (a.indexOf(".") > 0) && (a.indexOf("@") > 0);
}

function botCheckInfo() {
	alert("To prevent automatic programs (bots) from submitting spam through this form, we have added a simple validation check to the form. You must enter these 4 digits in the box provided in order to submit the form.");
}