/*
Script name: formHandlers.js

Author: Marcus Kielly
Date: 23 March 2009
Company: Deckchair

Description:
This script contains a group of function which prepare
the host document's forms for validation and pass data
between the core DC form validation functions.
This enables greater abstraction of function and looser
coupling. Think of these functions as the controllers,
and the functions they call as the beasts of burden...
*/




/*
CHECK FORM VALUES
--------------------------------------------------------------------------
Description:
sends the form isolated by SUBMIT FORM to testFormInput() for validation
and return error array to callee

RELATIONSHIPS:
- FUNCTION: testFormInput()
- SCRIPT: formTest.js

*/

function checkFormValuesGrp(currentForm)
    {
    //initialise error array
    var errorArray=[];
    var inputArray = [];
    var textArray = [];

    //acquire references to all inputs for current form
    var currentFormInputs = currentForm.getElementsByTagName("input");
    var currentFormText = currentForm.getElementsByTagName("textarea");

    if(currentFormInputs.length>0){
        for(var j=0; j < currentFormInputs.length; j++){
            inputArray.push(currentFormInputs[j]);
            }
        }

    if(currentFormText.length>0){
        for(var k=0; k < currentFormText.length; k++){
            inputArray.push(currentFormText[k]);
            }
        }

    if(inputArray.length>0){

        //iterate through the inputs and validate in the testFormInput function
        for ( var i=0; i < inputArray.length; i++ ){
            //create an array of all elements returned from testFormInput
            errorArray.push(testFormInput(inputArray[i]));
            }
        return errorArray;

        }

    }




/*
SUBMIT FORM
--------------------------------------------------------------------------
Description:
Handles the form events and passes data between the various core functions

RELATIONSHIPS:

- FUNCTION: checkFormValues()
- SCRIPT: formHandle.js
- sends current form to checkform, which retrieves an array of errors

- FUNCTION: formErrorDisplay()
- SCRIPT: formErrorDisplay.js
- sends array to form formErrorDisplay() (in formErrorDisplay.js) for styling

*/

function submitFormGrp(currentForm)
{

var errorArray=checkFormValuesGrp(currentForm);

var msgArray = [];
var inputMsg = [];


for (var i=0;i < errorArray.length;i++){
    //only report on those elements that return errors

    var errorDefined=(typeof(errorArray[i]) == "undefined")?  false: true;
    if(errorDefined){

        //extract title from the returned elements
        var inputTitle = errorArray[i][0].getAttribute("title");

        //extract error type
        var inputErrorType = errorArray[i][1];
        inputMsg=[inputTitle,inputErrorType];
        msgArray.push(inputMsg);
        }
    //retrieve messages from msgArrayParsedFunction
    var msgArrayParsed = errMsgGen(msgArray);
    }
    //pass results to formErrorDisplay
    formErrorDisplay(currentForm,msgArrayParsed,"dom","errors","err");

if (msgArray.length>0){
    return false;
    }
else
    {
    return true;
    }
}




/*
PREPARE FORM
--------------------------------------------------------------------------
Description:
This script automatically locates all forms in a document and
applies an event triggered function (submitForm) to them

RELATIONSHIPS:

- FUNCTION: submitForm()
- SCRIPT: formHandle.js
- sends current form to submitForm(), which then handles interchange of data
- between core functions

to separate bulk checks from individual checks
*/

function prepForms() {
    forms=document.getElementsByTagName("form");
    for(var i=0;i<forms.length;i++){
        forms[i].onsubmit= function(){return submitFormGrp(this)};
        }
    }




// INITIATE SCRIPT SUITE
//--------------------------------------------------------------------------------------------------
window.onload = prepForms;

