/*tools.js */
if (typeof (GetObjectFromDocument) != "function") {
   var GetObjectFromDocument = function(_id1) {
      try {
         if (document.getElementById(_id1) != null) {
            return document.getElementById(_id1);
         } else if (document.all) {
            return document.all[_id1];
         } else if (typeof (document[_id1]) != "undefined") {
            return document[_id1];
         }
      } catch (e) {
         return null;
      }
   };
}

if (typeof ($) != "function") {
   var $ = function(_id) {
      if (typeof ($get) == "function") {
         return $get(_id);
      } else {
         return GetObjectFromDocument(_id);
      }
   };
}

if (typeof (ReplaceUrl) != "function") {
   var ReplaceUrl = function(tp) {
      try { ShowLoading(); } catch (e) { }
      switch (tp) {
         case 0:
            top.location.replace("/");
            break;
         case 1:
            top.location.replace("/webadmin/p801.aspx");
            break;
      }
   };
}
/*cookie存取相關函數 BEGIN=============================================*/
/*取得指定名稱的cookie值*/
var getCookie = function(_strName) {
   var _objCookie, _start, _end;
   _strName += "=";
   _objCookie = document.cookie + ";";
   _start = _objCookie.indexOf(_strName);
   if (_start != -1) {
      _end = _objCookie.indexOf(";", _start);
      return unescape(_objCookie.substring(_start + _strName.length, _end));
   }
   return null;
};
/*寫入cookie的值：_strName:cookie名稱，_strValue:cookie值，_numDay:有效期限(天)*/
var setCookie = function(_strName, _strValue, _numDay) {
   var _setDay1;
   var _expDay1 = "Wed, 01 Jan 2020 23:59:59 GMT"; /*預設截止日*/
   if ((_strName != null) && (_strValue != null)) {
      if (_numDay != null) {
         _numDay = eval(_numDay);
         _setDay1 = new Date();
         _setDay1.setTime(_setDay1.getTime() + (_numDay * 1000 * 60 * 60 * 24));
         _expDay1 = _setDay1.toGMTString();
      }
      document.cookie = _strName + "=" + escape(_strValue) + ";path=/;expires=" + _expDay1;
      return true;
   }
   return false;
};
/*刪除cookie：theName:cookie名稱*/
var deleteCookie = function(_strName) {
   document.cookie = _strName + "=;expires=Thu, 01 Jan 1900 00:00:01 GMT";
   return true;
};
/*cookie存取相關函數 END=============================================*/

/*排除url字串中的HTML語法標籤*/
var encodeHtml = function(_strHml) {
return _strHml.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;"); // Do not change orders
};

/*排除title字串中的HTML語法標籤*/
var encodeHtmlTitle = function(_strHml) {
   return _strHml.split("<").join("＜").split(">").join("＞");
};

/*設定元件的事件處理函式*/
var SetEventHandle = function(_objTarget, _strEventName, _strEventHandler) {
   try {
      if (_objTarget.addEventListener) {
         _objTarget.addEventListener(_strEventName, eval(_strEventHandler), false);
      } else if (_objTarget.attachEvent) {
         _objTarget.attachEvent("on" + _strEventName, eval(_strEventHandler));
      } else {
         eval("_objTarget.on" + _strEventName + "=" + _strEventHandler + ";");
      }
   } catch (e) {
   }
};

/*移除元件的事件處理函式*/
var RemoveEventHandle = function(_objTarget, _strEventName, _strEventHandler) {
   try {
      if (_objTarget.removeEventListener) {
         _objTarget.removeEventListener(_strEventName, eval(_strEventHandler), false);
      } else if (_objTarget.detachEvent) {
         _objTarget.detachEvent("on" + _strEventName, eval(_strEventHandler));
      } else {
         eval("_objTarget.on" + _strEventName + "=null;");
      }
   } catch (e) {
   }
};

/*設定元件的style屬性值*/
var SetElementStyle = function(_strId, _strAttr, _strValue) {
   var _objElement = $(_strId), _strScript;
   if (_objElement) {
      if (typeof (_strValue) == "Number") {
         _strScript = "$('" + _strId + "').style." + _strAttr + "=" + _strValue + ";"
      } else {
         _strScript = "$('" + _strId + "').style." + _strAttr + "='" + _strValue + "';"
      }
      eval(_strScript);
      try {
         _objElement.style[_strAttr] = _strValue;
      } catch (e) {
      }
   }
};

/*設定元件的屬性質*/
var SetElementAttribute = function(_strId, _strAttribute, _strValue) {
   var _objElement = $(_strId), _strScript;
   if (_objElement) {
      try {
         _objElement.setAttribute(_strAttribute, _strValue);
      } catch (e) {
      }
      try {
         _objElement.setAttributeNode(_strAttribute, _strValue);
      } catch (e) {
      }
   }
};
var SetElementHtml = function(_strId, _strHtml) {
   var _objElement = $(_strId);
   if (_objElement) {
      if (typeof (_objElement.innerHTML) != "undefined") {
         _objElement.innerHTML = _strHtml;
      }
   }
}
/*Tools Class 常用工具類別*/
var Tools = function(_objMaskDiv1, _objMaskDiv2, _objLoadingDiv) {
   this.intBodyHeight = 0;
   this.intBodyWidth = 0;
   this.objMaskDiv1 = typeof (_objMaskDiv1) != "undefined" ? _objMaskDiv1 : null;
   this.objMaskDiv2 = typeof (_objMaskDiv2) != "undefined" ? _objMaskDiv2 : null;
   this.objLoadingDiv = typeof (_objLoadingDiv) != "undefined" ? _objLoadingDiv : null;
   this.intSizeType = 0;
   if (this.objMaskDiv1) {
      this.objMaskDiv1.style.top = "0px";
      this.objMaskDiv1.style.left = "0px";
   }
};
Tools.prototype.ShowHideMask = function(_tp) {
   if (this.objMaskDiv1) {
      if (_tp == 1) {
         this.objMaskDiv1.style.visibility = "visible";
         this.objMaskDiv1.style.display = "block";
      } else {
         this.objMaskDiv1.style.visibility = "hidden";
         this.objMaskDiv1.style.display = "none";
         if (this.objLoadingDiv) {
            this.objLoadingDiv.style.visibility = "hidden";
            this.objLoadingDiv.style.display = "none";
         }
      }
   }
};
Tools.prototype.ShowHideLoading = function(_tp) {
   if (this.objLoadingDiv && this.objMaskDiv1) {
      if (_tp == 1) {
         this.objLoadingDiv.style.visibility = "visible";
         this.objLoadingDiv.style.display = "block";
         this.objMaskDiv1.style.visibility = "visible";
         this.objMaskDiv1.style.display = "block";
         this.objMaskDiv1.style.zIndex = 100;
         document.body.scrollTop = 0;
      } else {
         this.objLoadingDiv.style.visibility = "hidden";
         this.objLoadingDiv.style.display = "none";
         this.objMaskDiv1.style.zIndex = 1;
      }
   }
};
/*設定元件的style屬性值*/
Tools.prototype.SetElementStyle = function(_strId, _strAttr, _strValue) {
   var _objElement = $(_strId), _strScript;
   if (_objElement) {
      if (typeof (_strValue) == "Number") {
         _strScript = "$('" + _strId + "').style." + _strAttr + "=" + _strValue + ";"
      } else {
         _strScript = "$('" + _strId + "').style." + _strAttr + "='" + _strValue + "';"
      }
      eval(_strScript);
      try {
         _objElement.style[_strAttr] = _strValue;
      } catch (e) {
      }
   }
};
/*設定元件的屬性質*/
Tools.prototype.SetElementAttribute = function(_strId, _strAttribute, _strValue) {
   var _objElement = $(_strId), _strScript;
   if (_objElement) {
      try {
         _objElement.setAttribute(_strAttribute, _strValue);
      } catch (e) {
      }
      try {
         _objElement.setAttributeNode(_strAttribute, _strValue);
      } catch (e) {
      }
   }
};
/*設定元件的事件處理函式*/
Tools.prototype.SetEventHandle = function(_objTarget, _strEventName, _strEventHandler) {
   if (_objTarget.addEventListener) {
      _objTarget.addEventListener(_strEventName, eval(_strEventHandler), false);
   } else if (_objTarget.attachEvent) {
      _objTarget.attachEvent("on" + _strEventName, eval(_strEventHandler));
   } else {
      eval("_objTarget.on" + _strEventName + "=" + _strEventHandler + ";");
   }
};
/*移除元件的事件處理函式*/
Tools.prototype.RemoveEventHandle = function(_objTarget, _strEventName, _strEventHandler) {
   if (_objTarget.removeEventListener) {
      _objTarget.removeEventListener(_strEventName, eval(_strEventHandler), false);
   } else if (_objTarget.detachEvent) {
      _objTarget.detachEvent("on" + _strEventName, eval(_strEventHandler));
   } else {
      eval("_objTarget.on" + _strEventName + "=null;");
   }
};
/*排除title字串中的HTML語法標籤*/
Tools.prototype.EncodeHtmlTitle = function(_strHtml) {
   return _strHtml.split("<").join("＜").split(">").join("＞");
};
/*排除url字串中的HTML語法標籤*/
Tools.prototype.EncodeHtml = function(_strHtml) {
   return _strHtml.split("<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;").split("&").join("&amp;"); // Do not modify ("\"")
};
/*排除HTML與法中的<script></script>區段*/
Tools.prototype.EncodeHtmlScript = function(_strHtml) {
   var i, _pos, _strTemp = "";
   var _strHtml2 = _strHtml.toLowerCase();
   var arrHtml = _strHtml2.split("<script");
   alert(arrHtml);
   if (arrHtml.length > 1) {
      for (i = 0, _pos = 0; i < arrHtml.length; i++) {
         if (i == (arrHtml.length - 1)) {
            _strTemp += arrHtml[i];
         } else {
            _strTemp += _strHtml.substr(_pos, arrHtml[i].length) + "&lt;script";
            _pos = arrHtml[i].length + 7;
         }
      }
      return _strTemp;
   } else {
      return _strHtml;
   }
};
/*更新版面尺寸資訊*/
Tools.prototype.UpdateWindowSize = function() {
   var _intHeight, _intWidth;
   if (this.objMaskDiv2 == null) {
      return;
   }
   this.objMaskDiv1.style.width = this.intSizeType == 1 ? document.body.scrollWidth : this.objMaskDiv2.offsetWidth + "px";
   this.objMaskDiv1.style.height = this.intSizeType == 1 ? document.body.scrollHeight : this.objMaskDiv2.offsetHeight + "px";
   this.intBodyHeight = this.objMaskDiv2.offsetHeight;
   this.intBodyWidth = this.objMaskDiv2.offsetWidth;
   this.SetPosition(this.objLoadingDiv, 400, 20);
};
/*設定物件位置*/
Tools.prototype.SetPosition = function(_objDiv, _intLeft, _intTop) {
   if (_objDiv == null) {
      return;
   }
   if (typeof (_intTop) == "undefined" || isNaN(_intTop)) {
      _intTop = Math.floor((this.intBodyHeight - _objDiv.offsetHeight) / 2) + (this.intSizeType == 1 ? document.body.scrollTop : 0);
   }
   if (typeof (_intLeft) == "undefined" || isNaN(_intLeft)) {
      _intLeft = Math.floor((this.intBodyWidth - _objDiv.offsetWidth) / 2) + (this.intSizeType == 1 ? document.body.scrollLeft : 0);
   }
   _intTop = (_intTop > 0 ? _intTop.toString() : "0") + "px";
   _intLeft = (_intLeft > 0 ? _intLeft.toString() : "0") + "px";
   setTimeout("SetElementStyle('" + _objDiv.id + "', 'top', '" + _intTop + "');", 0);
   setTimeout("SetElementStyle('" + _objDiv.id + "', 'left', '" + _intLeft + "');", 0);
};
/*設定顯示介面語言*/
var setLang = function(strLang) {
   setCookie("sysLang", strLang, 30);
   if (strLang != "") {
      top.location.href = "/" + strLang + "/default.aspx";
   } else {
      top.location.href = "/default.aspx";
   }
};
/* 開新視窗 */
var openWin = function(strUrl, strWinName, strFeatures) {
   if (strWinName == null) {
      strWinName = "popupWindow";
   }
   if (strFeatures == null) {
      strFeatures = "";
   }

   var openWinId = window.open(strUrl, strWinName, strFeatures, true);
   if (openWinId != null) {
      try {
         openWinId.focus();
      } catch (e) {
         // Do Nothing...
      }
   }
};
