function ltrim(str)
{
	while(str.length > 0){
		if(str.substr(0, 1) == " ")
			str = str.substr(1);
		else break;
	}

	return str;
}

function rtrim(str)
{
	while(str.length > 0){
		if(str.substr(str.length - 1, 1) == " ")
			str = str.substr(0, str.length - 1);
		else break;
	}

	return str;
}

function trim(str)
{
	return rtrim(ltrim(str));
}		


//This function checks validity of date for DD/MM/YYYY format...

function valDate(source)
{

	var datestr;
	datestr = source;
	
	if(datestr.length != 10)
	{
		alert("Enter date in DD/MM/YYYY format only...");
		//document.frmreg.txtbdate.focus();
		return false;
	}

	// Check whether there is any non-digit value
	for(var i = 0; i < datestr.length; i++)
	{
		digit = datestr.substring(i, i+1);
		if( (digit < "0" || digit > "9") && digit != "/")
		{
			alert("Enter either digits or / only...");
			//document.frmreg.txtbdate.focus();
			return false;
		}
	}
	
	//alert("datestr is : " + datestr);
	//alert("first char is : " + datestr.charAt(0));
	
	if(datestr.charAt(2) != '/' || datestr.charAt(5) != '/')
	{
		alert("Enter date in DD/MM/YYYY format only...");
		return false;
	}
	
	var strday = datestr.substr(0, 2);
	var day = parseInt(strday, 10);

	var strmonth = datestr.substr(3, 2);
	var month = parseInt(strmonth, 10);

	var stryear = datestr.substr(6, 4);
	var year = parseInt(stryear, 10);
	
	if(month < 1 || month > 12)
	{
		alert("Enter Month Between 1 and 12");
		//document.frmreg.txtbdate.focus();
		return false;
	}

	if(day < 1 || day > 31)
	{
		alert("Enter Day Between 1 and 31");
		//document.frmreg.txtbdate.focus();
		return false;
	}

	if(month == 2)
	{
		if(day > 29)
		{
			alert("There are not more than 29 days in February");
			//document.frmreg.txtbdate.focus();
			return false;
		}
		else
		{
			if(day == 29)
			{
				var div4 = year % 4;
				var div100 = year % 100;
				var div400 = year % 400;

				if(div4 != 0)
				{
					alert("This is not a leap year - Days Can't increase 28");
					//document.frmreg.txtbdate.focus();
					return false;
				}

				if( (div100 == 0) && (div400 != 0) )
				{
					alert("This is not a leap year - Days Can't increase 28");
					//document.frmreg.txtbdate.focus();
					return false;
				}
			}
		}
	}
	else
	{
		if(month == 4 || month == 6 || month == 9 || month == 11)
		{
			if(day > 30)
			{
				alert("There are not more than 30 days in this month");
				//document.frmreg.txtbdate.focus();
				return false;
			}
		}
	}
	
}
