// JavaScript Document
// include ('../forms/site.js');

function validateEnquiryContact(theForm) {
    var errstr = "";
    if (theForm.company.value.length == 0) {
  	     errstr += "Company Name\n";
    }
    if (theForm.contact_name.value.length == 0) {
        errstr += "Contact name\n";
    }
    if (theForm.phone_office.value.length <= 10) {
  	     errstr += "Telephone number\n";
    }
    if (theForm.billing_address_street.value.length == 0) {
  	     errstr += "Address\n";
    }
    if (theForm.billing_address_postalcode.value.length == 0) {
 	     errstr += "Postcode\n";
    }
	else {
	     errstr += checkPostcode(theForm.billing_address_postalcode.value);
	}
    if (theForm.email1.value.length <= 10) {
  	     errstr += "email address\n";
    }
    if (errstr != '') {
	    alert("Please correct the following information: \n\n" + errstr);
	    return false;
	}
	return true;
}

function validateContact(theForm) {
    var errstr = "";

	if (theForm.company.value.length == 0) {
  	     errstr += "Company Name\n";
    }
    if (theForm.contact_name.value.length == 0) {
        errstr += "Contact name\n";
    }
    if (theForm.street.value.length == 0) {
  	     errstr += "Address\n";
    }
    if (theForm.zip.value.length == 0) {
 	     errstr += "Postcode\n";
    }
	else {
	     errstr += checkPostcode(theForm.zip.value);
	}
    if (theForm.phone.value.length <= 10) {
  	     errstr += "Telephone number\n";
    }
    if (theForm.email.value.length <= 10) {
  	     errstr += "email address\n";
    }
    if (errstr != '') {
	    alert("Please correct the following information: \n\n" + errstr);
	    return false;
	}
	return true;
}




function checkPostcode( pCode )
{ 
	var error = "";

	//check postcode format is valid

	 test = pCode; size = test.length
	 test = test.toUpperCase(); //Change to uppercase

	 while (test.slice(0,1) == " ") //Strip leading spaces
	  {
	      test = test.substr(1,size-1);size = test.length

	  }
	 while(test.slice(size-1,size)== " ") //Strip trailing spaces
	  {test = test.substr(0,size-1);size = test.length

	  }
	 if (size == 0 ){ //Not supplied
	  error = "Postcode has not been filled in.\n";

	  }
	 else if (size < 6 || size > 8){ //Code length rule
	  error = "* Invalid postcode - wrong length\n";
	  }
	 else if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
	   error = "* Invalid postcode - cannot start with a number\n";
	  }
	 else if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
	   error = "* Invalid postcode - alphabetic character in wrong position\n";
	  }
	 else if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
	   error = "* Invalid postcode - number in wrong position\n";
	  }
	 else if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
	   error = "* Invalid postcode - number in wrong position\n";
	  }
	 else if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
	   error = "* Invalid postcode - no space or space in wrong position\n";
	   }
	 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");

	  if (count1 != count2){//only one space rule

	   error = "* Invalid postcode - only one space allowed\n";

	  }
	return error;
}


function checkEmail (strng) {

var error="";

if( strng != "") {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "* A valid email address must be supplied.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "* The email address contains illegal characters.\n";
       }
    }
}	
return error;    

}

