function digit(key_code)
{
	//only digit
	if (key_code < 48 || key_code > 57)
		return false;
	else
		return true;
}

function pnum(key_code)
{
	//only positive number
	if (key_code < 46 || key_code > 57)
		return false;
	else
		return true;
}

function ppint(key_code)
{
	//only positive integer and zero
	if (key_code < 47 || key_code > 57)
		event.returnValue =  false;
	else
		event.returnValue =  true;
}

function num(key_code)
{
	//only  number
	if (key_code < 45 || key_code > 57)
		return false;
	else
		return true;
}
function only_num(key_code, ascii_code)
{
	//only positive number
	if (key_code != ascii_code)
		event.returnValue =  false;
	else
		event.returnValue =  true;
}
function trim_string(str)
{
	while(''+str.charAt(0)==' ')
		str=str.substring(1,str.length);		//trim leading space

	while(''+str.charAt(str.length-1)==' ')
		str=str.substring(0,str.length-1);	//trim trailing space
	
	return str;
	
}

function trim_action(ctrl)
{
	while(''+ctrl.value.charAt(0)==' ')
		ctrl.value=ctrl.value.substring(1,ctrl.value.length);	//trim leading space
	while(''+ctrl.value.charAt(ctrl.value.length-1)==' ')
		ctrl.value=ctrl.value.substring(0,ctrl.value.length-1);	//trim trailing space
}


function is_num(ctrl)
{
	
	trim_action(ctrl);
	var str = ctrl.value;
	var re = /,/g;
	var r = str.replace(re, "");

	if	(isNaN(r) && (r !=''))
	{
		alert("You have to enter a valid number!")
		ctrl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function less_num(ctrl, int_num)
{
	
	trim_action(ctrl);
	var str = ctrl.value;

	if	(isNaN(str) && (str !=''))
	{
		alert("You have to enter a valid number!");
		ctrl.focus();
	}
	
	if ( parseFloat(str) > int_num )
	{
		alert("The number you entered should be less than " + int_num)
		ctrl.focus();
	}
}

function max_length(ctrl, len)
{
		
	var str = ctrl.value;
	var str2 =  trim_string(str);
	
	if (str2.length > len)
	{
		alert("The maxlength for this field is " + len + ".");
		ctrl.focus();
	}
}


//////////////////////////////////////////////////////////below is for email

function validEmail(email) {
	invalidChars = " /:,;"
	
	if (email == "") {						// cannot be empty
		return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
		return false
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
		return false
	}
	return true
}

function valid_email(ctrl)
{
	trim_action(ctrl);
	var strEmail = ctrl.value
	if ((! validEmail(strEmail)) && (strEmail != ''))
	{
		alert('You have to enter a valid email address!');
		ctrl.focus();
	}
}

////////////////////////////////////////////////////////////below is for date

function chk_date(str_date)
{
	var separator; //valid sparator is / and -
	
	if (str_date.indexOf("/",0) > -1)
	{
		separator = "/";
	}
	else 
	{	
		if (str_date.indexOf("-",0) > -1)
			separator = "-";
		else
			return false;
	}
	
	var date_array = str_date.split(separator, 3);
	//check if the elements are number
	var intMonth = parseInt(date_array[0], 10) ;
	if (isNaN(intMonth))
	{
		return false;
	}
	
	var intDay =  parseInt(date_array[1], 10);
	if (isNaN(intDay))
	{
		return false;
	}

	var intYear = parseInt(date_array[2], 10); 
	if (isNaN(intYear))
		return false;
	//valid year is 1950 - 2049 for short date
	if (intYear < 50)
	{
		intYear += 2000;
	}
	else
	{
		if (intYear <100)
			intYear += 1900;
	}

	//judge month and day

	if (intMonth>12 || intMonth<1)
		return false;

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) 
		return false;
	
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1)) 
		return false;

	if (intMonth == 2) 
	{
		if (intDay < 1) 
			return false;
		
		if (LeapYear(intYear) == true) 
		{
			if (intDay > 29) 
				return false;
		}
		else 
		{
			if (intDay > 28) 
				return false;
		}
	}
	return true;
}
	
function LeapYear(intYear)
{
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function check_date(ctrl)
{
	strDate = ctrl.value
	if (trim_string(strDate) != "")
	{
		if (chk_date(strDate) == false)
		{
			alert("You have to enter a valid date!");
			ctrl.focus();
		}

	}
}

