﻿var popupwindow = null;
var refreshPage = false;

function move_box(box, width, height)
{

    /*
    var cleft = 0;
    var ctop = 0;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }
    
    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }
    */

     var xMax, yMax, xOffset, yOffset;
			xMax = document.body.clientWidth;
			yMax = document.body.clientHeight;
			scrollMax = document.body.scrollTop;
      		yOffset = (yMax - height) /2 + scrollMax;
    		xOffset = (xMax - width) / 2 + document.body.scrollLeft;
		
//    box.style.top = ctop + 'px';
    box.style.top = yOffset;
    box.style.left = xOffset;
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function CustomPopup( url, width, height, refreshparent)
{
    if (popupwindow != null)
    document.body.removeChild(document.all[popupwindow]);
    var href = url;
    var boxdiv = document.getElementById(href);
    refreshPage = refreshparent;
    
    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            // Show existing box, move it
            // if document changed layout
            move_box(boxdiv, width, height);
            boxdiv.style.display='block';
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    popupwindow = href;
    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);
    borderStyle = '1px solid #979797';
    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    
    var xMax = document.body.clientWidth;
    var yMax = document.body.clientHeight;
    
    if (xMax < width) width = xMax;
    if (yMax < height) height = yMax;

    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = borderStyle;
    boxdiv.style.textAlign = 'right';
    //boxdiv.style.padding = '4px';
    boxdiv.style.background = '#FFFFFF';
    document.body.appendChild(boxdiv);

    var offset = 0;

    // Remove the following code if 'Close' hyperlink
    // is not needed.
    
    // End of 'Close' hyperlink code.

    var contents = document.createElement('iframe');
    //contents.scrolling = 'no';
    contents.overflowX = 'hidden';
    contents.overflowY = 'scroll';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.appendChild(contents);

    move_box(boxdiv, width, height);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}


 function ClosePopUp()
{
    document.all[popupwindow].style.display = "none";
    document.body.removeChild(document.all[popupwindow]);
    popupwindow = null;
    if (refreshPage)
        window.location.reload();
}