﻿//仿IE模式对话框
function ModalDialog(id,title,innerHtml,mod,btnEvent,width,height,btnText){
    if (document.body){
        if (typeof(btnText)!="string")
            btnText="确 定";
	    if (typeof(width)!="number")
		    width=450;
	    if (typeof(height)!="number")
		    height=50;
	    var dialog=$(id);
	    if (dialog!=null){
		    var obj=$(id+"_DialogDivEntity");		
		    var btnBar=$(id+"_DialogDivEntity_toolsBar");
		    switch (mod){
			    case 1:
				    btnBar.innerHTML="<input class='ImageButton' type='button' onclick='HiddenModalDialog(\""+id+"\")' value=' "+btnText+" '>";
				    break;
			    case 2:
				    btnBar.innerHTML="<input class='ImageButton' onclick=\""+btnEvent.replace(/"/g,"&quot;")+"HiddenModalDialog('"+id+"');\" type='button' id='"+id+"_execBtn' value=' "+btnText+" '>&nbsp;&nbsp;<input class='ImageButton' type='button' onclick='HiddenModalDialog(\""+id+"\")' value=' 取 消 '>";
				    break;
		        case 3:
				    btnBar.innerHTML="<input class='ImageButton' onclick=\""+btnEvent.replace(/"/g,"&quot;")+"HiddenModalDialog('"+id+"');\" type='button' id='"+id+"_execBtn' value=' "+btnText+" '>";
		            break;
			    default:
				    //btnBar.innerHTML="<BUTTON onclick='HiddenModalDialog(\""+id+"\")'> 确 定 </BUTTON>"
				    break;
		    }
		    var btn=$(id+"_execBtn");
//		    if (btn!=null&&(mod==2||mod==3)){
//		        btn.btnEvent=btnEvent;
//		        btn.onclick=function(){
//		            eval(this.btnEvent);
//		            //alert(this.btnEvent);
//		            HiddenModalDialog(this.id.replace("_execBtn",""));
//		        }
//		    }
		    //alert(btnBar.innerHTML);
		    var titleBar=$(id+"_MsgBox_Title");
		    titleBar.innerText=title;
		    var contentBar=$(id+"_DialogDivEntity_body");
		    contentBar.innerHTML=innerHtml;		
		    dialog.style.display="block";
		    ActFirstFormElement(obj);
		    UI.Init(btnBar);
		    UI.Init(obj);
		    return dialog;
	    }
	    else{
		    var DialogDiv=document.createElement("DIV");
		    var s="";
		    s+="<div style='width:100%;height:100%;filter:alpha(opacity=10);opacity:0.1;";
		    if (!(Browser.isIE&&Int.Parse(Browser.Version)<=6))
		        s+="background:#000;"
		    s+="'><iframe id='"+id+"_DialogDivEntity_helper' allowtransparency='true' width='100%' height='100%' frameborder='0' style='width:100%;height:100%;z-index:3000;filter:alpha(opacity=0);opacity:0;border:none;' src='/transparent.htm'></iframe></div>";
		    s+="<div id='"+id+"_DialogDivEntity'><table border='0' cellspacing='0' cellpadding='0'><tr><td class='MsgBox_Title_Left'></td><td class='MsgBox_Title' id='"+id+"_DialogDivEntity_drager'><div id='"+id+"_MsgBox_Title' class='MsgBox_Title_Text'>"+title+"</div>";
		    if (mod==1||mod==2)
		        s+="<div class='MsgBox_CloseButton_on' onclick='HiddenModalDialog(\""+id+"\")' />";
		    s+="</td><td class='MsgBox_Title_Right'></td></tr><tr><td class='MsgBox_Middle_Left'></td><td class='MsgBox_Middle' align='center'><table width='100%' height='100%' border='0' align='center' cellpadding='0' cellspacing='0'><tr><td class='MsgBox_Body_Border_Top_Left' valign='top'></td><td  class='MsgBox_Body_Border_Top_Center'><img name='' src='' width='1' height='1' alt=''></td><td class='MsgBox_Body_Border_Top_Right'></td></tr><tr><td class='MsgBox_Body_Border_Middle_Left'><img name='' src='' width='1' height='1' alt=''></td><td class='MsgBox_Body' id='"+id+"_DialogDivEntity_body' style='text-align:left;width:"+width+"px;height:"+height+"px;' valign='top'>"+innerHtml+"</td><td class='MsgBox_Body_Border_Middle_Right'><img name='' src='' width='1' height='1' alt=''></td></tr><tr><td class='MsgBox_Body_Border_Middle_Left'><img name='' src='' width='1' height='1' alt=''></td><td class='MsgBox_Body' id='"+id+"_DialogDivEntity_toolsBar' align='center'>";
		    switch (mod){
			    case 1:
				    s+="<input class='ImageButton' type='button' onclick='HiddenModalDialog(\""+id+"\")' value=' "+btnText+" '>";
				    break;
			    case 2:
				    s+="<input class='ImageButton' type='button' onclick=\""+btnEvent.replace(/"/g,"&quot;")+"HiddenModalDialog('"+id+"');\" id='"+id+"_execBtn' value=' "+btnText+" '>&nbsp;&nbsp;<input class='ImageButton' type='button' onclick='HiddenModalDialog(\""+id+"\")' value=' 取 消 '>";
				    break;
			    case 3:
				    s+="<input class='ImageButton' type='button' onclick=\""+btnEvent.replace(/"/g,"&quot;")+"HiddenModalDialog('"+id+"');\" id='"+id+"_execBtn' value=' "+btnText+" '>";
				    break;
			    default:
				    //s+="<BUTTON onclick='HiddenModalDialog(\""+id+"\")'> 确 定 </BUTTON>"
				    break;
		    }
		    s+="</td><td class='MsgBox_Body_Border_Middle_Right'><img name='' src='' width='1' height='1' alt=''></td></tr><tr><td class='MsgBox_Body_Border_Bottom_Left'></td><td class='MsgBox_Body_Border_Bottom_Center'><img name='' src='' width='1' height='1' alt=''></td><td class='MsgBox_Body_Border_Bottom_Right'></td></tr></table></td><td class='MsgBox_Middle_Right'></td></tr><tr><td class='MsgBox_Bottom_Left'><img name='' src='' width='1' height='1' alt='' /></td><td class='MsgBox_Bottom'><img name='' src='' width='1' height='1' alt='' /></td><td class='MsgBox_Bottom_Right'><img name='' src='' width='1' height='1' alt='' /></td></tr></table></div>";
		    DialogDiv.innerHTML = s;
		    //alert(DialogDiv.innerHTML);
		    DialogDiv.id=id;
		    DialogDiv.style.position=(Browser.isIE&&Int.Parse(Browser.Version)<7)?"absolute":"fixed";
		    DialogDiv.style.top="0px";
		    DialogDiv.style.left="0px";
		    DialogDiv.style.display="block";
		    DialogDiv.style.cursor="default";
		    DialogDiv.style.width="100%";
		    DialogDiv.style.height="100%";
		    DialogDiv.style.zIndex=2000;
		    document.body.appendChild(DialogDiv);
		    var obj=$(id+"_DialogDivEntity");
		    obj.style.position="absolute";
		    obj.style.top=(DialogDiv.offsetHeight-obj.offsetHeight)/2+"px";
		    obj.style.left=(DialogDiv.offsetWidth-obj.offsetWidth)/2+"px";
		    var titleBar=$(id+"_DialogDivEntity_drager");
		    titleBar.onselectstart=function(){
			    return false;
		    }
		    var toolsBar=$(id+"_DialogDivEntity_toolsBar");
		    toolsBar.onselectstart=function(){
			    return false;
		    }
		    var btn=$(id+"_execBtn");
//		    if (btn!=null&&(mod==2||mod==3)){
//		        btn.btnEvent=btnEvent;
//		        btn.onclick=function(){
//		            eval(this.btnEvent);
//		            //alert(this.btnEvent);
//		            HiddenModalDialog(this.id.replace("_execBtn",""));
//		        }
//		    }
		    AddDrager(obj,null,DialogDiv);
		    ActFirstFormElement(obj);
		    DialogDiv.AddBtn=function(btnText,btnEvent,desabled){
		        AddBtnToWindow(this.id,btnText,btnEvent,desabled);
		    }
		    UI.Init(DialogDiv);
//		    document.body.style.width=DialogDiv.offsetWidth+"px";
//		    document.body.style.height="10000px";
//		    alert(document.body.offsetHeight+" "+DialogDiv.offsetHeight);
		    return DialogDiv;
	    }
    }
    else{
        Page.RegLoadEvent(function(){ModalDialog(id,title,innerHtml,mod,btnEvent,width,height,btnText)})
    }
}

function ActFirstFormElement(obj){
	var obj=FindFirstFormElement(obj);
	if (obj!=null) obj.focus();
}
function FindFirstFormElement(obj){
	if (typeof(obj)!="object") obj=document.body;
	var firstElement=null;
	for (var i=0;i<obj.childNodes.length;i++){
		if (firstElement!=null) break;
		try{
			if (obj.childNodes[i].tagName=="INPUT"||obj.childNodes[i].tagName=="SELECT"||obj.childNodes[i].tagName=="TEXTAREA"){
				firstElement=obj.childNodes[i];
				break;
			}
			else{
				if (obj.childNodes[i].hasChildNodes)
					firstElement=FindFirstFormElement(obj.childNodes[i]);
			}
		}
		catch(e){
		}
	}
	return firstElement;
}

function HiddenModalDialog(id){
	obj=$(id);
	if (typeof(obj)=="object"||obj!=null)
		obj.style.display="none";
}

function AddBtnToWindow(winID,btnText,btnEvent,desabled){
    if (typeof(winID)!="string"||win==null)
        return;
    var btnBar=$(winID+"_DialogDivEntity_toolsBar");
    if (btnBar==null)
        return;
    var btn=document.createElement("input");
    btn.type="button";
    btn.className=(desabled)?"MsgBox_Button_off":"MsgBox_Button";
    if (desabled)
        btn.desabled=true;
    else{            
        btn.onmouseover=function(){OnMouseOver(this)};
        btn.onmouseout=function(){OnMouseOut(this)};
        btn.onmousedown=function(){OnMouseDown(this)};
        btn.onmouseup=function(){OnMouseUp(this)};
        if (typeof(btnEvent)=="function")
            btn.onclick=btnEvent;
        else
            btn.onclick=function(){
                eval(btnEvent);
            }
    }
    btn.value=btnText;
    btnBar.appendChild(btn);
}

function AddDrager(module,drager,panel){
	if (typeof (drager)!="object"||drager==null)
		drager=$(module.id+"_drager");
	//alert(drager==null);
	if (drager==null) return;
	drager.module=module;
	DragEvent.init(drager,panel);
}

var DragEvent={
	"obj":null,
	"panel":null,
	"init":function(drager,panel){
	    if (typeof (drager)!="object"||drager==null)
	        DragEvent.panel=document.body;
	    else
	        DragEvent.panel=panel;
		drager.onmousedown=DragEvent.start;
		if(isNaN(drager.module.style.posLeft))
			drager.module.style.posLeft=0;
		if(isNaN(drager.module.style.posTop))
			drager.module.style.posTop=0;
		drager.onDragStart=new Function();
		drager.onDragEnd=new Function();
		drager.onDrag=new Function();
		drager.module.style.width=drager.module.offsetWidth+"px";
		drager.module.style.height=drager.module.offsetHeight+"px";
	},
	"start":function(e){
	    //alert("ss");
		var self=DragEvent.obj=this;
		e=DragEvent.fixE(e);
		var helperTop=Int.Parse(DragEvent.obj.module.style.top);
		var helperLeft=Int.Parse(DragEvent.obj.module.style.left);
		self.onDragStart(helperLeft,helperTop,e.clientX,e.clientY);
		self.lastMouseX=e.clientX;
		self.lastMouseY=e.clientY;
		document.onmousemove=DragEvent.drag;
		document.onmouseup=DragEvent.end;
		return false
	},
	"drag":function(e){
		e=DragEvent.fixE(e);
		var self=DragEvent.obj;
		if (Browser.isIE) self.setCapture();
		var cliY=e.clientY;
		var cliX=e.clientX;
		var helperTop=Int.Parse(DragEvent.obj.module.style.top);
		var helperLeft=Int.Parse(DragEvent.obj.module.style.left);
		var newHelperLeft,newHelperTop;
		newHelperLeft=helperLeft+cliX-self.lastMouseX;
		newHelperTop=helperTop+cliY-self.lastMouseY;
		if (newHelperLeft<0) newHelperLeft=0;
		if (newHelperLeft>DragEvent.panel.offsetWidth-DragEvent.obj.module.offsetWidth) {
		    newHelperLeft=DragEvent.panel.offsetWidth-DragEvent.obj.module.offsetWidth;
		}
		if (newHelperTop<0) newHelperTop=0;
		if (newHelperTop>DragEvent.panel.offsetHeight-DragEvent.obj.module.offsetHeight) {
		    newHelperTop=DragEvent.panel.offsetHeight-DragEvent.obj.module.offsetHeight;
		}
		DragEvent.obj.module.style.left=newHelperLeft+"px";
		DragEvent.obj.module.style.top=newHelperTop+"px";
		self.lastMouseX=cliX;
		self.lastMouseY=cliY;
		return false;

	},
	"end":function(){
		if (Browser.isIE) DragEvent.obj.releaseCapture();
		document.onmousemove=null;
		document.onmouseup=null;
		DragEvent.obj=null
	},
	"fixE":function(e){
		if(typeof e=="undefined")
			e=window.event;
		if(typeof e.layerX=="undefined")
			e.layerX=e.offsetX;
		if(typeof e.layerY=="undefined")
			e.layerY=e.offsetY;
		return e
	}
}
