function trim(str)
{
	// this is the most efficient trim fuction taken from site to remove the begining and trailing space.
	while(''+ str.charAt(str.length-1)==' ') str = str.substring(0,str.length-1);
	while(''+ str.charAt(0)==' ') str = str.substring(1,str.length);
	return str
}	

function textAreaLength(field,maxlimit)
{
	// function to restrict character to specified field length in text area
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);
	}	
}

//This email valdation is perfect in all respect ,get it for future reference
function eMailCheck(str) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))
		return true
	else
	{
		alert("Invalid E-mail address !")
		return false
	}	
}

function check_url(addr) 
{
	//validation for url, it can't have spaces 
	var address=addr.value;
	
	if (address.indexOf ('http://')!=-1) 
	{
		alert("You should not enter <<http://>> here ");		
		addr.focus;
		addr.select();
		return 1;
	}
	var valid = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-/&=%?";
	var ok;
	for (var j=0; j<addr.value.length; j++)
	{
		temp = "" + addr.value.substring(j, j+1);
		if (valid.indexOf(temp) == "-1") 
			{
			alert(temp)
			ok = "no";
			}
	}
	if (ok == "no") 
	{
		alert("Enter a valid URL");
		addr.focus();
		addr.select();
		return 1;	
	}	
	var ok=0;
	//condition for checking http
	

	if ((address == "")  || (address.indexOf ('.') == -1))
		ok=1;
	var dotpos=address.indexOf ('.');

	if (dotpos==0) ok=1;
	if (dotpos>0)
	{
		var afterdotpos=dotpos+1
		var beforedotpos=dotpos-1
		var afterdotval=address.substring(afterdotpos, afterdotpos+1);
		var beforedotval=address.substring(beforedotpos, beforedotpos+1);
		if ((afterdotval.length ==0)||(afterdotval==" ")||(beforedotval==" "))
			ok=1;
	}
	if (ok==0)
		return 0;
	else
	{
		alert("Enter a valid URL");
		addr.focus;
		addr.select();
		return 1
	}	
}

function compDate(fdate, tdate)	
	{	
	if ((fdate.value !="") && (tdate.value !="" ))
			{
	    
		    var arrayfromdt=new Array();
			val=fdate.value;
			arrayfromdt=val.split("/")
			frommonth=parseInt(arrayfromdt[0],10);
			fromdate=parseInt(arrayfromdt[1],10);
			fromyear=parseInt(arrayfromdt[2]);
			//alert("fromyear:"+ fromyear)
			//alert("frommonth:"+ frommonth)
			//alert("fromdate:"+ fromdate)
			
			var arraytodt=new Array();
			val=tdate.value;
			arraytodt=val.split("/")
			tomonth=parseInt(arraytodt[0],10);
			todate=parseInt(arraytodt[1],10);
			toyear=parseInt(arraytodt[2]);
			
			// Month in java script always takes from 0 to 11
			
			
		// splitting the date from Text Box
			if (fromyear>toyear)  // first validation		
			{
				alert("year cannot be greater than " + fromyear);
				fdate.select();
				fdate.focus;
				return false;
			}
			if (frommonth>tomonth)  // second validation  selected date:31/05/02  todaterent: 5/4/01
			{
				if (fromyear>=toyear)
					{
					alert(fromtoDate)
					fdate.select();
					fdate.focus();
					return false;
					}
			}
			if (frommonth<=tomonth)   	
			{
				if (fromyear>toyear) //Third vaidation selected date:31/03/02  current:5/4/01

					{
					alert(fromtoDate)
					fdate.select();
					fdate.focus();
					return false;
					}
			}		
			if (fromdate<=todate)  //Fourth validation selected date:02/06/02 current:5/4/01 
			{
				if (frommonth>tomonth)
					{
					if (fromyear>=toyear)
						{
						alert(fromtoDate)
						fdate.select();
						fdate.focus();
						return false;
						}	
					}
			    else if (frommonth<=tomonth)//Fifth validation selected date:02/03/02 cur:5/4/01	
					{
					if (fromyear>toyear)
						{
						alert(fromtoDate)
						fdate.select();
						fdate.focus();
						return false;
						}
					}
			}
			if (fromdate>todate)// 6 th validation selected date:06/06/02	 todaterent:5/4/01
			{
				if (frommonth>tomonth)
					{
					if (fromyear>=toyear)
						{
						alert(fromtoDate)
						fdate.select();
						fdate.focus();
						return false;
						}	
					}
				else if (frommonth<tomonth)// 7 th validation select date:06/02/02 cur:5/4/01
					{
					if (fromyear>toyear)
						{
						alert(fromtoDate)
						fdate.select();
						fdate.focus();
						return false;
						}
					}
				else
					{
				 if (fromyear>=toyear)//8 th validation select date:06/04/01 cur:5/4/1
						{
						alert(fromtoDate)
						fdate.select();
						fdate.focus();
						return false;
						}
					}	
			}	
		}	
	}	
	

