﻿/*
编写者：郑亮
时间：  2007-4-25

调用方法例子：onclick="showModalWindow('xwin1','标题',400,400,'BaseModule/MessageConfig.aspx');return false;"
*/

var x0=0,y0=0,x1=0,y1=0;
var offx =0,offy = 0;
//var offx =6,offy = 6;
var moveable = false;
//var hover = 'orange';
//var normal = '#264F8A';//color;

var className = "";//整个层CSS名称
var headClassName = "";//头部CSS名称

//创建一个对象;
function xWin(id,w,h,l,t,tit,msg,isShowTopWindow)
{
	this.id        = id;
	this.width   = w;
	this.height  = h;
	this.left      = l;
	this.top      = t;
	
	
	
	this.zIndex  = getGlobalZIndex();
	this.title     = tit;
	this.message = msg;
	this.obj      = null;
	
    this.TopWindow    = isShowTopWindow;

	this.bulid    = bulid;
	
	addGlobalZIndex(3);
	
	this.bulid();
}

//初始化;
function bulid()
{
    var twin = getWindow(this.TopWindow);
    var tdoc = twin.document;
    

    
	var str = ""
		//主体层1
		+ "<div id='xMsg" + this.id + "' "
		+ "class = '"+ className +"' " //定义样式
		+ "style='"
		+ "z-index:" + this.zIndex + ";"
		+ "width:" + this.width + "px;"
		+ "height:" + this.height + "px;"
		+ "left:" + this.left + "px;"
		+ "top:" + this.top + "px;"
		+ "position:absolute;"
		+ "cursor:default;"
//		+ "background-color:" + normal + ";"
//		+ "color:" + normal + ";"
//		+ "border:2px solid " + normal + ";"
		+ "' "
//		+ "onmousedown='getFocus(this)'"
		+">"
			//窗体头层1.1
			+ "<div "
		    + "class = '"+ headClassName +"' "//定义样式
			+ "style='"
			+ "width:" + (this.width - 4) + "px;"
			+ "height:20px;"
			+ "color:white;"
			+"cursor:move;"
			+ "' "
			+ "onmousedown='startDrag(this,event)' "
			+ "onmouseup='stopDrag(this,event)' "
			+ "onmousemove='drag(this,event)' "
			+ ">"
				+ "<span style='padding-left:3px;padding-top:1px;position:absolute;top:2px;font-weight: bold;'>" + this.title + "</span>"
			
				+"<span style='position:absolute;right:2px;border-width:0px;color:white;top:2px;cursor:pointer;vertical-align:baseline;'><a onclick='closeModalWindow(\""+this.id+"\","+this.TopWindow+");' style='color:#FFFFFF;cursor:hand;'>[关闭]</a></span>"
			+ "</div>"//窗体头层1.1结束
			//内容层1.2
			+ "<div id='xMsgBody"+this.id+"'  style='"
			+ "width:100%;"
			+ "height:" + (this.height - 20) + "px;"
			+ "background-color:white;"
			+ "line-height:14px;"
			+ "word-break:break-all;"
			+ "padding:0px;"
			+ "'>" + this.message 
			+ "</div>"//内容层1.2结束
		+ "</div>"//主体层1结束
		
//		//遮盖本窗体大小的层，不透明2
//		+ "<div id='xMsg" + this.id + "bg' style='"
//	    + "width:" + (this.width + 0) + ";"
//	    + "height:" + (this.height + 0) + ";"
//	    + "left:" + (this.left + 0) + ";"
//	    + "top:" + (this.top + 0) + ";"
//	    + "z-index:" + (this.zIndex-1) + ";"
//	    + "position:absolute;"
//	    + "'>"
//	    + createiFrame("../../script/_mesageShow.htm")
//	    +"</div>"//遮盖本窗体大小的层，不透明2结束
		
		//遮盖整页的层，透明3
		+ "<div id='xMsg" + this.id + "bg2' style='"
		+ "width:" + (tdoc.body.scrollWidth) + "px;"
		+ "height:" +(tdoc.body.scrollHeight) + "px;"
		+ "top:0px;"
		+ "left:0px;"
		+ "z-index:" + (this.zIndex-2) + ";"
		+ "position:absolute;"
		+ "background-color:Gray;"
		+ "filter:alpha(opacity=70);opacity:0.7;"
		+ "'>"
	    //+ createiFrame("../../script/_mesageShow.htm")
		+"</div>";//遮盖整页的层，透明3结束
	

	
	tdoc.body.insertAdjacentHTML("beforeEnd",str);  
}

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
     HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
     {
        switch (where)
        {
            case 'beforeBegin':
                this.parentNode.insertBefore(parsedNode,this)
                break;
            case 'afterBegin':
                this.insertBefore(parsedNode,this.firstChild);
                break;
            case 'beforeEnd':
                this.appendChild(parsedNode);
                break;
            case 'afterEnd':
                if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
                    else this.parentNode.appendChild(parsedNode);
                break;
         }
     }

     HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr)
     {
         var r = this.ownerDocument.createRange();
         r.setStartBefore(this);
         var parsedHTML = r.createContextualFragment(htmlStr);
         this.insertAdjacentElement(where,parsedHTML)
     }

     HTMLElement.prototype.insertAdjacentText = function (where,txtStr)
     {
         var parsedText = document.createTextNode(txtStr)
         this.insertAdjacentElement(where,parsedText)
     }
}




//核心基本方法
function createxWindow(wid,title,width,height,left,top,msg,isTop)
{
	var _isTop = isTop == null ? false : isTop;
	
	
	var a = new xWin(wid,width,height,left,top,title,msg,_isTop);
}

//关闭窗口
function closeModalWindow(wid,isTop)
{	
	var d = getDocument(isTop);
	
	var w1 = d.getElementById("xMsg"+wid);
	//var w2 = d.getElementById("xMsg"+id+"bg");
	var w3 = d.getElementById("xMsg"+wid+"bg2");
	
	if (w1 != null) d.body.removeChild(w1);
	//if (w2 != null) d.body.removeChild(w2);
	if (w3 != null) d.body.removeChild(w3);
	
	subGlobalZIndex(3);//减去记录z轴高度的全局变量
	removeContexts(wid);//移除记录子父窗口关联的全局变量
	
    return false;
}

//确认框关闭窗口
function confirmCloseModalWindow(id,isConfirm,controlID)
{
	closeModalWindow(id,false);
	if (!isConfirm) return;
	
	__doPostBack(controlID,'');
}

//确认框关闭窗口
function confirmToolbarCloseModalWindow(id,isConfirm,toolbarID,buttonKey)
{
	closeModalWindow(id,false);
	if (!isConfirm) return;
	
	var toolbar = igtbar_getToolbarById(toolbarID);
	var button = toolbar.Items.fromKey(buttonKey);
	
	toolbar.post(button.Id + ":DOWN");
}

//得到要显示窗口的左边距
function getWindowLeft(width,isTop)
{
    var w = getWindow(isTop);
    //return (w.screen.availWidth - width) / 2;
    
  
    
    return (w.document.body.clientWidth - width) / 2;
}

//得到要显示窗口的顶边距
function getWindowTop(height,isTop)
{
    var w = getWindow(isTop);
    
 return (w.screen.height - height) / 2;
}

//------------------------------------------------------------创建内容------------------------------------------------------------

//创建iframe
function createiFrame(url)
{
    return "<iframe style=' visibility:inherit; width:100%; border-collapse: collapse; height:100%' src='" +url+"' frameborder='no'></iframe>";
}

//创建提示框标签
function createAlertTag(msg,wid)
{
    return "<table style='width:100%;height:100%;' border='0' cellspacing='0' cellpadding='6'><tr><td align='center' valign='middle'>"+msg+"</td></tr><tr><td align='center' valign='buttom' style='height:20px;'><input type='button' name='Button' class='Button' style='width:85px;' onclick='closeModalWindow(\""+wid+"\",false);' value='确定'></td></tr><tr><td style='height:8px'></td></tr></table>"
}

//创建确认框标签
function createConfirmTag(msg,wid,controlID) 
{
    return "<table style='width:100%;height:100%;' border='0' cellspacing='0' cellpadding='6'><tr><td align='center' valign='middle' colspan='2'>"+msg+"</td></tr><tr><td align='right' style='height:20px'><input type='button' name='Button' class='Button' style='width:85px;' onclick='confirmCloseModalWindow(\""+wid+"\",true);' value='确定'></td><td align='left'><input type='button' name='Cancel' class='Button' style='width:85px;' onclick='confirmCloseModalWindow(\""+wid+"\",false);' value='取消'></td></tr></table>"
}

//创建确认框标签
function createToolBarConfirmTag(msg,wid,toolbarID,buttonKey)
{
    return "<table style='width:100%;height:100%;' border='0' cellspacing='0' cellpadding='6'><tr><td align='center' valign='middle' colspan='2'>"+msg+"</td></tr><tr><td align='right' style='height:20px'><input type='button' name='Button' class='Button' style='width:85px;' onclick='confirmToolbarCloseModalWindow(\""+wid+"\",true,\""+toolbarID+"\",\""+buttonKey+"\");' value='确定'></td><td align='left'><input type='button' name='Cancel' class='Button' style='width:85px;' onclick='confirmToolbarCloseModalWindow(\""+wid+"\",false,null,null);' value='取消'></td></tr></table>"
} 

//得到根窗口对象
function getWindow(isTop)
{
    
    
    if (!isTop) return window;
    var w = window;
   
    while (w.parent.location.href != w.location.href)
    {
        w = w.parent;
    }
    
    return w;
}

//得到文档对象
function getDocument(isTop)
{
    var w = getWindow(isTop);
    return w.document;
}

//得到指定名称的对象
function getObject(objName)
{
    
    var o = null;
    o = window.document.getElementById(objName);//当前文档对象
    
    
    
    if (o != null) return o;

	var pd = getDocument(true);//得到根
    
    
    
    o = pd.getElementById(objName);//根文档对象
    
   
    
    if (o != null) return o;
       
     if(GetBrowser()=="IE")
     {
        if (pd.frames.length != 0)
        {
            for (i=0;i<pd.frames.length;i++)
            {
                o = pd.frames[i].self.window.document.getElementById(objName);//第二级
                
                
                 
                if (o != null) return o;
                
                if (pd.frames[i].frames.length != 0)
                {
                    for (j=0;j<pd.frames[i].frames.length;j++)
                    {
                    //2009-07-08徐宏删除掉服务器报没有权限错误
                        //o = pd.frames[i].frames[j].self.window.document.getElementById(objName);//第三级
                        
                         
                        if (o != null) return o;
                    } 
                } 
            }
        }
    }
    
    return null;
}

//得到全局zIndex
function getGlobalZIndex()
{
    var o = getObject("_zIndexControl");
    if (o == null) return 10000;
    if (o.value == "") o.value = 10000;
    
    return parseInt(o.value);
}

//累加全局zIndex
function addGlobalZIndex(val)
{
    var o = getObject("_zIndexControl");
    if (o == null) return;
    if (o.value == "") o.value = 10000;
    
    var z = parseInt(o.value);
    
    o.value = z + val;
}

//减全局zIndex
function subGlobalZIndex(val)
{
    var o = getObject("_zIndexControl");
    if (o == null) return;
    if (o.value == "") o.value = 10000;
    
    var z = parseInt(o.value);
    
    o.value = z - val;
}

//--------------------------------------拖动--------------------------------------

//开始拖动;
function startDrag(obj,e)
{
    var IEBr = GetBrowser();
	//alert(e.button );return;
	if((e.button==1 && IEBr=="IE") || (e.button==0 && IEBr=="FIREFOX"))
	{
	    
		//锁定标题栏;
		
		if(IEBr=="IE")
		{
		    obj.setCapture();
		}
		//定义对象;
		var win = obj.parentNode;
		var sha = win.nextSibling;
		
		
		//记录鼠标和层位置;
		x0 = e.clientX;
		y0 = e.clientY;
		x1 = parseInt(win.style.left);
		y1 = parseInt(win.style.top);
		
		moveable = true;
	}
}
//拖动;
function drag(obj,e)
{
	if(moveable)
	{
	   var win = obj.parentNode;
	   var twin = getWindow(this.TopWindow);
       var tdoc = twin.document;
       var objWidth= obj.clientWidth;
	   var objHeight=obj.clientHeight;

	   var leftPo= x1+e.clientX-x0;
	   if ( leftPo < 0 ) 
	   {
	     leftPo=0;
	   }
	    
	    //tdoc.body.scrollWidth
	    var bsw = tdoc.body.clientWidth;
	    if ( leftPo > bsw - objWidth ) {
		    leftPo = bsw-objWidth-5;
	    }

	    var topPo=y1+e.clientY-y0;
	    if ( topPo < 0 ) {
		    topPo=0;
	    }
       // alert(objHeight);

	    var bsh = tdoc.body.clientHeight;
	    if ( topPo > bsh - objHeight ) {
		    topPo = bsh - objHeight;
	    }
	    
	    win.style.left=leftPo+'px';
	    win.style.top=topPo+'px';
	}
}
//停止拖动;
function stopDrag(obj,e)
{
	if(moveable)
	{
	
		var win = obj.parentNode;
		var sha = win.nextSibling;
		var msg = obj.nextSibling;
		obj.releaseCapture();
		moveable = false;
	}
}

function getUCTDateString(){
   var d, s = "";
   d = new Date();
   s += d.getHours();
   s += d.getMinutes();
   s += d.getSeconds();
   s += d.getMilliseconds();
   
   return(s);
}


//--------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------得到父文档对象----------------------------------------------------

//父对象得到打开子窗口文档对象
function parentGetDoc(wid)
{
    var url = getContexts(wid,"p");
    if (url =="") return null;
    
    return getDocumentByUrl(url);
}

//子对象得到从属的父对文档对象
function dependentGetDoc(wid)
{
    var url = getContexts(wid,"d");
    if (url =="") return null;
    
    return getDocumentByUrl(url);
}

//得到指定Url文档对象
function getDocumentByUrl(docUrl)
{
    var w = getWindow(true);
    
    return getDoc(w.document.frames,docUrl);
}

//递归查询文档对象，内部使用
function getDocumentObject(frames,docUrl)
{
    var fs = frames;
    if (fs && fs.length != 0)
    {
        for (var i=0;i<fs.length;i++)
        {
            var doc = fs[i].self.window.document;
            
            var url = doc.location.href.toString();
            url = url.substring(url.lastIndexOf("/") + 1,url.length);
            var fUrl = docUrl.substring(docUrl.lastIndexOf("/") + 1,docUrl.length);
            
            if (url == fUrl) return doc;
            
            var docr = getDocumentObject(doc.frames,docUrl);
            if(docr) return docr;
        }
    }
}

//向全局变量加入文档关联
function addContexts(wid,purl,wurl)
{
    var o = getObject("_upWindowContext");//问题
    
    if (o == null) return;
    
    if (o.value.indexOf(wid) > -1) return;
    o.value += wid + "|" + purl + "|" + wurl + ",";
}

//从全局变量减去文档关联
function removeContexts(wid)
{
    var o = getObject("_upWindowContext");
    if (o == null) return;
    
    if (o.value.indexOf(wid) < 0) return;
    var allstr = o.value;
    var s1,s2;
    if (allstr.indexOf(wid) == 0)
    {
        s1 = "";
    }
    else
    {
        s1 = allstr.substring(0,allstr.indexOf(wid));
    }
	
    s2 = allstr.substring(allstr.indexOf(wid),allstr.length);
    s2 = s2.substring(s2.indexOf(",") + 1,s2.length);
    
    o.value = s1 + s2;
}

//得到关联，内部使用
function getContexts(wid,type)
{
    var o = getObject("_upWindowContext");
    if (o == null || o.value == "") return "";
    if (o.value.indexOf(wid) < 0) return "";
    var all = o.value;
    
    var arrAll = all.split(",");
    for (i=0;i<arrAll.length;i++)
    {
        var arr = arrAll[i].split("|");
        if (arr[0] == wid) return type == "p" ? arr[2] : arr[1];
    }
    
    return "";
}



//-----------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------外部调用方法----------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------

//显示模态窗口
function showModalWindow(wid,title,width,height,url)
{    
    headClassName = "ModalWindowHeadStyle";
    className = "ModalWindowStyle";
    
	var purl = this.document.location.href;
    //加入文档关联
	
	addContexts(wid,purl,url);
	
    
    var l = getWindowLeft(width,true);
    var t = getWindowTop(height,true);


    
	createxWindow(wid,title,width,height,l,t,createiFrame(url),true);
    
    return false;
}

//显示出错信息窗口
function showAlert(title,width,height,msg)
{
    wid = getUCTDateString();

    headClassName = "ErrModalWindowHeadStyle";
    className = "ErrModalWindowStyle";
    
    var l = getWindowLeft(width,false);
    var t = getWindowTop(height,false);
   
    createxWindow(wid,title,width,height,l,t,createAlertTag(msg,wid),false);

    return false;
}

//按钮显示提示信息窗口
function showConfirm(title,width,height,msg,controlID)
{
    wid = getUCTDateString();
   
    headClassName = "ModalWindowHeadStyle";
    className = "ModalWindowStyle";

    var l = getWindowLeft(width,false);
    var t = getWindowTop(height,false);
	
	createxWindow(wid,title,width,height,l,t,createConfirmTag(msg,wid,controlID),false);

    return false;
}

//Toolbar显示提示信息窗口(内部使用)
function showToolbarConfirm(title,width,height,msg,toolbarID,buttonKey)
{
    wid = getUCTDateString();
    
    headClassName = "ModalWindowHeadStyle";
    className = "ModalWindowStyle";

    var l = getWindowLeft(width,false);
    var t = getWindowTop(height,false);

    createxWindow(wid,title,width,height,l,t,createToolBarConfirmTag(msg,wid,toolbarID,buttonKey),false);

    return false;
}

//Toolbar显示提示信息窗口(编码者调用)
function toolbarConfirm(msg, oToolbar, oButton, oEvent)
{
    showToolbarConfirm("系统提示",200,120,msg,oToolbar.Id,oButton.Key);
    
    oEvent.needPostBack = false;
}

function GetBrowser()
{
    var oType = "";
    if(navigator.userAgent.indexOf("MSIE")!=-1){
        oType="IE";
    }else if(navigator.userAgent.indexOf("Firefox")!=-1){
        oType="FIREFOX";
    }
    return oType;
} 

/*
//最小化;
function min(obj)
{
	var win = obj.parentNode.parentNode;
	var sha = win.nextSibling;
	var tit = obj.parentNode;
	var msg = tit.nextSibling;
	var flg = msg.style.display=="none";
	if(flg)
	{
		win.style.height  = parseInt(msg.style.height) + parseInt(tit.style.height) + 2*2;
		//sha.style.height  = win.style.height;
		msg.style.display = "block";
		obj.innerHTML = "0";
	}
	else
	{
		win.style.height  = parseInt(tit.style.height) + 2*2;
		//sha.style.height  = win.style.height;
		obj.innerHTML = "2";
		msg.style.display = "none";
	}
}

//设置焦点
function setFocus(obj)
{
    try
    {
        var pObj = obj.parentElement;
        var a =(xWMouseX >= pObj.offsetLeft && xWMouseX <= (pObj.offsetLeft + pObj.offsetWidth)) && (xWMouseY >= pObj.offsetTop && xWMouseY <= (pObj.offsetTop + pObj.offsetHeight))
        if (a) return;
        if (obj != null) obj.focus();
    }
    catch(e)
    {
    
    }
}

//获得焦点;
function getFocus(obj)
{
	if (obj.style.zIndex != g_index)
	{
		g_index = g_index + 2;
		var idx = g_index;
		obj.style.zIndex = idx;
		obj.nextSibling.style.zIndex = idx - 1;
		obj.nextSibling.nextSibling.style.zIndex = idx - 2;
	}
}
*/

