﻿function AdaptWindowSize() {
    var rect = GetWindownInfo();
    var height0 = rect.Height;
    if (height0 <= 0) {
        return;
    }

    var headerH = document.getElementById("emHeader").scrollHeight;
    var toolBarH = document.getElementById("udpMailTooBar").scrollHeight;
    var height = height0 - (headerH + toolBarH);
    var ua = window.navigator.userAgent.toLowerCase();
    if (/webkit/i.test(ua)) //safari
    {
        height = height - 33;
    }
    var height1 = height;

    if (document.getElementById("divContentTop") && document.getElementById("divContentMiddle") &&
	     document.getElementById("divContentBottom")) {
        document.getElementById("divLeft").style.height = (height) + "px";

        var divCTopHeight = convertToValue(document.getElementById("divContentTop").style.height);
        if (divCTopHeight == 0)
            divCTopHeight = document.getElementById("divContentTop").scrollHeight;

        var divCButtomHeight = convertToValue(document.getElementById("divContentBottom").style.height);
        if (divCButtomHeight == 0)
            divCButtomHeight = document.getElementById("divContentBottom").scrollHeight;
        height = height - divCButtomHeight - divCTopHeight - 2;
        if (height > 0) {
            document.getElementById("divContentMiddle").style.height = (height) + "px";
            document.getElementById("divContentMiddle").className = "scrollDiv";
            if (/msie/i.test(ua)) {
                //fix a bug. display scroll in IE.
                document.getElementById("divContentMiddle").focus();
            }
            document.getElementById("divLeft").className = "normalDiv";
        }
    }
    else if (document.getElementById("divContentTop") && document.getElementById("divContentBottom")) {
        document.getElementById("divLeft").style.height = (height) + "px";

        var divCButtomHeight = convertToValue(document.getElementById("divContentBottom").style.height);

        height = height - divCButtomHeight - 1;
        if (height > 0) {
            document.getElementById("divContentTop").style.height = (height) + "px";
            document.getElementById("divContentTop").className = "scrollDiv";
            document.getElementById("divLeft").className = "normalDiv";
        }
    }
    else if (document.getElementById("divLeft") && height > 0) {
        document.getElementById("divLeft").style.height = (height) + "px";
        document.getElementById("divLeft").className = "scrollDiv";
    }

    if (document.getElementById("divTree")) {
        var tbcb = document.getElementById("tbCommonButton");
        if (tbcb) {
            height1 -= tbcb.scrollHeight;
        }
        if (height1 > 0) {
            document.getElementById("divTree").style.height = (height1) + "px";
        }
    }
}

//convert 30px to 30
function convertToValue(obj) {
    if (obj == null || obj == "")
        return 0;
    else
        return parseInt(obj.substring(0, obj.length - 2));
}

function GetWindownInfo() {
    var scrollX = 0, scrollY = 0, width = 0, height = 0, contentWidth = 0, contentHeight = 0;
    if (typeof (window.pageXOffset) == 'number') {
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrollX = document.body.scrollLeft;
        scrollY = document.body.scrollTop;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrollX = document.documentElement.scrollLeft; scrollY = document.documentElement.scrollTop;
    }
    if (typeof (window.innerWidth) == 'number') {
        width = window.innerWidth; height = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        width = document.documentElement.clientWidth; height = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        width = document.body.clientWidth; height = document.body.clientHeight;
    }
    if (document.documentElement && (document.documentElement.scrollHeight || document.documentElement.offsetHeight)) {
        if (document.documentElement.scrollHeight > document.documentElement.offsetHeight) {
            contentWidth = document.documentElement.scrollWidth;
            contentHeight = document.documentElement.scrollHeight;
        }
        else {
            contentWidth = document.documentElement.offsetWidth;
            contentHeight = document.documentElement.offsetHeight;
        }
    }
    else if (document.body && (document.body.scrollHeight || document.body.offsetHeight)) {
        if (document.body.scrollHeight > document.body.offsetHeight) {
            contentWidth = document.body.scrollWidth;
            contentHeight = document.body.scrollHeight;
        }
        else {
            contentWidth = document.body.offsetWidth;
            contentHeight = document.body.offsetHeight;
        }
    }
    else {
        contentWidth = width;
        contentHeight = height;
    }

    if (height > contentHeight)
        height = contentHeight;
    if (width > contentWidth)
        width = contentWidth;
    var rect = new Object();
    rect.ScrollX = scrollX;
    rect.ScrollY = scrollY;
    rect.Width = width;
    rect.Height = height;
    rect.ContentWidth = contentWidth;
    rect.ContentHeight = contentHeight;
    return rect;
}
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoadedHandler);
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(UpdateProgressHandler);
Sys.Application.add_load(applicationLoadHandler);

function applicationLoadHandler(sender, args) {
    //debugger;
}
var previousRequestTime = (new Date()).getTime();
function InitializeRequestHandler(sender, args) {
    var now = new Date();
    var interval = (now - previousRequestTime) / 1000;
    previousRequestTime = now;
    /*if(interval<0.2)//cancel
    {
    args.set_cancel(true);
    return;
    }
    else
    {
    //continue?
    }*/
    document.body.style.cursor = "wait";
}

function EndRequestHandler(sender, args) {
    if (args.get_error() != null) {
        args.set_errorHandled(true);
        if (args.get_response().get_timedOut()) {
            //time out?
            showMessage("Request timed out!");
        }
        else if (args.get_response().get_statusCode() != 200) {
            //unknow error?
            showMessage("Occur unkown error!");
        }
        else {
            //server exception? args.get_error().message
            showMessage("An error has occurred on server!");
        }
    }
    else if (args.get_response().get_aborted()) {
        //abort request?
        //showMessage("You has cancelled request!");
    }
    else {
        //item = args.get_dataItems()[upId];
    }
    document.body.style.cursor = "default";
}

function UpdateProgressHandler(sender, e) {
    var msgDiv = $get("udProcegress");
    msgDiv.style.display = "block";
}

function PageLoadedHandler(sender, args) {
    //GetClientControl();	
    AdaptWindowSize();
}

function showMessage(msg) {
    document.getElementById("btnMessage").click();
    var msgObj = document.getElementById("msg");
    msgObj.innerHTML = msg;
    window.setTimeout(
    function() {
        msgObj.innerHTML = "";
        document.getElementById("btnPopupClose").click();
    },
    5000);
}

function HideTree(hideController, expandController, win) {
    var hide = document.getElementById(hideController);
    var expand = document.getElementById(expandController);

    document.getElementById(win).parentNode.style.display = "none";

    if (hide) {
        hide.style.display = "none";
    }

    if (expand) {
        expand.style.display = "";
    }
}

function ExpandTree(hideController, expandController, win, size) {
    var hide = document.getElementById(hideController);
    var expand = document.getElementById(expandController);

    document.getElementById(win).parentNode.style.display = "block";

    if (hide) {
        hide.style.display = "";
    }

    if (expand) {
        expand.style.display = "none";
    }
}
