
RegisterNamespace("MF");

MF.initModalForm = function()
{
    var frm = document.getElementById("modalform");
    if (frm != null)
       MF.showModalForm(frm);
    else   
       MF.restoreState();
}

MF.getModalFormCoords = function()
{
    var res = new Object();
    var frm = document.getElementById("modalform");
    if (frm != null)
    {
       res.Top = parseInt(frm.style.top, 10);
       res.Left = parseInt(frm.style.left, 10);
    }
    else
    {
       res.Top = 0;
       res.Left = 0;
    }
    return res;
}

MF.showModalForm = function(container)
{

    container.style.zIndex = 700; 
    container.style.position = "absolute";
    container.style.left = (document.body.clientWidth - container.offsetWidth)/2 + "px";
    if (container.offsetHeight < (document.body.clientHeight + document.body.scrollTop))
		container.style.top = ((document.body.clientHeight - container.offsetHeight)/2  + document.body.scrollTop) + "px";
    else		
        container.style.top = "10px";
    container.style.visibility = "visible";
    

    var shadow = document.getElementById("modalform_shadow");
    shadow.style.zIndex = 699; 
    shadow.style.position = "absolute";
    shadow.style.backgroundColor = "#000"; 
    shadow.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
    shadow.style.MozOpacity = 0.5;
    shadow.style.opacity = 0.5;
    shadow.style.left="0px";
    shadow.style.top="0px";
    shadow.style.width = document.body.scrollWidth + "px";
    shadow.style.height = document.body.scrollHeight + "px";

    if (document.all)
    {
		var selects = document.getElementsByTagName("SELECT");
		for(var i=0; i<selects.length; i++)
		{
			if (!MF.isContainerSelect(selects[i], container))
			{
			  selects[i].prevVisibility = selects[i].style.visibility;
			  selects[i].style.visibility="hidden";
			}  
		}
    }
    
    if (window.attachEvent)
        window.attachEvent("onresize", MF.resizeShadow);
    else    
        window.addEventListener("resize", MF.resizeShadow, false);
}

MF.isContainerSelect = function(sel, container)
{
   var el = sel.offsetParent;
   while(el != null)
   {
      if (el == container)
        return true;
      el = el.offsetParent;  
   }
   return false;
}

MF.restoreState = function()
{
    if (document.all)
    {
		var selects = document.getElementsByTagName("SELECT");
		for(var i=0; i<selects.length; i++)
		{
		   if (selects[i].prevVisibility != null)
         	  selects[i].style.visibility = selects[i].prevVisibility;
        } 	  
    }
}

MF.resizeShadow = function()
{
    var shadow = document.getElementById("modalform_shadow");
    if (shadow != null)
    {
        shadow.style.width = document.body.scrollWidth + "px";
        shadow.style.height = document.body.scrollHeight + "px";
    }
    
    var container = document.getElementById("modalform");
    if (container != null)
    {
        container.style.left = (document.body.clientWidth - container.offsetWidth)/2 + "px";
        if (container.offsetHeight < document.body.clientHeight)
		    container.style.top = ((document.body.clientHeight - container.offsetHeight)/2  + document.body.scrollTop) + "px";
        else		
            container.style.top = "10px";
    }
    
}

MF.restoreStateAfterPostBack = function()
{
    if (window.attachEvent)
        window.attachEvent("onload", MF.initModalForm);
    else    
        window.addEventListener("load", MF.initModalForm, false);
}

MF.restoreStateAfterPostBack();

