//Static array elements
var formNumbers 	= new Array('Zip', 'Phone', 'Fax');
var formEmail 		= new Array('txtEmail');
var formStates		= new Array('AK','AL','AR','AS','AZ','CA','CO','CT','DC','DE','FL','FM','GA','GU','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MH','MI','MN','MO','MP','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','PR','PW','RI','SC','SD','TN','TX','UT','VA','VI','VT','WA','WI','WV','WY');


//Variable to let all functions know that the form is processing
var processing = false;

//Function to create an empty error message, for use in displayModalMessage
function errorMessage(errorFields, errorType) {
	//Send an alert describing the error
	var strReturn = document.createElement("DIV");
	switch(errorType)
	{
		case 'emptyField':
			objTxt = document.createTextNode("Please complete these fields:");
		break;

		case 'invalidValue':
			objTxt = document.createTextNode("Please re-enter these fields:");
		break;

		case 'NaN':
			objTxt = document.createTextNode("Theses fields must be numeric");
		break;

		default:
			objTxt = document.createTextNode("There was an error with");
		break;
	}

	objBr = document.createElement("BR");

	strReturn.appendChild(objTxt);
	strReturn.appendChild(objBr);

	for(strId in errorFields) {
		//Set a string variable equal to the array value
		strField = errorFields[strId];

		//Highlight the field with an error
		objElement = document.getElementById(strField);
		objElement.style.border = '2px solid #f00';
		objElement.style.backgroundColor = '#ffc';

		//Create new text for the field with an error
		objElement.value = "please re-enter this field";
		//As long as the element isn't an option, set the onclick handler to clear the value
		if(objElement.id != 'optState') {
			objElement.onclick = function(){this.value='';}
		}
		//Filter the string variable for unnecessary or unacceptable values
		strField = cleanInput(strField);
		objTxt = document.createTextNode(strField);
		objBr = document.createElement("BR");

		strReturn.appendChild(objTxt);
		strReturn.appendChild(objBr);
	}
	//Send the node to the function that displays everything
	displayModalMessage(strReturn);
	return false;
}

function validateFields() {
	if(!processing) {
		processing = true;
		var whiteSpace = /^[\s]+$/;
		var sendMail = true;
		var errorFields = new Array();
		objForm = document.getElementById("cForm");
		//Loop through all of the submitted form elements
		for(var i = 0; i < objForm.elements.length; i++)
		{
			//Make sure that the element being passed isnt an empty string, null, or "undefined"
			if(objForm.elements[i].name != ""
				&& objForm.elements[i].name != null
				&& objForm.elements[i].name != "undefined")
			{
				var objElement = objForm.elements[i];
				//Clean up the value
				strLabel = cleanInput(objElement.name);
				strInput = cleanInput(objElement.value);

  			//check to see if the value should be validated
  			//per JB email, the title and fax should not be required
				if(strLabel != 'Title' && strLabel != 'Fax'){
          //If the value is null, set the fail flag
				  if(strInput == '' || strInput == "please re-enter this field") {
					 sendMail = false;
					 errorType = "emptyField";
					 errorFields.push(objElement.id);
				  }
				}
				//Make sure the state value is valid
				if(strLabel == 'State') {
					var boolAccept = false;
					for(var x in formStates) {
						if(strInput == formStates[x]) {
							boolAccept = true;
						}
					}
					if(!boolAccept)
					{
						sendMail = false;
						errorType = "invalidValue";
						errorFields.push(objElement.id);
					}
				}
				//Check if the input element is supposed to be numeric
				for(var x in formNumbers) {
					if(strLabel == formNumbers[x]) {
						strInput = strInput.replace('(', '');
						strInput = strInput.replace(')', '');
						strInput = strInput.replace('-', '');
						strInput = strInput.replace(' ', '');
						strInput = strInput.replace('.', '');
						if(isNaN(strInput)) {
							sendMail = false;
							errorType = "NaN";
							errorFields.push(objElement.id);
						}
					}
				}
				for(var x in formEmail) {
					if(strLabel == formEmail[x]) {

					}
				}
				if(sendMail)
				{
					objElement.style.border = '';
					objElement.style.backgroundColor = '';
				}
			}
		}
		//If nothing has tripped up in the looping through submitted values, then send the e-mail
		if(sendMail) {
			sendPosEmail();
			processing = false;
			return false;
		} else {
			errorMessage(errorFields, errorType);
			processing = false;
			return false;
		}

		processing = false;
	}

}

//Standard function for cleaning input values
function cleanInput(str) {
	//str = str.replace(/&/g, "**am**");
	//str = str.replace(/=/g, "**eq**");
	//str = str.replace(/\+/g, "**pl**");
	str = str.replace(/txt/, '');
	str = str.replace(/opt/, '');
	return str;
}

function sendPosEmail ()
{
	//Initialize function scope variables
	var strPost 		= '';
	var page 			= "includes/xmlHttpRequest.php?contact=true&xml=true";

	//Display the progress bar
	showContactTimer ();

	objForm = document.getElementById("cForm");
	//Loop through the form elements
	for(var i = 0; i < objForm.elements.length; i++)
	{
		//Make sure that the element being passed isnt an empty string, null, or "undefined"
		if(objForm.elements[i].name != ""
			&& objForm.elements[i].name != null
			&& objForm.elements[i].name != "undefined")
		{
			//Assign each form element to a variable, named objElement
			var objElement = objForm.elements[i];
			//Call the cleanInput function for each value posted to the function
			strVar = cleanInput(objElement.value);
			//Append the string to send to the AJAX function
			strPost = strPost + '&' + objElement.name + '=' + strVar;
		}
	}
	//Call the AJAX request function
	loadXMLPosDoc(page, strPost);

	return false;
}
function showContactTimer () {
	var objSuccess = document.getElementById('emailSuccess');
	if(objSuccess.style.display != 'none')
	{
		objSuccess.style.display = 'none';
	}
	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('cForm');
	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";
	objBr1 = document.createElement("BR");
	objBr2 = document.createElement("BR");
	objMessage = document.createTextNode(grabPosXML("confirmation"));

	success.appendChild(objBr1);
	success.appendChild(objBr2);
	success.appendChild(objMessage);

	// 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 = '';
	}




	/* Added by CRC 070504 */
	objForm = document.getElementById('cForm');
	objForm.style.display = 'none';
	objH1Title = document.getElementById("h1Contact");
	objH1Title.innerHTML = "Thank You";

}

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

addEvent(window, 'load', ajaxContact, false);