// JavaScript Document

var xmlHttp
var xmlHttpOv
var myimages = new Array()

function createRequestObject() {
  FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
  separator = ',';
    // The token used to separate data from multi-select inputs
  query = '' + this.location;
  qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
  query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  keypairs = new Object();
  numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
      // Split the query string at each '&', storing the left-hand side
      // of the split in a new keypairs[] holder, and chopping the query
      // so that it gets the value of the right-hand string.
  }
  keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
    keyValue = unescape(keyValue);
      // Unescape non-alphanumerics
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }
  return FORM_DATA;
}





function preLoad()
{
	// Preload Images
	var d=document;
	if(d.images)
	{
		if(!d.p)
		{
			d.p=new Array();
		}

    	var i,j=d.p.length,a=Submit.arguments;

		for(i=0; i<a.length; i++)
		{
    		if (a[i].indexOf("#")!=0)
			{
				d.p[j]=new Image;
				d.p[j++].src=a[i];
			}
		}
	}
}
var feeds_done = "";
var urlCT = "";
var req = false;
var req_counter_tmp = 0;


   function createAjaxObj()
   {
      var http_request = false;
      if (window.XMLHttpRequest)
      {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType)
         {
            http_request.overrideMimeType('text/xml');
         }
      }
      else if (window.ActiveXObject)
      {
         try
         {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         }
         catch (e)
         {
            try
            {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {

            }
         }
      }
      if (!http_request)
      {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      return http_request;

   }


function req_co()
{

  if(req == false)
  {
    req = true;

    var http_request = createAjaxObj();
    http_request.onreadystatechange =  function() { alertContents(http_request); };
    var rand=Math.random();

    var url = urlCT +"&ran="+rand+"&feeds_done="+feeds_done;

    http_request.open('GET', url, true);
    http_request.send(null);
  }
}

var ajaxBadL = null;

function checkEnteredLocation()
{
  if(document.getElementById("loca").value == "Type an airport, city or town."  ||
    document.getElementById("loca").value.length < 3)
  {
    alert("Please enter a valid pick up location.");
	return false;
  }

  var locd = document.getElementById("locd").value;

  if(parseInt(locd) == 0)
  {
    if(document.getElementById("locc").value == "Type an airport, city or town."  ||
    document.getElementById("locc").value.length < 3)
    {
      alert("Please enter a valid drop off location.");
	  return false;
    }
    return false;
  }

  return true;
}

var request_id2 = "";



function GetXmlHttpObject()
{
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}



function tryPostingForm()
{
  if(!document.getElementById('pDate') || document.getElementById('pDate').value == "dd/mm/yyyy")
    return;

  if(!document.getElementById('dDate') || document.getElementById('dDate').value == "dd/mm/yyyy")
    return;

  document.frmSearch.submit();
}
var setL_params_hlp = new Array();

function setLocation_ch()
{
  var str = setL_params_hlp['str'];
  var postForm = setL_params_hlp['postForm'];

  var loc = new Array();
  loc = str.split('#');
  document.getElementById("loca").value=loc[2];
  document.getElementById("locb").value=loc[1];
  document.getElementById("locc").value=loc[2];
  document.getElementById("locd").value=loc[1];

  checkEnteredLocation();
}

function setLocation(str, postForm)
{
  document.getElementById("age").focus();
  setL_params_hlp['str'] = str;
  setL_params_hlp['postForm'] = postForm;
  setTimeout("setLocation_ch()", 50);
  return;
}


function setLocation2_ch()
{
  var str = setL_params_hlp['str'];
  var postForm = setL_params_hlp['postForm'];
  var loc = Array();

  loc = str.split('#');
  document.getElementById("locc").value=loc[2];
  document.getElementById("locd").value=loc[1];

  if(postForm)	tryPostingForm();
}

function setLocation2(str, postForm)
{
  document.getElementById("age").focus();
  setL_params_hlp['str'] = str;
  setL_params_hlp['postForm'] = postForm;
  setTimeout("setLocation2_ch()", 50);
  return;
}

function clearbox(elm)
{
	elm.value = "";
	/*
	if(elm.value == "Please enter a city, town or airport.")
	{
		elm.value = "";
	}
	else if(elm.value == "Please enter a location.")
	{
		elm.value = "";
	}
	else if(elm.value == "Type an airport, city or town.")
	{
		elm.value = "";
	}
	*/
}


/*********************************************************************************************************************************************************/

var xmlHttp

function sortByLocation(a, b) {
	var x = a[0];
	var y = b[0];
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		document.getElementById("locb").options.length = 0;
		document.getElementById("locd").options.length = 0;

		textToSplit = xmlHttp.responseText
		textToSplit = escape(textToSplit);
		textToSplit = textToSplit.replace(/\%u[ABCDEF]{4}/g, "");
		textToSplit = unescape(textToSplit);

		//Split the document
		returnElements = textToSplit.split("#")

		//Process each of the elements
		var pickupLocations = new Array();
		var dropoffLocations = new Array();
		for ( var i=0; i<returnElements.length; i++ )
		{
			valueLabelPair = returnElements[i].split("@")
			pickupSelected = false;
			dropoffSelected = false;
			value1 = valueLabelPair[2]
			value2 = valueLabelPair[3]
			if (value1 == "selpickup") {
				pickupSelected = true;
			}
			if (value1 == "seldropoff") {
				dropoffSelected = true;
			}
			if (value2 == "selpickup") {
				pickupSelected = true;
			}
			if (value2 == "seldropoff") {
				dropoffSelected = true;
			}
			document.getElementById('locb').options[i] = new Option(valueLabelPair[1], valueLabelPair[0],false,pickupSelected);
			document.getElementById('locd').options[i] = new Option(valueLabelPair[1], valueLabelPair[0],false,dropoffSelected);

			pickupLocations[i] = new Array(valueLabelPair[1], valueLabelPair[0],pickupSelected);
			dropoffLocations[i] = new Array(valueLabelPair[1], valueLabelPair[0],dropoffSelected);
		}


	}
}

function changeSecond()
{
	for (i=0; i<document.getElementById('locd').length; i++)
	{
		if(document.getElementById('locb')[i].selected == true)
		{
			 document.getElementById('locd')[i].selected = true;
		}
	}
}

function checkText(el)
{
	if(el.value == "")
	{
		el.value = "dd/mm/yyyy";
	}
}
