﻿/*---------------------------------------------------------------------------------
*文件名：Confirm.js
*Copyright 2009 石家庄开发区华彤计算机有限公司 版权所有
*
*功能描述：
*作    者：赵兵祥
*创建日期：2009-6-4 17:48:29
*
*参数说明：iframesrc -- iframe中打开页面中的URL地址(可以传Get参数)  必选项
*          isConfirm -- 是否在层中显示处理页面
*          title -- 新打开层的标题                                  isConfirm为true时才需提供
*          isbackDiv -- 是否添加背景色  默认true                  ┐
*          isMove -- 窗口是否可以移动   默认true                  │
*          isScroll -- 滚动条状态 0 禁用 1 启用 3 自动   默认3    │─ 非必选项 但是必须按顺序提供参数 比如设置isScroll 则isMove和isbackDiv 必须设置 可以实用null表示使用默认值
*          isFullScreen -- 是否已全屏模式打开   默认false         ┘
*
*调用方法：javascript:Confirm('test2.aspx','选择产品类型',true,true,true,0,true)
*关闭方法：CloseFloatDiv() 如果是在新开窗口中关闭则是：parent.CloseFloatDiv()
*
*修改标识：
*修改描述：
---------------------------------------------------------------------------------*/
MaxHeight = 600;
MaxWidth = 600;

function $(elem){
	return document.getElementById(elem);
}
function Confirm(iframesrc,isConfirm,title,isbackDiv,isMove,isScroll,isFullScreen){
    //初始化变量
    if (isbackDiv == 'undefined' || isbackDiv == null) {
        isbackDiv = true;
    }
    if (isMove == 'undefined' || isMove == null) {
        isMove = true;
    }
    if (isScroll == 'undefined' || isScroll == null) {
        isScroll = 3;
    }
    if (isFullScreen == 'undefined' || isFullScreen == null) {
        isFullScreen = false
    }
    
	if($('floatDiv')==null || $('floatDiv_Confirm')== null){
	    
	    var s = "";
	    if (isConfirm == false)
	    {	     
	        if ($('floatDiv')==null)
	        {   
	            s = "<div style=\"width:120px;height:32px;background-image: url(../images/load.gif);background-repeat: no-repeat;background-position: 8px center;font-size: 14px;color: #000000;padding-left: 60px;padding-top: 18px;background-color: #FFFFFF;\">请求处理中...</div> ";
	            s += "<iframe id=\"floatIframe\" frameborder=\"0\" scrolling=\"no\" width=\"0%\" height=\"0%\" src=\""+iframesrc+"\"></iframe>";
    	        
    	        var floatDiv = document.createElement('div');
		        floatDiv.id = "floatDiv"; 
		        floatDiv.style.position = "absolute";
		        floatDiv.style.width = "180px";
		        floatDiv.style.height = "50px";
                floatDiv.style.border="0px";
                floatDiv.style.padding="0px"; 
                floatDiv.style.margin="0px";
                floatDiv.style.backgroundColor = "#FFFFFF";
		        floatDiv.style.left = (document.documentElement.clientWidth/2 + document.documentElement.scrollLeft - 91) +"px";
		        floatDiv.style.top = (document.documentElement.clientHeight/2 + document.documentElement.scrollTop - 26) +"px";
		        floatDiv.style.zIndex  = 1001;
		        floatDiv.innerHTML = s;
		        
		        document.body.appendChild(floatDiv);
		    }
	        else{
		        $('floatDiv').style.display = "block";
	        }
	    }
	    else
	    {
	        if ($('floatDiv_Confirm')==null){   
	        //E9F1FC
	            s = "<div id=\"headDiv\"";
	            if (isMove == true)
	            {
	                s += " onmousedown=\"moveDiv(event,'floatDiv_Confirm');\""
	            }
	            s += "><h3>"+title+"</h3><span><a href=\"javascript:void(null)\" onclick=\"CloseFloatDiv()\"><img src=\"../images/close.gif\" alt=\"关闭窗口\" style=\"border: 0px;\" /></a></span></div>";
	            s += "<div id=\"bodyDiv\">"
	            s += "<iframe id=\"floatIframe\" frameborder=\"0\" scrolling=\"auto\" width=\"1\" height=\"1\" src=\""+iframesrc+"\" onload=\"iframeSize(this,floatDiv_Confirm,"+isScroll+","+isFullScreen+")\"></iframe>";
	            s += "</div>"
    	        
    	        var floatDiv = document.createElement("DIV");
		        floatDiv.id = "floatDiv_Confirm"; 
		        floatDiv.style.position = "absolute";
		        floatDiv.style.width = "1px";
		        floatDiv.style.height = "1px";
                floatDiv.style.border="0px";
                floatDiv.style.padding="0px"; 
                floatDiv.style.margin="0px";
                floatDiv.style.backgroundColor = "#EEF7FE";
		        floatDiv.style.left = "-100px";
		        floatDiv.style.top = "-100px";
		        floatDiv.style.zIndex  = 1001;
		        floatDiv.innerHTML = s;
		        
		        //floatDiv.style.display = "none";
		        document.body.appendChild(floatDiv);
		    }
	        else{
		        $('floatDiv_Confirm').style.display = "block";
	        }
	    }
	}

	if($('backDiv')==null && isbackDiv ==true){
		var backDiv = document.createElement('div');
		backDiv.id = "backDiv";
		backDiv.style.backgroundColor = "#272727";
		backDiv.style.filter = "alpha(opacity=15)";
		backDiv.style.MozOpacity = "0.70";
		backDiv.style.position = "absolute";
		backDiv.style.left = "0px";
		backDiv.style.top = "0px";
		backDiv.style.width = Math.max(document.body.scrollWidth, document.documentElement.clientWidth) +"px";
		backDiv.style.height = Math.max(document.body.scrollHeight, document.documentElement.clientHeight)+"px";
		document.body.appendChild(backDiv);
		$('backDiv').style.zIndex = 1000;
	}
	else if(isbackDiv ==true){
		$('backDiv').style.display = "block";
	}
	
    //增加快捷键 按ESC关闭窗口
    document.body.onkeyup = function() {
	    if (event.keyCode == '27') {
	        CloseFloatDiv();
	    }
    }
    
    //创建回调环境
    if (document.forms.length > 0) {
        var fromname = document.forms[0].id;
        var theForm = $(fromname);
        
        if (document.getElementById('__EVENTTARGET') == null) {
            var eventTarget = document.createElement('input');
            eventTarget.setAttribute("type","hidden");
            eventTarget.setAttribute("name","__EVENTTARGET");
            eventTarget.setAttribute("id","__EVENTTARGET");
            eventTarget.setAttribute("value","");
            theForm.appendChild(eventTarget);
        }
        
        if (document.getElementById('__EVENTARGUMENT') == null) {
            var eventArgument = document.createElement('input');
            eventArgument.setAttribute("type","hidden");
            eventArgument.setAttribute("name","__EVENTARGUMENT");
            eventArgument.setAttribute("id","__EVENTARGUMENT");
            eventArgument.setAttribute("value","");
            theForm.appendChild(eventArgument);
        }
    }
}
//根据iframe的内容自动调整iframe的宽高
function iframeSize(iframe,floatDiv,isScroll,isFullScreen) {
    var Scroll = 0;
    
    if (isFullScreen == false) {  //是否全屏显示 全屏时iframe的高度和宽度均为90%
	    var bHeight = iframe.contentWindow.document.body.scrollHeight;
	    var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
	    var Height = Math.max(bHeight, dHeight);
    	
	    var bWidth = iframe.contentWindow.document.body.scrollWidth;
	    var dWidth = iframe.contentWindow.document.documentElement.scrollWidth;
	    var Width = Math.max(bWidth, dWidth);
    	
	    var addWidth = 0;
	    var addHeight = 0; //如果显示了垂直或横向滚动条 则增加对应宽度或高度
    	
	    if (Height > MaxHeight) {
	        addWidth = 20;
	    }
	    if (Width > MaxWidth) {
	        addHeight = 20;
	    }

	    if (isScroll == 0) {
            iframe.height = Height + addHeight;
            floatDiv.style.height = Height + addHeight + 50 + "px";
            floatDiv.style.top = (document.documentElement.clientHeight/2 + document.documentElement.scrollTop - Height/2) +"px";
    	    
            iframe.width = Width + addWidth;
            floatDiv.style.width = Width + addWidth + 15 + "px";
            floatDiv.style.left = (document.documentElement.clientWidth/2 + document.documentElement.scrollLeft - Width/2) +"px";
	    }
	    else {
	        if (Height > MaxHeight) {
	            iframe.height = MaxHeight + addHeight;
	            floatDiv.style.height = MaxHeight + addHeight + 50 + "px";
	            floatDiv.style.top = (document.documentElement.clientHeight/2 + document.documentElement.scrollTop - MaxHeight/2) +"px";
        	    
	            if (isScroll == 3) {
	                Scroll = 1;
	            }
	        }
	        else {
	            iframe.height = Height + addHeight;
	            floatDiv.style.height = Height + addHeight + 50 + "px";
	            floatDiv.style.top = (document.documentElement.clientHeight/2 + document.documentElement.scrollTop - Height/2) +"px";
	        }
	        if (Width > MaxWidth) {
	            iframe.width = MaxWidth + addWidth;
	            floatDiv.style.width = MaxWidth + addWidth + 15 + "px";
	            floatDiv.style.left = (document.documentElement.clientWidth/2 + document.documentElement.scrollLeft - MaxWidth/2) +"px";
        	    
        	    
	            if (isScroll == 3) {
	                Scroll = 1;
	            }
        	    
	        }
	        else {
	            iframe.width = Width + addWidth;
	            floatDiv.style.width = Width + addWidth + 15 + "px";
	            floatDiv.style.left = (document.documentElement.clientWidth/2 + document.documentElement.scrollLeft - Width/2) +"px";
	        }
	    }
	}
	else {
	    var bHeight = document.body.scrollHeight;
	    var dHeight = document.documentElement.scrollHeight;
	    var Height = Math.max(bHeight, dHeight);
    	
	    var bWidth = document.body.scrollWidth;
	    var dWidth = document.documentElement.scrollWidth;
	    var Width = Math.max(bWidth, dWidth);
	
        iframe.width = "100%";
        iframe.height = "100%";
        floatDiv.style.width = Width - 100 + "px";
        floatDiv.style.height = Height - 100 + "px";
		floatDiv.style.left = "50px";
		floatDiv.style.top = "20px";
	}
	
	//设置滚动条状态
	if (Scroll != 1) {
	    iframe.contentWindow.document.body.style.overflow = "hidden";
	}
	
    //增加快捷键 按ESC关闭窗口
    iframe.contentWindow.document.body.onkeyup = function() {
        if (iframe.contentWindow.event.keyCode == '27') {
            CloseFloatDiv();
        }
    }
}
function moveDiv(event, elem)
{
	var oObj = $(elem);	
	oObj.onmousemove = mousemove;
	oObj.onmouseup = mouseup;
	oObj.setCapture ? oObj.setCapture() : function(){};
	oEvent = window.event ? window.event : event;
	var dragData = {x : oEvent.clientX, y : oEvent.clientY};
	var backData = {x : parseInt(oObj.style.top), y : parseInt(oObj.style.left)};
	function mousemove()
	{
		var oEvent = window.event ? window.event : event;
		var iLeft = oEvent.clientX - dragData["x"] + parseInt(oObj.style.left);
		var iTop = oEvent.clientY - dragData["y"] + parseInt(oObj.style.top);
		oObj.style.left = iLeft;
		oObj.style.top = iTop;
		dragData = {x: oEvent.clientX, y: oEvent.clientY};
	}
	function mouseup()
	{
		var oEvent = window.event ? window.event : event;
		oObj.onmousemove = null;
		oObj.onmouseup = null;
		oObj.style.filter = "";
		if(oEvent.clientX < 1 || oEvent.clientY < 1)
		{
			oObj.style.left = backData.y;
			oObj.style.top = backData.x;
		}
			oObj.releaseCapture ? oObj.releaseCapture() : function(){};
	}
}
//回调刷新
function ConfirmReload(eventTarget, eventArgument) {
    if (document.forms.length < 1) {
        location.reload();
    }
    else {
        var fromname = document.forms[0].id;
        var theForm = $(fromname);
        
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
}
//关闭
function CloseFloatDiv(){
    var oObj = $('floatDiv_Confirm');
    var bObj = $('backDiv');
    document.body.removeChild(oObj)
	if(bObj){
		bObj.style.display = "none";
	}
}
