	/*
	****************************************************
	Author : BDille
	Date : 5/16/2003
	Version 1.1
	******************************************************
	*/

//************************************************************
//Form Rules and Validation - step 2, requirements and finals
//************************************************************
function ValidateForm(){	
		var ErrorMessage="";
		var strCurrentChar;
		var selectedItem = document.frmcontact.State.selectedIndex;
		var selectedValue = document.frmcontact.State.options[selectedItem].value;
		var selectedText = document.frmcontact.State.options[selectedItem].text;

	// fields
	if( document.frmcontact.Name.value==""){
		ErrorMessage=ErrorMessage+"Name is required.\n"; 
	}	
	if(document.frmcontact.FinancialInst.value==""){
		ErrorMessage=ErrorMessage+"Name of Financial Institution is required.\n"; 
	}
	if( document.frmcontact.BranchCount.value==""){
		ErrorMessage=ErrorMessage+"Number of Branches is required.\n"; 
	}
	if( document.frmcontact.email.value==""){
		ErrorMessage=ErrorMessage+"Your email address is required.\n"; 
	}
	if( document.frmcontact.City.value==""){
		ErrorMessage=ErrorMessage+"City is required.\n"; 
	}
	if( selectedValue==""){
		ErrorMessage=ErrorMessage+"State is required.\n"; 
	}
	if( document.frmcontact.PostalCode.value==""){
		ErrorMessage=ErrorMessage+"PostalCode is required.\n"; 
	}
	if( document.frmcontact.Phone.value==""){
		ErrorMessage=ErrorMessage+"Phone is required.\n"; 
	}	

	var strGenComments = String(document.frmcontact.gencomments.value);
	var max=250;
	var bolValidGenComments = true;
	if (strGenComments.length > max) {
		   bolValidGenComments = false;
	}
	if (!bolValidGenComments){
			var charsCount = strGenComments.length
			ErrorMessage=ErrorMessage+"The comments area is limited to 250 characters.\n";
			ErrorMessage += "   You have entered "+charsCount+".\n";
			ErrorMessage += "   Please shorten your entry and submit again..\n";
	}
	
	if(ErrorMessage){
		alert("Incomplete Form or Errors found \n\n" + ErrorMessage);		
		if (!bolValidGenComments){
		document.frmcontact.gencomments.focus();
		document.frmcontact.gencomments.select();
		}
	}

	else {
		// Validates email
		emailStr = String(document.frmcontact.email.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)");
			document.forms["frmcontact"].elements["email"].focus();
			document.forms["frmcontact"].elements["email"].select();
			return false;
		}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		alert("The Email User Name doesn't seem to be valid.\nPlease also check for spaces left at the beginning of your email address.");
		document.forms["frmcontact"].elements["email"].focus();
		document.forms["frmcontact"].elements["email"].select();
		return false;
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("The email address destination IP address is invalid!");
		   	document.forms["frmcontact"].elements["email"].focus();
			document.forms["frmcontact"].elements["email"].select();
			return false;
		    }
		 }
    return true;
   }
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
	alert("The email address domain name doesn't seem to be valid.");
	document.forms["frmcontact"].elements["email"].focus();
	document.forms["frmcontact"].elements["email"].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) {
    alert("Your email address must end in a three-letter domain, or two letter country,\nfor instance, .com, .net, .org or .to, .cc");
   	document.forms["frmcontact"].elements["email"].focus();
	document.forms["frmcontact"].elements["email"].select();
	return false;
}
if (len<2) {
   var errStr="Your email address is missing a hostname!"
   alert(errStr);
	document.forms["frmcontact"].elements["email"].focus();
	document.forms["frmcontact"].elements["email"].select();
	return false;
}
	document.frmcontact.submit();
} // end of else 
} //end of function 

