/* Validates form data prior to submission.  Any fields with errors will
   be colored a specified color.
   Usage:
    1)  Create a new Validator object, specifying the type of validation
        to be performed and the color to use for invalid fields.
    2)  Call the Validator.AddField method to add the fields to be
        validated.
    3)  Call the Validator.Validate method in the submit button's onClick
        event. Returns true if all fields are valid. Returns false one or
        fields are not valid.
*/

function Validator_SetInvalidColor(strColor) {
    // Set the color for invalid fields.
    this.invalidColor = strColor;
}

function Validator_AddFields(objFields) {
    // Add one or more fields to be validated.
    this.fields = this.fields.concat(objFields);
}

function Validator_AddFieldNames(strFieldNames) {
    // Add one or more fields, based on field name, to be validated.
    for (var i = 0; i < strFieldNames.length; i++) {
		for (var j = 0; j < document.getElementsByName(strFieldNames[i]).length; j++) {
	        objField = document.getElementsByName(strFieldNames[i])[j];
	        this.fields = this.fields.concat(objField);
		}
    }
}

function Validator_SetMessage(strMessage) {
    // Set the message displayed when fields are invalid.
    this.message = strMessage;
}

function Validator_Validate() {
    // Validate all the fields for this Validator.
    var objField;
    var intInvalids = 0;
    for (var i = 0; i < this.fields.length; i++) {
        objField = this.fields[i];
        if (!this.validateFunction(objField)) {
            this.originalColors[objField] = objField.style.backgroundColor;
            objField.style.backgroundColor = this.invalidColor;
            intInvalids += 1;
        }
    }
    if (intInvalids == 0) {
        return true;
    } else {
        alert(this.message)
        return false;
    }
}

function Validator_ValidateSilent() {
    // Validate all the fields for this Validator.
    var objField;
    var intInvalids = 0;
    for (var i = 0; i < this.fields.length; i++) {
        objField = this.fields[i];
        if (!this.validateFunction(objField)) {
            //this.originalColors[objField] = objField.style.backgroundColor;
            //objField.style.backgroundColor = this.invalidColor;
            intInvalids += 1;
        }
    }
    if (intInvalids == 0) {
        return true;
    } else {
        //alert(this.message)
        return false;
    }
}

function Validator_ResetFields() {
    // Reset the color of invalid fields back to their stored color.
    var objField;
    for (var i = 0; i < this.fields.length; i++) {
        objField = this.fields[i];
        objField.style.backgroundColor = this.originalColors[objField];
    }
    this.originalColors = new Array();
}

function Validator(funValidate, strColor, strMessage) {
    // Constructor for new Validator objects.
    this.validateFunction = funValidate; // Function used to validate fields
    this.invalidColor = strColor;        // Color to use on invlaid fields
    this.message = strMessage;           // Message to display when fields invalid
    this.originalColors = new Array();   // An array
    this.fields = new Array();
}

Validator.prototype.SetInvalidColor = Validator_SetInvalidColor;
Validator.prototype.AddFields = Validator_AddFields;
Validator.prototype.AddFieldNames = Validator_AddFieldNames;
Validator.prototype.SetMessage = Validator_SetMessage;
Validator.prototype.Validate = Validator_Validate;
Validator.prototype.ValidateSilent = Validator_ValidateSilent;
Validator.prototype.ResetFields = Validator_ResetFields;

/* VALIDATION FUNCTIONS
   One of these functions must be supplied to the Validator constructor.
*/

function VF_NonEmptyText(objField) {
    // True: length of the field's value is greater than zero.
    // False: length of the field's value is zero.
    return (objField.value.length > 0);
}

function VF_USDate(objField) {
    // True: value fits format of MM/DD/YYYY.
    // False: value does not fit format.
    var intDay, intMonth, intYear;
    var arrDate;
    if (objField.value.length == 0) {
        return true;
    }
    arrDate = objField.value.split("/");
    if (arrDate.length != 3) {
        return false;
    }
    intMonth = Number(arrDate[0]);
    intDay = Number(arrDate[1]);
    intYear = Number(arrDate[2]);
    if ((isNaN(intMonth) || isNaN(intDay) || isNaN(intYear)) ||
        (intMonth < 1 || intMonth > 12) ||
        (intDay < 1 || intDay > 31) ||
        (intYear < 1990)) {
        return false;
    }
    return true;
}

function VF_NonEmptySelect(objField) {
	// True: length of selected option value is greater than zero.
	// False: length of selected option value is zero.
	return (objField.options[objField.selectedIndex].value.length > 0);
}
