﻿function _$(_sId)
{
    return document.getElementById(_sId)
}
function HideMsgBox()
{
    _$("MsgBoxDiv").style.display = "none";
}

function MessageBox()
{
    this.width = 300;
    this.height = 200;
    this.left = (document.body.scrollWidth-this.width)/2;
    this.top = (document.body.scrollHeight-this.height)/2;
    this.zindex = 10;
    this.bgColor = "#ffffff";
    this.alpha = 80;
    this.title = "提示信息";
    
    this.init = function()
    {
        try
        {
            var divMsg = document.createElement("DIV");
            document.body.appendChild(divMsg);
            
            divMsg.id='MsgBoxDiv';
            divMsg.style.display = "none";
            divMsg.style.width = this.width;
            divMsg.style.height = this.height;
            divMsg.style.position = "absolute";
            divMsg.style.borderWidth = "1px";
            divMsg.style.left = this.left;
            divMsg.style.top = this.top;
            divMsg.style.zIndex = this.zindex;
            divMsg.style.bgColor = this.bgColor;
            divMsg.style.filter = "alpha(opacity="+this.alpha+")";
            
            var strTitle = '<table width="100%" onselectstart="return false;" border="0" cellpadding="0" cellspacing="0">'+
                        '<tr>'+
                        '<td id="MsgBoxTitleImg" class="MsgBoxTitleImgWarn"></td><td id="MsgBoxTitle" onmousedown="MoveLayer(MsgBoxDiv);" class="MsgBoxTitle">系统提示信息</td>'+
                        '<td class="MsgBoxClose" onclick="HideMsgBox()"></td>'+
                        '</tr>'+
                        '</table>';
                        
            var strBody = '<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">'+
                        '<tr><td id="MsgBoxImg" class="MsgBoxImgSorry" align="center" width="100px"></td>'+
                        '<td id="MsgBoxBody" style="border-width:0px;height:120px"></td></tr>'+
                        '<tr><td colspan="2" align="center">'+ 
                        '<input id="btnOK" type="button" value="确 定" onclick="HideMsgBox()"/><input id="btnCancel" type="button" value="取 消" onclick="HideMsgBox()" style="display:none" />' +
                        '</tr></table>';
                        
            divMsg.innerHTML = strTitle + strBody;  
            return divMsg;
        }
        catch(e)
        {
            alert("MessageBox.init() 错误。错误信息："+e);
        }
    }
    this.sound = function()
    {
       /* var bs = document.getElementById("bgSound");
        if(bs!=null) document.body.removeChild(bs);
        bs = document.createElement('<embed id="bgSound" autostart="true" style="display:none"/>');
        bs.src = "Music/Error.wav";
        document.body.appendChild(bs);*/
    }
    this.set = function(_sAtt,_sValue)
    {
        try
        {
            _sAtt = _sAtt.toLowerCase();
        
            if(_$("MsgBoxDiv") == null)//如果层未创建
            {
                switch(_sAtt)
                {
                    case "width" :   msgBox.style.width = _sValue;break;
                    case "height" :  msgBox.style.height = _sValue;break;
                    case "zindex" :  msgBox.style.zindex = _sValue;break;
                    case "left" :    msgBox.style.left = _sValue;break;
                    case "top" :     msgBox.style.top = _sValue;break;
                    case "bgColor":  msgBox.style.backgroundColor = _sValue;break;
                    case "alpha":    msgBox.style.filter = "alpha(opacity="+_sValue+")";
                    default :        break;
                }
            }
            else//如果层已创建
            {
                var MsgBoxTitle = _$("MsgBoxTitle");
                var MsgBoxTitleImg = _$("MsgBoxTitleImg");
                
                var MsgBoxImg = _$("MsgBoxImg");
                var MsgBoxBody = _$("MsgBoxBody");
                switch(_sAtt)
                {
                    case "title" :   MsgBoxTitle.innerHTML = _sValue;break;
                    case "message":  MsgBoxBody.innerHTML = _sValue;break;
                    case "titleimg": MsgBoxTitleImg.className=_sValue;break;
                    case "boximg":   MsgBoxImg.className=_sValue;break;
                    default :        break;
                }
            }
        }
        catch(e)
        {
            alert("MessageBox.set() 错误。错误信息："+e);
        }
    }
    this.show = function(_sMsg,_sTitle,_sType)
    { 
        try
        {
            alert(_sMsg)
			/*
			var msgBox = (_$("MsgBoxDiv") == null) ? this.init() : _$("MsgBoxDiv"); 
            msgBox.style.display = "block";
            
            var MsgBoxTitle = _$("MsgBoxTitle");
            var MsgBoxTitleImg = _$("MsgBoxTitleImg");
            var MsgBoxImg = _$("MsgBoxImg");
            var MsgBoxBody = _$("MsgBoxBody"); 
               
            if(_sTitle==null || _sTitle=="") _sTitle = "提示信息";
            if(_sType==null || _sType=="") _sType = "sorry";
            
            if(_sMsg && _sMsg!="") MsgBoxBody.innerHTML = _sMsg.replace("'","\'");
            if(_sTitle && _sTitle!="") MsgBoxTitle.innerHTML = _sTitle;
            
            if(_sType)
            {
                _sType = _sType.toLowerCase();
                switch(_sType)
                {
                    case "pass":    MsgBoxImg.className="MsgBoxImgPass";   break;
                    case "sorry" :  MsgBoxImg.className="MsgBoxImgSorry";   break;
                    case "smile" :  MsgBoxImg.className="MsgBoxImgSmile";   break;
                    case "forbid" : MsgBoxImg.className="MsgBoxImgForbid";   break;
                    case "forget" : MsgBoxImg.className="MsgBoxImgForget";   break;                
                    case "error":   MsgBoxImg.className="MsgBoxImgError";   break;
                    default :       MsgBoxImg.className="MsgBoxImgSorry";   break;
                }
            }
            this.sound();
			*/
        }
        catch(e)
        {
            alert("MessageBox.show() 错误。错误信息："+e);
        }
    }
    this.close = function ()
    {
        try
        {
            _$("MsgBoxDiv").style.display = "none";
        }
        catch(e)
        {
            alert("MessageBox.close() 错误。错误信息："+e);
        }
    }
    this.remove = function()
    {
        try
        {
            if(_$("MsgBoxDiv") != null)
                document.body.removeChild(_$("MsgBoxDiv")); 
        }
        catch(e)
        {
            alert("MessageBox.remove() 错误。错误信息："+e);
        }
    }
    this.move = function(nTop,nLeft)
    {
        try
        {
            var msgBox = (_$("MsgBoxDiv") == null) ? this.init() : _$("MsgBoxDiv"); 
            msgBox.style.display = "block";
            
            msgBox.style.pixelTop = nTop;
            msgBox.style.pixelLeft = nLeft ;
        }
        catch(e)
        {
            alert("MessageBox.move() 错误。错误信息："+e);
        }
    }
    this.rebuild = function()//重新生成
    {
        try
        {
            this.remove();
            this.init();
        }
        catch(e)
        {
            alert("MessageBox.rebuild() 错误。错误信息："+e);
        }
    }
}

var max_zIndex = 1;
function MoveLayer(oDiv)
{
    if(typeof(oDiv)!="object")
    {
        oDiv = _$(oDiv);
        if(oDiv == null)
        {
            alert("移动层：传入的参数oDiv错误");
            return false;
        }
    }
    //window.status = oDiv.clientLeft+","+oDiv.clientTop+ "  z-index:"+max_zIndex;
    try
    {
        oDiv.style.zIndex = max_zIndex ++ ;
        oDrag = event.srcElement ;
        oDrag.setCapture();  
        var pX = event.clientX - oDiv.style.pixelLeft;
        var pY = event.clientY - oDiv.style.pixelTop;
        oDrag.onmousemove = function()
        {
            if(event.button==1)
            {
                oDiv.style.pixelLeft = event.clientX - pX;
                oDiv.style.pixelTop  = event.clientY - pY;
            }
        }
        oDrag.onmouseup = function()
        {
            oDrag.releaseCapture();
        }
    }
    catch(e)
    {
        alert("MessageBox文件 MoveLayer() 错误。错误信息："+e);
    }
}
