﻿
var MessageBox =
{    
    Options: {"Title":"Alert", "Caption":"Information"},

    PopupWin: function(sourceID, msg, type, btnObj, args)
    {
        var msgbox = document.getElementById("divMessageBox");
        if(msgbox == null)
        {
            
            msgbox = document.createElement("div");
            msgbox.id                    = "divMessageBox";
            msgbox.style.fontFamily      = "Arial, Helvetica, sans-serif"; 
            msgbox.style.fontSize        = "14px"; 
            msgbox.style.width           = "452px";
            msgbox.style.height          = "148px";
            msgbox.style.padding         = "0px";
            msgbox.style.border          = "0px solid red";
            msgbox.style.margin          = "auto";
            msgbox.style.position        = "absolute";
            msgbox.style.zIndex          = 100000;
            msgbox.style.display         = "block";
            document.body.appendChild(msgbox);
            
            var frame = document.createElement("iframe");
            frame.style.width           = "452px";
            frame.style.height          = "148px";
            frame.style.position        = "absolute";
            frame.style.zIndex          = -1;
            frame.style.borderStyle     = "none";
            frame.style.borderWidth     = "0px";
            frame.frameBorder           = 0;
            msgbox.appendChild(frame);
            
            msgboxBody = document.createElement("div");

            msgboxBody.id                    = "divMessageBoxBody";
            msgboxBody.style.fontFamily      = "Arial, Helvetica, sans-serif"; 
            msgboxBody.style.fontSize        = "14px"; 
            msgboxBody.style.width           = "450px";
            msgboxBody.style.height          = "146px";
            msgboxBody.style.padding         = "0px";
            msgboxBody.style.border          = "1px solid #2D605E";
            msgboxBody.style.margin          = "auto";
            msgboxBody.style.position        = "absolute";
            //msgbox.style.zIndex          = 100000;
            msgboxBody.style.display         = "block";
            msgboxBody.style.backgroundColor = "#ffffff";
            
            msgbox.appendChild(msgboxBody);            
                        
            var title = document.createElement("div");
            title.id                        = "divMessageBoxTitle";
            title.style.height              = "24px"; 
            title.style.paddingTop          = "4px";
            title.style.paddingLeft         = "10px";
            title.style.backgroundImage = "url(/images/messagebox/left_t_bg2.gif)";
            title.style.color               = "#ffffff";
            title.style.fontWeight          = "bold";            
            title.style.fontSize            = "12px"; 
            title.style.cursor              = "move";
            title.style.position            = "relative";
            
            var caption = document.createElement("div");
            if(type == "alert")
                caption.innerHTML = "Information";
            else
                caption.innerHTML = "Question";
            title.appendChild(caption); 
            
            msgboxBody.appendChild(title);
            
            var container = document.createElement("div");
            container.style.height = "87px"
            msgboxBody.appendChild(container);
            
            var content = document.createElement("div");
            content.id = "divMessageBoxContent";
            content.style.padding = "10px 20px 30px 20px";
            container.appendChild(content);
            
            var buttons = document.createElement("div");
            buttons.style.height            = "26px";
            buttons.style.paddingTop        = "4px";
            buttons.style.backgroundImage = "url(/images/messagebox/warning_bottom_bg.gif)";
            buttons.style.textAlign         = "center";
            msgboxBody.appendChild(buttons);
            
            var btnOK = document.createElement("img"); 
            btnOK.id = "btnOKOK";          
            btnOK.style.cursor = "pointer";
            btnOK.style.width  = "65px";
            btnOK.style.height = "22px";
            btnOK.src = "/images/messagebox/buttom_sure.gif";    

            buttons.appendChild(btnOK);
            
            if(type != "alert")
            {
                var btnCancel = document.createElement("img");            
                btnCancel.style.cursor = "pointer";
                btnCancel.style.width  = "65px";
                btnCancel.style.height = "22px";
                btnCancel.style.marginLeft = "22px";
                btnCancel.src = "/images/messagebox/button_cancel.gif";  
                btnCancel.onclick = function ()
                {
                    document.getElementById("divMessageBox").style.display = "none";
                    MaskWindow.Hide();                
                }
                buttons.appendChild(btnCancel);                        
            }                        
        }
        
        document.getElementById("btnOKOK").onclick = function ()
        { 
            if(sourceID != null && document.getElementById(sourceID) != null)
            {
                document.getElementById(sourceID).focus();
            }
            document.getElementById("divMessageBox").style.display = "none";
            MaskWindow.Hide();
            if(btnObj != null)
                __doPostBack(btnObj.id, args);    
        }
        
        var w = window.innerWidth  || document.body.clientWidth;
        var h = window.innerHeight || document.body.clientHeight;
        
        msgbox.style.left = ((w - parseFloat(msgbox.style.width )) / 2) + document.body.scrollLeft + "px";
        msgbox.style.top  = ((h - parseFloat(msgbox.style.height)) / 2) + document.body.scrollTop  + "px"; 
        if(type == "alert")
            document.getElementById("divMessageBoxContent").innerHTML = "<img src='/images/messagebox/warning_2.gif' width='32' height='32' align='left' style='margin-left:40px;margin-right:10px;margin-top:10px;'><br/>" + msg;
        else
            document.getElementById("divMessageBoxContent").innerHTML = "<img src='/images/messagebox/warning_3.gif' width='32' height='32' align='left' style='margin-left:40px;margin-right:10px;margin-top:10px;'><br/>" + msg;
        MaskWindow.Show();
        
        document.onkeypress = function(e)
        {
            var e = e || window.event;  

            if(document.getElementById("divMessageBox").style.display != "none")
            {
                if(e.keyCode == 32 || e.keyCode == 13 || e.keyCode == 0)
                {
                    document.getElementById("divMessageBox").style.display = "none";
                    MaskWindow.Hide();
//                    if(btnObj != null)
//                        __doPostBack(btnObj.id, args); 
                }
            }                        
        }
                        
        return msgbox;
    },
    
    Alert : function(sourceID, msg)
    {        
        var p = this.PopupWin(sourceID, msg, "alert", null, ""); 
        p.style.display = "block";               
        p.focus();
        Drag(p);
    },
    
    Confirm : function(sourceID, msg, btnObj, args)
    {
        var p = this.PopupWin(sourceID, msg, "confirm", btnObj, args);
        p.style.display = "block";   
        p.focus();
        Drag(p);
    }   
}

function Drag(obj)
{
	var ie=document.all;
	var nn6=document.getElementById&&!document.all;
	var isdrag=false;
	var y,x;
	obj.onmousedown = function(e) 
	{	
	    var divx = obj.getElementsByTagName("div");
	    for(var i=0; i<divx.length; i++)
	    {
            if (divx[i].id == "divMessageBoxTitle") 
            {
                isdrag = true;
                                
                nTY = parseInt(obj.style.top+0);
                
                y = nn6 ? e.clientY : event.clientY;
                
                nTX = parseInt(obj.style.left+0);
                
                x = nn6 ? e.clientX : event.clientX;
                
                divx[i].onmousemove= function(e)
                {
                    if (isdrag) 
                    {
                        obj.style.top  =  (nn6 ? nTY + e.clientY - y : nTY + event.clientY - y) + "px";
                        obj.style.left =  (nn6 ? nTX + e.clientX - x : nTX + event.clientX - x) + "px";
                    	
                        return false;
                    }
                };
                return false;
            }
		 }
	}
	obj.onmouseup=function(){isdrag=false;};
}

var MaskWindow =
{
    Show : function()
    {
        var displayer = document.getElementById("divMaskWinDisplayer");
        if(displayer == null)
        {            
            displayer = document.createElement("div");
            document.body.appendChild(displayer);
             
            displayer.style.position = "absolute";
            displayer.id = "divMaskWinDisplayer";
            displayer.style.zIndex = 9999;
            displayer.style.left = "0px";
            displayer.style.top = "0px";

            displayer.style.width = Math.max(document.body.scrollWidth, document.body.offsetWidth)    + "px";
	        displayer.style.height = Math.max(document.body.scrollHeight, document.body.offsetHeight) + "px";  
		        
            displayer.child = null;	
            displayer.style.opacity = 0.1;
            displayer.style.filter = "alpha(opacity=20)";	
            displayer.style.backgroundColor = "#FFFFFF";            
        }
        
        displayer.style.display = "block";
    },
    
    Hide : function()
    {
        var displayer = document.getElementById("divMaskWinDisplayer"); 
        
        if(displayer != null)
        {
            displayer.style.display = "none";
        }        
    }           
}
