function trim (str) 
{
  str = str.replace(/^\s+/, '');
  for (var i = str.length - 1; i >= 0; i--)
  {
    if (/\S/.test(str.charAt(i)))
    {
      str = str.substring(0, i + 1);
      break;
    }
  }
  return str;
}
function isvalidemail(emailStr) {


var emailPat=/^(.+)@(.+)$/


var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var specialChars1="\\(\\)<>@_,;:\\\\\\\"\\.\\[\\]"

/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
var validChars1="\[^\\s" + specialChars1 + "\]"


var quotedUser="(\"[^\"]*\")"


var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/


var atom=validChars + '+'
var atom1=validChars1 + '+'


var word="(" + atom + "|" + quotedUser + ")"

// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom1 + "(\\." + atom1 +")*$")


var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
 
	return false
}

var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	   //     alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    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.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid! 
return true;
}

function validate(form)
{
  if(trim(form.name.value)=="")
  {
    alert("Please enter your name.")
    form.name.focus();
    return false;
  }
  if(trim(form.email.value)=="" && trim(form.phone.value)=="")
  {
    alert("Please enter your phone number or email address");
    form.email.focus();
    return false;
  }
  if(trim(form.email.value)!="" && !isvalidemail(form.email.value))
  {
    alert("Please enter a valid email address");
    form.email.focus();
    return false;
  }
  if(!form.agree_to_terms.checked)
  {
    alert("You must agree to our privacy policy to continue");
    form.agree_to_terms.focus();
    return false;
  }
  return true;
}
