var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[1] = "Please enter a UK telephone number";
telNumberErrors[2] = "Please enter a UK telephone number without the country code";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "A UK telephone number should start with a 0";
telNumberErrors[5] = "Please enter a valid UK telephone number";

function ValidateForm(linkName, formName){

  var submitbtn = document.getElementById(linkName);
  var theform = document.forms[formName];

  var Phone = theform.phone;
  var postCode = theform.postcode;

  if (ValidPostCode(Phone, postCode) == false) {
     return;
  }

  if (Phone)
  {
    if (ValidPhone(Phone) == false)
     return;
  }

  if (submitbtn)
  {
    submitbtn.innerHTML  = "<span>Please Wait...</span>";
  }
  document.frmCheck.submit();
  return;
 }

function ValidPhone(Phone){

	if ((Phone.value==null)||(Phone.value=="")){
   // phone is optional, so OK
   return true;
 }
 
 var validNum = checkUKTelephone (Phone.value)
 if (!validNum) {
     Phone.className="iError";
     alert (telNumberErrors[telNumberErrorNo]);
    	Phone.focus()
	   	return false;
 }

 // update phone field with stripped number
 Phone.className="text";
 Phone.value = validNum;
	return true;
}

function ValidPostCode(Phone, postCode){ //check postcode format is valid

 var postCodeVal = postCode.value + " ";

 if (postCodeVal.length == 1){
	  if (Phone) Phone.className="text";
   postCode.className="iError";
   alert("Please Enter Your Postcode");
	  postCode.focus();
	  return false;
	}

 // Remove spaces from the postcode to help validation
  while (postCodeVal.indexOf(" ")!= -1)  {
    postCodeVal = postCodeVal.slice (0,postCodeVal.indexOf(" ")) + postCodeVal.slice (postCodeVal.indexOf(" ")+1);
  }
 
 var size = postCodeVal.length;

 // add in space prior to last three
 postCodeVal = postCodeVal.slice (0,size-3) + " " + postCodeVal.slice (size-3);
 size++;

 if (size < 6 || size > 8){ //Code length rule
  return invalidPostCode(postCode, Phone);
 }
 if (!(isNaN(postCodeVal.charAt(0)))){ //leftmost character must be alpha character rule
   return invalidPostCode(postCode, Phone);
  }
 if (isNaN(postCodeVal.charAt(size-3))){ //first character of inward code must be numeric rule
   return invalidPostCode(postCode, Phone);
  }
 if (!(isNaN(postCodeVal.charAt(size-2)))){ //second character of inward code must be alpha rule
   return invalidPostCode(postCode, Phone);
  }
 if (!(isNaN(postCodeVal.charAt(size-1)))){ //third character of inward code must be alpha rule
   return invalidPostCode(postCode, Phone);
  }
  
 postCode.className="text";
 postCode.value = postCodeVal; //write back to form field

 return true;
}

function invalidPostCode(postCode,Phone){
  if (Phone) Phone.className="text";
  postCode.className="iError";
  alert("Please Enter a Valid Postcode");
  postCode.focus();
  return false;
}

/*==============================================================================

This routine checks the value of the string variable specified by the parameter
for a valid UK telphone number. It returns false for an invalid number and the
reformatted telephone number false a valid number.

If false is returned, the global variable telNumberError contains an error
number, which may be used to index into the array of error descriptions 
contained in the global array telNumberErrors.

The definition of a valid telephone number has been taken from:
http://www.oftel.gov.uk/publications/numbering/2003/ntnp_final_c0703.pdf

All inappropriate telephone numbers are disallowed (e.g. premium lines, sex 
lines, radio-paging services etc.)

Author:    John Gardner
Date:      16th November 2003

*/

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;
}

var exchangeMarker;
var userMarker;
var exchInfo;
var userInfo;
var opts;

function displayMap(exchLat, exchLong, userLat, userLong, exchName, postcode)
{
  // create exchange and user Points
  var exchangePoint = new GLatLng(exchLat,exchLong);
  var userPoint = new GLatLng(userLat,userLong);

  // create global markers and infos
  exchangeMarker = new GMarker(exchangePoint);
  userMarker = new GMarker(userPoint);
  exchInfo = "<br><b>Your Local Exchange</b><br>"+exchName;
  userInfo = "<br><b>Your Location</b><br>"+postcode;
  
  // set global options for info balloon width
  opts = new Object();
  opts.maxWidth = 217;

  var map = new GMap(document.getElementById("map"));
  map.addControl(new GSmallMapControl());
  map.setCenter(exchangePoint, 14);
  var polyline = new GPolyline([exchangePoint, userPoint], "#ff0000", 3);
  map.addOverlay(polyline);                                                                                      

  GEvent.addListener(exchangeMarker, "click", function() {                                                       
      exchangeMarker.openInfoWindowHtml(exchInfo, opts);
    });                                                                                                          
  map.addOverlay(exchangeMarker);
  GEvent.addListener(userMarker, "click", function() {
      userMarker.openInfoWindowHtml(userInfo, opts);
    });                                                                                                          
  map.addOverlay(userMarker);
}

function displayExhangeInfo()
{
  exchangeMarker.openInfoWindowHtml(exchInfo, opts);
}
function displayUserInfo()
{
  userMarker.openInfoWindowHtml(userInfo, opts);
}

