﻿var openedModalPopup = null;

function ModalPopup(popupId)
{

    this.popupId = popupId; 
    this.show = show;
    this.hide = hide;
    this.resizeShadow = resizeShadow;

   
    function resizeShadow()
    {
        if (openedModalPopup!=null)
        {
            var elw = $(openedModalPopup.popupId);
            openedModalPopup.shadow1.style.top = (parseInt(elw.style.top.replace("px",""))+5)+"px"
            openedModalPopup.shadow1.style.left = (parseInt(elw.style.left.replace("px",""))+elw.clientWidth-2)+"px"
            openedModalPopup.shadow1.style.width = "7px";
            openedModalPopup.shadow1.style.height = elw.clientHeight+"px";

            wtop = (parseInt(elw.style.top.replace("px",""))+elw.clientHeight-2);
            if (isIE6()) wtop -= 6;
            openedModalPopup.shadow2.style.top = wtop+"px"
            openedModalPopup.shadow2.style.left = (parseInt(elw.style.left.replace("px",""))+5)+"px"
            openedModalPopup.shadow2.style.width = (elw.clientWidth-7)+"px";
            openedModalPopup.shadow2.style.height = "7px";
        
        }
    }
    
    function show()
    {

        popupEl = $(this.popupId);
        grayScreen();

        popupEl.className += " modal-dialog-position";
        popupEl.style.display = "block";

        scTop = (document.documentElement.scrollTop || document.body.scrollTop);
        wLeft = Math.round((document.documentElement.clientWidth-popupEl.clientWidth)/2);
        wTop = Math.round((document.documentElement.clientHeight-popupEl.clientHeight)/2);
        
        popupEl.style.top = wTop+"px";
        popupEl.style.left = wLeft+"px";
        popupEl.style.zIndex = "120002";
    
        openedModalPopup = this;
        this.shadow1 = document.createElement("div");
        document.body.appendChild(this.shadow1);
        this.shadow1.className="modal-dialog-shadow";
        
        this.shadow2 = document.createElement("div");
        document.body.appendChild(this.shadow2);
        this.shadow2.className="modal-dialog-shadow";
        
        this.resizeShadow(); 
        if (isIE())
        {
            document.getElementById(this.popupId).attachEvent("onresize",resizeShadow);
        }
        else
        {
            document.getElementById(this.popupId).addEventListener("resize", resizeShadow, false); 
        }    
        
        //document.documentElement.style.overflow = "hidden";
        
    }
    
    function hide(ev) 
    {
        popupEl = $(this.popupId);
        popupEl.style.display = "none";
        ungrayScreen(); 
        try{
        document.body.removeChild(this.shadow1);
        document.body.removeChild(this.shadow2);
        }
        catch(e){}
        openedModalPopup = null;
        //document.documentElement.style.overflow = "auto"
    }
}


var controlModal = null;

function OpenModalWindow(controlId)
{ 
    controlModal = new ModalPopup(controlId); 
    controlModal.show(true);         
    if (window.SetFocus!=null)
    {
        try
        {
         SetFocus();
        }
        catch(e){}
    }
    return false;
}

function CloseModalWindow()
{
    try
    {
        if (controlModal)
        {
            controlModal.hide();
        }
    }
    catch(e){}
    return false;
}

function IsModalOpen()
{
    if (controlModal != null)
    {
        return true;
    }
    return false;
}





