/*

Form Validate
Version: 0.1
Author:  Nicholas Berry <work@nickberry.com>
Description:
  Form Validate will verify and check input/select/textarea fields in a given form and report to the end-user
  on failed conditions.

Options:
    required ~ if a field is not required (i.e. true), then it does not need to be in the list.
        Possible values: true, false, optional
        
    type     ~ verifies the value of the field matches the data type
                   needed (i.e. credit card numbers are always numeric)

    title    ~ used for Javascript alerts, user-friendly references to failed fields

    length   ~ if defined, the field will have its maxlength defined as this number

Example:

var fields = {
    form_amount: {
        required: true,
        type: 'numeric',
        title: 'Donation Amount' },
    form_FirstName: {
        required: true,
        title: 'Your First Name' },
    form_Zip: {
        required: true,
        type: 'numeric',
        length: 5,
        title: "Your Zip Code" }
    };
var fv = new formValidate('FormID',fields);
fv.verify();
fv.check();


*/

var formValidate = new Class({
    options: {
        formID: '',
        formFields: []
    },

    initialize: function(options){
        //this.setOptions(options);
        this.form = options['formID'];
        this.formFields = options['formFields'];
    },

    verify: function() {
        var message = '';
        lookForField = this.lookForField;
        var form = this.form;

        this.formFields.each(function(options,fieldName){
            var found = lookForField(form,fieldName);

            if (!found && options['required']) {
                message += "\n - "+fieldName;
            }

            if (found && options['length']) {
                found.setProperty('maxlength',options['length']);
            }
        });

        if (message) {
            alert("Error!  Fields not found:"+message+"\n\nPlease check your configuration");
        }
    },

    lookForField: function(form,fieldName){
        var found;
        var form = $(form);
        ['input','select','textarea'].each(function(fieldType){
            // comes out like this: input[name=fname], searches for <input name='fname'>
            var selector = fieldType+'[name='+fieldName+']';
            if (!found) { found = form.getElement(selector); }
        });
        return found;
    },

    check: function(form,formFields) {
        var form = this.form;
        var formFields = this.formFields;
        var message = '';
        lookForField = this.lookForField;
        formFields.each(function(options,fieldName){
            var found = lookForField(form,fieldName);
            if (found && options['required'] && found.value == '') {
                message += "\n - "+options['title'];
            }
        });
        if (message) {
            alert("The following fields were not completed:"+message+"\n");
            return false;
        }
        return true;
    }

});
