//------------------------------------------------------------------------------------------------------------------------------
function trimString(string) {
	if (string==null || string.length<1) return '';
	var start; start=0;
	var end; end = string.length-1;
	while ((start<=end) && (string.charAt(start)==" " || string.charCodeAt(start)==13 || string.charCodeAt(start)==10)) start++;
	while ((end>start) && (string.charAt(end)==" " || string.charCodeAt(end)==13 || string.charCodeAt(end)==10)) end--;
	if (start>end) return '';
	return string.slice(start,end+1);
}
function validNumberFormat(value) {
	var valid, code, i;
	valid=true;
	for (i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (!((code >= "0" && code <= "9")|| (code == ".") || (code == "-" && i==0))) { 
			valid=false; 
			break; 
		}
	}
	return valid;
}
function validIntFormat(value) {
	var valid, code, i;
	valid=true;
	for (i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (!((code >= "0" && code <= "9") || (code == "-"))) { 
			valid=false; 
			break; 
		}
	}
	return valid;
}

function validInteger(value) {
	if(!validIntFormat(value))return false;
	if(isNaN(parseInt(value)))return false;
	return true;
}
function validNumber(value) {
	if (!validNumberFormat(value)) return false;
	if (isNaN(parseFloat(value))==true) return false;
	return true;
}
function validDate(s){
	var i = 0;
	var d, m, y;
	var separate = "/";
		m = s.substr(0,s.indexOf(separate));
		s = s.substr(s.indexOf(separate) + 1);
		d = s.substr(0,s.indexOf(separate));
		y = s.substr(s.indexOf(separate) + 1);
	if (!validIntFormat(m) || isNaN(parseInt(m)) || m<1 || m>12) {return false;} 		
	if (!validIntFormat(y) || isNaN(parseInt(y)) || y<1900 || y.length>9999) {return false;} 
	if (!validIntFormat(d) || isNaN(parseInt(d)) || d<1 || d>31) { return false;} 	
	if (((m == 4)||(m == 6)||(m == 9)||(m == 11))&&(d > 30)){return false;}
	if (m == 2){
			if (y%4 == 0){
				if ((y%100 != 0)||(y%400 == 0)) {	if (d > 29) {return false; }	}
				else if (d > 28){ return false;	}
			}
			else {	if (d > 28){ return false;	}	}
	}
	return true;
}
//------------------------------------------------------------------------------------------------------------------------------
// type=0: field allow null
// type=1: field not allow null 
function IsNull (field,msg){
	if (trimString(field.value)=='') {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}
function IsNumber(type,field,msg) {
	if (type==0) 
		if (trimString(field.value)!='') 
			if (!validIntFormat(field.value) || isNaN(parseInt(field.value))) {
				alert(msg);
				field.focus();
				return false;
			}
	if (type==1)
		if (!validIntFormat(field.value) || isNaN(parseInt(field.value)) || trimString(field.value)=='') {
				alert(msg);
				field.focus();
				return false;
		}	
	return true;	
}

function IsNumeric(type,field,msg) {
	if (type==0) 
		if (trimString(field.value)!='') 
			if (!validNumberFormat(field.value) || isNaN(parseFloat(field.value))) {
				alert(msg);
				field.focus();
				return false;
			}
	if (type==1)
		if (!validNumberFormat(field.value) || isNaN(parseFloat(field.value)) || trimString(field.value)=='') {
				alert(msg);
				field.focus();
				return false;
		}	
	return true;	
}

function IsEmail(type,field,msg) {
	if (type==0) 
		if (trimString(field.value)!='') 
			if (!validEmail(field.value)) {
				alert(msg);
				field.focus();
				return false;
			}
	if (type==1)
		if (!validEmail(field.value) || trimString(field.value)=='') {
				alert(msg);
				field.focus();
				return false;
		}	
	return true;	
}
//check format mm/dd/yyyy
function IsDate(type,field,msg) {
	if (type==0) 
		if (trimString(field.value)!='') 
			if (!validDate(field.value)) {
				alert(msg);
				field.focus();
				return false;
			}
	if (type==1)
		if (!validDate(field.value) || trimString(field.value)=='') {
				alert(msg);
				field.focus();
				return false;
		}	
	return true;	
}


function validEmail(value) {
	/*
		Email Address's Format: username@subdomain.domain
		Email Address must be include 3 part:
			part 1: username
			part 2: @
			part 3: <domainname[.domainname,...]>.<domainname>
	*/
	if (value.indexOf(" ")>=0)		return false;

	var state, code, username, domain, amountOfDot, i;
	state = 1; username=''; domain=''; amountOfDot = 0;
	for (i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (state==1) {
			if (	code == "<" || code == ">" 
					|| code == "(" || code == ")"	) return false;
			else if (	code == "@"	)
				if (username == '') return false;
				else state = 3;
			username += code;
		}
		else if (state==3) {
			if (	(code >= "0" && code <= "9")
					|| (code >= "A" && code <= "Z")
					|| (code >= "a" && code <= "z")
					|| code == "_"
					|| code == "-"
				) ;
			else if (code == ".")
				if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
				else amountOfDot++;
			else return false;
			domain += code;
		}
	}
	if (state != 3) return false;
	if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
	if (amountOfDot <1) return false;
	return true;
}