<!--
/**
  Call basic form information by causing an onsubmit to verifyInput(this)
  
  
  To do basic validation types:
  <input type="hidden" name="<elementNameToBeChecked>_verify" value="">
  
  To add a visual image to show what items are missing on a failure of a validation.
  
**/
var strError = "";
var strFuncError = "";
var blnError = false;
var aryFunc = new Array();
var funcCounter = 0;
var strSearchNoiseWords = "";
var strSearchOperators = "";
var blnErrorImg = false;
var txtErrorImg = "/images/formError.gif";
var blnFirstRun = true;
var aryOrigErrorImg = new Array();


function appendError(strVal) {
	var i;
	errorImgNotify(strVal);	
	if (strVal.indexOf("_") != -1 && strVal.length != -1) {
		for (var i = 0; i < strVal.length; i++){
			if (strVal.indexOf("_") != -1) { strVal = strVal.substring(0, strVal.indexOf("_")) + " " + strVal.substring(strVal.indexOf("_") + 1, strVal.length) }
		}
	} 
	strError = strError + strVal + "\n";
	blnError = true;
}

function errorImgNotify(strVal) {
	var imgCol = document.images;
	var imgname = strVal + "_imageverify";
	if (imgCol[imgname] != null ) {
		imgCol[imgname].src = txtErrorImg;
		return true;
	}
	else {
		return false;
	}
}
function formatCC(strVal, strType) {
	strVal.value = strVal.value.replace(/\D+/g, "");
	if (strVal.value.length > 0) {
		if (strType == "MasterCard" || strType == "Discover" || (strType == "Visa" && strVal.value.length == 16)) {
			strVal.value = strVal.value.substring(0, 4) + "-" + strVal.value.substring(4, 8) + "-" + strVal.value.substring(8, 12) + "-" + strVal.value.substring(12, 16);
		}
		if (strType == "Visa" && strVal.value.length == 13) {
			strVal.value = strVal.value.substring(0, 3) + "-" + strVal.value.substring(3, 6) + "-" + strVal.value.substring(6, 9) + "-" + strVal.value.substring(9, 13);
		}
		if (strType == "AMEX") {
			strVal.value = strVal.value.substring(0, 4) + "-" + strVal.value.substring(4, 10) + "-" + strVal.value.substring(10, 15);
		}
	}
}

function validCCType(strVal, strType) {
	if (strVal.value.length > 0) {
		if (strVal.value.substring(0, 1) == "3" && strType != "AMEX") { return false; }
		if (strVal.value.substring(0, 1) == "4" && strType != "Visa") { return false; }
		if (strVal.value.substring(0, 1) == "5" && strType != "MasterCard") { return false; }
		if (strVal.value.substring(0, 1) == "6" && strType != "Discover") { return false; }
	}
	return true;
}

function validCCNumber(strVal, strType) {
	if (strType == "MasterCard" || strType == "Discover") {
		if (strVal.value.match(/^(\d{4,4})(-{1,1})(\d{4,4})(-{1,1})(\d{4,4})(-{1,1})(\d{4,4})$/) == null) { return false; }
	}
	if (strType == "Visa") {
		if (strVal.value.match(/^(\d{4,4})(-{1,1})(\d{4,4})(-{1,1})(\d{4,4})(-{1,1})(\d{4,4})$/) == null && strVal.value.match(/^(\d{3,3})(-{1,1})(\d{3,3})(-{1,1})(\d{3,3})(-{1,1})(\d{4,4})$/) == null) { return false; }
	}
	if (strType == "AMEX") {
		if (strVal.value.match(/^(\d{4,4})(-{1,1})(\d{6,6})(-{1,1})(\d{5,5})$/) == null) { return false; }
	}
	var strTmp = strVal.value;
	var intTotal = 0;
	var intTmp = 0;
	strTmp = strTmp.replace(/\D+/g, "");
	for (var i = 0; i < strTmp.length; i++) {
		var j = 0;
		intTmp = strTmp.substring(strTmp.length - i - 1, strTmp.length - i);
		if (((i) % 2) != 0) {
			intTmp = strTmp.substring(strTmp.length - i - 1, strTmp.length - i) * 2;
			if (intTmp >= 10) { intTmp = (intTmp - 10) + 1; }
		}
		intTotal = (intTotal - 0) + (intTmp - 0);
	}
	if (intTotal % 10 != 0) { return false; }
	return true;
}

function IsInArray(strFind, aryIn) {
	for (var i = 0; i < aryIn.length; i++) {
		if (aryIn[i][0] == strFind) { return true; }
	}
	return false;
}

function formatusphone(strVal) {
	strVal.value = strVal.value.replace(/\D+/g, "");
	if (strVal.value.length > 0) { strVal.value = "(" + strVal.value.substring(0, 3) + ") " + strVal.value.substring(3, 6) + "-" + strVal.value.substring(6, 10); }
}

function verifyusphone(strVal, blnChkEmpty) {
	formatusphone(strVal);
	if (strVal.value.length > 0) {
		if (strVal.value.match(/^(\({1,1})(\d{3,3})(\) {1,1})(\d{3,3})(-{1,1})(\d{4,4})$/) == null) { return false; }
		else { return true; }
	}
	if (blnChkEmpty) { return false; }
	else { return true; }
}

function validSearchTerms(strVal) {
	strVal.value = strVal.value.toLowerCase();
	var aryTerms = strVal.value.split(" ");
	var strTerms = "";
	for (var i = 0; i < aryTerms.length; i++) {
		if (strSearchNoiseWords.indexOf(" " + aryTerms[i] + " ") == -1) { strTerms = strTerms + aryTerms[i] + " " }
	}
	strVal.value = strTerms;
	if (strTerms.replace(/\s+/g, "") == "" || strTerms.replace(/\s+/g, "") == null) { appendError("Your search contains only ignored words"); return false; }
	var aryTerms = strVal.value.split(" ");
	if (strSearchOperators.indexOf(" " + aryTerms[0] + " ") != -1 || strSearchOperators.indexOf(" " + aryTerms[aryTerms.length - 2] + " ") != -1) { appendError("Your search begins or ends with an operator"); return false;}
	return true;
}

function imageInitialize(frmVal) {
// access to the form at this point is fairly redundant.
	var elem = frmVal.elements;
	var iCount = 0;
	var imgCollection = document.images;

// Deterimine if the error image has been passed in
	for (var i = 0; i < elem.length; i++) {
		if (elem[i].name.indexOf("FormErrorImg") != -1) {
			txtErrorImg = elem[i].value;
		}
	}
// If first run through save all the images into an array
	if (blnFirstRun) {
		 blnFirstRun = false;
		for (var kex = 0; kex < imgCollection.length; kex++) {
			if (imgCollection[kex].name.indexOf("_imageverify") != -1) {
				aryOrigErrorImg[kex] = imgCollection[kex].src;
			}			
		}
	}
	return true;
}

function validateInput(frmVal) {
	var elem = frmVal.elements;
	var counter = 0;
	aryCbGroup = new Array();
	var imgCollection = document.images;

	imageInitialize(frmVal);
// Reset all images to the original format before doing validation
	if (blnFirstRun == false) {
		for (var kex = 0; kex < imgCollection.length; kex++) {
			if (imgCollection[kex].name.indexOf("_imageverify") != -1) {
				imgCollection[kex].src = aryOrigErrorImg[kex];
			}			
		}
	}	
	for (var i = 0; i < elem.length; i++) {
		if (elem[i].name.indexOf("_verify") != -1 && elem[i].value == "group") {
			aryCbGroup[counter] = new Array(2);
			aryCbGroup[counter][0] = elem[i].name.substring(0, elem[i].name.indexOf("_verify"))
			counter++;
		}
	}

	for (var i = 0; i < elem.length; i++) {
		if (elem[i].type == "text") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[i].value.replace(/\s+/g, "") == "" || elem[i].value.replace(/\s+/g, "") == null) { appendError(elem[i].name); }
				else {
					if (elem[tmp].value == "email") { if (elem[i].value.match(/^((([a-zA-Z0-9-_]*)([.]{0,1})){1,9})(@{1,1})(([a-zA-Z0-9-]*[.]{1,1}){1,9})(\w{2,3})$/) == null) { appendError("Invalid Email"); } }
					if (elem[tmp].value == "usphone") { if (!verifyusphone(elem[i], 1)) { appendError("Invalid phone format"); } }
					if (elem[tmp].value == "uspostal") {
						elem[i].value = elem[i].value.replace(/[^\d, -]+/g, "");
						if (elem[i].value.match(/^(\d{5,5})((-{1,1}\d{4,4})?)$/) == null) { appendError("Invalid Postal Code"); }
					}
					if (elem[tmp].value == "dec-integer") {
						if (parseInt(elem[i].value, 10) == 0 || isNaN(parseInt(elem[i].value, 10))) { appendError(elem[i].name + " is not a valid number"); }
						else { elem[i].value = Math.round(elem[i].value); }
					}
					if (elem[tmp].value == "dec") {
						if (parseInt(elem[i].value, 10) == 0 || isNaN(parseInt(elem[i].value, 10))) { appendError(elem[i].name + " is not a valid number"); }
					
					}
					if (elem[tmp].value == "cc") {
						var tmp2 = elem[i].name + "_type";
						if (elem[tmp2] != null) {
							formatCC(elem[i], elem[tmp2].options[elem[tmp2].selectedIndex].value);
							if (!validCCType(elem[i], elem[tmp2].options[elem[tmp2].selectedIndex].value)) { appendError("Credit Card does not match type selected"); }
							else if (!validCCNumber(elem[i], elem[tmp2].options[elem[tmp2].selectedIndex].value)) { appendError("Credit Card number is invalid"); }
						}
					}
					if (elem[tmp].value == "search") { validSearchTerms(elem[i]); }
				}
			}
		}
		
		if (elem[i].type == "password") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[i].value == "" || elem[i].value == null) { appendError(elem[i].name); }
				if (elem[tmp].value != "" && elem[tmp].value != null) {
					if (elem[i].value != elem[elem[tmp].value].value) { appendError("The passwords provided do not match."); }
				}
			}
		}

		if (elem[i].type == "textarea") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[i].value.replace(/\s+/g, "") == "" || elem[i].value.replace(/\s+/g, "") == null) { appendError(elem[i].name); }
			}
		}
		
		if (elem[i].type == "select-multiple") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[tmp].value == "length") {
					if (elem[i].length == 0) { appendError(elem[i].name); }
				}
			}
		}

		if (elem[i].type == "select-one") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[tmp].value == "length") {
					if (elem[i].length == 0) { appendError(elem[i].name); }
				}
				else {
					if (elem[i].options[elem[i].selectedIndex].value == "" || elem[i].options[elem[i].selectedIndex].value == null) { appendError(elem[i].name); }
				}
			}
		}

		if (elem[i].type == "file") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[i].value.replace(/\s+/g, "") == "" || elem[i].value.replace(/\s+/g, "") == null) { appendError(elem[i].name); }
				else {
					elem[i].value.toLowerCase();
					if (elem[tmp].value.toLowerCase() != (elem[i].value.substring(elem[i].value.length - 3, elem[i].value.length)).toLowerCase()) { appendError(elem[i].name + " must be of type " + elem[tmp].value); }
				}
			}
		}

		if (elem[i].type == "checkbox") {
			var tmp = elem[i].name + "_verify";
			if (elem[tmp] != null) {
				if (elem[tmp].value == "group") {
					if (elem[i].checked) {
						for (j = 0; j < aryCbGroup.length; j++) {
							if (aryCbGroup[j][0] == elem[tmp].name.substring(0, elem[tmp].name.indexOf("_verify"))) { aryCbGroup[j][1] = true; }
						}
					}
				}
				else {
					if (!elem[i].checked) { appendError(elem[i].name); 	}
				}
			}
		}
	}
	
	for (var i = 0; i < aryCbGroup.length; i++) {
		if (aryCbGroup[i][1] != true) { 
		appendError(aryCbGroup[i][0]); 
		}
	}

	for (var i = 0; i < aryFunc.length; i++) { strFuncError = strFuncError + eval(aryFunc[i]); }

	if (strFuncError.length > 0) { blnError = true; }

	if (blnError) {
		var strMsg = "There are errors in your request.\nPlease correct the following errors and try again.\n______________________________________\n\n";
		if (strFuncError.length > 0) { strError = strMsg + strFuncError; }
		else { strError = strMsg + strError; }
		alert (strError);
		strError = "";
		strFuncError = "";
		funcCounter = 0;
		aryFunc.length = 0;
		blnError = false;
		return false;
	}
	return true;
}
// -->
