/***********************************************
 ** Form validation
 ** ===============
 ** Usage:
 ** 
 ***********************************************/
var beginRequestAlertForText = "Please include a ";
var beginRequestAlertGeneric = "Please choose a ";
var endRequestAlert = ".";
var beginInvalidAlert = " is an invalid ";
var endInvalidAlert = "!";
var beginFormatAlert = "	Use this format: ";

function validation(realName, formEltName, eltType, validationFunction, format, canBeBlank, relationship) {
	this.realName = realName;
	this.formEltName = formEltName;
	this.eltType = eltType;
	this.validationFunction = validationFunction;
	this.format = format;
	this.canBeBlank = canBeBlank;
	this.relationship = relationship;
}

function isSelect(formObj)
{
	return (formObj.selectedIndex != 0);
}

function isRadio(formObj)
{
	for (j=0; j<formObj.length; j++)
	{
		if (formObj[j].checked)
		{
			return true;
		}
	}
	return false;
}

function isCheck(formObj, form, begin, num)
{
	for (j=begin; j<begin+num; j++)
	{
		if (form.elements[j].checked)
		{
			return true;
		}
	}
	return false;
}

function isText(str)
{
	return (str != "");
}

function isNumeric(str, digits)
{
	var re = /^[0-9.]+$/gi;
	if(digits != null && str.length < digits){return false;}
	if(re.test)
    {
		if (!re.test(str)){return false;}
    }
    else
    {
		for (j=0; j < str.length; j++)
		{
			if(((str.charAt(j) < "0") || (str.charAt(j) > "9")) && str.charAt(j) != "."){return false;}
		}
    }
	return true;
}

function isEmail(str)
{
	var re = /^(\.|\w+)+@\w+(\.\w+)+$/gi;
	if(re.test)
    {
		if (!re.test(str)){return false;}
    }
    else
    {
		if((str.indexOf("@") == -1) || (str.indexOf(".") == -1)){return false;}
    }
	return true;
}

function isPhoneNum(str)
{
	var re = /^([0-9]+\-|\ ){0,2}[0-9]+$/gi;
	var vNoNumber = 0;
	//If regular expressions are supported, use them otherwise use backward compatible javascript
	if(re.test)
	{
		if (!re.test(str))
		{
			return false;
		}
	}
	else
	{
		if((str.length > 12) || (str.length < 3)){vRet = false;}
		for (j=0; j<str.length; j++)
		{
			if((str.charAt(j) != "-") && 
				(str.charAt(j) != " ") && 
				((str.charAt(j) < "0") || (str.charAt(j) > "9")))
			{
				return false;
			}
			//Count the number of non-number characters
			if((str.charAt(j) == "-") || (str.charAt(j) == " ")){vNoNumber++;}
		}
		//If more than 2 non-number characters, return false;
		if(vNoNumber > 2){return false;}
	}
	return true;
}

function isDate(str) {
	if (str.length != 10) {return false;}

	for (j = 0; j < str.length; j++)
	{
		if ((j == 4) || (j == 7))
		{
			if (str.charAt(j) != "-") {return false;}
		}
		else
		{
			if ((str.charAt(j) < "0") || (str.charAt(j) > "9")) {return false;}
		}
	}

	var month = str.charAt(5) == "0" ? parseInt(str.substring(6,7)) : parseInt(str.substring(5,7));
	var day = str.charAt(8) == "0" ? parseInt(str.substring(9,10)) : parseInt(str.substring(8,10));
	var begin = str.charAt(0) == "0" ? (str.charAt(1) == "0" ? (str.charAt(2) == "0" ? 3 : 2) : 1) : 0;
	var year = parseInt(str.substring(begin, 4));

	if (day == 0) { return false }
	if (month == 0 || month > 12) { return false }
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
		if (day > 31) { return false }
	} else {
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			if (day > 30) { return false }
		} else {
			if (year%4 != 0) {
				if (day > 28) { return false }
			} else {
				if (day > 29) { return false }
			}
		}
	}
	return true;
}

function validateForm(form, validationArray)
{
	var formEltName = "";
	var formObj = "";
	var str = "";
	var canBeBlank = false;
	var relationship = "";
	var realName = "";
	var alertText = "";
	var firstMissingElt = null;
	var hardReturn = "\r\n";

	for(var i = 0; i < validationArray.length; i++)
	{
		formEltName = validationArray[i].formEltName;
		formObj = eval("form." + formEltName + ";");
		realName = validationArray[i].realName;
		canBeBlank = validationArray[i].canBeBlank;
		relationship = validationArray[i].relationship;
		
		if(relationship != null && formObj.value != "")
		{
			str = eval("form." + validationArray[relationship].formEltName + ".value");
			if (eval(validationArray[relationship].validationFunction) && 
					eval("form." + validationArray[relationship].formEltName + ".value != ''")){}
			else
			{
				if(eval("form." + validationArray[relationship].formEltName + ".value == ''"))
                {
					alertText = beginRequestAlertForText + validationArray[relationship].realName + endRequestAlert + hardReturn;
					alert(alertText);
					formObj = eval("form." + validationArray[relationship].formEltName);
					if (formObj.select) formObj.select();
					if (formObj.focus) formObj.focus();
					return false;
                }
				if(!eval(validationArray[relationship].validationFunction))
                {
					alertText = str + beginInvalidAlert + validationArray[relationship].realName + endInvalidAlert + hardReturn;
					if (validationArray[relationship].format != null)
					{
						alertText += beginFormatAlert + validationArray[relationship].format + hardReturn;
					}
					alert(alertText);

					formObj = eval("form." + validationArray[relationship].formEltName);
					if (formObj.select) formObj.select();
					if (formObj.focus) formObj.focus();
					return false;
                }
			}
		}
		
		if (validationArray[i].eltType == "text")
		{
			str = formObj.value;

			if(canBeBlank && str == ""){continue;}
			 			
			if (eval(validationArray[i].validationFunction)) continue;

			if (str == "")
			{
				alertText = beginRequestAlertForText + realName + endRequestAlert + hardReturn;
				alert(alertText);
			}
			else
			{
				alertText = str + beginInvalidAlert + realName + endInvalidAlert + hardReturn;
				if (validationArray[i].format != null)
				{
					alertText += beginFormatAlert + validationArray[i].format + hardReturn;
				}
				alert(alertText);
			}
		}
		else
		{
			if (eval(validationArray[i].validationFunction)) continue;
			alertText = beginRequestAlertGeneric + realName + endRequestAlert + hardReturn;
			alert(alertText);
		}
		if (formObj.select) formObj.select();
		if (formObj.focus) formObj.focus();
		return false;
	}
	return true;
}