// JScript File
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

function fncResetFormErrors(strFormName)
{
	//make those label colors change back to original color whenever the form
	//is validated just in case form user makes another mistake, but has corrected
	//all of the previouse errors.
	
	for(var i = 0; i < document.forms[strFormName].length; i++)
	{
		var objFormElement = document.forms[strFormName].elements[i];
		fncHighlightFormErrors('#999',objFormElement.name);
	}
}

function fncHighlightFormErrors(strHighlightColor,strElementName)
{
    if(strElementName.trim().length != 0)
    {
	    var objFormElement;
	    if(document.getElementById(strElementName))
	    {
	        objFormElement = document.getElementById(strElementName).parentNode.parentNode.getElementsByTagName("td")[0];
	    }
	    else
	    {
	        objFormElement = document.forms[0].elements[strElementName];
	        if(objFormElement)
	            objFormElement.parentNode.parentNode.getElementsByTagName("td")[0];
	    }	    
	    if(objFormElement) 
		    objFormElement.style.color = strHighlightColor;
	}
}

function fncIsValidEmail(strElementValue)
{
	var blnIsValid;
	strElementValue = strElementValue.trim();
	var expBad = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\!)|(^\#)|(^\$)|(^\%)|(^\^)|(^\&)|(^\*)|(^\()|(^\))|(^\+)|(^\{)|(^\})/; 
	// not valid
	var expGood = /^[^\[\]].+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 
	// valid
	blnIsValid =(!expBad.test(strElementValue) && expGood.test(strElementValue));
	return blnIsValid;
}

function fncIsValidPhone(strElementValue)
{
	strElementValue = strElementValue.trim();
	var blnIsValid;
	//Change the following expression if format of 10 digit number truly matters for presentation! See below!
	//The following is a catch all for almost any phone entry
	var expGood = /^\d{3}\-\d{3}\-\d{4}$/; 
	blnIsValid =(expGood.test(strElementValue));
	return blnIsValid;
}

function fncGetFormErrors(strElementName)
{
	var strHighlightColor = "#c00";
	var strErr;
	var astrErr = new Array();
	astrErr["YourName"] = " \t - Please enter your name. \n";
	astrErr["PhoneNumber"] = " \t - Please enter a valid Phone. For example: 999-999-9999. \n";
	astrErr["EmailAddress"] = " \t - Please provide a valid E-mail address. \n";
	astrErr["QuestionsComments"] = " \t - Please enter your questions and comments. \n";
	astrErr["txtCode"] = " \t - Please enter the text you see in the picture. \n";
	fncHighlightFormErrors(strHighlightColor,strElementName);
	strErr = (astrErr[strElementName]) ? astrErr[strElementName] : ""; 
	return strErr;
}

function fncIsEmpty(strFormElementValue)
{
	var blnIsEmpty;
	blnIsEmpty = (strFormElementValue.trim().length < 1);
	return blnIsEmpty;
}

function fncValidateForm(strFormName)
	{
		var i = 0;
		var strErrors = "";
		strInstructions = "There were errors processing your request \n";
		strInstructions += "The items highlighted in red are incorrect \n";
		strInstructions += "Please correct the following before continuing: \n \n";
		//reset form error color back to black if this is the second time being submitted
		fncResetFormErrors(strFormName);
		while(i < document.forms[strFormName].length)
		{
			var objFormElement = document.forms[strFormName].elements[i];

			switch(objFormElement.name)
			{
				case "PhoneNumber":
					if(!(fncIsValidPhone(objFormElement.value))) strErrors+=fncGetFormErrors(objFormElement.name);
					break;
				case "EmailAddress":
					if(!(fncIsValidEmail(objFormElement.value))) strErrors+=fncGetFormErrors(objFormElement.name);
					break;
				case "YourName":
				case "txtCode":
				case "QuestionsComments":
					if(fncIsEmpty(objFormElement.value))
					    strErrors+=fncGetFormErrors(objFormElement.name);
					break;
				default:
					break;
			}
			i++;
		}
		
		document.getElementById('submitmode').value = "submit";
		submitmode = "submit";
		
		(strErrors)?alert(strErrors = strInstructions + strErrors):document.forms[strFormName].submit();
}


function fncGetNewCaptchaImage(strFormName)
{
    document.getElementById('submitmode').value = "newcaptcha";
    document.forms[strFormName].submit();
}