//updated 20081009
function js_isEmpty(obj,msg){
	var valstr = obj.value + "";
	valstr = js_trim(valstr);
	if (valstr.length == 0){
		type = 2;
		title = 'Validation';
		content = 'Please fill in the '+msg+'.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);
		js_changeObjColor(obj, 0);
		alert("Please fill in the "+msg+".");
		obj.focus();
		return true;
	}else{
		js_changeObjColor(obj, 1);
		return false;
	}
}

function js_isEmptyChi (obj, msg) {
	alert('ok');
	var valstr = obj.value + "";
	valstr = js_trim(valstr);
	if (valstr.length == 0){
		type = 2;
		title = 'Validation';
		content = msg+'必須填寫<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);
		js_changeObjColor(obj, 0);
		alert (msg+'必須填寫');
		obj.focus();
		return true;
	}else{
		js_changeObjColor(obj, 1);
		return false;
	}
}

function js_isChkpw(obj){
	var valstr = obj.value + "";
	valstr = js_trim(valstr);
	if (valstr.length<5 || valstr.length>15){
		type = 2;
		title = 'Validation';
		content = 'Please enter a 5 ~ 15 digit password.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);
		js_changeObjColor(obj, 0);
		alert("Please enter a 5 ~ 15 digit password.");
		obj.focus();
		return true;
	}else{
		js_changeObjColor(obj, 1);
		return false;
	}
}

function js_isConpw(obj1,obj2){
	var valstr1 = obj1.value + "";
	var valstr2 = obj2.value + "";
	valstr1 = js_trim(valstr1);
	valstr2 = js_trim(valstr2);
	if (valstr1 != valstr2){
		type = 2;
		title = 'Validation';
		content = 'Please verify your password.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);
		js_changeObjColor(obj2, 0);
		alert("Please verify your password.");
		obj2.focus();
		return true;
	}else{
		js_changeObjColor(obj2, 1);
		return false;
	}
}


function js_trim(stringToTrim){
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function js_chkNumber(obj){

	if (obj.value != ''){

		if (isNaN(obj.value)){
			obj.focus();
			obj.value = '';


			type = 2;
			title = 'Validation';
			content = 'Must be a numeral.<br>';
			content = content + '<a onClick=\'hidebox();return false\'>';
			content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';

			//showbox(type, title, content);
			js_changeObjColor(obj, 0);
			alert("Must be a numeral.");

			return false;

		}

		js_changeObjColor(obj, 1)
		return true;

	}
}

function js_isEmail(obj){
  var valstr = obj.value+"";
  valstr = js_trim(valstr);
  if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valstr))){
  	type = 2;
	title = 'Validation';
	content = 'Email address incorrect.<br>';
	content = content + '<a onClick=\'hidebox();return false\'>';
	content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
	//showbox(type, title, content);
	js_changeObjColor(obj, 0);
	alert("Email address incorrect.");
	obj.focus();
  	return true;
  }else{
	js_changeObjColor(obj, 1);
	return false;
  }
}

function js_isEmailChi(obj){
  var valstr = obj.value+"";
  valstr = js_trim(valstr);
  if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valstr))){
  	type = 2;
	title = 'Validation';
	content = '電郵格式不正確<br>';
	content = content + '<a onClick=\'hidebox();return false\'>';
	content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
	//showbox(type, title, content);
	js_changeObjColor(obj, 0);
	alert("電郵格式不正確");
	obj.focus();
  	return true;
  }else{
	js_changeObjColor(obj, 1);
	return false;
  }
}

function js_changeObjColor(jObj, xNormal){
	if (xNormal){
		//jObj.style.backgroundColor = '#FFFFFF';
	}else{
		//jObj.style.backgroundColor = '#FFFFCC';
	}
}


// Declaring valid date character, minimum year and maximum year // Start
var dtCh= "-";
var minYear=1900;
var maxYear=2100;


function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}


function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)

	strYr=strYear

	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : yyyy-mm-dd")

		type = 2;
		title = 'Validation';
		content = 'The date format should be : yyyy-mm-dd.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);

		return false
	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)

		type = 2;
		title = 'Validation';
		content = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
		content = content + '<br><a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);


		return false
	}

	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")

		type = 2;
		title = 'Validation';
		content = 'Please enter a valid month.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);

		return false
	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")

		type = 2;
		title = 'Validation';
		content = 'Please enter a valid day.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);

		return false
	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")

		type = 2;
		title = 'Validation';
		content = 'Please enter a valid date.<br>';
		content = content + '<a onClick=\'hidebox();return false\'>';
		content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
		//showbox(type, title, content);

		return false
	}

	return true
}


function js_isDate(obj){

	if (isDate(obj.value)==false){
		obj.focus()
		js_changeObjColor(obj, 0);

		return false
	}

	js_changeObjColor(obj, 1);
	return true

}
// Declaring valid date character, minimum year and maximum year // End


function js_chkHour(obj){

	if (js_chkNumber(obj)){

		if (obj.value > 23){

			type = 2;
			title = 'Validation';
			content = "Please enter a valid 2 digit year between 0 and 23<br>";
			content = content + '<a onClick=\'hidebox();return false\'>';
			content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
			//showbox(type, title, content);
			js_changeObjColor(obj, 0);
			alert("Please enter a valid 2 digit year between 0 and 23");

			return false;

		}

		return true;

	}

	return false;

}


function js_chkMinute(obj){

	if (js_chkNumber(obj)){

		if (obj.value > 59){

			type = 2;
			title = 'Validation';
			content = "Please enter a valid 2 digit year between 0 and 59<br>";
			content = content + '<a onClick=\'hidebox();return false\'>';
			content = content + '<font color=\'#CC0000\' style=\'cursor:hand\'>OK</font></a>';
			//showbox(type, title, content);
			js_changeObjColor(obj, 0);
			alert("Please enter a valid 2 digit year between 0 and 59");

			return false;

		}

		return true;

	}

	return false;

}


function js_chkfiletype(obj, type){

	filedata = obj.value;

	if (filedata.length > 4){
		begin = filedata.length - 4
		filetype = filedata.substr(begin, 4).toLowerCase()

	}else{
		js_changeObjColor(obj, 0)
		alert("file incorrect.")
		return false

	}

	if (filetype==type){
		js_changeObjColor(obj, 1)
		return true

	}else{
		js_changeObjColor(obj, 0)
		alert("file incorrect.")
		return false

	}

}
