  // Ajax functionality
  function ajaxFunction(id,session){
  	var ajaxRequest = GetXmlHttpObject();
  	var Gurl = "ajax_tripadd.php?id="+id+"&sid="+Math.random();
    
    // Note this HAD to call function() { then function } it couldn't just call the function
    ajaxRequest.onreadystatechange = function() { stateChanged(ajaxRequest,'mytrips') };
  	ajaxRequest.open("GET",Gurl,true);
  	ajaxRequest.send(null); 
  }
  
  // Same as hideMaps in main tpl
  function hideMaps2(){
    var no = document.getElementById('count').value; // Number of Maps     

    // Hide all Maps
    for (i=1;i <= no;i++){                   
      document.getElementById('map_'+i).style.height = "1px";    
      document.getElementById('map_'+i).style.visibility = "hidden";
    }        
  }  
    
  function ajaxSchools(){
  	var ajaxRequest = GetXmlHttpObject();
  	var Gurl = "ajax_schools.php?id="+getSValue('school')+"&sid="+Math.random();
    
    // Note this HAD to call function() { then function } it couldn't just call the function
    ajaxRequest.onreadystatechange = function() { stateChanged(ajaxRequest,'yourschool'); setTimeout('hideMaps2()',700); };
  	ajaxRequest.open("GET",Gurl,true);
  	ajaxRequest.send(null); 
  }  

  function ajaxLive(){
  	var ajaxRequest = GetXmlHttpObject();
  	var Gurl = "ajax_livestatus.php?sid="+Math.random();
    
    // Note this HAD to call function() { then function } it couldn't just call the function
    ajaxRequest.onreadystatechange = function() { stateChanged(ajaxRequest,'livearea') };
  	ajaxRequest.open("GET",Gurl,true);
  	ajaxRequest.send(null); 
  }    
  
  function kickstartlive(){
    var currentTime = new Date()
    var hours = currentTime.getHours();
    var day = currentTime.getDay();

    // Make sure this page only refreshes on a Mon-Fri minimal
    // Make sure that its between 7-10 and 2-5 to really make sure we only run when definitely relevant
    if ((day >= 1 && day <=5) && ((hours >=7 && hours <=10) || (hours >=14 && hours <=17))) {
      setInterval('ajaxLive();',60000);
    }
  }
  
  // Check all selected boxes have been selected
  function checkSelectedBoxes(){
    if (gEbId('sel_errors').value > 0){
      alert('error');
    }
  }
  
  // Ajax functionality
  function ajaxAddPickup(id,lo){
  	var ajaxRequest = GetXmlHttpObject();
  	var Gurl = "ajax_pickupadd.php?id="+id+"&loc="+getSValue(lo)+"&sid="+Math.random();
    
    // Note this HAD to call function() { then function } it couldn't just call the function
    ajaxRequest.onreadystatechange = function() { stateChanged(ajaxRequest,'mytrips') };
  	ajaxRequest.open("GET",Gurl,true);
  	ajaxRequest.send(null); 
  }  
  
  // Display trip pickup times
  function trips(dif,out,rtn,lo,div){
    var loc = getSValue(lo); 

    if (loc !== "Select Pickup Point"){
      /*if ((dif == "y") && (loc == "Rainham")){
        alert('The bus picks up at Rainham White Horse and returns to Rainham White Horse');  
      } else if (dif == "n" && loc == "Rainham") {
        alert('The bus picks up at Rainham Cricketers and returns to Rainham White Horse');
      }
      
      if (loc == "Chatham Military Road"){
        alert('Coaches do not pick up in Pentagon Bus Station');
      }*/
      
    	var ajaxRequest = GetXmlHttpObject();
    	var Gurl = "ajax_pickup.php?out="+out+"&rtn="+rtn+"&point="+loc+"&sid="+Math.random();
      
      if (rtn == "TT"){ alert('Coach leaves after performance has finished'); }
      
      // Note this HAD to call function() { then function } it couldn't just call the function
      ajaxRequest.onreadystatechange = function() { stateChanged(ajaxRequest,div) };
    	ajaxRequest.open("GET",Gurl,true);
    	ajaxRequest.send(null);
    }   
  }
  
  // Short hand for getElementById
  function gEbId (item){
    return document.getElementById(item);
  }
  
  function clear(id){
    return gEbId(id).value = null;
  }
  
  function addPassenger(id){
    var field = ["initial_","surname_","age_"];

    if (gEbId('need_'+id).value == 1){ field[3] = "passport_"; }

    for (i=0; i < field.length; i++){
      // Is the length 0?
      if ((gEbId(field[i]+id).value.length)==0){
        alert('All fields are required');
        return false;
      }
      
      if (i==2 && isNaN(gEbId(field[i]+id).value)){
        alert('Age must be a number');
        return false;
      }
      
      if (i==3 && isNaN(gEbId(field[i]+id).value)){
        alert('Passport number must be numeric');
        return false;
      }      
    }
  
  return true;
  }

  function checkTerms(){
    if (gEbId('terms').checked){
      return true;
    } else {
      alert('You must read and agree to the Terms & Conditions');
      return false;
    }
  }

  function addCustomer(){
    var field = ["first","surname","addr1","town","county","postcode","tel","email"];
    
    for (i=0; i < field.length; i++){
      // Is the length 0?
      if ((gEbId(field[i]).value.length)==0){
        alert('All fields are required');
        return false;
      }
      
      // Is the email valid?
      if (field[i]=="email"){  
        if (!(checkEmail(gEbId(field[i]).value))){
          alert('Valid email required');
          return false;
        }
      }
    }
  
    document.details_form.submit();
  }
  
  function untick(tick){
    gEbId(tick).checked = false;
  }
  
  function stateChanged(obj,div){
  	if(obj.readyState == 4){ gEbId(div).innerHTML=obj.responseText; }
  }
  
  function GetXmlHttpObject()
  {
  	try{
  		// Opera 8.0+, Firefox, Safari
  		return new XMLHttpRequest();
  	} catch (e){
  		// Internet Explorer Browsers
  		try{
  			return new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (e) {
  			try{
  				return new ActiveXObject("Microsoft.XMLHTTP");
  			} catch (e){
  				// Something went wrong
  				alert("Your browser broke!");
  				return false;
  			}
  		}
  	}
  }
  
  // Get the Selected value from a select box
  function getSValue(id){
    var sel = gEbId(id);
  return sel.options[sel.selectedIndex].value;
  }
