//-----------------------------------------
// FormElements - usage
// - create a FormElements Object for each form required
// - add form elements to the frmElemsObject
// - if you want error/instructional messages, define an element with same id as element
//   to validate prefixed with e_  e.g. MyDiv --> e_MyDiv
// - call writeInstruction() and validate() on each element
// - write individual validation functions named as validate[elemId] returning either error message or false if no errors
//-----------------------------------------

//-----------------------------------------
// Container for all elements of the form
//-----------------------------------------
function FormElements() {
    this.elems = new Array();

    this.add = FormElements_add;
    this.validate = FormElements_validate;
    this.validateAll = FormElements_validateAll;
    this.writeInstruction = FormElements_writeInstruction;
}

// Adds an element to the forms object
function FormElements_add(id, required, instructionText, e_id) {
    if (arguments.length == 2) this.elems.push(new FormElement(id, required));
    else if (arguments.length == 3) this.elems.push(new FormElement(id, required, instructionText));
    else this.elems.push(new FormElement(id, required, instructionText, e_id));
}

// Validates the the element
function FormElements_validate(elem) {
    tmpId = elem.id;

    for (var i = 0; i < this.elems.length; i++) {
        if (this.elems[i].id == tmpId) {
            return this.elems[i].validate();
        }
    }

    return false;
}

function FormElements_validateAll() {
    allValid = true;

    for (var i = 0; i < this.elems.length; i++) {
        if (this.elems[i].validate() == false) {
            allValid = false;
        }
    }

    return allValid;
}

// Write the element's instructional text (if any)
function FormElements_writeInstruction(elem) {
    tmpId = elem.id;

    for (var i = 0; i < this.elems.length; i++) {
        if (this.elems[i].id == tmpId) {
            this.elems[i].writeInstruction();
            break;
        }
    }
}


//-----------------------------------------
// Object to map to elements on the form
//-----------------------------------------
function FormElement(id, required, instructionText, e_id) {
    this.id = id;
    this.e_id = (e_id) ? e_id : "e_" + id;
    this.required = required;
    this.isValid = (required) ? false : true;
    this.instructionText = (instructionText) ? instructionText : false;
    this.validationFunction = eval("validate" + id);

    this.validate = FormElement_validate;
    this.writeInstruction = FormElement_writeInstruction;
}

// Validates the element
function FormElement_validate() {
    elem = document.getElementById(this.id);

    if (elem.type == "text" || elem.type == "textarea") elem.value = elem.value.trim();

    validationMsg = this.validationFunction(elem);
    if (!validationMsg) {
        this.isValid = true;
        if (this.instructionText) {
            tmp = document.getElementById(this.e_id);
            tmp.className = "message";
            tmp.innerHTML = "";
        }
    }
    else if (this.instructionText) {
        tmp = document.getElementById(this.e_id);
        tmp.className = "errorMessage";
        tmp.innerHTML = validationMsg;
    }
    return this.isValid;
}

// Write the element's instructional text (if any)
function FormElement_writeInstruction() {
    if (this.required) this.isValid = false;
    if (this.instructionText) {
        tmp = document.getElementById(this.e_id);
        tmp.className = "message";
        tmp.innerHTML = this.instructionText;
    }
}

