window.onload = Client_Initialize;
/** *************************************鼠标动作函数****************************************** */
function Client_Initialize(){
  document.onmouseover = MouseOver;
  document.onmouseout = MouseOut;
  document.onmousedown = MouseDown;
  document.onmouseup = MouseUp;
  if(window.document.forms.length > 0) AddScript("/Public/Code/Validator.Js")
  try{
    initSelectForm()
  }catch(e){}
}

function AddScript(sUrl){
  var ds = document.getElementsByTagName("SCRIPT")
  for(var i=0;i<ds.length;i++){
    if(ds[i].src==sUrl) return;
  }
  var o = document.createElement("SCRIPT");
  o.src = sUrl;
  // document.getElementsByTagName("head")[0].insertAdjacentElement("afterbegin",
  // o)
  document.getElementsByTagName("head")[0].appendChild(o)
  ds = null;
  o = null;
}

function AddStyle(sUrl){
  var ds = document.getElementsByTagName("LINK")
  for(var i=0;i<ds.length;i++){
    if(ds[i].src==sUrl) return;
  }
  var o = document.createElement("LINK");
  o.rel="stylesheet"
  o.href=sUrl
  o.type="text/css"
  document.getElementsByTagName("head")[0].appendChild(o)
  ds = null;
  o = null;
}

function Print(){
  print()
}

function MouseOver(){
  var srcElement,clsName,chk
  srcElement = window.event.toElement;
  clsName = srcElement.className;
  chk = clsName.substr(0,3)
  switch(chk){
    case "btn":
    case "lnk":
    // case "grd":
      if(clsName.charAt(4)!="4"){
        srcElement._oldClass = clsName
        srcElement.className = chk + "02" + clsName.substring(5)
      }
      break
    default:
      return true
  }
  return false
}

function MouseOut(){
  var srcElement,clsName,chk
  srcElement = window.event.fromElement;
  clsName = srcElement.className;
  chk = clsName.substr(0,3)
  switch(chk){
    case "btn":
    case "lnk":
    // case "grd":
      if(clsName.charAt(4)!="4")  srcElement.className = srcElement._oldClass
      break
    default:
      return true
  }
  return false
}

function MouseDown(){
  var srcElement,clsName,chk
  if(window.event.button != 1) return true;
  srcElement = window.event.srcElement;
  clsName = srcElement.className;
  chk = clsName.substr(0,3)
  switch(chk){
    case "btn":
    case "lnk":
      if(clsName.charAt(4)!="4"){
        srcElement._dwnClass = clsName;
        srcElement.className = chk + "03" + clsName.substring(5);
      }
      break
/*
 * //case "grd": if(clsName.charAt(4)!="4"){ clsName = chk + "03" +
 * clsName.substring(5); if(!srcElement._dwnstate){ srcElement._dwnstate = true;
 * srcElement._dwnClass = srcElement._oldClass; srcElement._oldClass = clsName;
 * }else{ srcElement._dwnstate = false; srcElement._oldClass =
 * srcElement._dwnClass; } srcElement.className = clsName; } break
 */    default:
      switch(clsName){
        case "inputtime":
          IsCalendarTime = true;
          GetCalendar(srcElement);
          break;
        case "inputdate":
          IsCalendarTime = false;
          GetCalendar(srcElement);
          break;
        default:
          return true
      }
  }
  return false
}

function MouseUp(){
  var srcElement,clsName,chk,lnk,target
  if(window.event.button != 1) return true;
  srcElement = window.event.srcElement;
  clsName = srcElement._dwnClass;
  if(!clsName) clsName = srcElement.className;
  chk = clsName.substr(0,3)
  switch(chk){
    case "btn":
    case "lnk":
      if(clsName.charAt(4)!="4"){
        srcElement.className = clsName;
        lnk = srcElement.getAttribute("href");
        if (lnk==null) break
        if(lnk.indexOf("(")>0){
          eval(lnk);
        }else{
          target = srcElement.getAttribute("target");
          if(target == null) target = "_self";
          window.open(lnk,target);
        }
      }
      break
    // case "grd":
    default:
      return true
  }
  return false
}
/** ********************************************************************************************** */
function OpenSelectWin(URI){
  loc_x = document.body.scrollLeft+event.clientX-event.offsetX - 50;
  loc_y = document.body.scrollTop+event.clientY-event.offsetY + 20;
// var sUser =
// window.showModalDialog(userURL,this,"edge:raised;scroll:1;status:0;help:0;resizable:1;dialogWidth:460px;dialogHeight:300px;dialogTop:"+loc_y+"px;dialogLeft:"+loc_x+"px");
  var childWin = window.open(URI, "NewWin", "scrollbars=1,width=848,height=500,top=50,left=100");
  childWin.opener = this
  childWin.focus()
}

function UpdateFormSelect(frmId, frmName, sError){
  var theForm = window.document.forms["SelectForm"]
  for(var i = 0;i < theForm.length; i++){
    if(theForm[i].checked){
      var v = theForm[i].value;
      a = v.split(":");
      theForm = window.opener.window.document.forms[0];
      theForm[frmId].value = a[0];
      theForm[frmName].value = a[1];
      window.opener.focus();
      window.opener = null;
      self.close();
      return false;
    }
  }
  alert(sError)
  return false;
}

function CheckUserExist(){
  var theForm,chk
  theForm = window.document.forms["RegForm"]
  chk = theForm["Accounts"].value
  if(chk == ""){
    alert("请输入账号！")
    theForm["Accounts"].focus();
    return false
  }
  window.location.href = theForm.action + "?step=6&Accounts=" + chk + "&Domain=" + theForm["Domain"].value
}

function InitEditForm(){
  var theForm,Parts,Items,Fields,i
  theForm = window.document.forms["PartEditForm"]
  Parts = theForm["PurviewSign"].value
  if(Parts != ""){
    Items = Parts.split("|")
    for(i=0;i<Items.length;i++){
      Fields = Items[i].split("@")
      theForm["PartEdit"].options.add(new Option(Fields[1],Fields[0]))
    }
  }
}

function CloseWindow(){
  self.window.close();
}
function SelectAttend(obj, target){
  var countunit, unitname;
  countunit = obj.options[obj.selectedIndex].value.charAt(0);
  switch(parseInt(countunit)){
    case 1:
      unitname = "天";
      break;
    case 3:
      unitname = "分钟";
      break;
    case 4:
      unitname = "元";
      break;
    case 5:
      unitname = "次";
      break;
    default:
      unitname = "小时"
  }
  document.getElementById(target).innerHTML = unitname;
}
function FormKeyDown(formname,i){
  var theForm
  if(typeof(formname) == "object") theForm = formname;
  else theForm = window.document.forms[formname];
  if(window.event.keyCode != 13) return true;
  if(window.event.srcElement.tagName != "INPUT") return true;
  SubmitForm(theForm,i)
  window.event.returnValue = false;
}

function FireEvent(divEvent,evtName)
{
    var evt = document.createEventObject();
    evt.button = 2;
    evt.type = evtName;
    var rect = divEvent.getBoundingClientRect();
    evt.offsetX = (rect.right + rect.left)/2;
    evt.offsetY = (rect.bottom - rect.top)/2;
    divEvent.fireEvent(evtName, evt);
}

function SubmitForm(formname,i){
  var theForm
  window.event.cancelBubble = true;
  if(typeof(formname) == "object") theForm = formname
  else theForm = window.document.forms[formname]
  if(Validator.Validate(theForm,i)){
    theForm.onsubmit = BlockSubmit;
    theForm.submit()
    return true;
  }
  window.event.returnValue = false;
}

function CheckForm(formname,i){
  var theForm
  window.event.cancelBubble = true;
  if(typeof(formname) == "object") theForm = formname
  else theForm = window.document.forms[formname]
  if(Validator.Validate(theForm,i)){
    theForm.onsubmit = BlockSubmit;
    return true;
  }
  window.event.returnValue = false;
  return false
}

function ResetForm(formname){
  var theForm
  if(typeof(formname) == "object") theForm = formname
  else theForm = window.document.forms[formname]
  var count = theForm.elements.length;
  for(var i = 0;i < count;i++) Validator.ClearState(theForm.elements[i]);
  theForm.reset()
}

function BlockSubmit(){
  window.event.cancelBubble = true;
  alert("您已提交过表单了！");
  window.event.returnValue = false;
}

function PromptInfo(msg,href){
  if(window.confirm(msg))   window.location.href = href
}

function AddEditPart(){
  userURL = "/public/selectpart.asp?st=2";
  loc_x=document.body.scrollLeft+event.clientX-event.offsetX - 200;
  loc_y=document.body.scrollTop+event.clientY-event.offsetY - 80;
  Parts = window.showModalDialog(userURL,window,"edge:raised;scroll:0;status:0;help:0;resizable:1;dialogWidth:390px;dialogHeight:240px;dialogTop:"+200+"px;dialogLeft:"+200+"px");
  if(Parts != ""){
    var theForm = window.document.forms["PartEditForm"]
    Items = Parts.split("|||")
    for(i=0;i<Items.length;i++){
      Fields = Items[i].split("@@@")
      theForm["PartEdit"].options.add(new Option(Fields[1],Fields[0]))
    }
  }
  ModPartValue()
}
function DelEditPart(){
  var theForm = window.document.forms["PartEditForm"]
  var PartObj = theForm["PartEdit"].options
  for(i = 0;i < PartObj.length;i++){
    if(PartObj[i].selected) PartObj.remove(i)
  }
  ModPartValue()
}
function ModPartValue(){
  var theForm = window.document.forms["PartEditForm"]
  var PartObj = theForm["PartEdit"].options
  var Parts = ""
  for (i=0;i< PartObj.length;i++){
    Parts += "|" + PartObj[i].value + "@" + PartObj[i].text
  }
  theForm["PurviewSign"].value = Parts.substring(1)
}

function InitPartForm(){
  var theForm,vParts,Part,Items,i,Fields
  theForm = window.document.forms["SelectPartForm"]
  vParts = theForm["Parts"].value
  if(vParts != ""){
    Items = vParts.split("|||")
    for(i=0;i<Items.length;i++){
      Fields = Items[i].split("@@@")
      theForm["PartEdit"].options.add(new Option(Fields[2] + "(" + Fields[3] + ")",Fields[0]))
    }
  }
}

function SelectPart(n){
  var theForm,pg,sUrl,Parts
  theForm = window.document.forms["SelectPartForm"]
  sUrl = theForm.action + "?kw=" + theForm["kw"].value + "&st=" + theForm["st"].value
  switch(n){
    case 0:
      window.open(sUrl,"_self")
      break
    case 1:
      pg = theForm["pg"].value
      if(pg<1) return
      pg--
      this.location.href = sUrl + "&pg=" + pg
      break
    case 2:
      pg = theForm["pg"].value
      if(pg>=theForm["mg"].value) return
      pg++
      this.location.href = sUrl + "&pg=" + pg
      break
    case 3:
      window.returnValue = ""
      window.close()
      break
    case 4:
      window.returnValue = false
      break
    default:
      Parts = ""
      var PartObj = theForm["PartEdit"].options
      for(i = 0;i < PartObj.length;i++){
        if(PartObj[i].selected){
          Parts = Parts + "|||" + PartObj[i].value + "@@@" + PartObj[i].text
        }
      }
      if(Parts!="") Parts = Parts.substring(3)
      window.returnValue = Parts
      window.close()
      // parent_window = parent.dialogArguments
  }
}
function ShowHideFilterDiv(ftName){
  var filterDiv = document.getElementById(ftName);
  if(filterDiv.style.display == "none"){
    filterDiv.style.display = "block";
  }else{
    filterDiv.style.display = "none";
  }
}
/*-------------------------------------栅格函数--------------------------------------------*/
function GridOver(){
  var po,clsName,chk
  po = window.event.toElement;
  while(po.tagName.toLowerCase() != "tr"){po = po.parentElement}
  clsName = po.className;
  chk = clsName.substr(0,5)
  switch(chk){
    case "grd02":
    case "grd03":
    case "grd04":
    case "grd05":
      po._oldClass = clsName
      po.className = chk.substr(0,4) + "4" + clsName.substring(5)
      break
    default:
      return true;
  }
  return false
}
function GridOut(){
  var po,clsName,chk
  po = window.event.fromElement;
  while(po.tagName.toLowerCase() != "tr"){po = po.parentElement}
  clsName = po.className;
  chk = clsName.substr(0,5)
  switch(chk){
    case "grd02":
    case "grd03":
    case "grd04":
    case "grd05":
      po.className = po._oldClass
      break
    default:
      return true;
  }
  return false
}
function GridDown(){
  var po,clsName,chk
  if(window.event.button != 1) return true;
  po = window.event.srcElement;
  while(po.tagName.toLowerCase() != "tr"){po = po.parentElement}
  clsName = po.className;
  chk = clsName.substr(0,5)
  switch(chk){
    case "grd02":
    case "grd03":
    case "grd04":
      clsName = chk.substr(0,4) + "5" + clsName.substring(5);
      if(!po._dwnstate){
        po._dwnstate = true;
        po._dwnClass = po._oldClass;
        po._oldClass = clsName;
      }else{
        po._dwnstate = false;
        po._oldClass = po._dwnClass;
      }
      po.className = clsName;
      break
    default:
      return true;
  }
  return false
}
function SaveSelectForm(){
  var theform = window.document.forms[formname];
  var element = document.getElementsByName("newset"), oldset = [], delset = [];
  var oldIds = new String(theform["oldset"].value), setId;
  var el = element.length
  for(var i = 0;i < el; i++){
    setId = element[i].value.split(":")[0];
    if(setId && oldIds.indexOf(setId) != -1){
      if(element[i].checked){
        oldset.push(setId);
      }else{
        delset.push(setId);
      }
    }
  }
  theform["oldset"].value = oldset.join(",");
  theform["delset"].value = delset.join(",");
  theform.submit();
  return false;
}
function initSelectForm(){
  var theform = window.document.forms[formname], setId, oldIds, freeid = 0, r = 0;
  isself = parseInt(theform["isself"].value);
  ismore = parseInt(theform["ismore"].value);
  if(isself == 1){
		oldIds = new String(theform["oldset"].value);
		if(theform["oldset"].disabled) r = 1;
  }else{
    formId = theform["formid"].value;
    formName = theform["formname"].value;
    oldIds = new String(window.opener.window.document.forms[0][formId].value);
  }
  if(oldIds.length == 0) return;
  var element = document.getElementsByName("newset");
  var el = element.length
  for(var i = 0;i < el; i++){
    setId = element[i].value.split(":")[0];
    if(setId && oldIds.indexOf(setId) != -1){
			if(r == 1) element[i].disabled = true;
			element[i].checked = true
		} else freeid = freeid + 1
  }
  if(freeid == 0 && ismore == 1) theform["allset"].checked = true;
}
function SaveAndNaviatePage(page){
  var theform = window.document.forms[formname];
  theform["page"].value = page;
  if(isself == 1){
    SaveSelectForm();
  }else{
    UpdateAllSelectId();
    window.location.href = theform["selfurl"].value + "&page=" + theform["page"].value;
  }
}
function CheckAllSelect(){
  var theform = window.document.forms[formname];
  var allset = theform['allset'].checked;
  var element = document.getElementsByName("newset");
  var el = element.length
  for(var i = 0;i < el; i++) element[i].checked = allset;
  if(isself == 0) UpdateAllSelectId();
}
function UpdateSelectId(){
  window.event.cancelBubble = true;
  element = event.srcElement;
  setForm = window.opener.window.document.forms[0];
  var v = element.value;
  var a = v.split(":");
  switch(ismore){
    case 0:
      if(element.checked){
        setForm[formId].value = a[0];
        setForm[formName].value = a[1];
        CloseOpenedWin();
      }
      return false;
    case 1:
        var oldIds = new String(setForm[formId].value), arrId, arrName;
        if(element.checked){
          if(oldIds.length == 0){
            setForm[formId].value = a[0];
            setForm[formName].value = a[1];
          }else{
            if(oldIds.indexOf(a[0]) == -1){
              arrId = oldIds.split(", ");
              arrName = new String(setForm[formName].value).split(", ")
              arrId.push(a[0]);
              arrName.push(a[1]);
              setForm[formId].value = arrId.join(", ");
              setForm[formName].value = arrName.join(", ");
            }
          }
        }else{
          if(oldIds.length == 0) return;
          arrId = oldIds.split(", ");
          arrName = new String(setForm[formName].value).split(", ");
          var newId = [], newName = [], al;
		  al = arrId.length;
          for(var i = 0; i < al; i++){
            if(arrId[i].indexOf(a[0]) == -1){
              newId.push(arrId[i]);
              newName.push(arrName[i]);
          }
        }
        setForm[formId].value = newId.join(", ");
        setForm[formName].value = newName.join(", ");
      }
    default:
      return false;
  }
}
function UpdateAllSelectId(){
  var setForm = window.opener.window.document.forms[0];
  var elements = document.getElementsByName("newset");
  var oldIds = new String(setForm[formId].value), arrId, arrName, i;
  var setIds = {}, setId, al, v, a;
	if(oldIds.length > 0){
    arrId = oldIds.split(", ");
    arrName = new String(setForm[formName].value).split(", ")
    al = arrId.length;
    for(i = 0; i < al; i++) setIds[arrId[i]] = arrName[i];
  }
  al = elements.length;
  for(i = 0;i < al; i++){
    v = elements[i].value;
    a = v.split(":");
    if(elements[i].checked){
      setIds[a[0]] = a[1];
    }else{
      delete setIds[a[0]];
    }
  }
  arrId = [];
  arrName = [];
  for(setId in setIds){
    if(setIds.hasOwnProperty(setId)){
      arrId.push(setId);
      arrName.push(setIds[setId]);
    }
  }
  setForm[formId].value = arrId.join(", ");
  setForm[formName].value = arrName.join(", ");
}
function CloseOpenedWin(){
  window.opener.focus();
  window.opener = null;
  self.close();
}
/*------------------------显示消息-----------------------------------------*/
function DymancMessage(){
  if(typeof(window._msgbox)!="object"){
    var o = document.createElement("DIV");
    
    
    
    
  }
}
/*-------------------------------上传文件-----------------------------------*/
function UploadImage(sUrl){
  // 打开上传对话框
  var childWin = window.open(sUrl)
  childWin.opener = this
}

function UpLoadComplete(sName){
  var obj = window.opener.window.document.all["ImagePreview"]
  if(obj){
    obj.innerHTML = '<img src="/Public/Asp/ImagePreview.asp?Id='+ sName + '" class="UserImage" alt="照片">'
  }
  window.opener.focus()
  self.close()
}

function SelectMember(fn){
  var href = new String(location.href), host = location.host;
  var userURL = href.substr(href.indexOf(host) + host.length + 1)
  userURL = "http://" + host + "/" + userURL.substr(0, userURL.indexOf("/")) + "/include/user_select/index1.asp?fn=" + fn;
  loc_x=document.body.scrollLeft+event.clientX-event.offsetX - 100;
  loc_y=document.body.scrollTop+event.clientY-event.offsetY + 80;
// var sUser =
// window.showModalDialog(userURL,this,"edge:raised;scroll:0;status:0;help:0;resizable:1;dialogWidth:460px;dialogHeight:300px;dialogTop:"+loc_y+"px;dialogLeft:"+loc_x+"px");
  var childWin = window.open(userURL,"NewWin","scrollbars=1,status=0,Width=800,Height=600,Top=100,Left=100");
  childWin.opener = this
  childWin.focus()
}

function SelectMultiMember(fn){
  var href = new String(location.href), host = location.host;
  var userURL = href.substr(href.indexOf(host) + host.length + 1)
  userURL = "http://" + host + "/" + userURL.substr(0, userURL.indexOf("/")) + "/include/user_select/index2.asp?fn=" + fn;
  loc_x=document.body.scrollLeft+event.clientX-event.offsetX - 100;
  loc_y=document.body.scrollTop+event.clientY-event.offsetY + 80;
// var sUser =
// window.showModalDialog(userURL,this,"edge:raised;scroll:0;status:0;help:0;resizable:1;dialogWidth:460px;dialogHeight:300px;dialogTop:"+loc_y+"px;dialogLeft:"+loc_x+"px");
  var childWin = window.open(userURL,"NewWin","scrollbars=1,status=0,Width=800,Height=600,Top=100,Left=100");
  childWin.opener = this
  childWin.focus()
}

function OpenSelectPerson(formId, formName, isMore, sUrl, relform, relmsg){
  var href = new String(location.href), host = location.host;
  href = href.substr(href.indexOf(host) + host.length + 1)
  var userURL = "http://" + host
  if(sUrl.charAt(0) != "/") userURL += "/" + href.substr(0, href.lastIndexOf("/") + 1)
  userURL += sUrl;
	if(userURL.indexOf("?") > 0) userURL += "&";
	else userURL += "?"
	userURL += "formid=" + formId + "&formname=" +formName + "&ismore=" + isMore;
	if(relform){
		var relid = document.forms[0][relform].value;
		if(!relid) {
			alert(relmsg);
			return false;
		}
    userURL += "&" + relform + "=" + relid;
  }
  loc_x=document.body.scrollLeft+event.clientX-event.offsetX - 100;
  loc_y=document.body.scrollTop+event.clientY-event.offsetY + 80;
	// window.showModalDialog(userURL,this,"edge:raised;scroll:0;status:0;help:0;resizable:1;dialogWidth:460px;dialogHeight:300px;dialogTop:"+loc_y+"px;dialogLeft:"+loc_x+"px");
  var childWin = window.open(userURL, "NewWin","scrollbars=1,status=0,Width=800,Height=600,Top=100,Left=100");
  childWin.opener = this
  childWin.focus()
}

function SelectComplete(v){
  var a = v.split(":")
  var fn = a[0]
  document.all[fn].value = a[2]
  this.window.focus()
}
function Add_user(v){
  var a = v.split(":")
  var fn = a[0]
  var oldvalue = document.all[fn].value;
    newvalue = a[2];
    if(oldvalue != ""){
        var arr = oldvalue.split(",");
        for(var i = 0; i < arr.length; i++){
            if(arr[i] == newvalue) return false;
        }
        document.all[fn].value = oldvalue + "," + newvalue;
    }else document.all[fn].value = newvalue;
}
