function getElementID(ID) {
  if ( document.getElementById )
    return document.getElementById(ID);
  else if ( document.all && document.all(ID) )
    return document.all(ID);
  else if ( document.layers && document.layers[ID] )
    return document.layers[ID];
  else
    return false;
}

function createHTTPRequest() {
  var request = false;
  try {
    request = new XMLHttpRequest();
  }
  catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (failed) {
        request = false;
      }
    }
  }
  return request;
}

Array.prototype.indexOf = function (vItem) {
  for ( var i=0; i<this.length; i++ ) {
    if ( vItem == this[i] ) return i;
  }
  return -1;
}

Array.prototype.Swap = function (k1, k2) {
  var tmp  = this[k1];
  this[k1] = this[k2];
  this[k2] = tmp;
}

function max_window() {
  window.self.resizeTo(screen.width, screen.height);
}

function move_window() {
  window.self.moveTo(0, 0);
}

// 偵測 IE
function browser_name() {
  var bn = "Microsoft Internet Explorer";
  if ( navigator.appName == bn ) return "IE";
  else                           return "notIE";
}

function go_to(href, target, title) {
  if ( target == "self" ) {
    if ( window.name == "frm_main" ) window.location.href = href;
    else                             window.parent.location.href = href;
  }
  if ( target == "myself" ) window.location.href = href;
  if ( target == "new" )    open_win(href, title, 800, 600);
  if ( target == "ad" )     window.open(href, title);
}

// 開新視窗函數
// clink 參數代表欲連結的 url
// win_name 參數代表新視窗名稱
// w 參數代表新視窗的寬度
// h 參數代表新視窗的高度
function open_win(clink, win_name, w, h) {
  var url = "";
  if ( clink != "" ) url = clink;
  if ( w == null ) w = 640;
  if ( h == null ) h = 480;
  param = "toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=yes,width=" + w + ",height=" + h;
//  open_window = window.open(url, win_name, param);
  var open_window = window.open(url, win_name, param);
  if ( open_window == null )
    alert("您的瀏覽器將本站彈出的視窗誤認為廣告，並且攔阻了它！\n\n請解除這項攔阻功能，以便使用後續的服務！\n\n在本站首頁「求助」當中，「其他」／「雜項問題」裡有相關資訊可供參考。");
}

var expiryDate = new Date();
expiryDate.setTime(expiryDate.getTime() + (365*24*60*60*1000));

function GetCookieVal(offset) {
  var endstr = document.cookie.indexOf(";", offset);
  if ( endstr == -1 ) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(Name) {
  var arg  = Name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while ( i < clen ) {
    var j = 1 + alen;
    if ( document.cookie.substring(i,j) == arg ) return GetCookieVal(j);
    i = document.cookie.indexOf(" ", i) + 1;
    if ( i == 0 ) break;
  }
  return null;
}

function SetCookie(name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path    = (argc > 3) ? argv[3] : null;
  var domain  = (argc > 4) ? argv[4] : null;
  var secure  = (argc > 5) ? argv[5] : null;
  document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path    == null) ? "" : ("; path="    + path)) +
    ((domain  == null) ? "" : ("; domain="  + domain)) +
    ((secure  == true) ? "; secure" : "");
}

// 身分證字號檢查
function checkid(id) {
  m0 = "          "+"ABCDEFGHJKLMNPQRSTUVXYWZIO";
  m  = m0.indexOf(id.charAt(0).toUpperCase());
  if (m < 10) {
    return 0;
  }
  else {
    m = Math.floor(m/10) * 1 + (m%10) * 9;
    for (i=1; i<9 ; i++) {
      m = m + id.charAt(i) * (9-i);
    }
    m = 10 - m % 10;
    if ( m == 10 ) {
      m = 0;
    }
    if ( id.charAt(9) != "" && id.charAt(9) == m ) {
      return 1;
    }
    else {
      return 0;
    }
  }
}

// 檢查日期輸入是否合法
function validdate(y,m,d) {
  if (m==1  && (d>31 || d<1)) return 0;
  if (y%4 == 0 && y%100>0  &&             m==2 && (d>29 || d<1)) return 0;
  if (y%4 == 0 && y%100==0 && y%400>0  && m==2 && (d>28 || d<1)) return 0;
  if (y%4 == 0 && y%100==0 && y%400==0 && m==2 && (d>29 || d<1)) return 0;
  if (y%4 >0 && m==2 && (d>28 || d<1)) return 0;
  if (m==3  && (d>31 || d<1)) return 0;
  if (m==4  && (d>30 || d<1)) return 0;
  if (m==5  && (d>31 || d<1)) return 0;
  if (m==6  && (d>30 || d<1)) return 0;
  if (m==7  && (d>31 || d<1)) return 0;
  if (m==8  && (d>31 || d<1)) return 0;
  if (m==9  && (d>30 || d<1)) return 0;
  if (m==10 && (d>31 || d<1)) return 0;
  if (m==11 && (d>30 || d<1)) return 0;
  if (m==12 && (d>31 || d<1)) return 0;
  if (m>12 || m<1) return 0;
  return 1;
}

// 檢查數字函數
// 傳回值 1 代表沒有數字以外的字母
function isNumeric(ch) {
  m0 = "0123456789";
  lh = ch.length;
  for ( i=0; i<lh; i++ ) {
    m = m0.indexOf(ch.charAt(i));
    if ( m < 0 ) return 0;
  }
  return 1;
}

// 檢查數字函數
// 傳回值 1 代表是浮點數或整數
function isFloat(ch) {
  m0 = "0123456789.";
  lh = ch.length;
  for ( i=0; i<lh; i++ ) {
    m = m0.indexOf(ch.charAt(i));
    if ( m < 0 ) return 0;
  }
  return 1;
}

// 檢查字元函數（for Register）
// 傳回值 0 代表含有 m0 字串以外的字元
// 傳回值 1 代表第一個字元非英文字母
function checkcs1(ch) {
  m0 = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  cc = 1;
  lh = ch.length;
  for ( i=0; i<lh; i++ ) {
    m = m0.indexOf(ch.charAt(i).toUpperCase());
    if ( m < 0 ) return 0;
    if (( i == 0 ) && ( m > 10 )) cc = 2;
  }
  return cc;
}

// 檢查字元函數
// 傳回值 0 代表含有 m0 字串以外的字元，或是字串長度小於 6
function checkcs2(ch) {
  m0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_-+=|\[]{}:;',.<>?/`~";
  lh = ch.length;
  if ( lh < 6 ) return 0;
  for ( i=0; i<lh; i++ ) {
    m = m0.indexOf(ch.charAt(i).toUpperCase());
    if ( m < 0 ) return 0;
  }
  return 1;
}

// 檢查字元函數（for Jewelbox）
// 傳回值 0 代表沒有 m0 字串以外的字母
function checkcs3(ch) {
  m0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
  lh = ch.length;
  for ( i=0; i<lh; i++ ) {
    m = m0.indexOf(ch.charAt(i).toUpperCase());
    if ( m < 0 ) return 0;
  }
  return 1;
}

// 去除字串之左側空白字元函數
function StringLTrim(ch) {
  lh = ch.length;
  new_ch = ch;
  for ( i=0; i<lh; i++ ) {
    if ( ch.substring(i, i+1) == " " )
      new_ch = ch.substring(i+1, lh);
    else
      break;
  }
  return new_ch;
}

// 去除字串之兩側空白字元函數
function StringTrim(ch) {
   var lh;
   var new_ch;
   var i;
   // 先去左側空白
   ch = StringLTrim(ch);
   lh = ch.length;
   new_ch = ch;
   for ( i=lh-1; i>0; i-- ) {
       if ( ch.substring(i, i+1) == " " )
          new_ch = ch.substring(0, i);
       else
         break;
   }
   return new_ch;
}

function reload_verify_image() {
  if ( document.getElementById ) {
    var R = Math.floor(Math.random()*1000);
    document.getElementById("verify_image").src = "/public/verify_img.php?R=" + R;
  }
  else {
    self.location.reload();
  }
}

function Compose_Other(to) {
      newmail = open("/webmail/compose.php?fto=" + to ,
                     "newmail",
                     "toolbar=no, "
                     + "directories=no, "
                     + "status=yes, "
                     + "MenuBar=0, "
                     + "scrollbars=yes, "
                     + "resizable=yes, "
                     + "width=730, "
                     + "height=450"
                    );
}

function Compose_data_Other(who, type, no) {
  newmail = open("/webmail/compose.php?who=" + who + "&op=" + type + "&no=" + no ,
                 "newmail",
                 "toolbar=no, "
                 + "directories=no, "
                 + "status=yes, "
                 + "MenuBar=0, "
                 + "scrollbars=yes, "
                 + "resizable=yes, "
                 + "width=730, "
                 + "height=450"
                );
}

function clearField(this_Field) {
  if ( this_Field.value == this_Field.defaultValue ) this_Field.value = "";
}

function checkField(this_Field) {
  if ( this_Field.value == "" ) this_Field.value = this_Field.defaultValue;
}

// Only for Fellowship
function showPage(Page) {
  document.PageChange.Page.value = Page;
  document.PageChange.submit();
}

function AddFriend(friend_name) {
  document.Friend_Add.friend_name.value = friend_name;
  document.Friend_Add.action_from.value = location.href;
  document.Friend_Add.submit();
}

function MailtoMember(mail_address) {
  document.MailtoMember.fto.value = mail_address;
  document.MailtoMember.submit();
}

function sortPage() {
  document.PageReSort.submit();
}