if(window.attachEvent) {
	window.attachEvent("onload",setListeners);
}

function setListeners(){
	inputList = document.getElementsByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
	  inputList[i].attachEvent("onpropertychange",restoreStyles);
	  inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");
	for(i=0;i<selectList.length;i++){
	  selectList[i].attachEvent("onpropertychange",restoreStyles);
	  selectList[i].style.backgroundColor = "";
	}
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "")
	  event.srcElement.style.backgroundColor = "";
}

var errorobj = new Array();	 

function highliteError(src)
   {
   	   var dom = (document.getElementById && !document.all);
       var obj;
       if(dom)
	   {
           obj = document.getElementsByName(src)[0];
	   }
       else
	   {
           obj = document.all[src];
	   }

       obj.className = 'errorElementStyle';
       errorobj.push(obj);
   }
   
////////// Regular Expressions //////////
	
    var dateExp = /^\d{2}(\/)\d{2}\1\d{4}$/  // dd/dd/dddd
	var integerExp = /^([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])$/ //allows for only integer values, no commas
	var moneyExp = /^\d+\.\d{2}$/  //allows for only monitary values, no "$" or "," (0.00)
	
	
	var emailExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i
	var locExp = /^\d{4}-\d{2}$/
	var phoneExp = /^\d{3}-\d{3}-\d{4}$/
	var fedidExp = /^\d{2}-\d{7}$/
	var ssExp = /^\d{3}-\d{2}-\d{4}$/
	var fivedigitsExp = /^\d{5}$/
	var districtExp = /^\d{4}$/
	var threedigExp = /^\d{3}$/
	var numericZip = /^\d{5}(-\d{4})?$/ //USA zip
	var nousaExp =  /(^\usa)|(^\america)|(^\united states)$/i;

function validateContactUsForm(){
	while(errorobj.length){
    	errorobj.shift().className = 'normalElementStyle';}
	
	var errFound = false;
	var errPhone = false;
	var errEmail = false;
	var noContact = false;
	var errFax = false;

	if (document.contactus.name.value == '')
	{
	 highliteError(document.contactus.name.name);
	 errFound = true;
	}

	if (document.contactus.company.value == '')
	{
	 highliteError(document.contactus.company.name);
	 errFound = true;
	}

	if (document.contactus.email.value == 'example@domain.com')
	{
		document.contactus.email.value = '';
	}
	
	if ((document.contactus.email.value != '') && (emailExp.test(document.contactus.email.value) == false))
	{
	 highliteError(document.contactus.email.name);
	 errEmail = true;
	 errFound = true;
	}

	if (document.contactus.phone.value == '555-555-5555')
	{
		document.contactus.phone.value = '';
	}

	if ((document.contactus.phone.value != '') && (phoneExp.test(document.contactus.phone.value) == false))
	{
	 highliteError(document.contactus.phone.name);
	 errPhone = true;
	 errFound = true;
	}

	if (document.contactus.fax.value == '555-555-5555')
	{
		document.contactus.fax.value = '';
	}

	if ((document.contactus.fax.value != '') && (phoneExp.test(document.contactus.fax.value) == false))
	{
	 highliteError(document.contactus.fax.name);
	 errFax = true;
	 errFound = true;
	}
	
	if (document.contactus.email.value == '')
	{
	 highliteError(document.contactus.email.name);
	 noContact = true;
	 errFound = true;
	}
	
	if (document.contactus.phone.value == '')
	{
	 highliteError(document.contactus.phone.name);
	 noContact = true;
	 errFound = true;
	}
		
	if (document.contactus.comments.value == '')
	{
	 highliteError(document.contactus.comments.name);
	 errFound = true;
	}

	if (errFound == true)
	{
		var msg;
		msg = 'Not all required fields have been filled in.  ';
		msg += 'Required fields have been highlighted.\n\n';
		if (noContact == true)
		{
			msg += 'Please enter your phone number and e-mail address so that we can contact you.\n';
		}
		if (errPhone == true)
		{
			msg += 'Please enter your phone number in this format: 555-555-1212.\n';
		}
		if (errFax == true)
		{
			msg += 'Please enter your fax number in this format: 555-555-1212.\n';
		}
		if (errEmail == true)
		{
			msg += 'Please enter your e-mail address in this format: example@domain.com.\n';
		}
		alert(msg);
		return false;
	}
	
	else
   {
      return true;
   }
}

