// JavaScript Document
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { 
			num = parseFloat(val);
          	num  = string.replace(' ','');
			alert(num);
			alert(num.length);
			if (num.length < 7) {
				errors+='- '+nm+' must be a valid telephone number.\n';
			}
		  if (isNaN(val)) {
			  	errors+='- '+nm+' must contain a telephone number.\n';
		  }
		  else if (val.length < 7) {
			  	alert(val.length);
				errors+='- '+nm+' must be a valid telephone number.\n';
			}
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function validateForm() {
	var errormessage
	errormessage = ""
	if (document.form1.name.value == "") {
			errormessage = errormessage + "\n" + "Contact Name"; 
	}
	if (document.form1.phone.value == "") {
		errormessage = errormessage + "\n" + "Telephone"; 
	}
	else if (!checkInternationalPhone(document.form1.phone.value)) {
			errormessage = errormessage + "\n" + "Telephone"; 
	}

	if (!isValidEmail(document.form1.contact_email_address.value)) {
			errormessage = errormessage + "\n" + "Email"; 
	}
	if (document.form1.worktostart.value == "") {
			errormessage = errormessage + "\n" + "When you would like the work to start";
	}	
	if (document.form1.whenavailable.value == "") {
			errormessage = errormessage + "\n" + "Available Between"; 
	}
	if (document.form1.message.value == "") {
			errormessage = errormessage + "\n" + "Brief Job Summary"; 
	}
	if (document.form1.postcode.value == "") {
			errormessage = errormessage + "\n" + "Postcode"; 
	}
	if (errormessage != "") {
		errormessage = "The following fields are required or invalid:-\n" + errormessage;
		alert(errormessage);
	 	return false;
	}
	else {
		document.form1.submit();
	}
}

function validateShortForm() {
	var errormessage
	errormessage = ""
	if (document.form1.name.value == "") {
			errormessage = errormessage + "\n" + "Contact Name"; 
	}
	if (document.form1.phone.value == "") {
		errormessage = errormessage + "\n" + "Telephone"; 
	}
	else if (!checkInternationalPhone(document.form1.phone.value)) {
			errormessage = errormessage + "\n" + "Telephone"; 
	}
	if (!isValidEmail(document.form1.contact_email_address.value)) {
			errormessage = errormessage + "\n" + "Email"; 
	}
	if (document.form1.postcode.value == "") {
			errormessage = errormessage + "\n" + "Postcode"; 
	}
	else if (testPostCode(document.form1.postcode.value)==false) {
			errormessage = errormessage + "\n" + "Postcode Invalid"; 		
	}
	

	if (errormessage != "") {
		errormessage = "The following fields are required or invalid:-\n" + errormessage;
		alert(errormessage);
	 	return false;
	}
	else {
		document.form1.submit();
	}
}


// JavaScript Document
// JavaScript Document
/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
function isValidEmail(str) {
   return (str.indexOf(".") >= 1) && (str.indexOf("@") > 0);
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function testPostCode (inPostcode) {
  var myPostCode = inPostcode;
  if (checkPostCode (myPostCode)) {
    //document.getElementById('postcode').value = checkPostCode (myPostCode)
    //alert ("Postcode has a valid format")
	return true;
  } else {
	  return false;
	  //alert ("Postcode has invalid format")
  }
  
}

function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkpmnrstuvwxy]";                         // 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 + "{1}" + "?[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);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/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;
}
//-->
