function isInteger(id) {
	var v;
	v = getValue(id);
	if ( v == "" || isNaN(v) ) return false;
	return true;
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
/*
// toogle layer.
function tl(id) {
e=document.getElementById(id);
s=e.style.visibility;
if (s=='hidden' ) {
	setCookie(id,'visible');
}
else {
	deleteCookie(id);
}
//alert(getCookie(id));
d=(s=='hidden')?'block':'none';
s=(s=='hidden')?'visible':'hidden';
e.style.visibility=s;
e.style.display=d;
//e.refresh();
}
*/
function tl(id) {
	var i;
	for(i=1; i<=20; i++) {
		e=document.getElementById(i);
		if(e!=null) {
			e.style.visibility="hidden";
			e.style.display="none";
			deleteCookie(i);
		}
	}
	e=document.getElementById(id);
	e.style.visibility = "visible";
	e.style.display = "block";
	setCookie(id,'visible');
}

function setDivState(id,state) {
if(state!=null && state=="visible") {
e=document.getElementById(id);
e.style.visibility="visible";
e.style.display="block";
}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function getValue(id) {
	var o = document.getElementById(id);
	if ( o == null ) return null;
	return o.value;
}

function endsWith(id,ext) {
	var v=getValue(id);
	v=v.toLowerCase();
	var e=v.substring(v.length-ext.length,v.length);
	return e==ext;
}

function CheckUsernameChar(username){
	var rule = /^[a-z0-9_]$/;
	var ch = null;
	
	for (var i = 0; i < username.length; i++){
		ch = username.substr(i, 1);
		if (ch.match(rule) == null){
			return false;
		}
	}
	return true;
}

function checkLength(id, min, max) {
	v = getValue(id).length;
	// -1 means ignore
	if ( min != -1 ) {
		if ( v < min ) return false;
	}
	// -1 means ignore
	if ( max != -1 ) {
		if ( v > max ) return false;
	}
	return true
}

function CheckPasswordChar(password){
	var rule = /^[a-zA-Z0-9]$/;
	var ch = null;
	
	 for (var i = 0; i < password.length; i++){
		ch = password.substr(i, 1);
		if (ch.match(rule) == null){
			return false;
		}
	}
	return true;
}

function VerifyEmailFormat(email){
	var rule = new Array;
	rule[0] = /^(.+)@(.+)$/;
	rule[1] = /[\/\\\*\+\?\|\(\)\[\]\{\}\s\<>,:;@]/;
	rule[2] = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	rule[3] = /[\/\\\*\+\?\|\(\)\[\]\{\}\s\<>,:;@+]/;
	rule[4] = /[\.]/g;
	
	
	// check email on the format "user@domain.com"
	var match_array = (email.match(rule[0]));
	if (match_array != null){
		var user = match_array[1];
		var domain = match_array[2];
	} else{
		// 電郵地址格式錯誤!
		return false;
	}
	
	// check the user format "user"
	if (user.match(rule[1]) != null){
		// 電郵地址中使用者名稱錯誤!
		return false;
	}
	
	// check that the domain is using ip address represent
	var ip_array = domain.match(rule[2]);
	if (ip_array != null){
		for (var i =1; i < 4; i++){
			if (ip_array[i] > 255){
				// 電郵地址是用 ip 來代表, 但所輸入的 ip 有錯誤!
				return false;
			}
		}
	}
	
	// check if the domain is using domain represent
	var domain_array = domain.match(rule[3]);
	if (domain_array != null){
		// 電郵地址中所提供的 domain 錯誤!
		return false;
	}
	
	var point_match = domain.match(rule[4]);
	var domain_atoms = domain.split(".");
	if (point_match != null){
		// check the domain not missing hostname
		if (domain_atoms.length < 2){
			// 電郵地址沒有主機名稱!
			return false;
		}
	}
	
	// check the domain is ended with 3 letter domain or 2 letter country
	if ((domain_atoms[domain_atoms.length - 1].length < 2) || (domain_atoms[domain_atoms.length - 1].length > 3)){
		// 電郵地址最尾應是 3 個字母的 domain, 或 2 個字母的國家名!
		return false;
	}
	
	return true;
}
