// JavaScript Document

//To validate the required fields of the form
function Validate()
{
	if (document.frmConact.spm_check.value!='')
	{
		alert("Invalid character in one of the field")
		document.frmConact.spm_check.focus();
		return false;
	}
	if(isBlank(document.frmConact.name.value, "Please enter your name."))
	{
		document.frmConact.name.focus();
		return false;
	}
	else if((/<[^>]+>/.test(document.frmConact.name.value)) || (/(\[).*(\]).*(\[\/)*(\])/.test(document.frmConact.name.value)))
		 { 
			alert("Please enter a valid First Name");
			document.frmConact.name.focus();
			return false;
		 }
	
	if(isBlank(document.frmConact.email.value, "Please enter your email address."))
	{
		document.frmConact.email.focus();
		return false;
	}
	
	if(document.frmConact.email.value!= '')
	{
		if(checkemail(document.frmConact.email.value) == false)
		{
			document.frmConact.email.focus();
			return false;
		}
	}
	if((document.frmConact.street.value != "") && ( (/<[^>]+>/.test(document.frmConact.street.value)) || (/(\[).*(\]).*(\[\/)*(\])/.test(document.frmConact.street.value)) ) )
	{
		alert("Please enter valid Street Name");
		document.frmConact.street.focus();
		return false;
	}
	if((document.frmConact.town.value != "") && ( (/<[^>]+>/.test(document.frmConact.town.value)) || (/(\[).*(\]).*(\[\/)*(\])/.test(document.frmConact.town.value)) ) )
	{
		alert("Please enter valid Town");
		document.frmConact.town.focus();
		return false;
	}
	if(document.frmConact.postcode.value !="" && postit() == false)
	{
		alert("Please enter valid postcode");
		document.frmConact.postcode.focus();
		return false;
	}
	if((document.frmConact.tel.value != "") && (validTelephone(document.frmConact.tel.value) == false))
	{
		alert("Please enter valid Phone No.");
		document.frmConact.tel.focus();
		return false;
	}
	
	if(isBlank(document.frmConact.contact_about.value, "Please choose contact reason."))
	{
		document.frmConact.contact_about.focus();
		return false;
	}
	
	if(isBlank(document.frmConact.comments.value, "Please enter your comments."))
	{
		document.frmConact.comments.focus();
		return false;
	}
	else
	{
		if(containBadWord(document.frmConact.comments.value))
		{
			alert("Sorry - you can't send Comments containing offensive language.");
			document.frmConact.comments.focus();
			return false;
		}
		if((/<[^>]+>/.test(document.frmConact.comments.value)) || (/(\[).*(\]).*(\[\/)*(\])/.test(document.frmConact.comments.value)))
		 { 
			alert("Please enter a valid Comment");
			document.frmConact.comments.focus();
			return false;
		}
	}

	return true;
}

var postcodeError;
function postit(){ //check postcode format is valid
 test = document.frmConact.postcode.value; 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 < 6 || size > 8){ //Code length rule
  postcodeError =" Not a valid postcode - wrong length";
  postcodeError ="Please enter a valid Postcode"
 document.frmConact.postcode.focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   postcodeError= "Not a valid postcode - cannot start with a number";
   postcodeError ="Please enter a valid Postcode"
      document.frmConact.postcode.focus();;
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   postcodeError= "Not a valid postcode - alpha character in wrong position";
   postcodeError ="Please enter a valid Postcode"
      document.frmConact.postcode.focus();;
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   postcodeError= "Not a valid postcode - number in wrong position";
   postcodeError ="Please enter a valid Postcode"
   document.frmConact.postcode.focus();;
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   postcodeError="Not a valid postcode - number in wrong position";
   postcodeError ="Please enter a valid Postcode"
    document.frmConact.postcode.focus();;
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   postcodeError="Not a valid postcode - no space or space in wrong position";
   postcodeError ="Please enter a valid Postcode"
    document.frmConact.postcode.focus();;
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   postcodeError="Not a valid postcode - only one space allowed";
   postcodeError ="Please enter a valid Postcode"
    document.frmConact.postcode.focus();;
   return false;
  }
/*alert("Postcode Format OK");*/
return true;
}
