var regTrim = /^\s+|\s+$/g;
var reEmail = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
var reInt = /[0-9][0-9]*/;

function checkEmail_old(email) {
  return (reEmail.test(email));
}

function trim(s) {
  return s.replace(regTrim, "");
}

function isInteger(s) {
  return (reInt.test(s));
}

function confirmLogoff() {
  return confirm("確定登出？");
}

function setVisibility(objId, isVisible) {
  var obj = document.getElementById(objId);

  obj.style.visibility = (isVisible ? "visible" : "hidden");
  obj.style.display = (isVisible ? "" : "none");
}

function toggleDisplay(objId) {
  var obj = document.getElementById(objId);
  var shown = (obj.style.visibility!="hidden");

  if (shown) {
    obj.style.visibility = "hidden";
    obj.style.display = "none";
  } else {
    obj.style.visibility = "visible";
    obj.style.display = "";
  }
}

function doBlink() {
  var blink = document.all.tags("BLINK");
  for (var i=0; i<blink.length; i++)
    blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : "";
}

function startBlink() {
  if (document.all)
    setInterval("doBlink()", 1000);
}

function checkLogon(f) {
  f.userId.value = trim(f.userId.value);

  if (f.userId.value == "") {
    f.userId.focus();
    alert("請輸入用戶號碼");
    return false;
  }

  if (f.password.value == "") {
    f.password.focus();
    alert("請輸入密碼");
    return false;
  }

  f.scrambled.value = hex_md5(myRandKey + hex_md5(f.password.value));
  f.password.value = "";

  return true;
}

function checkActivate(f) {
  f.schoolId.value = trim(f.schoolId.value);
  f.studentNo.value = trim(f.studentNo.value);

  if (f.schoolId.value == "") {
    f.schoolId.focus();
    alert("請輸入學校編號");
    return false;
  }

  if (f.studentNo.value == "") {
    f.studentNo.focus();
    alert("請輸入學生編號");
    return false;
  }

  if (f.password.value == "") {
    f.password.focus();
    alert("請輸入密碼");
    return false;
  }

  return true;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function checkEmail(str) {
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);

  if (lat==-1 || lat==0 || lat==lstr-1)
    return false;

  if (ldot==-1 || ldot==0 || ldot==lstr-1)
    return false;

  if (str.indexOf(at,(lat+1))!=-1)
    return false;

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
     return false;

  if (str.indexOf(dot,(lat+2))==-1)
     return false;

  if (str.indexOf(" ")!=-1)
     return false;

  return true;
}

