﻿// JScript 文件
var fdDivId="div";//要拖动的层(弹出的层)的ID
var wwidth;//弹出层宽
var wheight;//弹出层高
//弹出层
//obj:显示内容的层ID
function openLayer(obj)
{
    fdDivId=obj;//记录显示内容的层ID

    var popupDiv = document.getElementById(obj);
    popupDiv.style.display = 'block';
    
    wwidth=popupDiv.clientWidth;//弹出层宽
    wheight=popupDiv.clientHeight;//弹出层高
    
    //alert(wheight+'    '+document.documentElement.clientHeight);
    
    //让弹出层在页面中垂直左右居中
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    popupDiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - wheight) / 2) + 'px') ;
    popupDiv.style.left = (((arrayPageSize[0] - 20 - wwidth) / 2) + 'px');
    //alert(popupDiv.style.top);
    popupDiv.style.top=(popupDiv.style.top<'0px')?'0px': popupDiv.style.top;
    popupDiv.style.top=(wheight > document.documentElement.clientHeight)? document.documentElement.scrollTop: popupDiv.style.top;//弹出层高大于网页可见区域高置顶

    //创建背景层
    var bodyBack
   /* if(document.getElementById("bodybg")!=null)
    {
    bodyBack=null;
    alert(bodyBack);
    }*/
    
    
    bodyBack = document.createElement("div");
    bodyBack.setAttribute("id","bodybg")
    
    bodyBack.style.width = "100%";
    //bodyBack.style.width = document.documentElement.scrollWidth;//网页正文宽(窗口不是最大化时点出后再最大化效果不好)
    bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
    bodyBack.style.zIndex = 50;
    bodyBack.style.position = "absolute";
    bodyBack.style.top = 0;
    bodyBack.style.left = 0;
    bodyBack.style.filter = "alpha(opacity=20)";
    bodyBack.style.opacity = 0.2;
    bodyBack.style.background = "#ffffff";
//    bodyBack.onclick=closeLayer;//点层时关闭弹出层和中间层(可选)
    

    //收工插入到目标元素之后
    insertAfter(bodyBack,popupDiv);//将背景层加到要弹出的层后
    
    ShowSelects(0);//后加,隐藏下拉列表
    
}


/*
function openLayer(obj)
{
    fdDivId=obj;//记录显示内容的层ID

    var popupDiv = document.getElementById(obj);
    popupDiv.style.display = 'block';
    
    wwidth=popupDiv.clientWidth;//弹出层宽
    wheight=popupDiv.clientHeight;//弹出层高
    
    //alert(wheight+'    '+document.documentElement.clientHeight);
    
    //让弹出层在页面中垂直左右居中
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    popupDiv.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - wheight) / 2) + 'px') ;
    popupDiv.style.left = (((arrayPageSize[0] - 20 - wwidth) / 2) + 'px');
    //alert(popupDiv.style.top);
    popupDiv.style.top=(popupDiv.style.top<'0px')?'0px': popupDiv.style.top;
    popupDiv.style.top=(wheight > document.documentElement.clientHeight)? document.documentElement.scrollTop: popupDiv.style.top;//弹出层高大于网页可见区域高置顶

    //创建背景层
    var bodyBack = document.createElement("div");
    bodyBack.setAttribute("id","bodybg")
    bodyBack.style.width = "100%";
    //bodyBack.style.width = document.documentElement.scrollWidth;//网页正文宽(窗口不是最大化时点出后再最大化效果不好)
    bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
    bodyBack.style.zIndex = 50;
    bodyBack.style.position = "absolute";
    bodyBack.style.top = 0;
    bodyBack.style.left = 0;
    bodyBack.style.filter = "alpha(opacity=20)";
    bodyBack.style.opacity = 0.2;
    bodyBack.style.background = "#000000";
//    bodyBack.onclick=closeLayer;//点层时关闭弹出层和中间层(可选)

    //收工插入到目标元素之后
    insertAfter(bodyBack,popupDiv);//将背景层加到要弹出的层后
    
    ShowSelects(0);//后加,隐藏下拉列表
    
}
*/




//元素插入另一个元素之后
function insertAfter(newElement, targetElement) 
{ 
    var parent = targetElement.parentNode; 
    if(parent.lastChild == targetElement) 
    { 
        parent.appendChild(newElement); 
    } 
    else 
    { 
        parent.insertBefore(newElement, targetElement.nextSibling); 
    } 
}

//获取滚动条的高度
function getPageScroll()
{
    var yScroll;
    if (self.pageYOffset) 
    {
       yScroll = self.pageYOffset;
    } 
    else if (document.documentElement && document.documentElement.scrollTop)
    { // Explorer 6 Strict
       yScroll = document.documentElement.scrollTop;
    } 
    else if (document.body) 
    {// all other Explorers
       yScroll = document.body.scrollTop;
    }
    arrayPageScroll = new Array('',yScroll) 
    return arrayPageScroll;
}

//获取页面实际大小
function getPageSize()
{ 
    
    var xScroll, yScroll; 
    
    if (window.innerHeight && window.scrollMaxY) {    
        xScroll = document.body.scrollWidth; 
        yScroll = window.innerHeight + window.scrollMaxY; 
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 
        xScroll = document.body.scrollWidth; 
        yScroll = document.body.scrollHeight; 
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 
        xScroll = document.body.offsetWidth; 
        yScroll = document.body.offsetHeight; 
    } 
    
    var windowWidth, windowHeight; 
    if (self.innerHeight) {    // all except Explorer 
        windowWidth = self.innerWidth; 
        windowHeight = self.innerHeight; 
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode 
        windowWidth = document.documentElement.clientWidth; 
        windowHeight = document.documentElement.clientHeight; 
    } else if (document.body) { // other Explorers 
        windowWidth = document.body.clientWidth; 
        windowHeight = document.body.clientHeight; 
    }    
    
    // for small pages with total height less then height of the viewport 
    if(yScroll < windowHeight){ 
        pageHeight = windowHeight; 
    } else { 
        pageHeight = yScroll; 
    } 

    // for small pages with total width less then width of the viewport 
    if(xScroll < windowWidth){    
        pageWidth = windowWidth; 
    } else { 
        pageWidth = xScroll; 
    } 

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize; 
}
/*
//关闭弹出层(原来要写入弹出层的ID)
function closeLayer(obj)
{
    obj.style.display = "none";
    document.getElementById("bodybg").style.display = "none";
    ShowSelects(1);//后加,显示下拉列表
    return false;
}*/
//关闭弹出层
function closeLayer()
{
    var obj = document.getElementById(fdDivId);
    obj.style.display = "none";
    //document.getElementById("bodybg").style.display = "none";
    closeDiv(document.getElementById("bodybg"));
    ShowSelects(1);//后加,显示下拉列表
    return false;
}


    function closeDiv(divobj)
   {
    divobj.parentNode.removeChild(divobj);
   }



//拖动函数
function mousedown(e)
{
    var obj = document.getElementById(fdDivId);//要拖动的层
    var e = window.event ? window.event : e;

    obj.startX = e.clientX - obj.offsetLeft;
    obj.startY = e.clientY - obj.offsetTop;


    document.onmousemove = mousemove;
    var temp = document.attachEvent ? document.attachEvent("onmouseup",mouseup) : document.addEventListener("mouseup",mouseup,"");
}

function mousemove(e)
{
    var obj = document.getElementById(fdDivId);//要拖动的层
    var e = window.event ? window.event : e;

    with(obj.style)
    {
       left = (e.clientX - obj.startX)<10 ? 10 : (e.clientX - obj.startX) + "px";
       top = (e.clientY - obj.startY)<10?10:(e.clientY - obj.startY) + "px";
       
    }
}
function mouseup(e)
{
    var e = window.event ? window.event : e;
    var obj = document.getElementById(fdDivId);//要拖动的层
    var currentX=e.clientX - obj.startX;
    var currentY=e.clientY - obj.startY;
    //让弹出层在页面中垂直左右居中
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    var maxX=arrayPageSize[0]-wwidth-24;
    var maxY=arrayPageScroll[1] +((arrayPageSize[3] - wheight));

    if(currentX>maxX)
    {
       obj.style.left=maxX-10 + "px";
    }
    if(currentY>maxY)
    {
        if(maxY<0)maxY=10;
       obj.style.top=maxY-10 + "px";
    }
    document.onmousemove = "";
    var temp = document.detachEvent ? document.detachEvent("onmouseup",mouseup) : document.addEventListener("mouseup",mouseup,"");
    obj.style.top=(wheight > document.documentElement.clientHeight)? document.documentElement.scrollTop: obj.style.top;//弹出层高大于网页可见区域高置顶
}
//END拖动函数

//----------------------------------------
var isIE = /msie/.test(navigator.userAgent.toLowerCase()); 
//或
//var sUserAgent = navigator.userAgent;
//var isOpera = sUserAgent.indexOf("Opera") > -1;
//var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
//隐藏(显示)下拉列表,
//z: 0: 隐藏  1:显示
function ShowSelects(z)
{
	if(!isIE)return;	
	var xs="";
	if(z==0){
		xs="hidden";
	}
	var i=0;
	var frm = document.getElementById('form1');
	for (i=0;i<frm.elements.length;i++){
		if (frm.elements[i].type == "select-one") 
		{
		    if(frm.elements[i].id.indexOf("_common")==-1 )//ID中有 _common 的为弹出层中的下拉列表,显示,其它的下拉列表不显示
		    {
	            frm.elements[i].style.visibility=xs;		        
		    }			
		}
	}
}