// JavaScript Document

function validateForm(){
var validForm =false;
var validPostCode= testPostCode();
var validEmail = testEmail();
var validTitel = testTitle();
var validfName = testfName();
var validlName = testlName();
var validTown = testTown();
var validCounty = testCounty();
var validAddress = testAddress();
var validPhone = testUKTelephone();
if ((validPostCode==true) && (validEmail==true) && (validTitle==true) && (validfName==true) && (validlName==true) && (validTown==true)&& (validCounty==true) && (validAddress==true)  && (validPhone==true) ) {
validForm = true;}
return validForm;}

function validateContactForm(){
var validForm =false;
var validName = testName();
var validEmail = testEmail();
var validSubject = testSubject();
var validMessage = testMessage();
if ((validName==true) && (validEmail==true) && (validSubject==true) && (validMessage==true)){
validForm = true;}
return validForm;
}

function testName () {
  var myName = document.getElementById('name').value;
  var validName= true
  if (!(checkName (myName))) {
    alert ("Please enter your name")
	validName= false;
  } return validName;};
  
  function testSubject () {
  var mySubject = document.getElementById('subject').value;
  var validSubject= true
  if (!(checkSubject (mySubject))) {
    alert ("Please enter a subject")
	validSubject= false;
  } return validSubject;};
  
  function testMessage () {
  var myMessage = document.getElementById('message').value;
  var validMessage= true
  if (!(checkMessage (myMessage))) {
    alert ("Please enter a message not less than 20 characters")
	validMessage= false;
  } return validMessage;};



function testUKTelephone(){

var myPhone = document.getElementById('phone').value;
var validPhone = true
if (!(checkUKTelephone (myPhone))) {
    alert ("Please enter a valid phone number")
	validPhone= false;
  } return validPhone;};


function testPostCode () {
  var myPostCode = document.getElementById('postcode').value;
  var validCode= true
  <!--if (!(checkPostCode (myPostCode))) {
    alert ("Please enter a valid post code")
	validCode= false;
  } -->
  return validCode;};
  
  function testEmail () {
  var myEmail = document.getElementById('email').value;
  var validEmailAddress= true
  if (!(checkEmail (myEmail))) {
    alert ("Please enter a valid email address")
	validEmailAddress= false;
  } return validEmailAddress;};
  
  function testTitle () {
  var myTitle = document.getElementById('title').value;
  var validTitle= true
  if (!(checkTitle (myTitle))) {
    alert ("Please choose a title")
	validTitle= false;
  } return validTitle;};
  
   function testfName () {
  var myfName = document.getElementById('fName').value;
  var validfName= true
  if (!(checkfName (myfName))) {
    alert ("Please enter your first name or intial")
	validfName= false;
  } return validfName;};
  
     function testlName () {
  var mylName = document.getElementById('lName').value;
  var validlName= true
  if (!(checklName (mylName))) {
    alert ("Please enter your last name")
	validlName= false;
  } return validlName;};

     function testTown () {
  var myTown = document.getElementById('town').value;
  var validTown= true
  if (!(checkTown (myTown))) {
    alert ("Please enter your town or city name")
	validTown= false;
  } return validTown;};
  
       function testCounty () {
  var myCounty = document.getElementById('county').value;
  var validCounty= true
  if (!(checkCounty (myCounty))) {
    alert ("Please enter your County name")
	validCounty= false;
  } return validCounty;};
  
         function testAddress () {
  var myAddress = document.getElementById('address').value;
  var validAddress= true
  if (!(checkAddress (myAddress))) {
    alert ("Please enter your Address")
	validAddress= false;
  } return validAddress;};


  
function checkTitle(titleIn){
  var validTitle = true;
  if (titleIn.length==0){
  	validTitle=false;}
	return validTitle;
}

function checkfName(fNameIn){
  var validfName = true;
  if (fNameIn.length==0){
  	validfName=false;}
	return validfName;
}

function checklName(lNameIn){
  var validlName = true;
  if (lNameIn.length==0){
  	validlName=false;}
	return validlName;
}

function checkTown(townIn){
  var validTown = true;
  if (townIn.length==0){
  	validTown=false;}
	return validTown;
}

function checkCounty(countyIn){
  var validCounty = true;
  if (countyIn.length==0){
  	validCounty=false;}
	return validCounty;
}

function checkAddress(addressIn){
  var validAddress = true;
  if (addressIn.length==0){
  	validAddress=false;}
	return validAddress;
}

function checkName(nameIn){
  var validName = true;
  if (nameIn.length==0){
  	validName=false;}
	return validName;
}
function checkSubject(subjectIn){
  var validSubject = true;
  if (subjectIn.length==0){
  	validSubject=false;}
	return validSubject;
}
function checkMessage(messageIn){
  var validMessage = true;
  if (messageIn.length < 20){
  	validMessage=false;}
	return validMessage;
}




function checkUKTelephone(phoneIn){
  var validPhone = true;
  if (phoneIn.length==0){
  	validPhone=false;}
	return validPhone;
}

function checkEmail(str){


		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		//  alert("Invalid E-mail address")
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//   alert("Invalid E-mail address")
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//    alert("Invalid E-mail address")
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		//    alert("Invalid E-mail address")
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//    alert("Invalid E-mail address")
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		//    alert("Invalid E-mail address")
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
	//	    alert("Invalid E-mail address")
		    return false;
		 }
		 
		 if (str == ""){
		 return true;
		 }

 		 return true;					
	
}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

/*function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Finally check that the telephone number is appropriate.
  exp = /^(01|02|05|070|077|078|079)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}*/