﻿
function _ggp_validate_radcontrol(sender, args) {

    var control = document.getElementById(sender.get_id());
    var control_id = control.id;
    var control_vid = control.vid;
    var control_value = sender.get_value();

    return _ggp_do_validation(control, control_id, control_vid, control_value);

}

function _ggp_validate_control(e) {

    var control = _ggp_get_event_element(e);
    var control_id = control.id;
    var control_vid = control.vid;
    var control_value = control.value;

    return _ggp_do_validation(control, control_id, control_vid, control_value);
}

function _ggp_do_validation(control,control_id,control_vid,control_value) {

    var tests = GGP.Validations[control_vid].Fields[control_id];
    var empty = control_value == null
                || control_value.length == 0
                || (control.type == "checkbox" && !control.checked);
    var msg = '';
    
    var pass = true;

    for (var t = 0; t < tests.length; t++) {

        var test = GGP.Validations[control_vid].Definitions[tests[t]];

        switch (test.type) {

            case 'Required':
                pass = !empty;
                break;

            case 'Regex':
                pass = empty || test.expr.test(control_value);
                break;

            case 'Confirmation':
                pass = document.getElementById(test.expr).value == control_value;
                break;

        }

        if (!pass) {
            msg = test.msg;
            break;
        }
    }

    var msgel = document.getElementById(control_id + '_error');
    if (msgel) {
        msgel.parentNode.removeChild(msgel)
    }

    if (!pass) {

        var msgtag = 'div'; //msg.length < 16 ? 'span' : 'div';
        var msgel = document.createElement(msgtag);
        msgel.id = control_id + '_error';
        msgel.style.color = 'red';
        msgel.style.fontSize = '9px';
        msgel.style.display = 'inline';
        //msgel.style.textAlign = msgel.style.float = 'right';
        msgel.innerHTML = msg;

        if (control.label) {
            //control.label.width = control.offsetWidth + 'px';
            msgel.style.paddingLeft = '2px';
            control.label.appendChild(msgel);
        } else {
            control.parentNode.insertBefore(msgel, control);
        }
    }

    return pass;
}

function _ggp_validation_init() {

    for (var vid in GGP.Validations) {
        
        for (var cid in GGP.Validations[vid].Fields) {

            var control = document.getElementById(cid);
            control.vid = vid;
            control.label = _ggp_previous_html_element(control);
            _ggp_add_event(control, 'blur', _ggp_validate_control);

        }
        theForm.onsubmit = _ggp_postback_validate;
    }
}

function _ggp_postback_itemclick(validation_id) {
    _ggp_validation_id = validation_id;
    theForm.onsubmit = _ggp_postback_validate;
    //return _ggp_postback_validate(window.event);
}

function _ggp_postback_validate(e) {

    var valid = true;

    if (_ggp_validation_id) {
    
        for (var cid in GGP.Validations[_ggp_validation_id].Fields) {

            var control = document.getElementById(cid);
            var control_value = control.value;

            valid &= _ggp_do_validation(control, cid, _ggp_validation_id, control_value);
        }

        _ggp_validation_id = null;
    }

    if (!valid) {
        if (e && e.preventDefault) {
            e.preventDefault();
        } else if (window.event) {
            window.event.returnValue = false;
        }
    }

    return valid;
}

if (GGP.Validations == undefined) GGP.Validations = {};
var _ggp_validation_id = null;
_ggp_add_event(window, 'load', _ggp_validation_init);


