var Document = {


/**
 * 获得对象
 * elementID:对象的id
 * objWindow:window对象
 * 姜敏
 */
  getObject: function (elementID, objWindow) {
    var obj;
    if (objWindow == null) {
      obj = window.document.getElementById(elementID);
    }
    else {
      obj = objWindow.document.getElementById(elementID);
    }
    return obj;
  },

/**
 * 获得对象数组
 * elementName:对象数组的名字
 * objWindow:window对象
 * 姜敏
 */
  getObjects: function (elementName, objWindow) {
    var objs;
    if (objWindow == null) {
      objs = window.document.getElementsByName(elementName);
    }
    else {
      objs = objWindow.document.getElementsByName(elementName);
    }
    return objs;
  },

/**
 * 文件下载
 * url 下载动作
 * updateElement 下载错误时显示错误信息的对象id
 */

  downLoad: function(url, progressTitle, updateElement) {
    var count = 0;
    if (updateElement == null) updateElement = "bodyContent";
    if(progressTitle!=null){
      Window.createProgress(progressTitle);
    }
    var interval;
    var iframeName = "downIFrame".concat(StringUtil.randomChar(10));
    var downIFrame = window.document.createElement(("<IFRAME src='about:blank' name='").concat(iframeName).concat("'></IFRAME>"));
    downIFrame.style.display = "none";
    window.document.body.appendChild(downIFrame);
    window.frames[iframeName].location.href = url;
    var updateFunction = function () {
      var readyStatus = window.frames[iframeName].document.readyState;
      if (readyStatus == "complete") {
        window.clearInterval(interval);
        if(progressTitle!=null){
          Window.destroyProgress();
        }
        var html = window.frames[iframeName].document.body.innerHTML;
        var head = window.frames[iframeName].document.getElementsByTagName("HEAD")[0];
        Document.includeScript(head);
        var upElement = Document.getObject(updateElement);
        if (upElement == null) {
          document.body.innerHTML = html;
        }
        else {
          upElement.innerHTML = html;
        }
        window.document.body.removeChild(downIFrame);
        window.setTimeout(function() {
          html.evalScripts()
        }, 10);
        Document.refreshStatus();
      }
      else if (readyStatus == "interactive") {
        count += 1;
        if (count > 20) {
          window.clearInterval(interval);
          if(progressTitle!=null){
            window.setTimeout(function() {
              Window.destroyProgress()
            }, 10);
          }
          window.document.body.removeChild(downIFrame);
          Document.refreshStatus();
        }
      }
    };
    interval = window.setInterval(updateFunction, 50);
  },

/**
 * 通过Ajax提交表单数据
 * formName:表单名称
 * progressTitle:进度条显示文字
 * updateElement:请求完成后ajax局部更新的对象id
 * 姜敏
 */
  submitByAjax: function(formName, progressTitle, updateElement) {
    var objForm = Document.getObject(formName);
    //    var objEvent = window.event.srcElement;
    //    objEvent.disabled = true;
    if (objForm.enctype.toLowerCase() == "multipart/form-data") {
      if (updateElement == null) updateElement = "bodyContent";
      if(progressTitle!=null)  {
        Window.createProgress(progressTitle);
      }
      var interval;
      var iframeName = "uploadIFrame".concat(StringUtil.randomChar(10));
      var uploadIFrame = window.document.createElement(("<IFRAME src='about:blank' name='").concat(iframeName).concat("'></IFRAME>"));
      uploadIFrame.style.display = "none";
      objForm.parentElement.appendChild(uploadIFrame);
      var oldTarget = objForm.target;
      objForm.target = iframeName;
      objForm.submit();
      objForm.target = oldTarget;
      var updateFunction = function () {
        if (window.frames[iframeName].document.readyState == "complete") {
          window.clearInterval(interval);
          if(progressTitle!=null)  {
            Window.destroyProgress();
          }
          var html = window.frames[iframeName].document.body.innerHTML;
          var head = window.frames[iframeName].document.getElementsByTagName("HEAD")[0];
          Document.includeScript(head);
          objForm.parentElement.removeChild(uploadIFrame);
          var upElement = Document.getObject(updateElement);
          if (upElement == null) {
            document.body.innerHTML = html;
          }
          else {
            upElement.innerHTML = html;
          }
          Document.refreshStatus();
          window.setTimeout(function() {
            html.evalScripts()
          }, 10);
        }
      };
      interval = window.setInterval(updateFunction, 50);
    }
    else {
      var param = Form.serialize(formName).concat("&timeSerial=").concat(new Date().getTime());
      url = objForm.action;
      if (updateElement == null) updateElement = "bodyContent";
      var options = {
        method: "post",
        parameters: param,
        asynchronous:true,
        onCreate:Document.showTitle(progressTitle),
        onComplete: function(request) {
          Document.responseComplete(request, updateElement);
        },
        onFailure:function(request) {
          if(progressTitle!=null){
            Window.destroyProgress();
          }
          alert("系统出现错误!");
        }
      };
      var ajax = new Ajax.Request(url, options);
    }
  },



/**
 * 通过Ajax触发链接请求
 * url:连接的jsp页面或action动作
 * progressTitle:进度条显示文字
 * updateElement:请求完成后ajax局部更新的对象id
 * 姜敏
 */
  linkByAjax:function (url, progressTitle, updateElement) {
    var param = ("timeSerial=").concat(new Date().getTime());
    if (updateElement == null) updateElement = "bodyContent";
    var options = {
      method: "get",
      parameters: param,
      asynchronous:true,
      onCreate: Document.showTitle(progressTitle),
      onComplete: function(request) {
        Document.responseComplete(request, updateElement);
      },
      onFailure:function(request) {
        if(progressTitle!=null){
          Window.destroyProgress();
        }
        alert("系统出现错误！");
      }
    };
    var ajax = new Ajax.Request(url, options);
  },

/**
 * 响应完成
 * request:ajax的请求对象
 * updateElement:请求完成后ajax局部更新的对象id
 */
  responseComplete:function(request, updateElement) {
    var html = request.responseText;
    Document.includeScript(html);
    var upElement = Document.getObject(updateElement);
    window.setTimeout(
        function () {
          Window.destroyProgress();
          if (upElement == null) {
            document.body.innerHTML = html.stripScripts();
          }
          else {
            upElement.innerHTML = html.stripScripts();
          }
          window.setTimeout(
              function() {
                html.evalScripts()
              }, 10);

        }, 10);
    Document.refreshStatus();
  },

/**
 * 执行一个ajax请求
 * url 请求动作
 * parameterObject 对数对象 如: {"name":"张三","sex":"男"}
 * disposeFunction ajax调用完成后的处理函数
 * progressTitle 进度条文本
 */
  executeAjaxRequest:function (url, parameterObject, disposeFunction, progressTitle) {
    var hash = $H(parameterObject);
    var param = hash.toQueryString();
    if (StringUtil.isEmpty(param)) {
      param = ("timeSerial=").concat(new Date().getTime());
    }
    else {
      param = param.concat("&timeSerial=").concat(new Date().getTime());
    }
    var options = {
      method: 'get',
      parameters: param,
      asynchronous:true,
      onCreate: Document.showTitle(progressTitle),
      onComplete: function(request) {
        //              window.setTimeout(
        //                function(){
        if(progressTitle!=null){
          Window.destroyProgress();
        }
        disposeFunction(request);
        //                },10)
      },
      onFailure:function(request) {
        if(progressTitle!=null){
          Window.destroyProgress();
        }
        alert("系统出现错误！");
      }
    }
    var ajax = new Ajax.Request(url, options);
  },
/**
 * 链接到新开窗口 普通
 * url:请求动作
 * transitionContent:过渡信息
 */
  linkNewWindow:function(url, transitionContent) {
    var oWin;
    oWin = Window.openNoBarWindow("about:blank", 800, 600, true);
    if (transitionContent == null) {
      oWin.location.href = url;
    }
    else{
      Window.writeTransition(oWin, transitionContent);
      window.setTimeout(
          function() {
            oWin.location.href = url;
          },
          400
      );
    }
    return oWin;
  },

/**
 * 提交表单到新开窗口 普通
 * formName:表单名字
 * url:请求动作
 * transitionContent:过渡信息
 */
  submitNewWindow:function(formName, url, transitionContent) {
    var oForm = Document.getObject(formName);
    var oldUrl = oForm.action ;
    var oldTarget = oForm.target ;
    if (url != null) {
      oForm.action = url;
    }
    var oWin = Window.openNoBarWindow("about:blank", 800, 600, true);
    if (transitionContent == null) {
      oForm.target = oWin.name;
      oForm.submit();
      oForm.action = oldUrl;
      oForm.target = oldTarget;
    }
    else{
      Window.writeTransition(oWin, transitionContent);
      window.setTimeout(
          function() {
            oForm.target = oWin.name;
            oForm.submit();
            oForm.action = oldUrl;
            oForm.target = oldTarget;
          },
          400
      );
    }
  },



/**
 * 设置链接,当前页面 普通
 * url:连接的jsp页面或action动作
 * progressTitle:进度条文字
 * 姜敏
 */
  link:function (url, progressTitle, frameID) {
    if (progressTitle == null) {
      if(frameID==null){
        window.location.href = url;
      }
      else{
        window.frames[frameID].location = url
      }
    }
    else{
     // window.top.Window.createProgress(progressTitle);
      if(frameID==null){
        window.location.href = url;
        Window.listenWindowState(window);
      }
      else{
        window.frames[frameID].location = url
       // window.top.Window.listenWindowState(window.frames[frameID]);
     }
    }
  },

/**
 * 提交当前页面的表单
 * formName:表单名称
 * progressTitle:进度条文字
 */
  submit:function (formName, progressTitle, frameID) {
    var oForm = Document.getObject(formName);
    if (progressTitle == null) {
      if(frameID==null){
        oForm.submit();
      }
      else{
        oForm.target = iframeID;
        oForm.submit();
      }
    }
    else{
      Window.createProgress(progressTitle);
      if(frameID==null){
        oForm.submit();
        Window.listenWindowState(window);
      }
      else{
        oForm.target = iframeID;
        oForm.submit();
        Window.listenWindowState(window.frames[iframeID]);
      }
    }
  },

/**
 * 将 request.responseText 中包含的<script src='xxx.js'></script> 加载到页面
 * 公用的 script 和 样式表不重复加载
 * object ajax返回的responseText字符 或 head对象
 */
  includeScript: function(object) {
    var pubScript = "js/common/string.js,"
        + "js/common/number.js,"
        + "js/common/date.js,"
        + "js/common/window.js,"
        + "js/common/document.js,"
        + "js/common/page.js,"
        + "js/common/check.js,"
        + "js/common/menu.js,"
        + "js/common/prototype.js";
    var head = document.getElementsByTagName("HEAD")[0];
    var childrens = head.children;
    for (var i = 0; i < childrens.length; i++) {
      var element = childrens[i];
      if (element.tagName == 'SCRIPT') {
        if (pubScript.indexOf(element.src) == -1) {
          head.removeChild(element);
        }
      }
    }
    if (object.tagName != null && object.tagName == 'HEAD') {
      var childNodes = object.children;
      for (var i = 0; i < childNodes.length; i++) {
        var element = childrens[i];
        if (element.tagName == 'SCRIPT') {
          if (pubScript.indexOf(element.src) == -1) {
            var script = document.createElement("SCRIPT");
            script.src = element.src;
            head.appendChild(script);
          }
        }
      }
    }
    else {
      var scripts = object.match(/<script([^>]*>)((.|\r|\n)*?)<\/script>/ig);
      if (scripts != null) {
        for (var i = 0; i < scripts.length; i++) {
          var src = scripts[i].match(/src\s*=\s*(['"]?)([^'">\s]*)\1/i);
          if (src != null) {
            if (pubScript.indexOf(src[2]) == -1) {
              var script = document.createElement("SCRIPT");
              script.src = src[2];
              head.appendChild(script);
            }
          }
        }
      }
    }
  },

/**
 * 触发动作的对象必须是inupt,button等
 * 清除表单上的非隐藏域输入框的值
 * formName: 表单名
 * clearHidden: 是否清空隐藏域 true or false
 * 姜敏
 */
  clearForm:function (formName, clearHidden) {
    var objForm = Document.getObject(formName);
    for (var i = 0; i < objForm.elements.length; i++) {
      var object = objForm.elements[i]
      if (object.tagName == "INPUT") {
        if (object.type == "file" || object.type == "password" || object.type == "text") {
          object.value = "";
        }
        /*if(object.type=="checkbox" || object.type=="radio"){
          object.checked=false;
        }*/
        if (object.type == "hidden") {
          if (clearHidden != null && clearHidden == true) {
            object.value = "";
          }
        }
      }
      if (object.tagName == "SELECT") {
        object.value = "";
      }
      if (object.tagName == "TEXTAREA") {
        object.value = "";
      }
    }
  },


/**
 * 显示和隐藏对象
 * IDName:对象ID
 * status:根据状态来显示和隐藏(可选) true or false
 * 姜敏
 */
  showHiddenObject: function (IDName, status) {
    try {
      var obj = Document.getObject(IDName);
      if (status != null) {
        obj.style.display = (status == true)?"block":"none";
      }
      else {
        obj.style.display = (obj.style.display == "none")?"block": "none";
      }
    }
    catch(e) {
    }
  },

/**
 * 复选框的状态选择
 * IDName:checkbox框id或name
 * status:选中状态 true or false
 */
  statusChecked:function (IDName, status) {
    var objCheckBoxs = Document.getObjects(IDName);
    if (objCheckBoxs != null) {
      for (var i = 0; i < objCheckBoxs.length; i++) {
        objCheckBoxs[i].checked = (status != null)?status:(!objCheckBoxs[i].checked);
      }
    }
  },

/**
 * 分页取码窗口的复选框状态选中函数()
 * parentHiddenName:父窗口中的隐藏域名称(数组)
 * selfCheckBoxName:当前窗口中的复选框名(数组)
 */
  statusCheckedById:function (parentHiddenName, selfCheckBoxName) {
    var objHiddens = window.parent.document.getElementsByName(parentHiddenName);
    var objCheckBoxs = window.document.getElementsByName(selfCheckBoxName);
    if (objHiddens == null) return;
    if (objCheckBoxs == null) return;
    var checkBoxValue;
    var hiddenValue;
    for (var i = 0; i < objCheckBoxs.length; i++) {
      checkBoxValue = objCheckBoxs[i].value;
      for (var j = 0; j < objHiddens.length; j++) {
        hiddenValue = objHiddens[j].value;
        if (checkBoxValue == hiddenValue) {
          objCheckBoxs[i].checked = true;
          break;
        }
      }
    }
  },

/**
 * 分页取码窗口的复选框状态选中函数(单个输入框中值以逗号分割)
 * parentInputName:父窗口中的输入框名称
 * selfCheckBoxName:当前窗口中的复选框名(数组)
 */
  statusCheckedByName:function (parentInputName, selfCheckBoxName) {
    var objInput = window.parent.document.getElementById(parentInputName);
    var objCheckBoxs = window.document.getElementsByName(selfCheckBoxName);
    if (objInput == null) return;
    if (objCheckBoxs == null) return;
    var inputValue = objInput.value;
    var checkBoxValue;
    for (var i = 0; i < objCheckBoxs.length; i++) {
      checkBoxValue = objCheckBoxs[i].value;
      if (inputValue.indexOf(checkBoxValue) != -1) {
        objCheckBoxs[i].checked = true;
      }
    }
  },

  /**
   * 所有子复选框被选中后，父复选框自动被选中；反之，则未选中
   * cellsBoxName 子复选框名称
   * mainBoxName全选父复选框名称
   * written by Clark Fan 
   */
    superStatusAutoChecked:function(cellsBoxName,mainBoxName){
        var objLen = 0;
            var objs = Document.getObjects(cellsBoxName);
            for(i = 0;i<objs.length;i++){
                if(objs[i].checked){
                    objLen += 1;
                }
            }
            if(objs.length==objLen){
                Document.getObject(mainBoxName).checked = true;
            }else{
                Document.getObject(mainBoxName).checked = false;
            }
    },

    /**
     * 像radio一样的复选框
     */
    statusCheckedAsRadio:function(object){
      var name = object.name;
      var objCheckBoxs = Document.getObjects(name);
       if(objCheckBoxs!=null){
         for(var i=0;i<objCheckBoxs.length;i++){
          if(objCheckBoxs[i]!=object){
           objCheckBoxs[i].checked=false;
          }
         }
      }
    },

/**
 * 刷新状态栏的进度
 */
  refreshStatus:function() {
    var objIFrame = window.document.createElement("IFRAME");
    objIFrame.style.display = "none";
    //    objIFrame.style.position="absolute";
    objIFrame.id = "refreshWindowStatus";
    objIFrame.name = "refreshWindowStatus";
    window.document.body.appendChild(objIFrame);
    var iframeDoc = window.frames["refreshWindowStatus"].document.open("text/html", "replace");
    with (iframeDoc) {
      writeln("<html>");
      writeln("<body>");
      writeln("<script>");
      writeln("window.top.status='完成';");
      // writeln("alert(window.top.status)");
      writeln("<\/script>");
      writeln("<\/body>");
      writeln("<\/html>");
    }
    iframeDoc.close();
    window.document.body.removeChild(objIFrame);
  }  ,
/**
 * 回车代替tab
 * 姜敏
 */
  handleKey:function () {
    var gk = window.event.keyCode;
    if (gk == 13) {
      if (window.event.srcElement.tagName != 'TEXTAREA') {
        window.event.keyCode = 9;
        return;
      }
    }
  },

  showTitle:function(progressTitle){
    if(progressTitle!=null){
      Window.createProgress(progressTitle);
    }
  }
} 
