function Trimming(str)
		{
				
			while(str.charAt(0)==' ')
				str=str.substring(1,str.length);
			while(str.charAt(str.length-1)==' ')
				str=str.substring(0,str.length-1);
			
			return str;
		}
		
//*****************Phone validation ****************************
		
	function validatePhone(obj,evt)
	{
			
		if(evt.type == "keyup")
		{
					if(navigator.appName != "Netscape") 
					{
						keypress=(window.Event) ? evt.which : evt.keyCode;
						strValue=Trimming(obj.value);
						//alert(keypress);
						if(keypress == 9 || keypress == 16)
							return;
						if(strValue.length >0 )
						{	
							/* keypress -> 	48 to 57 for numbers
											96 to 105 numpad numbers
											189 for "-" and 109 for numpad "-"
											8 for backspace
											9 for Shift+Tab, 
											37 to 40 for arrow keys
											46 is for delete
											32 is for space
												
							*/
							if((keypress>=48 && keypress<=57)||(keypress>=96 && keypress<=105) ||(keypress>=37 && keypress<=40) ||keypress==189 ||keypress==109 ||keypress==46 ||keypress==32 || keypress == 0 || keypress == 8 || keypress == 9 || keypress==16)
							{
								//to remove ( in the string
								expr=/\(/gi;
								strValue=strValue.replace(expr,"");
								//to remove ')'
								expr=/\)/gi;
								strValue=strValue.replace(expr,"");
								//to remove '-'
								expr=/-/gi;
								strValue=strValue.replace(expr,"");
								// to remove space
								expr=/ /gi;
								strValue=strValue.replace(expr,"");
													
								if(strValue.charAt(0) != '(')
									strValue="("+strValue;
								if(strValue.length >= 4)
									{
										if(strValue.charAt(4) != ')')
											strValue=strValue.substring(0,4)+")"+strValue.substring(4,strValue.length)
									}
								if(strValue.length >= 8)
									{
										if(strValue.charAt(8) != '-')
											strValue=strValue.substring(0,8)+"-"+strValue.substring(8,strValue.length)				
									}	
								if((keypress>=37 && keypress<=40)||keypress==46||keypress==32 ||keypress==8)// this if is for arrow keys
								{
								}
								else
									obj.value=strValue;	
							}
							else
							{
								obj.value=clearChars(strValue);
								//obj.value=strValue.substring(0,strValue.length-1);
								
							}
						}
					}
			
		}
		else if(evt.type =="blur")
		{
			strValue=Trimming(obj.value);
				// this if is for null checking
				if(strValue.length==0)
				{
					return false;
				}
				//to remove ( in the string
				expr=/\(/gi;
				strValue=strValue.replace(expr,"");
				//to remove ')'
				expr=/\)/gi;
				strValue=strValue.replace(expr,"");
				//to remove '-'
				expr=/-/gi;
				strValue=strValue.replace(expr,"");
				// to remove space
				expr=/ /gi;
				strValue=strValue.replace(expr,"");
				//alert(parseInt(strValue,10));
				if(isNaN(strValue))
				{
					alert("Please enter a valid phone number");
					//obj.value="";
					obj.focus();
					return false;
				}
		
				if(strValue.length >=10)
				{
					if(strValue.charAt(0) != '(')
						strValue="("+strValue;
					if(strValue.length > 4)
					{
						if(strValue.charAt(4) != ')')
							strValue=strValue.substring(0,4)+")"+strValue.substring(4,strValue.length)
					}
					if(strValue.length > 8)
					{
						if(strValue.charAt(8) != '-')
							strValue=strValue.substring(0,8)+"-"+strValue.substring(8,strValue.length)				
					}	
						obj.value=strValue.substring(0,13);	
						if(strValue.length <13)
							{
								alert("Incomplete Phone format");
								obj.focus();
							}
						
				}		
				else
				{
					alert("Please enter phone number in one of the formats (999)999-9999 or 9999999999 or 999 999 9999");
					//obj.value="";
					obj.focus();
				}
			
		}
	}	



//****************************************************************
// Function for zip code
//	function checkForm()
//	{
	
//	if (document.frmmain.stranswer.value == document.frmmain.strconfirmanswer.value) {
//		alert("Please enter correct confirm password");
//		document.frmmain.strconfirmanswer.value == " "
//		document.frmmain.strconfirmanser.focus();
//		return false;
//	}
//		return true;
//	}
		

//****************************************************************
// Function for zip code
	function validateZip(obj,evt,Cobj)
	{
	
	if(Cobj==null)
		Cobj="USA";
	
	if (Cobj=="USA"||Cobj.value=="USA") 
	{
		strValue=Trimming(obj.value);
		
		if(evt.type == "keyup")
		{
			if(navigator.appName != "Netscape") 
			{
				keypress=(window.Event) ? evt.which : evt.keyCode;
				
				if(keypress ==9 || keypress == 16)
					return;
				
				if((keypress>=48 && keypress<=57)||(keypress>=96 && keypress<=105) ||(keypress>=37 && keypress<=40) ||keypress==189 ||keypress==109 ||keypress==46 ||keypress==32 || keypress == 0 || keypress == 8 || keypress == 9)
				{
					
					expr=/-/gi;
					
				
					strValue=strValue.replace(expr,"");
					
					// to remove space
					
					expr=/ /gi;
					
				
					strValue=strValue.replace(expr,"");
					if(strValue.length >= 5)
					{
						if(strValue.charAt(5) != '-')
							strValue=strValue.substring(0,5)+"-"+strValue.substring(5,strValue.length)
					}
					
					
					if((keypress>=37 && keypress<=40)||keypress==46||keypress==32 ||keypress==8)// this if is for arrow keys
					{
					}
					else
						obj.value=strValue;	
				}	
				else
				{
					//obj.value=strValue.substring(0,strValue.length-1);
					
					//vishy
					obj.value=clearChars(strValue);
				}
				
			}	
			
		
			
		}
		else if(evt.type =="blur")
		{
			expr=/-/gi;
			
			
			
			strValue=strValue.replace(expr,"");
			
			
			// to remove space
			
			expr=/ /gi;
			
			//vishy
			strValue=strValue.replace(expr,"");	
			
			if(strValue.length==0)
			{
				return false;
			}
			
			
			if(isNaN(strValue))
			{
				alert("Please enter a valid Zip");
				//obj.value="";
				obj.focus();
				return false;
			}
			
			
			if(strValue.length ==5 ||strValue.length >=9)
			{
				
				if(strValue.length >=9)
				{
					strValue=strValue.substring(0,5)+"-"+strValue.substring(5,9);
					strValue=strValue.substring(0,10);
				}
				else
				
				obj.value=strValue;
			}	
			else
			{
				alert("Please enter zip in one of the formats 99999-9999 or 99999");
				obj.focus();
				
			}
		
		}
	
	
	}
	/* DON'T REQUIRE THE VALIDATION FOR ANY COUNTRY OTHER THAN USA
	else
	
	{
	
		strValue=Trimming(obj.value);
		
		if(evt.type == "keyup")
		{
			if(navigator.appName != "Netscape") 
			{
				keypress=(window.Event) ? evt.which : evt.keyCode;
				
				if(keypress ==9 || keypress == 16)
					return;
			
			}	
		}

		else if(evt.type =="blur")

		{
			
			if(strValue.length==0)
			{
				return false;
			}
			
			if(strValue.length ==3 ||strValue.length >=6)
			{
				
				
				if(strValue.length >=6)
				{
					strValue=strValue.substring(0,3)+" "+strValue.substring(3,6);
					strValue=strValue.substring(0,7);
				}
				
				obj.value=strValue;
			}	
			
			else
			
			{
				alert("Please enter zip in one of the formats 9AA AA9");
				obj.focus();
				
			}
		
		}
	
	}
	*/
	
	}
	
//**************************************************************************	
// this function is for email validation

	function validateEmail(obj)
	{
		
		var ArEmails=  Trimming(obj.value).toLowerCase().split(";");
		for(CountNo=0;CountNo<ArEmails.length;CountNo++)
		{
			emailStr=ArEmails[CountNo];
						
			if(emailStr=="")
			{
				return;
			}	
			
			var checkTLD=1;

			var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

			var emailPat=/^(.+)@(.+)$/;

			var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

			var validChars="\[^\\s" + specialChars + "\]";

			var quotedUser="(\"[^\"]*\")";

			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

			var atom=validChars + '+';

			var word="(" + atom + "|" + quotedUser + ")";

			var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

			var matchArray=emailStr.match(emailPat);

			if (matchArray==null) {

			alert("Email address incorrect (check @ and .'s)");
			obj.focus();
			return false;
			}
			
			var user=matchArray[1];
			var domain=matchArray[2];

			for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			obj.focus();
			return false;
			   }
			}
			for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			obj.focus();
			return false;
			   }
			}
			
			if (user.match(userPat)==null) {
			//alert("Invalid Username.");
			alert("Seperate Mails Id's with Semicolon.");
			obj.focus();
			return false;
			}
			
			var IPArray=domain.match(ipDomainPat);
			if (IPArray!=null) {

			for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert("Destination IP address is invalid!");
			obj.focus();
			return false;
			   }
			}
			return true;
			}
			
			var atomPat=new RegExp("^" + atom + "$");
			var domArr=domain.split(".");
			var len=domArr.length;
			for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
			alert("The domain name is invalid.");
			obj.focus();
			return false;
			   }
			}
		
			if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			alert("The address must end in a well-known domain or two letter " + "country.");
			obj.focus();
			return false;
			}
			
			// Make sure there's a host name preceding the domain.

			if (len<2) {
			alert("This address is missing a hostname!");
			obj.focus();
			return false;
			}
			
			// If we've gotten this far, everything's valid!
		  }	
			return true;
		
	}
	
//************************************************************************	
	function numb(obj,evt)
	{
		strValue=obj.value;
		if(evt.type == "keyup")
		{
			if(navigator.appName != "Netscape") 
			{
				keypress=(window.Event) ? evt.which : evt.keyCode;
				if(keypress ==9 || keypress == 16 || keypress == 0 || keypress == 35|| keypress == 36)
					return;
								
				if((keypress>=48 && keypress<=57)||(keypress>=96 && keypress<=105) ||(keypress>=37 && keypress<=40) ||keypress==46 ||keypress==190 || keypress == 0 || keypress == 8 || keypress == 9)
				{
				}
				else
				{
					//obj.value=strValue.substring(0,strValue.length-1);
					// for clearing the chars 
					obj.value=clearCharsNumb(strValue)
				}	
			}	
			
		}
		else if(evt.type =="blur")
		{
			if(isNaN(strValue))
			{
				alert("please enter a numeric value");
				obj.focus();
			}
			
		}
	
	
	}
	function clearCharsNumb(str)
	{
		for(i=0;i<str.length;i++)
		{
			code=str.charCodeAt(i);
			if((code>=48 && code<=57)|| code == 46)
			{
			}
			else
				str=str.substring(0,i)+""+str.substring(i+1,str.length);
				
		}
		return str;
	}
	function clearChars(str)
	{
		for(i=0;i<str.length;i++)
		{
			code=str.charCodeAt(i);
			if((code>=48 && code<=57) ||code == 40 || code == 41 || code== 45)
			{
			}
			else
				str=str.substring(0,i)+""+str.substring(i+1,str.length)
		}
		return str;
	}


// Function for url validation
//******************************************************************
function validateURL(obj)
{
	str=Trimming(obj.value);
	if(str != "")
	{
		Hstr=str.substring(0,7);	
		Wstr=str.substring(0,4);
		if((Hstr.toLowerCase()!= "http://") && (Wstr.toLowerCase() != "www."))
		{
			str="www."+str;
		}
		if(str.indexOf(".",4) == -1)
		{
			alert("Please enter a valid URL");
			
			obj.focus();
			return false;
		}
		
		obj.value=str;
		
			
	}
}
