﻿var arrTopMenu = new Array();
if (document.images) { //Preload images
    var prePath = "/TrainingTeams4.0/Media/Menu_";
    arrTopMenu[1] = new Object(); arrTopMenu[1].src = prePath + "SignIn_D.png";
    arrTopMenu[2] = new Object(); arrTopMenu[2].src = prePath + "SignIn_H.png";
    arrTopMenu[3] = new Object(); arrTopMenu[3].src = prePath + "SignOut_D.png";
    arrTopMenu[4] = new Object(); arrTopMenu[4].src = prePath + "SignOut_H.png";
    arrTopMenu[5] = new Object(); arrTopMenu[5].src = prePath + "AdminAccess_D.png";
    arrTopMenu[6] = new Object(); arrTopMenu[6].src = prePath + "AdminAccess_H.png";
}
function OnTopMenu(id, idx) { if (document.images) document.images[id].src = arrTopMenu[idx].src; }

function GetClientId(controlId, useOnlyStandardObject) {
    /// <summary>object GetClientId(controlId, useRadObject) to obtain control object.</summary>
    /// <param name="controlId">control ID of the </param>
    /// <param name="useOnlyStandardObject">true if use only standard object; otherwise false (default value is false)</param>
    /// <return>control object</param>
    var body = document.getElementsByTagName("body");
    var id = body[0].attributes["controlPrefix"];

    if (useOnlyStandardObject == undefined || useOnlyStandardObject == null) useOnlyStandardObject = false;
     
    var clientObject = null;
    var lastIndex = id.value.lastIndexOf("_");
    var postfix = ((lastIndex >= 0) ? postfix = id.value.substring(lastIndex, id.value.length) : "");
    var numOfMasters = (id.value.replace(new RegExp(postfix, "g"), "")).split("_");
    var prefix = id.value;

    for (index = 0; index <= numOfMasters.length; index++) {

        clientId = prefix + "_" + controlId;
        // telerik
        if (!useOnlyStandardObject) {
            if (clientObject == null) clientObject = $find(clientId);
            if (clientObject != null) clientObject.id = clientId;
        }
        // server control
        if (clientObject == null) clientObject = document.getElementById(clientId);

        // html control
        if (clientObject == null) clientObject = document.getElementById(controlId);

        if (clientObject != null) break;

        prefix = prefix.substring(0, prefix.lastIndexOf(postfix));
    }
    return clientObject;
}  
 
function SetScrollBar(controlId, offsetX, offsetY) {
    /// <summary>SetScrollBar(controlId, offsetX, offsetY) sets scroll position of target control object.</summary>
    /// <param name="controlId">control ID of the target</param>
    /// <param name="offsetX">offsetX of scroll position</param>
    /// <param name="offsetY">offsetY of scroll position</param>
    var node = GetClientId(controlId);
    if (node == null || node == undefined) return;
    var scrollLeft = node.offsetLeft;
    var scrollTop = node.offsetTop
    while (node) {
        scrollTop = scrollTop + node.offsetTop;
        node = node.offsetParent;
    }
    window.scrollTo(scrollLeft + offsetX, scrollTop + offsetY);
}

function SetFocus(focusedControlId) {
    /// <summary>SetFocus(focusedControlId) sets focus of target control object.</summary>
    /// <param name="focusedControlId">control ID of the target</param>
    var control = GetClientId(focusedControlId, true);
    try {
        control.focus();
        control.select();
    } catch (e) { }
}

function ShowAlert() {
    radalert('<span class="text_08">You must update the missing information before you can navigate the website.</span>', 470, 150, 'Required Information Missing');

    return false;
}

// Namespace
var TT = window.TT || {};
// Singleton
TT.Validation = function () {
    //----------------------
    // Private Interface
    //----------------------
    // Message Template
    var MSG_001 = "Please enter {0}.";
    var MSG_002 = "Invalid {0}.";
    var MSG_003 = "Angle brackets (< >) are not allowed.";
    var MSG_004 = "Please select {0}.";
    var MSG_005 = "Please enter valid {0}. Use alphabets and (<span class='text_10'> -', space</span>) only.";
    var MSG_006 = "{0}";
    var MSG_007 = "{0} does not match";
    var MSG_008 = "{0} should contain at least {1} characters.";
    var MSG_009 = "{0} should contain alphabets and numbers only.";
    var MSG_010 = "File extension is invalid.";
    var MSG_011 = "{0} should contain at most {1} characters.";
    var MSG_012 = "{0} should contain numbers only.";
    var MSG_013 = "{0} should be {1} digits";
    var focusedControlId = "";
    // Common validation
    function IsAlphaNumeric(value) { var pattern = /^([a-z]|[A-Z]|[0-9])+$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsAlphaSpecial(value) { /* accept [-] [,] [']*/var pattern = /^([a-z]|[A-Z])+([a-z]|[A-Z]|\-|\,|['']|\ )*$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsAngleBracket(value) { return (value.indexOf('<') > -1 || value.indexOf('>') > -1) ? true : false; }
    function IsAllowedFileExtensions(value1, value2) { var extensionfile = value1.split("."); var extensionAccepted = value2.split(":"); var i = 0; for (i = 0; i < extensionAccepted.length; i++) { if (("." + extensionfile[extensionfile.length - 1]).toLowerCase() == extensionAccepted[i].toLowerCase()) return true; } return false; }
    function IsEmail(value) { var pattern = /^([a-zA-Z0-9''_]+)([a-zA-Z0-9''_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsEmpty(value) { return (value == ""); }
    function IsEqual(value1, value2) { return (value1 == value2); }
    function IsLengthMin(value, length) { return (value.length >= length); }
    function IsLengthMax(value, length) { return (value.length <= length); }
    function IsGreater(value1, value2) { return (value1 > value2); }
    function IsLess(value1, value2) { return (value1 < value2); }
    function IsNumeric(value) { var pattern = /^([0-9])+$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsPhone(value) { var pattern = /^\d{3}-\d{3}-\d{4}$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsSelected(value) { return (value > 0) ? true : false; }
    function IsSelectedRad(value) { return (value >= 0) ? true : false; } // this is used for only rad combobox
    function IsZipcode(value) { var pattern = /^(\d{5}|\d{5}-\d{4})$/; return (value != "" && value.match(pattern) == null) ? false : true; }
    function IsListChecked(value) { for (var j = 0; j < value.rows.length; j++) { for (var i = 0; i < value.rows[j].cells.length; i++) { if (value.rows[j].cells[i].firstChild != null && value.rows[j].cells[i].firstChild.checked) return true; } } return false; }
    function IsUnSupportedFilterRad(value) { return (value.indexOf(';') > -1 || value.indexOf('&#') > -1) ? true : false; }

    // Utility Method
    function Trim(value) { return value.replace(/^\s*(\b.*\b|)\s*$/, "$1"); }
    function SetMessage(targetControl, focusedControl, errMsgControl, messageId, errMsgParam, errMsgAppend) {
        var errMsgControl = document.getElementById(errMsgControl.id);
        var parameters = errMsgParam.split(":");
        var message = messageId;
        for (i = 0; i < parameters.length; i++) message = message.replace("{" + i + "}", parameters[i]);
        if (messageId != "") {
            var imgIcon = document.createElement("img");
            imgIcon.src = "/TrainingTeams4.0/Media/Icon_Red_Cross.png";
            imgIcon.height = imgIcon.width = "14";
            imgIcon.alt = "";
            imgIcon.className = "vertical_bottom padding_01";
            try {
                // firebox needs this        
                imgIcon.outerHTML = "<img src='/TrainingTeams4.0/Media/Icon_Red_Cross.png' height='14px' class='vertical_bottom padding_01' width='14px' alt='' scr='/TrainingTeams4.0/Media/Icon_Red_Cross.png'/>";
            }
            catch (e) { }
            message = imgIcon.outerHTML + message;
        }
        errMsgControl.className = "vertical_middle text_08 text_09";
        errMsgControl.innerHTML = (errMsgAppend != undefined || errMsgAppend) ? (errMsgControl.innerHTML + message + "<br/>") : message + "<br/>";
        SetTargetControlStyle(targetControl.id);
        if (focusedControlId == "") focusedControlId = focusedControl.id;
    }
    function InitControls(targetControlId, errMsgControlId, errMsgAppend) {
        var targetControl = GetClientId(targetControlId, true);
        var errMsgControl = GetClientId(errMsgControlId);
        if (!errMsgAppend) errMsgControl.innerHTML = "";
        targetControl.className = targetControl.className.replace(" border_01", "");
    }
    function SetTargetControlStyle(targetControlId) {
        var targetControl = GetClientId(targetControlId);
        targetControl.className = targetControl.className + " border_01";
    }
    //----------------------
    // Public Interface
    //----------------------
    return {
        RemoveError: function (targetControlId, errMsgControlId) {
            InitControls(targetControlId, errMsgControlId, false);
        },

        SetFocus: function () {
            /// <summary>boolean SetFocus() to set first error control.</summary>
            if (focusedControlId != "") {
                var control = GetClientId(focusedControlId, true);
                try {
                    SetScrollBar(focusedControlId, 0, -40);
                    control.focus();
                    control.select();
                } catch (e) { }
                focusedControlId = "";
            }
        },

        // Wrapper - Specific validation check
        CheckCaptcha: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckCaptchatargetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with alphabets or numbers.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsAlphaNumeric(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckCurrency: function (targetControlId, mandatory, minAmount, maxAmount, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDefault(targetControlId, mandatory, minAmount, maxAmount, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) without any angle bracket.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="minAmount">minimum amount</param>
            /// <param name="maxAmount">maximum amount</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsGreater(parseFloat(Trim(targetControl.value)), maxAmount) || IsLess(parseFloat(Trim(targetControl.value)), minAmount)) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_006, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDateRad: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDateRad(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if date is selected (if mandatory is true).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId + "_wrapper", errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl._validationInput.value))) {
                SetMessage(GetClientId(targetControl.id + "_wrapper"), GetClientId(targetControl.id), errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDateCompare: function (targetControl1Id, targetControl2Id, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckEqual(ttargetControl1Id, targetControl2Id, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) and if two fields are matched.</summary>
            /// <param name="targetControl1Id">Id of 1st target control to validate</param>
            /// <param name="targetControl2Id">Id of 2nd target control to validate</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControl1Id, errMsgControlId, errMsgAppend);
            InitControls(targetControl2Id, errMsgControlId, errMsgAppend);
            var targetControl1 = GetClientId(targetControl1Id);
            var targetControl2 = GetClientId(targetControl2Id);
            var errMsgControl = GetClientId(errMsgControlId);
            if (IsGreater(targetControl1.get_value(), targetControl2.get_value())) {
                SetMessage(targetControl1, GetClientId(targetControl1.id + "_Input"), errMsgControl, MSG_006, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDefault: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDefault(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) without any angle bracket.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsAngleBracket(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_003, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDefaultWithMaxLength: function (targetControlId, mandatory, maxlength, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDefault(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) without any angle bracket.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsAngleBracket(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_003, errMsgParam, errMsgAppend); return false;
            }
            if (!IsLengthMax(Trim(targetControl.value), maxlength)) {
                errMsgParam = errMsgParam + ":" + maxlength;
                SetMessage(targetControl, targetControl, errMsgControl, MSG_011, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDefaultRad: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDefault(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) without any angle bracket.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl._text))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsAngleBracket(Trim(targetControl._text))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_003, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDropDownList: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDropDownList(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if item is selected (if mandatory is true).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && !IsSelected(targetControl.selectedIndex)) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_004, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckDropDownListRad: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckDropDownList(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if item is selected (if mandatory is true).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            var value = "";
            try { value = targetControl.get_selectedItem().get_value(); } catch (e) { }
            if (value == "") value = -1;
            if (mandatory && !IsSelectedRad(value)) {
                SetMessage(targetControl, GetClientId(targetControl.id + "_Input"), errMsgControl, MSG_004, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckEmail: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckEmail(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with a correct email format.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsEmail(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckEqual: function (targetControl1Id, targetControl2Id, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckEqual(ttargetControl1Id, targetControl2Id, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) and if two fields are matched.</summary>
            /// <param name="targetControl1Id">Id of 1st target control to validate</param>
            /// <param name="targetControl2Id">Id of 2nd target control to validate</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControl1Id, errMsgControlId, errMsgAppend);
            InitControls(targetControl2Id, errMsgControlId, errMsgAppend);
            var targetControl1 = GetClientId(targetControl1Id);
            var targetControl2 = GetClientId(targetControl2Id);
            var errMsgControl = GetClientId(errMsgControlId);
            if (!IsEqual(Trim(targetControl1.value), Trim(targetControl2.value))) {
                SetMessage(targetControl1, targetControl1, errMsgControl, MSG_007, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckFilterRad: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckFilter(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) without any invalid character.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsUnSupportedFilterRad(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_007, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckListAndOther: function (targetControl1Id, targetControl2Id, errMsgControl1Id, errMsgControl2Id, errMsgParam1, errMsgParam2, errMsgAppend1, errMsgAppend2) {
            /// <summary>boolean CheckRadio(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if radio is selcted (if mandatory is true).</summary>
            /// <param name="targetControl1Id">Id of target control 1 to validate (list control)</param>
            /// <param name="targetControl2Id">Id of target control 2 to validate (textbox)</param>
            /// <param name="errMsgControl1Id">Id of control 1 to set error message</param>
            /// <param name="errMsgControl2Id">Id of control 2 to set error message</param>
            /// <param name="errMsgParam1">parameterts for error message 1. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgParam2">parameterts for error message 2. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend1">true if message 1 is appended; otherwise false</param>
            /// <param name="errMsgAppend2">true if message 2 is appended; otherwise false</param>
            InitControls(targetControl1Id, errMsgControl1Id, errMsgAppend1);
            InitControls(targetControl2Id, errMsgControl2Id, errMsgAppend2);
            var resultList = this.CheckRadio(targetControl1Id, true, errMsgControl1Id, errMsgParam1);
            var targetControl1 = GetClientId(targetControl1Id);
            var targetControl2 = GetClientId(targetControl2Id);
            var errMsgControl1 = GetClientId(errMsgControl1Id);
            var errMsgControl2 = GetClientId(errMsgControl2Id);
            var resultIsLastListChecked = targetControl1.rows[targetControl1.rows.length - 1].cells[targetControl1.rows[targetControl1.rows.length - 1].cells.length - 1].firstChild.checked;

            // Case 1: none of the list is selected (checked)
            if (!resultList) {
                return false;
            }
            // Case 2: Other is selected and checks other's textbox {if it's empty or contains angle brackets}
            else {
                if (resultIsLastListChecked) {
                    return this.CheckDefault(targetControl2Id, true, errMsgControl2Id, errMsgParam2);
                }
                // Case 3: Other is not selected
                else {
                    var resultTextBoxEmpty = IsEmpty(Trim(targetControl2.value));
                    // Case 3.1: Other's textbox is empty
                    if (resultTextBoxEmpty) {
                        return true;
                    }
                    // Case 3.3: Other's textbox is not empty
                    else {
                        SetMessage(targetControl1, targetControl1, errMsgControl1, MSG_004, errMsgParam2 + " or clear 'Other' text box", errMsgAppend1);
                        return false;
                    }
                }
            }
        },

        CheckName: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckName(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with alphabets and accecpted special chars ([-] [,] [']).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsAlphaSpecial(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_005, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckNonNegativeNumeric: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckName(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with alphabets and accecpted special chars ([-] [,] [']).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsNumeric(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckNewPassword: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckName(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with alphabets, numbers and minimum length 4.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsAlphaNumeric(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_009, errMsgParam, errMsgAppend); return false;
            }
            if (!IsLengthMin(Trim(targetControl.value), 4)) {
                errMsgParam = errMsgParam + ":4";
                SetMessage(targetControl, targetControl, errMsgControl, MSG_008, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckNumericWithLength: function (targetControlId, mandatory, length, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckNumericWithMinLength(targetControlId, mandatory, minlength, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with specified minmum length of numeric.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="minlength">minmum length for entry</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsEmpty(Trim(targetControl.value)) && !IsNumeric(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_012, errMsgParam, errMsgAppend); return false;
            }
            if (!IsEmpty(Trim(targetControl.value)) && (!IsLengthMin(Trim(targetControl.value), length) || !IsLengthMax(Trim(targetControl.value), length))) {
                errMsgParam = errMsgParam + ":" + length;
                SetMessage(targetControl, targetControl, errMsgControl, MSG_013, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckPhone: function (targetControl1Id, targetControl2Id, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckPhone(targetControl1Id, targetControl2Id, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with a correct phone format (optioanl extension number).</summary>
            /// <param name="targetControl1Id">Id of target control to validate</param>
            /// <param name="targetControl2Id">Id of target control (extension number) to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControl1Id, errMsgControlId, errMsgAppend);
            InitControls(targetControl2Id, errMsgControlId, errMsgAppend);
            var targetControl1 = GetClientId(targetControl1Id);
            var targetControl2 = GetClientId(targetControl2Id);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl1.value))) {
                SetMessage(targetControl1, targetControl1, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (IsEmpty(Trim(targetControl1.value)) && !IsEmpty(Trim(targetControl2.value))) {
                SetMessage(targetControl1, targetControl1, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            if (!IsPhone(Trim(targetControl1.value))) {
                SetMessage(targetControl1, targetControl1, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            if (!IsEmpty(Trim(targetControl2.value)) && !IsNumeric(Trim(targetControl2.value))) {
                SetMessage(targetControl2, targetControl2, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckRadio: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckName(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if radio is selcted (if mandatory is true).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && !IsListChecked(targetControl)) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_004, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckUploadRad: function (targetControlId, mandatory, errMsgControlId, allowedFileExtensions, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckUploadRad(targetControlId, mandatory, errMsgControlId, allowedFileExtensionserrM, sgParam, errMsgAppend) checks if item is selected (if mandatory is true).</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(targetControl.getFileInputs()[0].value)) {
                SetMessage(targetControl, GetClientId(targetControl.id + "ListContainer"), errMsgControl, MSG_004, errMsgParam, errMsgAppend); return false;
            }
            if (!IsAllowedFileExtensions(targetControl.getFileInputs()[0].value, allowedFileExtensions)) {
                SetMessage(targetControl, GetClientId(targetControl.id + "ListContainer"), errMsgControl, MSG_010, errMsgParam, errMsgAppend); return false;
            }
            return true;
        },

        CheckZip: function (targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) {
            /// <summary>boolean CheckZip(targetControlId, mandatory, errMsgControlId, errMsgParam, errMsgAppend) checks if field is entered (if mandatory is true) with a correct zip format.</summary>
            /// <param name="targetControlId">Id of target control to validate</param>
            /// <param name="mandatory">true if mandatory; otherwise false</param>
            /// <param name="errMsgControlId">Id of control to set error message</param>
            /// <param name="errMsgParam">parameterts for error message. (use separator [:] for multiple parameters) e.g., "Message", "Message1:Message2"</param>
            /// <param name="errMsgAppend">true if message is appended; otherwise false</param>
            InitControls(targetControlId, errMsgControlId, errMsgAppend);
            var targetControl = GetClientId(targetControlId);
            var errMsgControl = GetClientId(errMsgControlId);
            if (mandatory && IsEmpty(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_001, errMsgParam, errMsgAppend); return false;
            }
            if (!IsZipcode(Trim(targetControl.value))) {
                SetMessage(targetControl, targetControl, errMsgControl, MSG_002, errMsgParam, errMsgAppend); return false;
            }
            return true;
        }
    }
} ();

//---------------------------------------------------------------
// The following code are "AJAX" error handling (Include connection handling)
//---------------------------------------------------------------

function EndRequestHandler(sender, args) {
    if (args.get_error() != undefined) {
        var message = args.get_error().message;
        var code = String(args.get_error().httpStatusCode);
        // ErroCode: http://support.microsoft.com/kb/193625
        var pattern = /^(([0])|([1][2][0][0-2][0-9])|([1][2][0][3][0|1|2|3|4|6|7|8|9])|([1][2][0][4][0-3])|([1][2][1][1][0-1])|([1][2][1][3][0-8])|([1][2][1][5][0-6]))$/;

        if (code != "" && code.match(pattern) != null) {
            alert("Internet Connection is not available. Please check your connection.");
            args.set_errorHandled(true);
        }
        else {
            // onerror is called
        }
    }
}

window.onerror = function (message, url, line) {

    var param = "";

    var errorAjax = false;

    try {
        var errorLog = message.split(":");

        if (errorLog != null && errorLog.length > 0) {

            for (i = 0; i < errorLog.length; i++) {

                if (errorLog[i].indexOf("errorLogId") >= 0) {

                    var parts = errorLog[i].split("=");

                    param = "?errorLogId=" + parts[1];
                }
                else if (errorLog[i].indexOf("errorAjax") >= 0) {

                    errorAjax = true;
                }
            }
        }
    }
    catch (e) { }

    // client script error
    if (!errorAjax) {
        param = "?errorClientScript=[" + message.replace("<", "#") + "]:url[" + url + "]:line[" + line + "]";
    }

    param = param + ((param == "") ? "?" : "&") + "master=" + GetClientId("uxInputImmediateMasterPage").value;

    window.location.href = "/TrainingTeams4.0/Error.aspx" + param;

    return true;
}

//---------------------------------------------------------------
// The following code are "Connectivity" error handling
//---------------------------------------------------------------

// Register onClick event handler for IE and FF
if (window.captureEvents) {
    window.captureEvents(Event.CLICK);
    window.onclick = OnClickHandler;
}
else {
    document.onclick = OnClickHandler;
}

// Checks connectivity for clicked controls
function OnClickHandler(e) {
    var el = (typeof event !== 'undefined') ? event.srcElement : e.target
    var type = "";
    var control = el;
    var result = true;

    while (control) {
        // this checks connectivity for controls
        if (control.nodeName.toLowerCase() == "input" && control.getAttribute("type").toLowerCase() == "submit" && !IsConnected()) {
            result = false;
        }
        else if (control.nodeName.toLowerCase() == "input" && control.getAttribute("type").toLowerCase() == "button" && !IsConnected()) {
            result = false;
        }
        else if (control.nodeName.toLowerCase() == "select" && !IsConnected()) {
            result = false;
        }
        else if (control.nodeName.toLowerCase() == "a" && !IsConnected()) {
            result = false;
        }

        if (!result) {
            alert("Internet Connection is not available. Please check your connection.");
            return false;
        }

        control = control.parentNode;
    }
}

// Checks connectivity using XMLHttpRequest or navigator.onLine property
var resultAfterXmlHttpRequest = false;
var xmlhttp
function IsConnected() {
    xmlhttp = null;
    try {
        if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
            var ffversion = new Number(RegExp.$1)
            if (ffversion >= 3) {
                return navigator.onLine;
            }
        }
        netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) {
        //For IE it comes here.
        //alert("Permission UniversalBrowserRead denied.");
    }
    // code for IE
    if (window.ActiveXObject) {
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var ieversion = new Number(RegExp.$1)
            if (ieversion >= 8) {
                return navigator.onLine;
            }
        }
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // code for Mozilla, etc.
    else if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }

    try {
        if (xmlhttp != null) {
            xmlhttp.onreadystatechange = state_Change;
            // the request should be handled synchronously
            // use any image to test
            xmlhttp.open("GET", "/TrainingTeams4.0/Media/Image_Connection.png?timestamp=" + new Date().getTime(), false);
            xmlhttp.send(null);
            return resultAfterXmlHttpRequest;
        }
        else {
            //browser does not support XMLHTTP
            return false;
        }
    }
    catch (e) {
        return false;
    }
    return connectionResult;
}

function state_Change() {
    // if xmlhttp shows "loaded"
    if (xmlhttp.readyState == 4) {
        try {
            // check if there is a problem retrieving XML data
            resultAfterXmlHttpRequest = (xmlhttp.status == 200);
        } catch (err) {
            //Internet is NOT Connected.
            resultAfterXmlHttpRequest = false;
        }
    }
}

