﻿function validate(frm) 
{
var digitPat = new RegExp("[^0-9] ");
var postcodePat = new RegExp("[^0-9]");
//name should have no digits or dodgy characters. Dots and names like "O'Brien" will be allowed. 
//Updated 05/05/2003 (igor)
var zeeName = new RegExp("[^$A-Za-z .'-]");

   if(frm.HNstate) {
   var found = false;
   var x;
   for(var x=0;x<frm.HNstate.length;x++)
   //for(x in frm.HNstate)
   {	  
   //	alert(x);
   	//alert(frm.HNstate[x].value);
   	// Check the country field to see if an option was selected
	 			if (frm.HNstate[x].checked) 
	 			//alert("lla");
	 				found = true;
	 	}
	 	if(!found)
	 	{
	 		alert("Please select the state you want to send this request to");
			document.location.href="#";
	 		return false;	
	 	}
   }   

    // Check the name field to see if any characters were entered
    if(frm.name) 
    {

			if (frm.name.value.length == 0) 
			{
					alert("Please enter your name");
					frm.name.focus();
					return false;
			}
		}  
		  
    // Check the telephone field to see if an option was selected
    if (frm.phone) {
			if (frm.phone.value.search(digitPat) != -1) {
					alert("Please enter your telephone number");
					frm.phone.focus();
					return false;
			}   
		}		
		
    // Check the Firstname field. Updated 05/05/2003 (igor)
    if (frm.Firstname)//if there is a field 
    {   
    	var testFName = zeeTrim(frm.Firstname.value);//remove white spaces
    	frm.Firstname.value = testFName;//enter updated value into form field
   // alert("'"+testName+"'");
   		//test if any good
			if (testFName.length == 0 || testFName =="First Name" || testFName.search(zeeName) != -1)//not valid 
			{
					alert("Please enter your first name");
					frm.Firstname.focus();
					return false;
			}
		}
    
    // Check the Surname field. Updated 05/05/2003 (igor)
    if (frm.Surname)//if there is a field  
    {
			var testSName = zeeTrim(frm.Surname.value);//remove white spaces
			frm.Surname.value = testSName;//enter updated value into form field
			//test if any good
			if (testSName.length == 0 || testSName=="Surname"  || testSName.search(zeeName) != -1)//not valid 
			{
					alert("Please enter your surname");
					frm.Surname.focus();
					return false;
			}
		}
    
    // Check the address field to see if any characters were entered
    if (frm.Address1) {
    //alert("'"+zeeTrim(frm.Address1.value)+"'");
    frm.Address1.value = zeeTrim(frm.Address1.value);
			if (frm.Address1.value.length== 0 || frm.Address1.value == 'Address') {
					alert("Please enter your address");
					frm.Address1.focus();
					return false;
			}
		}


    
    // Check the town field to see if any characters were entered
    if (frm.suburb) {
			if (frm.suburb.value.length == 0||frm.suburb.value == '-1') {
					alert("Please enter your Suburb");
					frm.suburb.focus();
					return false;
			}
		}


    // Check the Email field to see if any characters were entered
    if (frm.email) {
			if (frm.email.value.length == 0) 
			{
					alert("Please enter your e-mail address.");
					frm.email.focus();
					return false;
			}
		 if (!emailCheck(frm.email.value))
		 {
				//alert("Please enter a valid e-mail address.");
				frm.email.focus();
				return false;
		 } 			
		}
  
  		
    // Check the town field to see if any characters were entered
    if (frm.age) {
			if (frm.age.value.length == 0||frm.age.value == '-1') {
					alert("Please enter your age");
					frm.age.focus();
					return false;
			}
		}    

   if(frm.State) {
	     	// Check the country field to see if an option was selected
	 			if (frm.countrySel.value=="AU" && frm.State.selectedIndex==0) {
	 					alert("Please select your State");
	 					frm.State.focus();
	 					return false;
	 			}    
   }
    



		
   if(frm.countrySel) 
   {
   ///alert (frm.countrySel);
    	// Check the country field to see if an option was selected
			if (frm.countrySel.selectedIndex == -1 || frm.countrySel.value == -1) 
			{
					alert("Please select your country");
					frm.countrySel.focus();
					return false;
			}    
			// Check the country field to see if a value was entered
  /*
			if (frm.countrySel.value=="AU" && !frm.Postcode) 
			{
							var Postcode = prompt("Please enter yourPostcode.");
						if(Postcode.length == 0)
						 return false;
						else
						frm.Postcode.value = Postcode;
			}
*/
   }
       // Check the postcode field to see if any characters were entered
     if(frm.Postcode) 
     {
    // alert(frm.countrySel.value);
      if (frm.Postcode.value.length ==0 && frm.countrySel.value=="AU") 
      {
           	alert("You have selected Australia as your country of residence.\nPlease enter your 4 digits Australian post code.");
          // else
           	//alert("Please enter your post code.");
           frm.Postcode.focus();
           return false;
       }  
       
     
     
       if (frm.countrySel.value=="AU" && (frm.Postcode.value.length < 4 ||  frm.Postcode.value.search(postcodePat) != -1)) 
       {
           alert("You have selected Australia as your country of residence.\nPlease enter your 4 digit Australian postcode.");
           frm.Postcode.focus();
           return false;
    	}
       if (frm.countrySel.value!="AU" && frm.Postcode.value.length>0) 
       {
          // alert("You have selected 'Other' as your country of residence.\nAs we only require post code from Australian users, your post code will not be recorded.");
           alert("You have selected 'Other' as your country of residence.\nAs we only require postcodes from Australian users, your postcode will not be recorded.");

           frm.Postcode.value = "";
           //return false;
    	}    	
    	
    }
   if(frm.enquiry)
	 {
	 
    if (frm.enquiry[0].checked)
    {
			if (frm.projecttype.value=="" && frm.projectcost.value.length<3 && frm.project.value=="")
			{
				 alert("Please include your project details!");
				 return false;
				 frm.project.focus();
			}
			
    }
    
   }

   if(frm.Company)
	{
		
		if (frm.ProfessionCode && frm.Company.value == "") 
		{
			if(frm.ProfessionCode.selectedIndex != 22 && frm.ProfessionCode.selectedIndex != 11 && frm.ProfessionCode.selectedIndex != 2 )

			{
				 alert("Please include your Company details!");
				return false;
			}
		}   
	}

  // alert(frm.Postcode.value);
  // Otherwise, things are just dandy
  return true;
}

function emailCheck(emailStr) {
    // Using email validation script found at
    // http://javascript.internet.com/forms/email-address-validation.html

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("Please enter a valid email address");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Please enter a valid email address");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Please enter a valid email address");
return false;
   }
}
if (user.match(userPat)==null) {

alert("Please enter a valid email address");
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Please enter a valid email address");
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("Please enter a valid email address");
return false;
   }
}


if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("Please enter a valid email address");
return false;

}


if (len<2) {
alert("Please enter a valid email address");
return false;
}

 return true;
}
//Updated 05/05/2003 (igor)
function zeeTrim(zeeString)
{
	var done = false;
	var test = 0;
	var breaks = 15;//indefinite loop breaks
	//removing spaces at the beginning. this will take care of keywords with spaces only
	while(!done)
	{
		if(zeeString.charAt(0)==" ")
			zeeString = zeeString.substr(1);
		else
			done = true;
		test++;
		if(test>=breaks) break;//hit the breaks if overlooping
	}
	return zeeString;
}
