// JavaScript Document
// Common functions

function externalLinks() { 
if (!document.getElementsByTagName) return; 
var anchors = document.getElementsByTagName("a"); 
for (var i=0; i<anchors.length; i++) { 
	var anchor = anchors[i]; 
	if (anchor.getAttribute("href") && 
		anchor.getAttribute("rel") == "_blank") 
		anchor.target = "_blank"; 
	} 
} 
window.onload = externalLinks;

function Form_Validator(BookingForm)
{

  if (BookingForm.F01.value == "")
  {
    alert("You do not appear to have entered your Name");
    BookingForm.F01.focus();
    return (false);
  }
  
  if (BookingForm.F01.value.length < 3)
  {
    alert("This seems to be a little short for a name. Please check your entry.");
    BookingForm.F01.focus();
    return (false);
  }

  if (BookingForm.F03.value == "")
  {
    alert("Please enter your email address");
    BookingForm.F03.focus();
    return (false);
  }
  
  var first, last, x;
  y = BookingForm.F03.value;
  if (y.indexOf(" ") != -1) { x = "X"; }
  if (y.indexOf(".@") != -1) { x = "X"; }
  if (y.indexOf("@.") != -1) { x = "X"; }
  if (y.indexOf(".") == y.length-1) { x = "X"; }
  first = y.indexOf("@");
  if (first == -1 || first == 0) { x = "X"; }
  if (first != y.lastIndexOf("@")) { x = "X"; }
  if (y.lastIndexOf(".") != -1 && (y.length - y.lastIndexOf(".")) < 6) { x = ""; } else { x = "X"; }
  if (x == "X")
  {
   	alert("You appear to have entered an invalid email address");
    BookingForm.F03.focus();
    return (false);
  }
    
  if (BookingForm.F04.value == "")
  {
    alert("Please enter a Telephone Number on which we can contact you");
    BookingForm.F04.focus();
    return (false);
  }

  if (BookingForm.F06.value == "")
  {
    alert("Please enter the date your require our services");
    BookingForm.F06.focus();
    return (false);
  }
	
  if (BookingForm.F07.value == "")
  {
    alert("Please enter the time of collection");
    BookingForm.F07.focus();
    return (false);
  }

  if (BookingForm.F10.value == "")
  {
    alert("Please enter the address we should collect from");
    BookingForm.F10.focus();
    return (false);
  }

  if (BookingForm.F11.value == "")
  {
    alert("Please enter the town/city we shall be collecting from");
    BookingForm.F11.focus();
    return (false);
  }

  if (BookingForm.F20.value == "")
  {
    alert("Please enter the name of the passengers");
    BookingForm.F20.focus();
    return (false);
  }

  if (BookingForm.F22.value == "")
  {
    alert("Please tell us where you are going");
    BookingForm.F22.focus();
    return (false);
  }

  if (BookingForm.F23.value == "")
  {
    alert("Please enter the town/city of the destination");
    BookingForm.F23.focus();
    return (false);
  }

  return (true);
}

// Set Cookie
function setCookie() {
  var cookiexists = getCookie("Referer");
  var referer 	  = escape(document.referrer);  
  var referer     = referer.replace("http%3A//","");
  var referer2    = referer.replace("www.","");
  var myDomain    = document.domain.replace("www.","");
  var adWords	  = getURLParam("PPC");
  if (referer2.indexOf(myDomain) == 0) { referer = ""; } 
  if (referer != "" && !cookiexists) {
	var exp = new Date()
	  var expires = exp.getTime() + (275 * 24 * 60 * 60 * 1000)  // set cookie to expire in 9 months
	  exp.setTime(expires);
	  document.cookie = "Referer=" + referer + "; expires=" + exp.toGMTString() + "; path=/";
	  if (adWords.toLowerCase() == "adwords") {
	 	document.cookie = "PPC=AdWords" + "; expires=" + exp.toGMTString() + "; path=/";
 	  } 
  } 
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
		aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}


function getCookie(c_name) {
  if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}