function OnAddItem(sender) {

    switch (sender.value) {
        case "AddProject":
            $.colorbox({ transition: 'none', width: '95%', height: '98%', iframe: true, href: 'ProjectSelectionPage.aspx' });
            break;

        case "AddNonAuditCode":
            $.colorbox({ transition: 'none', width: '95%', height: '98%', iframe: true, href: 'NonAuditEntitySelectionPage.aspx' });
            break;

        case "AddAuditCode":
            $.colorbox({ transition: 'none', width: '95%', height: '98%', iframe: true, href: 'AuditEntitySelectionPage.aspx' });
            break;

        case "AddWorkProgramme":
            $.colorbox({ transition: 'none', width: '95%', height: '98%', iframe: true, href: 'WorkProgrammeEntitySelectionPage.aspx' });
            break;

        case "AddChecklist":
            $.colorbox({ transition: 'none', width: '95%', height: '98%', iframe: true, href: 'ChecklistEntitySelectionPage.aspx' });
            break;

    }

    sender.selectedIndex = 0;

}

function OnOpenEntityDataCustomHeight(sender, url, height) {

    $(document).ready(function () {
        var pos = $(sender).offset();
        var top = $(window).scrollTop();
        $.colorbox({ position: 'absolute', scrolling: false, top: pos.top - 15 - top + "px", left: pos.left + "px", transition: 'none', width: '400px', height: height, iframe: true, href: url });
    });
}

function OnOpenEntityDataWithClose(sender, url) {

    $(document).ready(function () {
        var pos = $(sender).offset();
        var top = $(window).scrollTop();
        $.colorbox({ position: 'absolute', scrolling: false, top: pos.top - 15 - top + "px", left: pos.left + "px", transition: 'none', width: '400px', height: '300px', iframe: true, href: url, onClosed: function () { ClearThisSession(); } });
    });
}

function OnOpenEntityData(sender, url) {

    $(document).ready(function () {
        var pos = $(sender).offset();
        var top = $(window).scrollTop();        
        $.colorbox({ position: 'absolute', scrolling: false, top: pos.top - 15 - top + "px", left: pos.left + "px", transition: 'none', width: '400px', height: '300px', iframe: true, href: url });
    });
}

function isOnlyNumber(evt) {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}

function isOnlyNumberWithDot(fldObj, e, posOnly) {
    var eCode; var eChar;
    var fldVal = fldObj.value;
    if (window.event) { eCode = window.event.keyCode; }
    else if (e) { eCode = e.which; }
    else { return true; }
    eChar = String.fromCharCode(eCode);

    if (fldVal.indexOf('.') >= 0) {
        // Prevent more than 1 decimal
        if (eChar == '.') {
            // Check if there is a decimal in the selection. If a decimal exists, if the key pressed was another decimal, allow it
            if (typeof document.selection != 'undefined') { // IE
                var dcr = document.selection.createRange();
                var start = Math.abs(dcr.moveStart("character", -1000000));
                var end = dcr.text.length;
            } else {
                var start = fldObj.selectionStart;
                var end = fldObj.selectionEnd;
            }
            if (fldObj.value.substring(start, end).indexOf('.') < 0) {
                return false;
            }
        }
        
    }
    if ((eCode == null) || (eCode == 0) || (eCode == 8) || (eCode == 9) || (eCode == 13) || (eCode == 27)) { return true; }
    else if (("0123456789.").indexOf(eChar) > -1) { return true; }
    else if (!posOnly && ("-").indexOf(eChar) > -1 && getCursorPos(fldObj) == 0) { return true; }
    else { return false };
}

function isOnlyNumberDefault(fldObj, e, posOnly) {
    var eCode; var eChar;
    var fldVal = fldObj.value;
    if (window.event) { eCode = window.event.keyCode; }
    else if (e) { eCode = e.which; }
    else { return true; }
    eChar = String.fromCharCode(eCode);

    if (fldVal.indexOf(':') >= 0) {
        // Prevent more than 1 decimal
        if (eChar == ':') {
            // Check if there is a decimal in the selection. If a decimal exists, if the key pressed was another decimal, allow it
            if (typeof document.selection != 'undefined') { // IE
                var dcr = document.selection.createRange();
                var start = Math.abs(dcr.moveStart("character", -1000000));
                var end = dcr.text.length;
            } else {
                var start = fldObj.selectionStart;
                var end = fldObj.selectionEnd;
            }
            if (fldObj.value.substring(start, end).indexOf(':') < 0) {
                return false;
            }
        }
        //if (eChar=='.'){return false;}
    }
    if ((eCode == null) || (eCode == 0) || (eCode == 8) || (eCode == 9) || (eCode == 13) || (eCode == 27)) { return true; }
    else if (("0123456789:.").indexOf(eChar) > -1) { return true; }
    else if (!posOnly && ("-").indexOf(eChar) > -1 && getCursorPos(fldObj) == 0) { return true; }
    else { return false };
}

function partialPostback() {

    $('[id*=btn_partial_refresh]', document).click();
    return false;
}

function focusEntityValue(sender) {
   
    try {
        var id = sender.attributes["entityid"].value;
        var rng = sender.attributes["rng"].value;
        var index = sender.attributes["index"].value;
        var edit = sender.attributes["e"].value;

        var hd = id + "|" + rng + "|" + index + "|" + sender.value;
        $('[id*=hd_cell_value]', document).val(hd);

        //var item = $('[id*=tb_AddNotes]', document);

        //if (edit==0)
        //{            
        //    item.prop('disabled', true);
        //    item.css('background', 'url(../images/addnote16d.png)');
        //}
        //else
        //{
        //    item.prop('disabled', false);
        //    item.css('background', 'url(../images/addnote16.png)');
        //}

        //item.css('background-position-y', '2px');
        //item.css('background-repeat', 'no-repeat');
        
    }

    catch (Err) { }

}

function vc(sender) {

    var id = sender.attributes["entityid"].value;
    var rng = sender.attributes["rng"].value;
    var index = sender.attributes["index"].value;

    var hd = id + "|" + rng + "|" + index + "|" + sender.value;
    $('[id*=hd_cell_value]', document).val(hd);

    $('[id*=btn_parital_refresh_updatevalues]', document).click();

}

function fc(sender) {

    var id = sender.attributes["pindex"].value;
    var rng = sender.attributes["rng"].value;
    var index = sender.attributes["index"].value;
    var inout = sender.attributes["inout"].value;

    var hd = id + "|" + rng + "|" + index + "|" + sender.value + "|" + inout;
    $('[id*=hd_flexi_value]', document).val(hd);

    $('[id*=btn_parital_refresh_updateflexi]', document).click();

}

//Note:The below script runs after every partial postback
$(document).ready(function () {

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function (s, e) {

        try {

            //Note: find next control id and set the focus - the parms 1 is need for delay to allow txt.select attr set by timeentryvalues class
            var controlid = $('[id*=hd_cell_focus]', document).val();
            if (controlid == "") return;
            setTimeout(function () { $("#" + controlid + "").focus(); }, 1);
        }
        catch (err) { }

    });
});



