/*function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
*/


function CheckEmail(field)
{
	if(field.value!='')
	{
	//	alert(field);
		var emailStr = new String();
		emailStr=field.value;
		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 seems incorrect (check @ and .'s)");
			field.focus();
			field.select();
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		if (user.match(userPat)==null) {
			alert("The username doesn't seem to be valid.");
			field.focus();
			field.select();

			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!");
					field.focus();
					field.select();
					return false;
					}
				}
			return true;
			}
		var domainArray=domain.match(domainPat);
		if (domainArray==null) 
			{
			alert("The domain name doesn't seem to be valid.");
			field.focus();
			field.select();
			return false;
			}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) 
			{
		   // the address must end in a two letter or three letter word.
			alert("The address must end in a three-letter domain, or two letter country.");
			field.focus();
			field.select();
			return false;
			}

		if (len<2) 
			{
			var errStr="This address is missing a hostname!";
			alert(errStr);
			field.focus();
			field.select();
			return false
			}
		return true;
	}

}//function CheckEmail - to check email address ends here.


function trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function chk()
	{ 
	   for(var i=0;i<frm1.elements.length;i++)
		{	 
          switch(frm1.elements[i].name)
			{
              case "name":
				    var result = trim(frm1.name.value);
                        if(result == '') 
						{
							alert('Please write your name');
							frm1.name.focus();
							return false;  
						}
						break;

              case "comment":                          
			            if(trim(frm1.comment.value)=='') 
						{
							alert('Please Enter page contents');
							frm1.comment.focus();
							return false;  
						}
			    		break;

              case "email":                          
			           if(!(CheckEmail(frm1.email)))
						{

							frm1.email.focus();
							return false;  
						}
						break;

			}
	       
		}
		
        return true;
	}		
