var highlight_color = "#FFFF99";

function getValue(id) {

	var o = document.getElementById(id);

	if ( o == null ) return null;

	return o.value.replace(/(^ +| +$)/, "");
}



function getObject(id) {

	var o = document.getElementById(id);

	return o;

}



function highlight_error(id) {

	document.getElementById(id).style.backgroundColor= highlight_color;

}



function undo_highlight_error(id) {

	document.getElementById(id).style.backgroundColor= "#FFFFFF";

}



function set_bgcolor(id,bgcolor) {

	document.getElementById(id).style.backgroundColor= bgcolor;

}



function checkLength(id, min, max) {

	var v;

	v = getValue(id).length;

		

	// -1 means ignore

	if ( min != null ) {

		if ( v < min ) {

			highlight_error(id);

			//document.getElementById(id).style.backgroundColor= highlight_color;

			return false;

		}

		else {

			undo_highlight_error(id);

		}



	}

	// -1 means ignore

	if ( max != null ) {

		if ( v > max ) {

			highlight_error(id);

			//document.getElementById(id).style.backgroundColor= highlight_error;

			return false;

		}

		else {

			undo_highlight_error(id);

		}

	}

	return true;

}



function checkLengthLstChk(form_listbox,id_hl,min, max) {
	//eg. <select name="indnature[]" id="indnature" style="width: 200px" multiple size="4"
	//if ( ! checkLength("indnature",1,null) ) msg+="工業性質必須選擇!\n";
	//else if ( ! checkLengthLstChk(form_jobpost.indnature,"indnature",null,3) ) msg+="工業性質可選最多三項!\n";
	
	var count_sel = 0;

	var id = id_hl; //id for hightlight

	//alert("len="+form_listbox.length);


	for (i = 0; i < form_listbox.length; i++)

	{
		if (form_listbox[i].selected ==true){

			count_sel++;

		}
	}
	
	//alert(count_sel);

	if ( min != null ) {

		if ( count_sel < min ) {

			highlight_error(id);

			return false;

		}

		else {

			undo_highlight_error(id);

		}



	}

	// -1 means ignore

	if ( max != null ) {

		if ( count_sel > max ) {

			highlight_error(id);

			return false;

		}
		else {

			undo_highlight_error(id);

		}

	}

	return true;

}	


//for radio name="radio_name" || checkbox, name="checkbox_name[]"
//please set hightlight ID var id = 'id_'+name ;

function checkRadioChkBox(name) {

	var radio_chk = false;

	var r;

	var id = 'id_'+name ;

	r = document.getElementsByName(name);

	for (i = 0; i < r.length; i++)

	{

		if (r[i].checked){

			radio_chk = true;

			break;

		}

	}



	if (! radio_chk) { 

		//document.getElementById('id_'+name).style.backgroundColor= highlight_color;

		highlight_error(id);

		return false;

	}

	else { 

		undo_highlight_error(id);

		return true;

	}

}


function is_integer(id) {

	var v;

	v = getValue(id);

	if (v == "")

	{

		undo_highlight_error(id);

		return true;

	}

	else if ( isNaN(v) )

	{

		highlight_error(id);

		return false;

	}

	else

	{

		undo_highlight_error(id);

		return true;

	}

}

function ctype_alnum(alphane) {

	var v;

	v = getValue(alphane);

	for(var j=0; j<v.length; j++) {

	var alphaa = v.charAt(j);

	var hh = alphaa.charCodeAt(0);

		if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123)) {

		}

		else {

			return false;

		}

	}

	return true;

}

function ctype_alnum2(alphane) {

	var v;

	v = getValue(alphane);

	for(var j=0; j<v.length; j++) {

	var alphaa = v.charAt(j);

	var hh = alphaa.charCodeAt(0);

		//if((hh >= 32 && hh <=47) || (hh >=58 && hh<=64) || (hh >=91 && hh<=94) || (hh >=123 && hh<=126)) {
		if((hh >= 48 && hh <=57) || (hh >=65 && hh<=90) || (hh >=97 && hh <=122) || hh == 95 || hh >256) {
		}
		else {
			return false;
		}
	}

	return true;

}

function isChnEngDig(id){
      var str;
	  str = getValue(id);
	  
	  var reg = /^[a-z0-9\u4E00-\u9FA5]+$/i;
      if(!reg.test(str)){
       return false;
      }
      return true;
}


function isChnOnly(id){
      var str;
	  str = getValue(id);
	  
	  var reg = /^[\u4E00-\u9FA5]+$/;
      if(!reg.test(str)){
       return false;
      }
      return true;
}

function engstr(id){
	var str;
	str = getValue(id);
   
   var subStr = "";
   for (i=0,n=str.length;i<n;i++){
      subStr = str.charCodeAt(i);
      if ((subStr > 256)){
         return false;
      }
   }
   return true;
}


function is_valid_date(d)

{

	if ( ! ( /^\d{1,4}-\d{1,2}-\d{1,2}$/.test(d) ) ) return false;



	var monthLength = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);



	var vv = d.split("-");

	var day = parseInt(vv[2]);

	var month = parseInt(vv[1]);

	var year = parseInt(vv[0]);

	if (!day || !month || !year)return false;



	// check for year.

	if ( year < 0 || year > 9999 ) return false;

	// check for month.

	if ( month < 1 || month > 12 ) return false;

	// check for day.

	if ( day < 1 || day > 31 ) return false;

	if (year/4 == parseInt(year/4)) monthLength[2] = 29;

	if (day > monthLength[month]) return false;

	return true;

}



/*

function is_valid_email(x)

{

	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (filter.test(x)) return true;

	return false;

}

*/



function is_valid_phone(id) {

	var x;

	var ret;

	x = getValue(id);

	//if (x == null)

	if (x == "")

	{

		undo_highlight_error(id);

		return true;

	}

	else

	{

		ret= /^(\d|-|\s)+$/.test(x);

		if (ret == false)

		{

			highlight_error(id);

			return false;

		}

		else 

		{

			undo_highlight_error(id);

			return true;

		}

	}

	//return /^(\d|-|\s)+$/.test(x);

}





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 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 trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function VerifyEmailFormat(id){

	var email_v;

	//email_v = getValue(id);
	email_v = document.getElementById(id).value;
	email_v = trim(email_v);

	if(email_v == null || email_v =="") return true;

	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_v on the format "user@domain.com"

	var match_array = (email_v.match(rule[0]));

	if (match_array != null){

		var user = match_array[1];

		var domain = match_array[2];

	} else{

		highlight_error(id);

		return false;

	}

	

	// check the user format "user"

	if (user.match(rule[1]) != null){

		highlight_error(id);

		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){

				highlight_error(id);

				return false;

			}

		}

	}

	

	// check if the domain is using domain represent

	var domain_array = domain.match(rule[3]);

	if (domain_array != null){

		highlight_error(id);

		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){

			highlight_error(id);

			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)){
	if ((domain_atoms[domain_atoms.length - 1].length < 2) || (domain_atoms[domain_atoms.length - 1].length > 8)){

		highlight_error(id);

		return false;

	}

	
	undo_highlight_error(id);
	return true;

}




