﻿function OpenModalUrl(strTitle, intWidth, intHeight, strUrl) {
    intHeight += 17;
    intHeight += document.all ? 2 : 0;

    var arrSize = GetWindowSize();
    var intWindowWidth = arrSize[0] - 20;
    var intWindowHeight = arrSize[1] - 20;
    if (intHeight > intWindowHeight) {
        intHeight = intWindowHeight;
        intWidth += 17;
    }
    intWidth = intWidth <= intWindowWidth ? intWidth : intWindowWidth;
    document.getElementById('ifmUrl').src = strUrl;
    document.getElementById('divModalTitle').innerHTML = "&nbsp;" + strTitle;
    document.getElementById('divModal').style.width = intWidth + "px";
    document.getElementById('divModal').style.height = (intHeight + 4) + "px";
    document.getElementById('divModalUrl').style.height = (intHeight - 17) + "px";
    setTimeout("DisplayModalDiv('divModal', " + intWidth + ", " + intHeight + ");", 100);
}

function OpenModalByConent(strContent, intWidth, intHeight) {
    document.getElementById('divModalContent').style.width = intWidth + "px";
    document.getElementById('divModalContent').style.height = intHeight + "px";
    document.getElementById('divModalContent').innerHTML = strContent;
    DisplayModalDiv('divModalContent', intWidth, intHeight);
}

function OpenModalPopUrl(strTitle, intWidth, intHeight, strUrl) {
    var arrSize = GetWindowSize();
    var intWindowWidth = arrSize[0] - 20;
    var intWindowHeight = arrSize[1] - 20;
    intWidth = intWidth <= intWindowWidth ? intWidth : intWindowWidth;
    intHeight = intHeight <= intWindowHeight ? intHeight : intWindowHeight;
    document.getElementById('divModalPopUrl').style.width = intWidth + "px";
    document.getElementById('divModalPopUrl').style.height = intHeight + "px";
    document.getElementById('ifmModalPopUrl').style.height = (intHeight - 17) + "px";
    document.getElementById('ifmModalPopUrl').src = strUrl;
    document.getElementById('divModalPopTitle').innerHTML = strTitle;
    SetBlankSize('divModalPopUrl', intWidth, intHeight);
    SetWindowPos('divModalPopUrl', intWidth, intHeight);
    ToggleModalDiv('divModalPopUrl');
}

function VisibleModalDiv(strDivName, bVisible) {
    var objDiv = document.getElementById(strDivName);
    if (bVisible)
        objDiv.style.display == 'block';
    else
        objDiv.style.display == 'none';
}

function ToggleModalDiv(div_id) {
    var el = document.getElementById(div_id);
    if (el.style.display == 'none') { el.style.display = 'block'; }
    else { el.style.display = 'none'; }
}

function SetBlankSize(divModalVar, intDivWidth, intDivHeight) {
    var intScrollTop = 0;
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
        intScrollTop = document.body.scrollTop;
    } else {
        viewportheight = document.documentElement.clientHeight;
        intScrollTop = document.documentElement.scrollTop;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight))
        intBlanketHeight = viewportheight;
    else {
        if (navigator.appName.indexOf('Microsoft') != -1)
            intBlanketHeight = document.body.parentNode.scrollHeight;
        else
            intBlanketHeight = document.getElementById("tblMain").scrollHeight;
    }
    var divBlanket = document.getElementById('divBlanket');
    divBlanket.style.height = intBlanketHeight + 'px';
    var divModal = document.getElementById(divModalVar);
    var intHalf = (viewportheight - intDivHeight) / 2;
    intHalf -= 3;
    intTop = intHalf <= 0 ? 10 : intHalf;
    intTop = intScrollTop + intHalf;
    divModal.style.top = intTop + 'px';
}

function SetWindowPos(divModalVar, intDivWidth, intDivHeight) {
    if (typeof window.innerWidth != 'undefined')
        viewportwidth = window.innerHeight;
    else
        viewportwidth = document.documentElement.clientHeight;

    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth))
        window_width = viewportwidth;
    else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth)
            window_width = document.body.parentNode.clientWidth;
        else
            window_width = document.body.parentNode.scrollWidth;
    }
    var divModal = document.getElementById(divModalVar);
    window_width = (window_width - intDivWidth) / 2;
    divModal.style.left = window_width + 'px';
}

function CloseModalDiv(strDivName) {
    ToggleModalDiv('divBlanket');
    ToggleModalDiv(strDivName);
}

function PopupModalDiv(strDivName) {
    SetBlankSize(strDivName, 0, 0);
    SetWindowPos(strDivName, 0, 0);
    ToggleModalDiv('divBlanket');
    ToggleModalDiv(strDivName);
}

function DisplayModalDiv(strDivName, intDivWidth, intDivHeight) {
    SetBlankSize(strDivName, intDivWidth, intDivHeight);
    SetWindowPos(strDivName, intDivWidth, intDivHeight);
    ToggleModalDiv('divBlanket');
    ToggleModalDiv(strDivName);
}

function GetWindowSize() {
    var intWidth = 0, intHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        intWidth = window.innerWidth;
        intHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        intWidth = document.documentElement.clientWidth;
        intHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        intWidth = document.body.clientWidth;
        intHeight = document.body.clientHeight;
    }
    return [intWidth, intHeight];
}
