﻿Type.registerNamespace("COMMON");
var loadingNode = null;
var DOC_ID = null;
var waitScreenIdList = new Array();
COMMON.UIUtil = function() {
}

COMMON.UIUtil.prototype = {
    showwaitscreen: function(targetobject) {
        if (targetobject != null) {
            var div = $get(targetobject);
            var loadingObj = $get('loadingNode');
            if (div != null && loadingObj != null) {
                // Not showing the loading screen at the div where content will be placed
                //div.appendChild(loadingObj.cloneNode(true));
                var loadingDivId = "divLoading" + (waitScreenIdList.length + 1);
                var loadingDiv = document.createElement("div");
                loadingDiv.setAttribute("id", loadingDivId);
                loadingDiv.appendChild(loadingObj.cloneNode(true));
                loadingDiv.style.position = "absolute";
                loadingDiv.style.top = findPosY(div) + "px";
                //loadingDiv.style.left = findPosX(div) + "px";
                loadingDiv.style.left = "45%";
                document.body.appendChild(loadingDiv);
                waitScreenIdList.push(loadingDivId);
            }
        }
    },

    hidewaitscreen: function() {
        if (waitScreenIdList.length > 0) {
            var waitScreenId;
            var contn = true;
            while (contn) {
                waitScreenId = waitScreenIdList.pop();
                if (null != waitScreenId) {
                    var loadingDiv = $get(waitScreenId);
                    if (null != loadingDiv) {
                        document.body.removeChild(loadingDiv);
                    }
                }
                else {
                    contn = false;
                }
            }
        }

        // refreshing the list
        waitScreenIdList = new Array();
    },

    dispose: function() {
        //disposed
    },
    downloadDocument: function(docId) {
        DOC_ID = docId;
        var docClient = new ITICClients.DocumentClient();
        docClient.downloadDocument(docId, this.downloadDocument_callback, this.downloadDocument_error_callback, this.showwaitscreen, "");
    },
    downloadDocument_callback: function(result) {
        if (result.IsSuccess) {
            //download the document
            var commonObj = new COMMON.UIUtil();
            commonObj.downloadUsingIFrame(result.Data);
        }
        else {
            $get('ctl00_ContentPlaceHolder1_popupLoginConfirm').click();
            //need to show login first
            //TODO: Munna:
            //            var returnval = window.confirm("This is sponsors document,\nyou need to login first,\nclick okay to login.");
            //            if (returnval == true)
            //                window.location = "../Login.aspx?ReturnUrl=Public%2fSearchResult.aspx&DocID=" + DOC_ID;
        }
    },
    downloadDocument_error_callback: function(result) {
        alert(result);
    },
    downloadUsingIFrame: function(docID) {
        var iFrameId = "DownloadiFrame";
        var iframe = $get(iFrameId);
        if (iframe) {
            document.documentElement.removeChild(iframe);
        }

        iframe = document.createElement("iframe");
        iframe.id = iFrameId;
        var downloadUrl = "";

        //TODO: Munna: create the absolute uri for Download page.
        if (window.location.hostname == 'localhost') {
            downloadUrl = window.location.protocol + "//" + window.location.hostname + "/iticcms/Download.aspx?QSDOCID=" + docID;
        }
        else {
            downloadUrl = window.location.protocol + "//" + window.location.hostname + "/Download.aspx?QSDOCID=" + docID;
        }

        //var downloadUrl = "~/Download.aspx?QSDOCID=" + docID;
        // point the iframe to the download page
        iframe.src = downloadUrl;

        // This makes the IFRAME invisible to the user.
        iframe.style.display = 'none';

        // Add the IFRAME to the page.  This will trigger
        // a request to GenerateFile now.
        //document.body.appendChild(iframe);
        document.documentElement.appendChild(iframe);
    },

    showServiceException: function(result) {
        //window.alert(result._message);
        var msg = "";
        if (null != result) {
            if (null != result._message) {
                msg = result._message;
            }
        }

        if (msg == "") {
            msg = "An error occur while loading page.";
        }
        showModalError(msg);
    }
}

COMMON.UIUtil.registerClass('COMMON.UIUtil', null, Sys.IDisposable)

function RedirectToLoginPage() {
    window.location = "../Login.aspx?ReturnUrl=Public%2fSearchResult.aspx&DocID=" + DOC_ID;
}
function downloadDocument(docID) {
    //alert(docID);
    var common = new COMMON.UIUtil();
    common.downloadDocument(docID);
}

function showModalError() {
    var msg = arguments[0];
    // creating the main div element
    var mainDiv = document.createElement("div");
    mainDiv.setAttribute("id", "divModalError");
    mainDiv.className = "modalPage";
    var modalBackgroundDiv = document.createElement("div");
    modalBackgroundDiv.className = "modalBackground";
    mainDiv.appendChild(modalBackgroundDiv);
    var modalContainerDiv = document.createElement("div");
    modalContainerDiv.className = "modalContainer";
    var modalDiv = document.createElement("div");
    modalDiv.className = "modal";
    var modalTopDiv = document.createElement("div");
    modalTopDiv.className = "modalTop";
    modalTopDiv.innerHTML = "<a style=\"cursor: pointer;\" onclick=\"javascript:hideModal('divModalError');\">[X]</a>";
    modalDiv.appendChild(modalTopDiv);
    var modalBodyDiv = document.createElement("div");
    modalBodyDiv.className = "modalBody";
    modalBodyDiv.innerHTML = "<p>" + msg + "</p>";
    modalDiv.appendChild(modalBodyDiv);
    modalContainerDiv.appendChild(modalDiv);
    mainDiv.appendChild(modalContainerDiv);
    document.body.appendChild(mainDiv);
}

function hideModal() {
    var divId = arguments[0];
    var modalDiv = $get(divId);
    if (null != modalDiv) {
        document.body.removeChild(modalDiv);
    }
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


// Notify ScriptManager that this is the end of the script.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
