// validator.js -- form validation 

// Form Validator //////////////////////////////////////////////////////////////
//                                                                            //
// Content: Haussmann Intranets, http://www.haussmann-hl.de                   //
// Contact: info@haussmann-hl.de                                              //
// Date: 05.12.99                                                             //
//                                                                            //
// Description:                                                               //
// Validation Object and associated routines                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

// REQUIRES //
// - common.js
// - forms.js
// - vldStd.js

// GLOBALS //
var DELIMITER = ',';

// MAIN //
function validateForm(theForm, reqfields) {
    var v = new Validator(theForm, reqfields);
    return v.approve();
}    
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// VALIDATOR OBJECT & METHODS                                                                             //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

function Validator(form, fields) {
    this.vdtStruct = _vdt_create(form, fields);
    this.approve   = _vdt_run_chk;
}

// array of check-elements
function _vdt_create(form, fstr) {
    var code = "", fields, vd_struct;
    
    if (String(fstr).length == 0) {
        return null;
    } else {
        fields = String(fstr).split(DELIMITER);
        // build constructor, initialize
        for (var i = 0; i < fields.length; i++) {
            code += 'this.' + fields[i] + '= thisForm.' + fields[i] + ';';  
        }
        vdt_struct = new Function('thisForm', code);
        return new vdt_struct(form);
    }
}

function _vdt_run_chk() {
    // nothing to check
    if (this.vdtStruct == null) { 
        return true;
    }
    
    for (var field in this.vdtStruct) {
        var fld = this.vdtStruct[field];
        if (!eval('_check_'+field+'(fld)')) {
            fld.focus();
            return false;
        }
    }
    
    return true;
}


