/*********************************************************
*  validate(string formName, string[] arrayValidationStrings, string errorDisplay)
*
*      Validates a given form using an array of validation options. The length 
*      of the array of validation options must must match the number of non-
*      button type elements in the given form.
*
*      arrayValidationStrings must contain comma (,) delimited strings
*      representing validation types, i.e. "mandatory,alpha,email".
*
*      The following are valid validation types:
*           alpha - denotes the field must contain only alpha chars
*           numeric - denotes the field must contain only numeric chars
*           email - denotes the field must match an email string-pattern
*           mandatory - denotes the field must not be empty
*
*  parameters:
*      formName - (string) the DOM name of the form to be validated
*
*      arrayValidationStrings - (string[]) an array of comma-delimited strings
*      representing validation types for each non-button element in the
*      form. Must match the number of non-buttom elements in the form
*
*      errorDisplay - (string) the DOM name of the error container. Errors
*      messages relating to the validation of the form are printed to this
*      container.
*
*  returns:
*      (boolean) true - if all form elements pass all validation checks.
*      Note: will also return true if invalid parameters are provided. In this
*      case validation must rely on a server-side validation.
*      (boolean) false - if a validation check fails.
*
*  version: 1.2
*  author: paolo.estrella@imdproductions.com
*  change log:
*      09-04-2005 v0.1 - beta version
*      04-06-2005 v1.0 - isNumeric(String) function added
*                             - numeric validation type added
*                             - bug that causes empty (non mandatory) fields to be 
*                               evaluated fixed
*      06-06-2005 v1.1 - fixed bug in isEmail(String) which caused emails with
*                                not from the US (.com, .edu, .net etc.) to be 
*                                incorrectly rejected, i.e. would not recognise emails 
*                                ending in .com.au or .co.uk to be valid emails.
*      12-06-2005 v1.2 - fixed bug in checking wether input type is valid type
*                                i.e., should not evaluate input types such as button,
*                                submit, image or hidden. isUserInput(Object)
*
*********************************************************/
function validateForm(formName, arrayValidationStrings, errorDisplay) {

    var nFields = eval("document." + formName + ".elements").length;

    // validate parameters
    var nValidFormElements = 0;
    for (var n=0; n<nFields; n++) {
        if (isUserInput(eval("document." + formName).elements[n])) {
            nValidFormElements ++;
        }
    }
    if (arrayValidationStrings.length!=nValidFormElements) {
        // invalid length: arrayValidationStrings. Function returns true - rely on form validation to be done server-side
        return true;
    }

    for (var i=0; i<nFields; i++) {
        if (!isUserInput(eval("document." + formName).elements[i])) {
            // only validates user input type fields (i.e., not buttom, submit, image, hidden)
            continue;
        }
        var fieldName = eval("document." + formName).elements[i].title;
        var fieldValue = eval("document." + formName).elements[i].value;
        var validationTypes = arrayValidationStrings[i].split(",");	// declare the validation array for this field

        for (var j=0; j<validationTypes.length; j++) {
            switch (validationTypes[j]) {
                case "mandatory" :
                    if (fieldValue=="") {
                        showError(errorDisplay, "! - Please provide all mandatory fields");
                        eval("document." + formName).elements[i].focus();
                        return false;
                    }
                    break;
                case "alpha" :
                    if (fieldValue.length>0 && !isAlpha(fieldValue)) {
                        showError(errorDisplay, "! - Please enter a valid " +fieldName+ ". Alpha characters only.");
                        eval("document." + formName).elements[i].focus();
                        return false;
                    }
                    break;
                case "numeric" :
                    if (fieldValue.length>0 && !isNumeric(fieldValue)) {
                        showError(errorDisplay, "!- Please enter valid " +fieldName+ ". Numeric characters only.")
                        eval("document." + formName).elements[i].focus();
                        return false;
                    }
                    break;
                case "email" :
                    if (fieldValue.length>0 && !isEmail(fieldValue)) {
                        showError(errorDisplay, "! - Please enter a valid email address.");
                        eval("document." + formName).elements[i].focus();
                        return false;
                    }
            }
        }
    }
}

// returns true if str contains only alpha characters
// note: allows '-' characters only when there are alpha chars aswell
function isAlpha(str) {
    if (str.search(/[^a-zA-Z-\s]/)==-1) {
        return (str.search(/[a-zA-Z]/)!=-1);
    }
    return false;
}

function isEmail(str) {
    if (str.search(/^[a-zA-Z0-9_.]+@[a-zA-Z0-9]+(\.[a-zA-Z]+)+$/)!=-1) {
        return true;
    }
    return false;
}

function isUserInput(formObj) {
    return (formObj.type.search(/^submit$|^button$|^image$|^hidden$/)==-1);
}

function showError(id, message) {
    errorHTML = document.getElementById(id);
    errorHTML.innerHTML = message;
    errorHTML.style.position = "relative";
    errorHTML.style.visibility = "visible";
}

function isNumeric(str) {
    return (str.search(/[^0-9]/)==-1);
}