﻿// JScript File

// Add listener for keypress event
if (document.addEventListener) {
    document.addEventListener("keypress", HandleEnterKey, true);
}
else {
    document.attachEvent("onkeypress", HandleEnterKey);
}

// Prevent the enter key from doing anything when pressed.
function HandleEnterKey(event) {
    var nav = window.Event ? true : false;
    if (nav) {
        return NetscapeEventHandler_KeyDown(event);
    }
    else {
        return MicrosoftEventHandler_KeyDown();
    }
}

function NetscapeEventHandler_KeyDown(e) {
    if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') {
        e.returnValue = false;
        e.cancel = true;
        e.preventDefault();
        var att = e.target.attributes['SubmitControl'];
        if (att != null)
            CallSubmit(att.value)
        return false;
    }
    return true;
}

function MicrosoftEventHandler_KeyDown() {
    if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') {
        event.returnValue = false;
        event.cancel = true;
        var att = event.srcElement.attributes['SubmitControl'];
        if (att != null)
            CallSubmit(att.value)
        return false;
    }
    return true;
}

// To allow the enter key to submit a form, use the logic below:
//
//   [FORM_FIELD].Attributes.Add("onkeypress", "return ClickButton(event,'" + [SUBMIT_BUTTON].ClientID + "')")
//
// - Replace [FORM_FIELD] with the name of the form field object on which you wish to allow submission of the form by hitting the ENTER key.
// - Replace [SUBMIT_BUTTON] with the name of the button object used for submitting the form.
//
function ClickButton(e, buttonid) {
    var bt = document.getElementById(buttonid);
    if (typeof bt == 'object') {
        if (navigator.appName.indexOf("Netscape") > (-1)) {
            if (e.keyCode == 13) {
                bt.click();
                return false;
            }
        }
        if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {
            if (event.keyCode == 13) {
                bt.click();
                return false;
            }
        }
    }
}

// JScript File
function LoadCssFile(url)
{
    var fileref=document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", url);
    
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}

function LoadJavascriptFile(url)
{
    var fileref = document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", url);
    
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}


        
/*
function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}*/

function RadWindow_OnClientShowWithConfirm(radWindow) {
    globalRadWindow = radWindow;
    radWindow.add_beforeClose(onClientBeforeClose);
    RadWindow_OnClientShow(radWindow);
}

function RadWindow_OnClientShowWithConfirmNoReload(radWindow) {
    globalRadWindow = radWindow;
    radWindow.add_beforeClose(onClientBeforeCloseNoReload);
    RadWindow_OnClientShow(radWindow);
}


function RadWindow_OnClientShow(radWindow) {
// removed functionality due to issues with lower resolutions
//    if (document.documentElement && document.documentElement.scrollTop) {
//      var oTop = document.documentElement.scrollTop;
//        document.documentElement.scroll = "no";
//        document.documentElement.style.overflow = "hidden";
//        document.documentElement.scrollTop = oTop;
//
//    } else if (document.body) {
//        var oTop = document.body.scrollTop;
//        document.body.scroll = "no";
//        document.body.style.overflow = "hidden";
//        document.body.scrollTop = oTop;
//    }
}

function RadWindow_OnClientClose(radWindow) {
//    if (document.documentElement && document.documentElement.scrollTop) {
//        document.documentElement.scroll = "";
//        document.documentElement.style.overflow = "";
//    } else if (document.body) {
//        document.body.scroll = "";
//        document.body.style.overflow = "";
//    }
}

function onClientBeforeClose(sender, arg) {

    arg.set_cancel(true);    
    //radconfirm("Your changes will not be saved if you choose to Continue or select Cancel to return and save your changes.", callbackFunction, 300, 100, null, "Confirmation");

//    var oBrowserWnd = sender.BrowserWindow;
    //    var oWnd = oBrowserWnd.radopen('/Common/Confirmation.aspx', 'Confirmation');
    var oWnd = radopen('/Common/Confirmation.aspx', 'Confirmation');
    oWnd.setSize(400, 200);
    oWnd.DestroyOnClose = true;
    oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
    oWnd.center();
}

function onClientBeforeCloseNoReload(sender, arg) {

    arg.set_cancel(true);
    //radconfirm("Your changes will not be saved if you choose to Continue or select Cancel to return and save your changes.", callbackFunction, 300, 100, null, "Confirmation");

    //    var oBrowserWnd = sender.BrowserWindow;
    //    var oWnd = oBrowserWnd.radopen('/Common/Confirmation.aspx', 'Confirmation');
    var oWnd = radopen('/Common/Confirmation.aspx?noreload=1', 'Confirmation');
    oWnd.setSize(400, 200);
    oWnd.DestroyOnClose = true;
    oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
    oWnd.center();
}


var globalRadWindow;

function callbackFunction(sender) {
    sender.Close();
    globalRadWindow.remove_beforeClose(onClientBeforeClose);
    globalRadWindow.BrowserWindow.location.reload();
    globalRadWindow.Close();
}

function callbackFunctionNoReload(sender) {
    sender.Close();
    globalRadWindow.remove_beforeClose(onClientBeforeCloseNoReload);    
    globalRadWindow.Close();
}

function CloseWithoutConfirm(sender, url) {    
    globalRadWindow.remove_beforeClose(onClientBeforeClose);
    globalRadWindow.BrowserWindow.location.href = url;
    globalRadWindow.Close();
}

function CloseWithoutConfirmReturnIdToParentNoReload(sender, url, id) {
    globalRadWindow.remove_beforeClose(onClientBeforeCloseNoReload);

    var oArg = new Object();
    oArg.id = id;

    globalRadWindow.Close(oArg);
}

function getDocHeight() {
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
}

function BlockUI(options) {
    var default_args = {
        'message': "Loading...",
        'modal': true
    }
    if (!options) { options = new Array(); }
    for (var index in default_args) {
        if (typeof options[index] == "undefined") options[index] = default_args[index];
    }

    var fnd = $("[id$=_UpdateProgressGlobal]");
    var modal = fnd.find("#divModalBackground");
    if (!options["modal"]) {
        modal.addClass("modalBackgroundTransparent");
    }
    modal.css("display", "block");

    var t = fnd.find("#UpdateProgressGlobalMessage")
    t.text(options["message"]);
    fnd.css("display", "block");
    
/*
    var height = getDocHeight();
    var windowHeight = $(window).height();
    $('<div ></div>').attr('id', 'uiLockId').css({
        'position': 'absolute',
        'top': '0',
        'left': '0',
        'opacity': '0.3',
        'background-color': 'gray',
        'z-index': '1000',
        'width': '100%',
        'height': height,
        'cursor': 'wait'
    }).appendTo('body');

    $('<div ></div>').attr('id', 'uiOverLayId').css({
        'position': 'fixed',
        'top': '45%',
        'left': '40%',
        'width': 100,
        'height': 35,
        'z-index': '1001',
        'opacity': '1',
        'border': 'solid 1px black',
        'background-color': 'White',
        'text-align': 'center',
        'padding': '10px',
        'text-align': 'center',
        'color': 'black'

    }).html('<table><tr><td><img src="App_Themes/Default/Telerik/Ajax/rising_sherbert_sun_small_gray.gif" alt="please wait"/></td><td style="padding:3px 5px;">Loading...</td></tr></table>  ').appendTo('body');
*/
}

function UnBlockUI() {

    var fnd = $("[id$=_UpdateProgressGlobal]");
    fnd.css("display", "none");

    var modal = $("#divModalBackground");
    modal.removeClass("modalBackgroundTransparent");
    modal.css("display", "none");
   
/*
    $('#uiLockId').remove();
    $('#uiOverLayId').remove();
    */
}

function ShowHideTopHeader(caller, id) {
    $('#' + id).slideToggle(function () {
        if (this.style.display == "none") {
            $('#' + caller).html("[+] Show");
        }
        else {
            $('#' + caller).html("[-] Hide");
        }
    });
}
