
/* ============================================================= */

//Purpose: Main validation function for forms
//Input: robjForm: form that needs validation
//Output: false if not a valid form, true if it is a valid form

function doValidation(robjForm) {
	var blnGoodField, strType, blnRequired, intLoopValidate;

	for (intLoopValidate = 0; intLoopValidate < robjForm.length; intLoopValidate++) {
		blnGoodField = true;
		//alert("The name is: " + robjForm[intLoopValidate].name + "\nThe value is: " + robjForm[intLoopValidate].value);

		if ((robjForm[intLoopValidate].type == "button") ||
			(robjForm[intLoopValidate].type == "submit"))
			{ continue; }

		strType = robjForm[intLoopValidate].name.substring(robjForm[intLoopValidate].name.length-4,robjForm[intLoopValidate].name.length);

		if (robjForm[intLoopValidate].name.substring(robjForm[intLoopValidate].name.length-6,robjForm[intLoopValidate].name.length-4) == "_R") {
			blnRequired = true;
		} else {
			blnRequired = false;
		}
		
		//alert("The name is: " + robjForm[intLoopValidate].name + "\nThe value is: " + robjForm[intLoopValidate].value + "\nReq = " + blnRequired + "\nType = " + strType);
				
		// Alpha Numberic
		if (strType == "_aln") {
						
			if (blnRequired) {
		
				if (robjForm[intLoopValidate].value == "")
				{
					blnGoodField = false;
				}
				else
				{
					blnGoodField = isAlphaNum(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isAlphaNum(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Please enter alpha or numeric characters only. \nPlease don't use any spaces.");
				robjForm[intLoopValidate].focus();
			}
		}
		
		// PHONE NUMBER
		if (strType == "_phn") {
			if (blnRequired) {
				blnGoodField = isPhoneNumber(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isPhoneNumber(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE
		if (strType == "_dat") {
			if (blnRequired) {
				blnGoodField = isValidDate(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDate(robjForm[intLoopValidate]);
				}
			}
		}

		// TIME
		if (strType == "_tim") {
			if (blnRequired) {
				blnGoodField = isValidTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidTime(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE or TIME or BOTH
		if (strType == "_dtm") {
			if (blnRequired) {
				blnGoodField = isValidDateTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDateTime(robjForm[intLoopValidate]);
				}
			}
		}

		// DATE and TIME
		if (strType == "_dtr") {
			if (blnRequired) {
				blnGoodField = isValidDateAndTime(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidDateAndTime(robjForm[intLoopValidate]);
				}
			}
		}

		// NUMBER
		if (strType == "_num") {
			if (blnRequired) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isNumber(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isNumber(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid number");
				robjForm[intLoopValidate].focus();
			}
		}

		// INTEGER
		if (strType == "_int") {
			if (blnRequired) {
				if (robjForm[intLoopValidate].value == "") {
					blnGoodField = false;
				} else {
					blnGoodField = isInteger(robjForm[intLoopValidate].value);
				}
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isInteger(robjForm[intLoopValidate].value);
				}
			}
			if (!blnGoodField) {
				alert("Not a valid integer");
				robjForm[intLoopValidate].focus();
			}
		}

		// CURRENCY
		if (strType == '_cur') {
			if (blnRequired) {
				if (robjForm[intLoopValidate].value == '') {
					blnGoodField = false;
				} else {
					blnGoodField = ForceCurrency(robjForm[intLoopValidate]);
				}
			} else {
				if (robjForm[intLoopValidate].value != '') {
					blnGoodField = ForceCurrency(robjForm[intLoopValidate]);
				}
			}
			if (!blnGoodField) {
				alert('Not a valid currency entry');
				robjForm[intLoopValidate].focus();
			}
		}

		// EMAIL ADDRESS
		if (strType == "_eml") {
			if (blnRequired) {
				blnGoodField = isValidEmail(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidEmail(robjForm[intLoopValidate]);
				}
			}
		}

		// URL (WEB ADDRESS)
		if (strType == "_url") {
			if (blnRequired) {
				blnGoodField = isValidURL(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidURL(robjForm[intLoopValidate]);
				}
			}
		}

		// TEXT STRING
		if (strType == "_txt") {
			if ((blnRequired) && (robjForm[intLoopValidate].value == "")) {
				alert("Please enter the proper information");
				robjForm[intLoopValidate].focus();
				blnGoodField = false;
				
			}
		}

		// ZIPCODE (5 OR 9 DIGITS)
		if (strType == "_zip") {
			if (blnRequired) {
				blnGoodField = isValidZip(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip(robjForm[intLoopValidate]);
				}
			}
		}

		// 5 DIGIT ZIPCODE
		if (strType == "_zp5") {
			if (blnRequired) {
				blnGoodField = isValidZip5(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip5(robjForm[intLoopValidate]);
				}
			}
		}

		// 4 DIGIT ZIPCODE
		if (strType == "_zp4") {
			if (blnRequired) {
				blnGoodField = isValidZip4(robjForm[intLoopValidate]);
			} else {
				if (robjForm[intLoopValidate].value != "") {
					blnGoodField = isValidZip4(robjForm[intLoopValidate]);
				}
			}
		}

		// SINGLE SELECTION BOX
		if (strType == "_sel") {
			if (blnRequired) {
				if (robjForm[intLoopValidate].selectedIndex == 0) {
					alert("Please select a value");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// SINGLE SELECT LIST BOX
		if (strType == "_ssl") {
			if (blnRequired) {
				if (robjForm[intLoopValidate].selectedIndex == -1) {
					alert("Please select a value from the list");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// MULTIPLE SELECT LIST BOX
		if (strType == "_msl") {
			if (blnRequired) {
				if (robjForm[intLoopValidate].selectedIndex == -1) {
					alert("Please select at least one value from the list");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// OPTION BUTTON
		if (strType == "_opt") {
			if (blnRequired) {
				var blnAnyOptionsSelected = false;
				for (var intOpts = 0; intOpts < robjForm(robjForm[intLoopValidate].name).length; intOpts++) {
					if (robjForm(robjForm[intLoopValidate].name)[intOpts].checked == true) {
						blnAnyOptionsSelected = true;
					}
				}
				if (blnAnyOptionsSelected == false) {
					alert("Please select one of these options");
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}

		// CHECK BOX
		if (strType == "_chk") {
			if (blnRequired) {
				var blnAnyCheckedOff = false;
				var intNumberOfCheckBoxes = 0;
				for (var intElems = 0; intElems < robjForm.length; intElems++) {
					if (similarCheckBoxNames(robjForm.elements[intElems].name,robjForm[intLoopValidate].name)) {
						intNumberOfCheckBoxes++;
						if (robjForm.elements[intElems].checked == true) {
							blnAnyCheckedOff = true;
						}
					}
				}
				if (blnAnyCheckedOff == false) {
					if (intNumberOfCheckBoxes == 1) {
						alert("Please check this box to continue");
					} else {
						alert("Please check at least one of these boxes to continue");
					}
					robjForm[intLoopValidate].focus();
					blnGoodField = false;
				}
			}
		}
		
		if (!blnGoodField) {
			return false;
		}
	}
	return true;
}

function similarCheckBoxNames(strName1, strName2) {
	if (strName1.substring(strName1.length-6,strName1.length) != '_R_chk') return false;
	if (strName2.substring(strName2.length-6,strName2.length) != '_R_chk') return false;
	var intPos1 = 0;
	var intPos2 = 0;
	intPos1 = strName1.indexOf('_');
	intPos2 = strName1.indexOf('_');
	//alert(strName1+'\n'+intPos1);
	//alert(strName2+'\n'+intPos2);
	if (strName1.substring(0,intPos1+1) == strName2.substring(0,intPos2+1)) {
		//alert('They are in the same checkbox typegroup thingies...');
		return true;
	} else {
		return false;
	}
}

/* ============================================================= */

