// JavaScript Form Validator
// April 13, 2004
// ken.dickson@osc.on.ca

bigError = new Array();

function validateForm(validateOptions) {
	for (x = 1; x < validateOptions.length; x++) for (y = 1; y < validateOptions[x].length; y++) if (validateCheckError(validateOptions[0][0], validateOptions[x][0], validateOptions[x][y])) break;
	
	if (bigError.length > 0) {
		theErrorMessage = validateOptions[0][1] + "\n\n";
		for (x = 0; x < bigError.length; x++) theErrorMessage += bigError[x] + "\n";
		bigError = new Array();
		alert(theErrorMessage);
		return false;
	} else return true;
}

function validateCheckError(formName, formField, evalOptions) {
	theError = "";

	//checkEmpty[error message, value]
	//checkEmail[error message, value]
	//checkInteger[error message, value]
	//checkNumber[error message, value]
	//checkAlpha[error message, value]
	//checkAlphaNumeric[error message, value]
	//checkAbsLength[error message, value, absLength]
	//checkMinLength[error message, value, minLength]
	//checkMaxLength[error message, value, maxLength]
	//checkMinValue[error message, value, low value]
	//checkMaxValue[error message, value, high value]
	//checkCanadianPostal[error message, value] A8A 8A8 or A8A8A8
	//checkAmericanZip[error message, value] 12345 or 12345-1234
	//checkPhone[error message, value] (999) 999-9999 or (999)999-9999
	//checkValidDate[error message, month value, day value, year value] 3 30 1975
	//checkMinAge[error message, value, month value, day value, year value] 28 3 30 1975
	//checkMatch[error message, other value]
	
	if (evalOptions[0] == "checkEmpty") theError = checkEmpty(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkEmail") theError = checkEmail(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkInteger") theError = checkInteger(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkNumber") theError = checkNumber(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkAlpha") theError = checkAlpha(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkAlphaNumeric") theError = checkAlphaNumeric(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkAbsLength") theError = checkAbsLength(evalOptions[1], getFieldValue(formName+"."+formField), evalOptions[2]);
	else if (evalOptions[0] == "checkMinLength") theError = checkMinLength(evalOptions[1], getFieldValue(formName+"."+formField), evalOptions[2]);
	else if (evalOptions[0] == "checkMaxLength") theError = checkMaxLength(evalOptions[1], getFieldValue(formName+"."+formField), evalOptions[2]);
	else if (evalOptions[0] == "checkMinValue") theError = checkMinValue(evalOptions[1], getFieldValue(formName+"."+formField), evalOptions[2]);
	else if (evalOptions[0] == "checkMaxValue") theError = checkMaxValue(evalOptions[1], getFieldValue(formName+"."+formField), evalOptions[2]);
	else if (evalOptions[0] == "checkCanadianPostal") theError = checkCanadianPostal(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkRejectURL") theError = checkRejectURL(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkAmericanZip") theError = checkAmericanZip(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkPhone") theError = checkPhone(evalOptions[1], getFieldValue(formName+"."+formField));
	else if (evalOptions[0] == "checkValidDate") theError = checkValidDate(evalOptions[1], getFieldValue(formName+"."+evalOptions[2]), getFieldValue(formName+"."+evalOptions[3]), getFieldValue(formName+"."+evalOptions[4]));
	else if (evalOptions[0] == "checkMinAge") theError = checkMinAge(evalOptions[1], evalOptions[2], getFieldValue(formName+"."+evalOptions[3]), getFieldValue(formName+"."+evalOptions[4]), getFieldValue(formName+"."+evalOptions[5]));
	else if (evalOptions[0] == "checkMatch") theError = checkMatch(evalOptions[1], getFieldValue(formName+"."+formField), getFieldValue(formName+"."+evalOptions[2]));
	else alert("BUG BUG: "+evalOptions[1]+"??");

	if (theError != "") {
		validateError(theError);
		return true;
	} else return false;
}

function checkEmpty(theError, theValue) {
	if (theValue.length == 0 || theValue == "" || (theValue == false && theValue != "0")) return theError; else return "";
}

function checkEmail(theError, theValue) {
	validEmail = /^[a-zA-Z0-9]([a-zA-Z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/;
	if ((theValue.length > 0) && (!validEmail.test(theValue))) return theError; else return "";
}

function checkInteger(theError, theValue) {
	validInteger = /^-?\d\d*$/;
	if ((theValue.length > 0) && (!validInteger.test(theValue))) return theError; else return "";
}

function checkNumber(theError, theValue) {
	validNumber = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	if ((theValue.length > 0) && (!validNumber.test(theValue))) return theError; else return "";
}

function checkAlpha(theError, theValue) {
	validAlpha = /^[a-zA-Z]*$/;
	if ((theValue.length > 0) && (!validAlpha.test(theValue))) return theError; else return "";
}

function checkAlphaNumeric(theError, theValue) {
	validAlphaNumeric = /^[a-zA-Z\d]*$/;
	if ((theValue.length > 0) && (!validAlphaNumeric.test(theValue))) return theError; else return "";
}

function checkAbsLength(theError, theValue, varLength) {
	if ((theValue.length > 0) && (theValue.length != eval(varLength))) return theError; else return "";
}

function checkMinLength(theError, theValue, varLength) {
	if ((theValue.length > 0) && (theValue.length < eval(varLength))) return theError; else return "";
}

function checkMaxLength(theError, theValue, varLength) {
	if ((theValue.length > 0) && (theValue.length > eval(varLength))) return theError; else return "";
}

function checkMinValue(theError, theValue, varValue) {
	if ((theValue.length > 0) && (isNaN(theValue) || (parseFloat(theValue) < parseFloat(eval(varValue))))) return theError; else return "";
}

function checkMaxValue(theError, theValue, varValue) {
	if ((theValue.length > 0) && (isNaN(theValue) || (parseFloat(theValue) > parseFloat(eval(varValue))))) return theError; else return "";
}

function checkCanadianPostal(theError, theValue) {
	validCanadianPostal = /[a-zA-Z][0-9][a-zA-Z]\s?[0-9][a-zA-Z][0-9]/;
	if ((theValue.length > 0) && (!validCanadianPostal.test(theValue))) return theError; else return "";
}

function checkAmericanZip(theError, theValue) {
	validAmericanZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if ((theValue.length > 0) && (!validAmericanZip.test(theValue))) return theError; else return "";
}

function checkPhone(theError, theValue) {
	validPhone = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
	if ((theValue.length > 0) && (!validPhone.test(theValue))) return theError; else return "";
}

function checkRejectURL(theError, theValue) {
validURL =  /(http:\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;
	if ((theValue.length > 0) && (validURL.test(theValue))) return theError; else return "";
}

function checkValidDate(theError, varMonth, varDay, varYear) {
	if (varMonth.length > 0 || varDay.length > 0 || varYear.length > 0) {
		errorFlag = 0;
		if (isNaN(varYear) || isNaN(varMonth) || isNaN(varDay)) errorFlag = 1;
		if (varMonth < 0 || varMonth > 11) errorFlag = 1;
		if (varDay < 1 || varDay > 31) errorFlag = 1;
		if (varMonth == 3 || varMonth == 5 || varMonth == 8 || varMonth == 10) if (varDay == 31) errorFlag = 1;
		if (varMonth == 1) {
			if (isNaN(parseInt(varYear / 4))) errorFlag = 1;
			if (varDay > 29) errorFlag = 1;
			if (varDay == 29 && ((varYear / 4) != parseInt(varYear / 4))) errorFlag = 1;
		}
		if (errorFlag == 1) return theError;
	}
	return "";
}

function checkMatch(theError, theValue1, theValue2) {
	if (theValue1 != theValue2) return theError; else return "";
}

function checkMinAge(theError, theValue, varMonth, varDay, varYear) {
	if (varMonth.length > 0 || varDay.length > 0 || varYear.length > 0) {
		testDate = new Date(parseFloat(varYear) + parseFloat(theValue), parseFloat(varMonth) - 1, parseFloat(varDay));
		theYear = new Date().getYear();
		if (theYear.length == 2) theYear = theYear + 1900;
		todayDate = new Date(theYear, new Date().getMonth(), new Date().getDate());
		if (testDate > todayDate) return theError;
	}
	return "";
}

function getFieldValue(evaluateField) {
	evaluateFieldObj = eval(evaluateField);
	if (evaluateFieldObj.type == 'select-one' || evaluateFieldObj.type == 'select-multiple') {
		if (evaluateFieldObj.selectedIndex == -1) return false; else return (evaluateFieldObj.options[evaluateFieldObj.selectedIndex].value);
	} else if (evaluateFieldObj.type == 'checkbox') {
		if (evaluateFieldObj.checked) return true; else return false;
	} else if (evaluateFieldObj[0] && evaluateFieldObj[0].type == 'radio') {
		for (i = 0; i < evaluateFieldObj.length; i++) if(evaluateFieldObj[i].checked) return evaluateFieldObj[i].value; return false;
	} else if (evaluateFieldObj.type == 'radio') {
		if (evaluateFieldObj.checked) return true; else return false;
	} else return(evaluateFieldObj.value);
}

function validateError(errorText) {
	bigError[bigError.length] = errorText;
}
