function validateFields() {
var frmEl = document.getElementById('cForm');
var posName = document.getElementById('posName');
var posPhone = document.getElementById('posPhone');
var posEmail = document.getElementById('posEmail');
var posRegard = document.getElementById('posRegard');
var posText = document.getElementById('posText');
var strCC = document.getElementById('selfCC');
var captcha = document.getElementById('captcha');

var whiteSpace = /^[\s]+$/;
	// MGA:  Modified client-side validation for Hawk Alarm use.  (PJB - 01/15/07)
	//MGA:if ( posName.value == '' || whiteSpace.test(posName.value) ) {
	//MGA:	alert("Please enter your name.");
	//MGA:}
	//MGA:else if ( posEmail.value == '' || whiteSpace.test(posEmail.value) ) {
	if (captcha.value != captchaCode)
	{
		alert("Please enter the code you see at the bottom of the form.");
	}
	else if ( posEmail.value == '' || whiteSpace.test(posEmail.value) ) {
		alert("Please enter your email address so that we may reply to you.");
	}
	else if ( posRegard.value == '' || whiteSpace.test(posRegard.value) ) {
		alert("Please enter the topic of your question/comment in the 'Subject' field.");
	}
	else if ( posText.value == '' || whiteSpace.test(posText.value) ) {
		alert("Please enter your question/comment in the 'Message' field.");
	}
	else {
		sendPosEmail();
	}
}
function sendPosEmail () {
	var success = document.getElementById('emailSuccess');
	var posName = document.getElementById('posName');
	var posPhone = document.getElementById('posPhone');
	var posEmail = document.getElementById('posEmail');
	var posRegard = document.getElementById('posRegard');
	var posText = document.getElementById('posText');

	// MGA:  Modified type of checkbox element variable.  (PJB - 12/21/06)
	//MGA:var strCC = document.getElementById('selfCC').value;
	//MGA:var strCC = document.getElementById('selfCC');

	// MGA:  Modified relative path for integration into site root.  (PJB - 12/21/06)
	//MGA:var page = "scripts/xmlHttpRequest.php?contact=true&xml=true";
	var page = "ajaxContact/scripts/xmlHttpRequest.php?contact=true&xml=true";

	// MGA:  Moved declaration of next var.  (PJB - 12/21/06)
	var stuff;

	showContactTimer(); // quickly begin the load bar
	success.style.display = 'none'; // hide the success bar (incase this is a multi-email
	
	// convert (&, +, =) to string equivs. Needed so URL encoded POST won't choke.
	var str1 = posName.value;
	str1 = str1.replace(/&/g,"**am**");
	str1 = str1.replace(/=/g,"**eq**");
	str1 = str1.replace(/\+/g,"**pl**");
  // MGA:  Added str5 for telephone field.  (PJB - 01/15/07)
	var str5 = posPhone.value;
	str5 = str5.replace(/&/g,"**am**");
	str5 = str5.replace(/=/g,"**eq**");
	str5 = str5.replace(/\+/g,"**pl**");
	var str2 = posEmail.value;
	str2 = str2.replace(/&/g,"**am**");
	str2 = str2.replace(/=/g,"**eq**");
	str2 = str2.replace(/\+/g,"**pl**");
	var str3 = posRegard.value;
	str3 = str3.replace(/&/g,"**am**");
	str3 = str3.replace(/=/g,"**eq**");
	str3 = str3.replace(/\+/g,"**pl**");
	var str4 = posText.value;
	str4 = str4.replace(/&/g,"**am**");
	str4 = str4.replace(/=/g,"**eq**");
	str4 = str4.replace(/\+/g,"**pl**");
	

	// MGA:  Modified following if test to reliably check state of CC checkbox.  (PJB - 12/21/06)
	//MGA:if (strCC == 'send') {
	//MGA:var stuff = "selfCC="+strCC+"&posName="+str1+"&posEmail="+str2+"&posRegard="+str3+"&posText="+str4;
	//MGA:if (strCC.checked == true) {
	//MGA:  stuff = "selfCC=send&posName="+str1+"&posEmail="+str2+"&posRegard="+str3+"&posText="+str4;
	//MGA:}
	//MGA:else {
	  stuff = "posName="+str1+"&posPhone="+str5+"&posEmail="+str2+"&posRegard="+str3+"&posText="+str4;
	//MGA:};

	loadXMLPosDoc(page,stuff)
}
function showContactTimer () {
	var loader = document.getElementById('loadBar');
	loader.style.display = 'block';
	sentTimer = setTimeout("hideContactTimer()",6000);
}

function hideContactTimer () {
	var loader = document.getElementById('loadBar');
	var success = document.getElementById('emailSuccess');
	var fieldArea = document.getElementById('contactFormArea');
	var inputs = fieldArea.getElementsByTagName('input');
	var inputsLen = inputs.length;
	var tAreas = fieldArea.getElementsByTagName('textarea');
	var tAreasLen = tAreas.length;
	// Hide the load bar alas! Done Loading
	loader.style.display = "none";
	success.style.display = "block";
  // MGA:  Modified innerHTML for Hawk Alarm colors on success.  (PJB - 01/15/07)
	//MGA:success.innerHTML = '<strong style="color:green;">'+grabPosXML("confirmation")+'</strong>';
	success.innerHTML = '<strong style="color:#FAA635;">'+grabPosXML("confirmation")+'</strong>';
	// Now Hijack the form elements
	for ( i=0;i<inputsLen;i++ ) {
		if ( inputs[i].getAttribute('type') == 'text' ) {
			inputs[i].value = '';
		}
	}
	for ( j=0;j<tAreasLen;j++ ) {
		tAreas[j].value = '';
	}
}

function ajaxContact() {
var frmEl = document.getElementById('cForm');
addEvent(frmEl, 'submit', validateFields, false);
frmEl.onsubmit = function() { return false; }
}
addEvent(window, 'load',ajaxContact, false);
