// (C) Copyright 2005 flashpoint productions.

String.prototype.replaceAll = function(s1, s2) { 
	return this.replace(new RegExp(s1,"g"), s2);
}

//Function limits the number of characters that can be keyed into
//a text field.
function limitTextArea(textArea, maxChars){
	if (textArea.value.length > maxChars){
		textArea.value = textArea.value.substring(0, maxChars);
	}
}

// Function to format a Phone number after data entry.
function formatPhone(id){
	var textArea;
	if(document.getElementById){	
		textArea=document.getElementById(id);
	}
	if(document.all) {
	   textArea=document.all[id];
	}
	if(document.form) {
	   textArea=document.form[id];
	}
	var formattedVal = textArea.value;
	formattedVal = formattedVal.replaceAll("-", "");
	formattedVal = formattedVal.replaceAll("[(]", "");
	formattedVal = formattedVal.replaceAll("[)]", "");
	formattedVal = formattedVal.replaceAll(" ", "");
	//if (formattedVal.length < 10) {
	//		formattedVal = "";
	//	}
	if (formattedVal.length >= 10) {
		formattedVal = "(" + formattedVal.substring(0, 3) + ") " + formattedVal.substring(3, 6) + "-" + formattedVal.substring(6, 10);
	}
	textArea.value = formattedVal;
}
// Function to require valid ZipCode characters (number or - only).
function requirePhoneChar(textArea) {
	var checkval = textArea.value;
	//Strip out any allowed characters.  Then we'll just check to make sure we have only numbers left.
	checkval = checkval.replaceAll("-", "");
	checkval = checkval.replaceAll("[(]", "");
	checkval = checkval.replaceAll("[)]", "");
	checkval = checkval.replaceAll(" ", "");
	//Check to make sure we have only numbers left.
	if (Number(checkval) != checkval){
		textArea.value = "";
	}
}

// Function to format a Zip Code after data entry.
function formatZip(id){
	var textArea;
	if(document.getElementById){	
		textArea=document.getElementById(id);
	}
	if(document.all) {
	   textArea=document.all[id];
	}
	if(document.form) {
	   textArea=document.form[id];
	}
	var formattedVal = textArea.value;
	formattedVal = formattedVal.replaceAll("-", "");
	//if (formattedVal.length < 5) {
	//		formattedVal = "";
	//	}
	if (formattedVal.length < 9) {
		formattedVal = formattedVal.substring(0, 5);
	}
	if (formattedVal.length >= 9) {
		formattedVal = formattedVal.substring(0, 5) + "-" + formattedVal.substring(5, 9);
	}
	textArea.value = formattedVal;
}
// Function to require valid Zip Code characters (number or - only).
function requireZipChar(textArea) {
	var checkval = textArea.value;
	//Strip out any allowed characters.  Then we'll just check to make sure we have only numbers left.
	checkval = checkval.replace("-", "");
	//Check to make sure we have only numbers left.
	if (Number(checkval) != checkval){
		textArea.value = "";
	}
}


// Function to format an e-mail address.
function formatEmail(id){
	var textArea;
	if(document.getElementById){	
		textArea=document.getElementById(id);
	}
	if(document.all) {
	   textArea=document.all[id];
	}
	if(document.form) {
	   textArea=document.form[id];
	}
	var formattedVal = textArea.value;
	formattedVal = formattedVal.replaceAll(" ", "");
	formattedVal = formattedVal.replaceAll(",", ".");
	formattedVal = formattedVal.replaceAll("[.]{1,}", ".");
	formattedVal = formattedVal.replaceAll("[@]{1,}", "@");
	textArea.value = formattedVal;
}

// Function checks the validity of an email address entered in a form field.
function validEmail(emailAddress){
	//Make sure there is an @ sign in the address
	if (emailAddress.indexOf('@', 0) == -1){
		return false;
	}
	//Make sure there is a period after the @ sign
	if (emailAddress.indexOf('@', 0) >= emailAddress.lastIndexOf('.', emailAddress.length)){
		return false;
	}
	//Make sure there is only one @ sign in the address
	if (emailAddress.indexOf('@', emailAddress.indexOf('@', 0) + 1) > -1){
		return false;
	}
	//Make sure domain extension is at least 2 positions long and less than or equal to 4 positions long
	if (((emailAddress.length - emailAddress.lastIndexOf('.', emailAddress.length) - 1) < 2) || ((emailAddress.length - emailAddress.lastIndexOf('.', emailAddress.length) - 1) > 4)){
		return false;
	}
	return true;

}

//Function verifies all form information before submitting.
function verifyAndSubmit(id){
	var FORMobj;
	if(document.getElementById){	
		FORMobj=document.getElementById(id);
	}
	if(document.all) {
	   FORMobj=document.all[id];
	}
	if(document.form) {
	   FORMobj=document.form[id];
	}
	//Check to make sure all required fields have been filled in.
	textBoxes = document.getElementsByTagName("input");
	var errorStr = "";
	for (textBoxCount = 0; textBoxCount < textBoxes.length; textBoxCount++){
		if (textBoxes[textBoxCount].name.substring(0,2) == "r_"){
			//Remove spaces to make sure there's actual content in the field.
			if(textBoxes[textBoxCount].value.replace(/ /g, "") == ""){
				var realFieldName = textBoxes[textBoxCount].name.substring(2, textBoxes[textBoxCount].name.length);
				errorStr += realFieldName.replace("_", " ") + "\n";
			}
		}
	}
	textBoxes = document.getElementsByTagName("textarea");
	for (textBoxCount = 0; textBoxCount < textBoxes.length; textBoxCount++){
		if (textBoxes[textBoxCount].name.substring(0,2) == "r_"){
			//Remove spaces to make sure there's actual content in the field.
			if(textBoxes[textBoxCount].value.replace(/ /g, "") == ""){
				var realFieldName = textBoxes[textBoxCount].name.substring(2, textBoxes[textBoxCount].name.length);
				errorStr += realFieldName.replace("_", " ") + "\n";
			}
		}
	}
	
	if (errorStr != ""){
		errorStr = "ERROR:\n" + errorStr + "\nAre required fields.\n\n";
	}
	
	//Check to make sure all email addresses are valid
	//Check to make sure all required fields have been filled in.
	textBoxes = document.getElementsByTagName("input");
	for (textBoxCount = 0; textBoxCount < textBoxes.length; textBoxCount++){
		if (textBoxes[textBoxCount].name.substring(0,2) == "e_"){
			if (!validEmail(textBoxes[textBoxCount].value)){
				errorStr += textBoxes[textBoxCount].name.replace("e_", "").replace("_", " ") + ". ERROR:\nInvalid email address entered.\n\n";
			}
		}
	}
	textBoxes = document.getElementsByTagName("textarea");
	for (textBoxCount = 0; textBoxCount < textBoxes.length; textBoxCount++){
		if (textBoxes[textBoxCount].name.substring(0,2) == "e_"){
			if (!validEmail(textBoxes[textBoxCount].value)){
				errorStr += textBoxes[textBoxCount].name.replace("e_", "").replace("_", " ") + ". ERROR:\nInvalid email address entered.\n\n";
			}
		}
	}
	
	//SPECIAL ERROR CHECK CONDITIONS TO HANDLE MAILING LIST FORM ONLY
	cnID = "r_Contact_Number";
	var ContactNum;
	if(document.getElementById){	
		ContactNum=document.getElementById(cnID);
	}
	if(document.all) {
	   ContactNum=document.all[cnID];
	}
	if(document.form) {
	   ContactNum=document.form[cnID];
	}
	if (ContactNum != null) {
		if (ContactNum.value.length<10) {
			errorStr += "Contact Number. ERROR:\nInvalid Contact Number entered.\nPlease enter a 10-digit phone number.\n\n";
		}
	}
	
	zipID = "r_Zip";
	var ZipCode;
	if(document.getElementById){	
		ZipCode=document.getElementById(zipID);
	}
	if(document.all) {
	   ZipCode=document.all[zipID];
	}
	if(document.form) {
	   ZipCode=document.form[zipID];
	}
	if (ZipCode != null) {
		if (ZipCode.value.length<5) {
			errorStr += "Zip. ERROR:\nInvalid Zip Code entered.\n\n";
		}
	}

	if (errorStr != ""){
		alert(errorStr);
	} else {
		FORMobj.submit();
	}
}