﻿/*Determine the number of bytes from english character and chinese character*/
function verify(str, val){
	var LEN = str.length;
	var STR_LEN = 0;
	for(var i=0; i<LEN; i++){
		if(str.charCodeAt(i)>127){
			STR_LEN += 2;
		}else{
			STR_LEN ++;
		}		
	}
	if(STR_LEN>val){
		return false;
	}else{
		return true;
	}
}

/*Verify email format*/
function checkmail(str){
	var j=0;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1)j++;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)j++;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)j++;
	if (str.indexOf(at,(lat+1))!=-1)j++;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)j++;
	if (str.indexOf(dot,(lat+2))==-1)j++;
	if (str.indexOf(" ")!=-1)j++;
	if(j>0){return false;}else{return true;}
}

/*check date style , style yyyy-mm-dd*/
function chkdate(cdate,cstring){
	nowdate = new Date();
	data = cdate.match(/(\d{4})\-(\d{2})\-(\d{2})/);

	if(data==null || !cdate) {alert(cstring); return false;}
	if((RegExp.$1 < 1901) ){
		alert(cstring); return false;
	}
	if((RegExp.$2 > 12) || (RegExp.$2 < 1)){
		alert(cstring); return false;
	}
	if((RegExp.$2 == 1) || (RegExp.$2 == 3) || (RegExp.$2 == 5) || (RegExp.$2 == 7) || (RegExp.$2 == 8) || (RegExp.$2 == 10) || (RegExp.$2 == 12)){
		if((RegExp.$3 > 31) || (RegExp.$2 < 1)){
			alert(cstring); return false;
		}
	}else if(RegExp.$2 == 2){
		if((RegExp.$1 % 4 !=0)){
			if((RegExp.$3 > 28) || (RegExp.$3 < 1)){
				alert(cstring); return false;
			}
		}else{
			if((RegExp.$3 > 29) || (RegExp.$3 < 1)){
				alert(cstring); return false;
			}
		}
	}else{
		if((RegExp.$3 > 30) || (RegExp.$3 < 1)){
			alert(cstring); return false;
		}
	}
	return true;

}
//確認結束日比開始日期大
function chkdatesize(odate,cdate,cstring){
	date1 = odate.match(/(\d{4})\-(\d{2})\-(\d{2})/);
	var opendate = new Date(RegExp.$1,RegExp.$2,RegExp.$3);

	date2 = cdate.match(/(\d{4})\-(\d{2})\-(\d{2})/);
	var closedate = new Date(RegExp.$1,RegExp.$2,RegExp.$3);
	
	if(closedate < opendate){
		alert(cstring);
		return false;
	}
}
//open new window.
function openwin(url,cstring){
	window.open(url,cstring,'width=400,height=400,scrollbars=1');
	return true;
}

//檢查空白字
function space_str(str){
	var LEN = str.length;
	var STR_LEN = 0;
	for(var i=0; i<LEN; i++){
		if(str.charCodeAt(i)==32){
			STR_LEN ++;
		}		
	}
	
	if(STR_LEN==LEN){
		return false;
	}else{
		return true;
	}
}

function float_num(str){
	var LEN = str.length;
	var STR_LEN = 0, check_val = 0;
	for(var i=0; i<LEN; i++){
		if(i==0 || i==LEN-1){
			if( str.charCodeAt(i)<48 || str.charCodeAt(i)>57 ){return false;}
		}

		if(str.charCodeAt(i)==46){check_val++;}

		if( (str.charCodeAt(i)>47 && str.charCodeAt(i)<58) || str.charCodeAt(i)==46 ){
			STR_LEN++;
		}
		

	}
	if(check_val>1){return false;}
	if(STR_LEN!=LEN){return false;}
	return true;
}

/*check if it's valid number*/
function verify_number(str){
	var LEN = str.length;
	var valid_num = 0;
	for(var i=0; i<LEN; i++){
		if(str.charCodeAt(i)<48 || str.charCodeAt(i)>57){
			valid_num++;
		}	
	}
	if(valid_num>0){return false;}else{return true;}
}



