var aeOL = []; function addEvent(o, n, f, l) { var a = 'addEventListener', h = 'on' + n, b = '', s = ''; if (o[a] && !l) return o[a](n, f, false); o._c |= 0; if (o[h]) { b = '_f' + o._c++; o[b] = o[h] } s = '_f' + o._c++; o[s] = f; o[h] = function(e) { e = e || window.event; var r = true; if (b) r = o[b] && o[b](e) != false && r; r = o[s] && o[s](e) != false && r; return r }; aeOL[aeOL.length] = { o: o, h: h} };
String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g, ''); }

var beValidatorQueue = new Array();
var isIE = (document.all != null) && (navigator.userAgent.indexOf('Opera') == -1);

function addValidator(v, name, required, pattern, message, initialValue) {
    var index = beValidatorQueue.length;

    if (!initialValue)
        initialValue = '';

    beValidatorQueue[index] = new Array();
    beValidatorQueue[index][0] = v;
    beValidatorQueue[index][1] = name;
    beValidatorQueue[index][2] = required;
    beValidatorQueue[index][3] = pattern;
    beValidatorQueue[index][4] = message;
    beValidatorQueue[index][5] = initialValue;

    document.write('<span id="beValidator_' + v + '" style="color: #ff0000; display: none; cursor: help">*</span>');
}

function initValidator() {
    for (var i = 0; i < beValidatorQueue.length; i++) {
        var v = beValidatorQueue[i][0];
        var name = beValidatorQueue[i][1];
        var required = beValidatorQueue[i][2];
        var pattern = beValidatorQueue[i][3];
        var message = beValidatorQueue[i][4];
        var initialValue = beValidatorQueue[i][5];

        var e = document.getElementById(v);

        if (e == null) {
            var eArray = document.getElementsByName(v);

            if (eArray.length == 1)
                e = eArray[0];
        }

        if (e != null) {
            eval('var beValidatorFunction = function() {return clientValidate(\'' + v + '\', \'' + name + '\', ' + required + ', ' + pattern + ', \'' + message + '\', \'' + initialValue + '\')}');
            e.validationFunction = beValidatorFunction;
            if (isIE) {
                e.onpropertychange = beValidatorFunction;

                if (e.onpropertychange == null)
                    e.attachEvent('onpropertychange', beValidatorFunction);
            }
            else {
                addEvent(e, 'change', beValidatorFunction);
                addEvent(e, 'keyup', beValidatorFunction);
                addEvent(e, 'mouseup', beValidatorFunction);
            }

            clientValidate(v, name, required, pattern, message, initialValue);
        }
    }

    for (var i = 0; i < document.forms.length; i++) {

        var fn = '';
        fn += 'if (validateForm(document.forms[' + i + ']))\r\n';
        fn += '{\r\n';
        fn += '    if (document.forms[' + i + '].onvalidate != null)\r\n';
        fn += '        return document.forms[' + i + '].onvalidate();\r\n';
        fn += '    else\r\n';
        fn += '        return true;\r\n';
        fn += '}\r\n';
        fn += 'else\r\n';
        fn += '{\r\n';
        fn += '    if (e && e.preventDefault)\r\n';
        fn += '        e.preventDefault();\r\n';
        fn += '    return false;\r\n';
        fn += '}\r\n';

        eval('var beValidatorFunction = function(e) {\r\n' + fn + '\r\n}');
        addEvent(document.forms[i], 'submit', beValidatorFunction);
    }
}

function validateForm(e) {

    var invalid = false;
    var message = '';

    for (var i = 0; i < e.elements.length; i++) {
        var f, t;

        f = eval(e.elements[i].validationFunction);

        if (f != null) {
            try {
                var validationMessage = f();

                if (validationMessage != null) {
                    invalid = true;

                    if (t == null)
                        t = e.elements[i];

                    message += ' - ' + validationMessage + '\n';
                }
            }
            catch (ex) { }
        }
    }

    if (invalid) {
        alert('Please check the following fields:\n\n' + message);

        if (t != null) {
            if (t.scrollIntoView) t.scrollIntoView();
            if (t.select) t.select();

            if (!t.blur)
                if (t.focus) t.focus();
        }

        return false;
    }
    else
        return true;
}

function clientValidate(v, name, required, pattern, message, initialValue) {
    var invalid = 0;
    var e = document.getElementById(v);

    if (e == null) {
        var eArray = document.getElementsByName(v);

        if (eArray.length == 1)
            e = eArray[0];
    }

    var isVisible = true;
    var cE = e;

    while (cE != null) {
        if (cE.style && (cE.style.visibility == 'hidden' || cE.style.display == 'none')) {
            isVisible = false; break;
        }

        cE = cE.parentNode;
    }

    if (isVisible) {
        if (!initialValue)
            initialValue = '';

        if (e != null) {
            if (e.type.toUpperCase() == 'RADIO' || e.type.toUpperCase() == 'CHECKBOX') {
                if (required == true) {
                    invalid = 1;

                    var radios = document.getElementsByName(e.name);

                    for (var i = 0; i < radios.length; i++) {
                        if (radios[i].checked) {
                            invalid = 0;
                            break;
                        }
                    }
                }
            }
            else if (required == true && e.value.trim() == initialValue)
                invalid = 1;
            else if (e.value.trim() != '' && pattern != null) {
                var re = new RegExp(pattern);
                re.global = true;

                if (re.test(e.value))
                    invalid = 0;
                else
                    invalid = 2;
            }
            else
                invalid = 0;
        }
    }

    var beValidator = document.getElementById('beValidator_' + v);

    if (invalid > 0)
        beValidator.style.display = 'inline';
    else
        beValidator.style.display = 'none';

    if (invalid == 1) {
        beValidator.title = 'Required Field';
        return name + ' (Required Field)';
    }
    else if (invalid == 2) {
        beValidator.title = message;
        return name + ' (' + message + ')';
    }
    else {
        return null;
    }
}

addEvent(window, 'load', new Function('initValidator()'));