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) { try { r = o[b](e) != false && r; } catch (ex) { r = false; } } r = 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 addInlineValidator(v, name, required, pattern, message, initialValue, onlyOneMustValidate) {
    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;
    beValidatorQueue[index][6] = onlyOneMustValidate;

    return '<span id="beValidator_' + v + '" style="color: #ff0000; display: none; cursor: help">*</span>';
}

function addValidator(v, name, required, pattern, message, initialValue, onlyOneMustValidate) {
    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;
    beValidatorQueue[index][6] = onlyOneMustValidate;

    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 onlyOneMustValidate = beValidatorQueue[i][6];

        var el = document.getElementById(v);

        if (el == null || document.getElementsByName(v).length > 1)
            el = document.getElementsByName(v);
        else
            el = new Array(el);

        for (var elIndex = 0; elIndex < el.length; elIndex++) {
            e = el[elIndex];

            eval('var beValidatorFunction = function() { return clientValidate(\'' + v + '\', \'' + name + '\', ' + required + ', ' + pattern + ', \'' + message + '\', \'' + initialValue + '\', \'' + onlyOneMustValidate + '\')}');
            e.validationFunction = beValidatorFunction;
            if (isIE) {
                if (e.onchange != null)
                    e.attachEvent('onpropertychange', function() { beValidatorFunction });
                else
                    e.onpropertychange = beValidatorFunction;
            }
            else {
                addEvent(e, 'change', beValidatorFunction);
                addEvent(e, 'keyup', beValidatorFunction);
                addEvent(e, 'mouseup', beValidatorFunction);
            }

            clientValidate(v, name, required, pattern, message, initialValue, onlyOneMustValidate);
        }
    }
}

function initSubmit() {
    for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].hasValidatorFunction != 'true') {
            var fn = '';
            fn += 'if (validateForm(this))\r\n';
            fn += '{\r\n';
            fn += '    if (this.onvalidate != null)\r\n';
            fn += '        return this.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);
            document.forms[i].hasValidatorFunction = 'true';
        }
    }
}

function validateForm(e) {

    var completedElements = new Array();
    var invalid = false;
    var message = '';

    for (var i = 0; i < e.elements.length; i++) {
        var f, t;
        var isComplete = false;

        for (var completedElementID = 0; completedElementID < completedElements.length; completedElementID++) {
            if (completedElements[completedElementID] == e.elements[i].name) {
                isComplete = true;
                break;
            }
        }

        f = eval(e.elements[i].validationFunction);

        if (f != null) {
            try {
                var validationMessage = f();

                if (validationMessage != null) {
                    invalid = true;

                    if (isComplete)
                        continue;

                    completedElements[completedElements.length] = e.elements[i].name;

                    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, onlyOneMustValidate) {
    var invalid = 0;
    var el = document.getElementById(v);

    if (el == null)
        el = document.getElementsByName(v);
    else
        el = new Array(el);

    for (var elIndex = 0; elIndex < el.length; elIndex++) {

        var e = el[elIndex];
        var isVisible = true;
        var cE = e;

        while (cE != null) {
            if (cE.style && (cE.style.visibility == 'hidden' || cE.style.display == 'none')) {
                isVisible = false; break;
            }
            else if (cE.disabled == true) {
                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 || (e.tagName.toUpperCase() == 'SELECT' && e.options[e.selectedIndex].disabled))) {
                    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;
            }
        }

        if (onlyOneMustValidate) {
            if (invalid == 0)
                break;
        }
        else if (invalid > 0)
            break;
    }

    var beValidator = document.getElementById('beValidator_' + v);

    if (beValidator) {
        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(); initSubmit()'));
