﻿/* Leon Roadley */

function exists(obj) {
    return (obj != 'undefined');
}

function isIE6() {
    return (navigator.appVersion.lastIndexOf('MSIE 6.0') > 0);
}

function isNavKey(evt) {
    // Parameter verification.
    if (!exists(evt)) {
        return false;
    }
    // Main code.
    switch (evt.keyCode) {
        case 8:  // Backspace
        case 17: // Control
        case 46: // DEL
        case 37: // Left Arrow
        case 38: // Up Arrow
        case 39: // Right Arrow
        case 40: // Down Arrow
            return true;
        default:
            return false;
    }
}

function isNumeric(evt) {
    // Parameter verification.
    if (!exists(evt)) {
        return false;
    }
    // Main code.
    var keyCode = evt.keyCode;
    var result = (isNavKey(evt) && !isIE6());

    if (keyCode == 0) {
        keyCode = evt.which;
        result = false;
    }
    return result || (!isSpace(keyCode) && !isNaN(String.fromCharCode(keyCode)))
}

function isSpace(keyCode) {
    // Parameter verification.
    if (!exists(keyCode)) {
        return false;
    } else if (!parseInt(keyCode)) {
        return false;
    }
    // Main code.
    return (keyCode == 32);
}

function isTextSelected() {
    return (document.selection.type == 'Text');
}

function maxLength(obj, evt, limit) {
    // Parameter verification.
    if (!exists(obj) || !exists(evt) || !exists(limit)) {
        return false;
    } else if (!parseInt(limit)) {
        return false;
    }
    // Main code.
    if (isNavKey(evt)) {
        return true;
    } else if (obj.value.length == limit) {
        return isTextSelected();
    }
}

function removeExcess(obj, limit) {
    // Parameter verification.
    if (!exists(obj) || !exists(limit)) {
        return false;
    } else if (!parseInt(limit)) {
        return false;
    }
    // Main code.
    if (obj.value.length > limit) {
        obj.value = obj.value.substring(0, limit);
    }
}

function validate(validationGroup) {
    // Main code.
    var isValid = Page_ClientValidate(validationGroup);

    if (!isValid) {
        for (var i = 0; (i < Page_Validators.length); i++) {
            if (Page_Validators[i].validationGroup == validationGroup) {
                var ctrl = $(Page_Validators[i].controltovalidate);

                if (Page_Validators[i].isvalid) {
                    ctrl.removeAttribute('style');
                }
                else {
                    ctrl.style.backgroundColor = '#F3EAEA';
                    ctrl.style.border = '1px solid #FF0000';
                    ctrl.style.padding = '2px';

                    if (ctrl.type == 'textarea' && isIE6()) {
                        var width = parseInt(ctrl.style.width.replace('px', ''));

                        if (ctrl.style.width == '' || width > 200) {
                            ctrl.style.width = (ctrl.offsetWidth - 10) + 'px';
                        }
                    }
                }
            }
        }
    }
    return isValid;
}

