function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function validate_form(myForm, fields, names, types)
{
	var i=0;
	var valid=true;
	//alert (fields.length);
	var fieldType;
	var fieldVal;
	while(valid && i<fields.length)
	{
		fieldType = typeof document.getElementById(fields[i]).value;
		fieldVal = trim(document.getElementById(fields[i]).value);
		//alert(typeof document.getElementById(fields[i]).value);
		//alert(Number(fieldVal));
		//If there is no value in a field, report it to the user.
		if (document.getElementById(fields[i])==null || fieldVal=='')
		{
			alert('You did not fill out the field: '+names[i]+'\nPlease fill out this form before submitting.');
			//valid=false;
			return false;
		}
		//If the user did not enter the correct TYPE of value, let them know!!
		//NOTE: if you want the types to be ignored (maybe you don't know or care) you can fill in 'N/A'
		else 
		{
			if (names[i]=='Email' || names[i]=='Email address' || names[i]=='email_address')
			{
				valid = email_address_validator(fieldVal.toLowerCase());
				if (valid==false) alert('You did not enter a valid email address. Please try again.');
			}
			else if (types[i]=='Integer')
			{
				if (isNaN(Number(fieldVal)))
				{
					alert('The field \''+names[i]+'\' was filled in incorrectly as: \''+fieldVal+'\'\nPlease fill it in as: \''+types[i]+'\'');
					//valid=false;
					return false;
				}
			}
		}

		i++;
	}
	//If valid is still true, it means the the above loop exited because all of the fields were OK.
	//In this case, submit the form!
	/*if (valid==true)
		myForm.submit();*/
	return valid;
}


function email_address_validator(emailaddress)
{
	/*regular expression for matching email addresses found at: http://www.regular-expressions.info/email.html*/
	/*could replace the ([A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name) with an actual list of ALL valid TLD's,
	and then just reference it here (if you can do that with regexp's in javascript)*/
	var email_pattern = /^[a-z0-9._%-]+@[a-z0-9-.]+\.([a-z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$/
	emailaddress = emailaddress.toLowerCase();
	if (emailaddress.match(email_pattern))
	{
		return true;
	}
	return false;
}

/** email address validator. found at: http://www.smartwebby.com/DHTML/email_validation.asp*/
/*function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }*/