﻿
function $(id)
{
    return document.getElementById(id);
}

//打开一个弹出窗口页
function ShowPopupWindow(url, width, height)
{
    var w = width;
    var h = height;
    
    window.open(url, "popupWindow", "height=,"+h+"width="+w+",toolbar=no,resizable=no,scrollbars=auto,location=no,status=yes");
}
function SAPWOnlyFilterQS(url)
{
    if(url.indexOf('?')>0)
    {
        var lastString = url.substr(url.indexOf('?')+1,url.length - url.indexOf('?')+1);
        var oldString = lastString;
        do{
            lastString = lastString.replace('+','%2b');
        }
        while(lastString.indexOf('+')>=0);
        do{
            lastString = lastString.replace('/','%2f');
        }while(lastString.indexOf('/')>=0);
        url = url.replace(oldString,lastString);
    }
    var popupWindow = window.open(url, "", "toolbar=no,resizable=no,location=no,status=yes");
    popupWindow.isPopup = true;
}
//打开一个弹出窗口页
function ShowAutoPopupWindow(url)
{ 
    do{
        url = url.replace('+','%2b');
    }
    while(url.indexOf('+')>=0);
    do{
        url = url.replace('/','%2f');
    }while(url.indexOf('/')>=0);
    var popupWindow = window.open(url, "", "toolbar=no,resizable=no,location=no,status=yes");
    popupWindow.isPopup = true;
}

function AutoFitPopupWindow()
{
    //获取窗口大小
    document.body.style.setAttribute("overflow", "hidden");
    var width = document.body.parentElement.offsetWidth;
    var height = document.body.parentElement.offsetHeight;
    //获取窗口位置
    var oLeft = window.screenLeft;
    var oTop = window.screenTop;
    var oWidth = width;
    var oHeight = height; 
    
    var tables = document.getElementsByTagName("table");
    var panelTable = null;
    if (tables)
    {
        panelTable = tables[0];
        width = panelTable.offsetWidth;
        height = panelTable.offsetHeight;
    }
    
    if (width > screen.availWidth - 50)
    {
        //document.body.setAttribute("scroll", "auto");
        document.body.style.setAttribute("overflow", "auto");
        document.body.style.setAttribute("overflow-x", "auto");
        width = screen.availWidth - 50;
        height = parseInt(height) + 35; // add scroll bar height
    }
    if (height > screen.availHeight - 100)
    {
        //document.body.setAttribute("scroll", "auto");
        document.body.style.setAttribute("overflow", "auto");
        document.body.style.setAttribute("overflow-y", "auto");
        height = screen.availHeight - 100; 
        width = parseInt(width) + 35; //add scroll bar width
    }
    height = parseInt(height) + 10;//add statusbar height
    if (navigator.userAgent.indexOf("MSIE 6.0") != -1)
    {
        if (navigator.appMinorVersion.indexOf("SP1") !=  -1)
        {
            height += 50;
        }
    }
    width = parseInt(width);// + 20;
//    if (height < 200)
//    {
//        height = 200;
//    }
    document.body.style.setAttribute("width", width);    
    document.body.style.setAttribute("height", height); 
    var left = (screen.availWidth - width) / 2;
    var top = (screen.availHeight - height) / 2;   
    
    FitWindow(left-oLeft, top-oTop, width-oWidth+5, height-oHeight);
   
}

function FitWindow(mLeft, mTop, sWith, sHeight)
{
    //var times = 50;
    var times = 5;//SizeTimes(Math.abs(mLeft), Math.abs(mTop), Math.abs(sWith), Math.abs(sHeight));
    
    var leftStep = 0;
    if (mLeft!=0)
    {
        leftStep = (mLeft ) / times;
        //leftStep = (leftStep == 0) ? left - oLeft : leftStep;
    }
    var topStep = 0;
    if (mTop!=0)
    {
        topStep = (mTop) / times;
        //topStep = (topStep == 0) ? top - oTop : topStep;
    }
    var widthStep = 0;
    if (sWith!=0)
    {
        widthStep = (sWith) / times;
        //widthStep = (widthStep == 0) ? width - oWidth : widthStep;
    }
    var heightStep = 0;
    if (sHeight!=0)
    {
        heightStep = (sHeight) / times;
        //heightStep = (heightStep == 0) ? height - oHeight : heightStep;
    }
    
    if(Math.abs(leftStep)<1)
        leftStep =0;
    if(Math.abs(topStep)<1)
        topStep =0;
    if(Math.abs(widthStep)<1)
        widthStep =0;
    if(Math.abs(heightStep)<1)
        heightStep =0;
    FitWindowInterval(times,widthStep,heightStep,leftStep,topStep);
//    oLeft += leftStep;
//    oTop += topStep;
//    oWidth += widthStep;
//    oHeight += heightStep;
    
//    window.resizeBy(widthStep, heightStep);
//    window.moveBy(leftStep, topStep);
//    if (leftStep !=0 || topStep != 0 || widthStep !=0 || heightStep != 0)
//    {
//        setTimeout(function(){FitWindow.call(this, oLeft, oTop, oWidth, oHeight, left, top, width, height);}, 10);
//    }
}

//在条件允许的情况下，尽可能的让次数多
function SizeTimes(mLeft, mTop, sWith, sHeight){
    var times = 1;
    if(mLeft==0&&mTop==0&&sWith==0&& sHeight==0)
        return times;       
    var minValue = 9999;
    var maxValue = 1;
    //获取最小值
    if(mLeft!=0 && mLeft<minValue)
        minValue=mLeft;
    if(mTop!=0 && mTop<minValue)
        minValue=mTop;
    if(sWith!=0 && sWith<minValue)
        minValue=sWith;
    if(sHeight!=0 && sHeight<minValue)
        minValue=sHeight;    
    //获取最大值    
    if(mTop>maxValue)  
        maxValue=mTop;     
    if(sWith>maxValue)  
        maxValue=sWith;     
    if(sHeight>maxValue)  
        maxValue=sHeight;     
        
    if(minValue==0)
        return times;
        
    while(minValue/times>1)
        times++;
    return times;
}

function FitWindowInterval(times,widthStep,heightStep,leftStep,topStep)
{
    times--;
    if(times>=0){
        window.resizeBy(widthStep, heightStep);
        window.moveBy(leftStep, topStep);
        setTimeout(function(){FitWindowInterval.call(this, times,widthStep,heightStep,leftStep,topStep);}, 20);
    }
    else
    {
    }
}

//全部选择或取消Table的一列的复选框
function changeSelect(obj, col)
{
   var cellIndex;
   var rowIndex;
   var container = obj;
   while  (container.tagName != "TABLE")
   {
      container = container.parentNode;
      if (container.tagName == "TH" || container.tagName == "TD")
      {
	        cellIndex = container.cellIndex;
      }
      else if (container.tagName == "TR")
      {
	        rowIndex = container.rowIndex;
      }

   }
   col = Number(col);
   if (col && col >= 0 && col < container.rows[0].cells.length)
   {
        cellIndex = col;
   }
   
   for (var i = 0; i < container.rows.length; i++)
   {
      var row = container.rows[i];
      if (row.rowIndex != rowIndex)
      {
	    var cb = row.cells[cellIndex].childNodes[0];
	    if (cb.type == "checkbox")
	    {
	       cb.checked = obj.checked;
	    }
      }      
   }
}

//展开/折叠容器
function toggleContainer(containerID, statusIconID, flag)
{
    var container = $(containerID);
    var statusIcon = $(statusIconID);
    var path = statusIcon.src;
    path = path.substring(0, path.lastIndexOf('/'));
    var pageContainer = new PageContainer(container);
    if (flag)
    {
        pageContainer.flag = flag;
    }
    pageContainer.statusIconObj = statusIcon;
    pageContainer.expandedIcon = path + "/minus_ie.gif";
    pageContainer.collapsedIcon = path + "/plus_ie.gif";
    
    if ("none" == container.style.display)
    {
        pageContainer.expand();
    }
    else
    {
        pageContainer.collapse();
    }
}

//页面容器类
PageContainer = function(container)
{
    this.obj = container;
    this.flag = 2; 
    this.statusIconObj = null;
    this.expandedIcon = null;
    this.collapsedIcon = null;
    this.symbol = window.Globle_pageContainerSymbol;
    this.initialize();
}

PageContainer.prototype = {
    
    initialize : function()
    {
        if (this.symbol)
            clearTimeout(this.symbol);
        this._conHeight = parseInt(this.obj.scrollHeight);
        this.obj.style.height = this._conHeight;
        this._conOverflow = this.obj.style.overflow;
        this.obj.style.overflow = "hidden";
        this._conWidth = parseInt(this.obj.scrollWidth);
        this.obj.style.width = this._conWidth;
        this._op = null;
    },

    _pullUp : function()
    {
        var h = parseInt(this.obj.style.height);
        var pContainer = this;
        if (h != 0)
        {
            h = h - 10;
            if (h < 0)
            {
                h = 0;
            }
            this.obj.style.height = h;
            window.Globle_pageContainerSymbol = setTimeout(function(){pContainer._pullUp.call(pContainer);}, 10);
        }
        else
        {
            this._completedToggle();
        }
    },
    
    _pullDown : function()
    {
        var h = parseInt(this.obj.style.height);
        var pContainer = this;
        if (h != this._conHeight)
        {
            h = parseInt(h) + 10;
            if (h > this._conHeight)
            {
                h = this._conHeight;
            }
            this.obj.style.height = h;
            window.Globle_pageContainerSymbol = setTimeout(function(){pContainer._pullDown.call(pContainer);}, 10);
        }
        else
        {
            this._completedToggle();
        }
    },
    
    _completedToggle : function()
    {
        this.obj.style.width = null;
        this.obj.style.height = null;
        this.obj.style.overflow = this._conOverflow;
        if ("hide" == this._op)
        {
            this.obj.style.width = this._conWidth;
            this.obj.style.height = this._conHeight;
            if (this.statusIconObj)
            {
                this.statusIconObj.src = this.collapsedIcon;
            }
            this.obj.style.display = "none";
        }
        else
        {
            this.obj.style.width = this._conWidth;
            this.obj.style.height = this._conHeight;
            if (this.statusIconObj)
            {
                this.statusIconObj.src = this.expandedIcon;
            }
            this.obj.style.display = "block";
        }
    },

    expand : function()
    {
        this._op = "show";
        switch (this.flag)
        {
            case 1:
                this._completedToggle();
                break;
            case 2:
                this.obj.style.height = 0;
                this.obj.style.display = "block";
                this._pullDown();
                break;
        }      
    },
    
    collapse : function()
    {
        this._op = "hide";
        switch (this.flag)
        {
            case 1:
                this._completedToggle();
                break;
            case 2:
                this._pullUp();
                break;
        }
    }
}

//当一次提交未完成时禁止重复提交
function clickOnce(obj)
{
//    if (obj.interectiving == true)
//    {
//        return false;
//    }
//    else
//    {
//        obj.style.color = "#cccccc";
//        obj.interectiving = true;
//    }
}

//移动选择框内容到另一选择框
function moveSelect(srcSelect, desSelect, flag)
{
    var srcOptions = srcSelect.options;
    for (var i = 0; i < srcOptions.length; i++)
    {
        if (flag || srcOptions[i].selected)
        {
            desSelect.options.appendChild(srcOptions[i]);
            i --;
        }
    }
}

//返回字符串的字节数
 function getlengthB(str){
      //return str.replace(/[^\x00-\xff]/g,"**").length;
      return str.length;
      }

//禁止输入字符超过multiple input的maxLen     (对于回车键还不能正确处理)
function checkInputLength(input)
{
    if (input.maxLen)
    {     
    
        if (event.type == "paste")
        {
            var value = clipboardData.getData("Text");
            input.value = input.value + value;
            if (input.value.length > input.maxLen)
            {
                input.value = input.value.substring(0, input.maxLen);
            }
            event.cancelBubble = true;
            event.returnValue = false;
            return false;
        }
        if (event.keyCode < 48 && event.keyCode != 13)
        {
            return true;
        }
        if (input.value.length >= input.maxLen)
        {
            event.cancelBubble = true;
            event.returnValue = false;
            return false;
        }
    }
}
//得到某obj的x,y坐标,兼容大部分的浏览器 Created By XiongJun
function getWinElementPos(obj)
{

 var ua = navigator.userAgent.toLowerCase();
 var isOpera = (ua.indexOf('opera') != -1);
 var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof

 var el = obj;

 if(el.parentNode === null || el.style.display == 'none') 
 {
  return false;
 }

 var parent = null;
 var pos = [];
 var box;

 if(el.getBoundingClientRect) //IE
 {
  box = el.getBoundingClientRect();
  var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);

  return {x:box.left + scrollLeft, y:box.top + scrollTop};
 }
 else if(document.getBoxObjectFor) // gecko
 {
  box = document.getBoxObjectFor(el);
     
  var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
  var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;

  pos = [box.x - borderLeft, box.y - borderTop];
 }
 else // safari & opera
 {
  pos = [el.offsetLeft, el.offsetTop];
  parent = el.offsetParent;
  if (parent != el) {
   while (parent) {
    pos[0] += parent.offsetLeft;
    pos[1] += parent.offsetTop;
    parent = parent.offsetParent;
   }
  }
  if (ua.indexOf('opera') != -1 
   || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) 
  {
    pos[0] -= document.body.offsetLeft;
    pos[1] -= document.body.offsetTop;
  } 
 }
  
 if (el.parentNode) { parent = el.parentNode; }
 else { parent = null; }
  
 while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') 
 { // account for any scrolled ancestors
  pos[0] -= parent.scrollLeft;
  pos[1] -= parent.scrollTop;
  
  if (parent.parentNode) { parent = parent.parentNode; } 
  else { parent = null; }
 }
 return {x:pos[0], y:pos[1]};
}
 function DragDrop(obj,target){ 
        var me = this; 
        this.element = (typeof(obj)=="string")?document.getElementById(obj):obj; 
        this.target = (typeof(obj)=="string")?document.getElementById(target):target; 
        this.element.onmousedown = function(e){ 
              var ele = me.target; 
              e = e||event; 
              if( e.layerX ){ 
                ele.oOffset = {x:e.layerX,  y:e.layerY }; 
                    } 
                    else{ 
                        ele.oOffset = {x:e.offsetX, y:e.offsetY };  
                        } 
              document.onmousemove = me.drag; 
              document.onmouseup   = me.drop; 
              document.onselectstart = function(){ return false; };          
        } 
         
        this.drag = function(e){ 
              var ele = me.target; 
              e=e||event; 
              ele.style.top  = e.clientY - ele.oOffset.y + "px"; 
              ele.style.left = e.clientX - ele.oOffset.x + "px"; 
        } 
         
        this.drop = function(e){ 
              e=e||event; 
              document.onmousemove = document.onmouseup = document.onselectstart = null;     
        }     
    } 