function chkTextNull(evt){
	if ($(evt).val()=="") {
		//$("#dialog").append($(evt+'_LABEL').text()+'不可空白!!<br />');
		$(evt).addClass('focus');
		$(evt+'_ERRMSG').html('必要欄位不可空白!!');
		return false;
	}
	return true;
}
function chkTextNull2(evt){
	var err=false;
	$(evt).each(function(){
		if ($(this).val()+""=="") {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">必要欄位不可空白!! (Must input!)</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

function chkTextNull3(evt){
	var err=false;
	evt.each(function(){
		if ($(this).val()+""=="") {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">必要欄位不可空白!! (Must input!)</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

function chkSelectNull(evt){
	if (($(evt).val()=="")||$(evt)[0].selectedIndex == -1) {
		//$("#dialog").append($(evt+'_LABEL').text()+'不可空白!!<br />');
		$(evt).addClass('focus');
		$(evt+'_ERRMSG').html('必要選擇不可不選!! (Mandatory)');
		return false;
	}
	return true;
}

function chkSelectNull2(evt){
	var err=false;
	$(evt).each(function(){
		if ($(this).val()+""==""||$(this)[0].selectedIndex == -1) {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">必要選擇不可不選!! (Mandatory)</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

function chkAllSelected(evt){
	var tgt=$(evt);
	var chk=0;
	$(tgt).each(function(){
		//alert($(this)[0].selectedIndex);
		if ( $(this)[0].selectedIndex == -1 ) {
			//$("#dialog").append($(evt+'_LABEL').text()+'不可空白!!<br />');
			$(this).focus().parent().addClass('focus').append("<div class=\"errmsg\" name=\"分類ID_Ln_ERRMSG\">必要選擇不可不選!! (Mandatory)</div>");
			chk++;
		}
	});
	if (chk>0)
	{
		return false;
	}else{
		return true;
	}
}


function chkIsNum(evt)
{
	var regExp=/^[0-9]*$/;
	if (!regExp.test($(evt).val()))
	{
		$(evt).addClass('focus');
		$(evt+'_ERRMSG').html('必須輸入數字。 (Must be a number!)');
		return false;
	}
	return true;
}

function chkIsNum2(evt){
	var err=false;
	var regExp=/^[0-9]*$/;
	$(evt).each(function(){
		if (!regExp.test($(this).val())) {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">必須輸入數字。 (Must be a number!)</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

function chkIsNumButInput(evt) {
	if ($(evt).val()+""!="")
	{
		var regExp=/^[0-9]*$/;
		if (!regExp.test($(evt).val()))
		{
			$(evt).addClass('focus');
			$(evt+'_ERRMSG').html('必須輸入數字。 (Must be a number!)');
			return false;
		}
	}
	return true;
}
function chkIsNumButInput2(evt) {
	var err=false;
	var regExp=/^[0-9]*$/;
	$(evt).each(function(){
		if ($(this).val()+""!="")
		{
			if (!regExp.test($(this).val())) {
				$(this).addClass('focus');
				$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">必須輸入數字。 (Must be a number!)</div>").insertAfter(this);
				err=true;
			}
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

//檢查護照號碼
function chkIsPassportID(evt){
	var err=false;
	$(evt).each(function(){
		if (  ( ($(this).val()+"").length<5 ) || ( !/^\w*$/.test($(evt).val()) )  ) {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">請輸入有效的護照號碼!!</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

//檢查身分證字號
function chkIsNationalID(evt){
	var err=false;
	$(evt).each(function(){
		if (TWNationalIDCheck($(this).val()+"")==false) {
			$(this).addClass('focus');
			$("<div class=\"errmsg\" name=\"CUSTOM_ERRMSG\">請輸入有效的身份證字號!!</div>").insertAfter(this);
			err=true;
		}
	});
	if (err){
		return false;
	}else{
		return true;
	}
}

//檢查是否為正確的中華民國身分證字號
function TWNationalIDCheck(value) { 
    var a = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'W', 'Z', 'I', 'O'); 
    var b = new Array(1, 9, 8, 7, 6, 5, 4, 3, 2, 1); 
    var c = new Array(2);
    var d; 
    var e; 
    var f; 
    var g = 0; 
    var h = /^[a-z](1|2)\d{8}$/i; 
    if (value.search(h) == -1) { 
        return false; 
    } else { 
        d = value.charAt(0).toUpperCase(); 
        f = value.charAt(9); 
    } 
    for (var i = 0; i < 26; i++) { 
        if (d == a[i]){ //a==a 
            e = i + 10; //10 
            c[0] = Math.floor(e / 10); //1 
            c[1] = e - (c[0] * 10); //10-(1*10) 
            break; 
        } 
    } 
    for (var i = 0; i < b.length; i++) { 
        if (i < 2) { 
            g += c[i] * b[i]; 
        } else { 
            g += parseInt(value.charAt(i - 1)) * b[i]; 
        } 
    } 
    if ((g % 10) == f) { 
        return true; 
    } 
    if ((10 - (g % 10)) != f) { 
        return false; 
    } 
    return true; 
}

function PopUp(_url,_width,_height)
{
	window.open(_url,'_blank','height='+_height+', width='+_width+', resizable=0, status=0, toolbar=0 ,scrollbars=1, location=0,menubar=0,directories=0' );
}
function AddToMyFavorite(url, web_title) {
	//javascript:AddToMyFavorite('http://www.sysfeather.com', 'XX網站');
	window.external.addFavorite(url, web_title);
}


//Date validation
//**DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
// Declaring valid date character, minimum year and maximum year
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 strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date");
		return false;
	}
	return true;
}