

var stopsArrayName=null;
var singleArrayName=null;
var returnArrayName=null;


function getSinglePrice(startPlaceId, endPlaceId)
{
	eval("var priceArray =" + singleArrayName + ";");

	var resultPrice = priceArray[startPlaceId][endPlaceId];
	return resultPrice;
}


function getReturnPrice(startPlaceId, endPlaceId)
{
	eval("var priceArray =" + returnArrayName + ";");
	var resultPrice = priceArray[startPlaceId][endPlaceId];
	return resultPrice;

}


function lookup()
{
	eval("var arrayOfStops = " + stopsArrayName + ";");

	var startSelectObj = document.getElementById('startPoint');
	var startSelectOptionIndex = startSelectObj.selectedIndex;
	var startOptionSelectedValue = startSelectObj.options[startSelectOptionIndex].value;

	// where it says startSelectObj.options - this is an array of the html option elements in the select object.

	var endSelectObj = document.getElementById('endPoint');
	var endSelectOptionIndex = endSelectObj.selectedIndex;
	var endOptionSelectedValue = endSelectObj.options[endSelectOptionIndex].value;

	var x = getSinglePrice(startOptionSelectedValue,endOptionSelectedValue);
	var y = getReturnPrice(startOptionSelectedValue,endOptionSelectedValue);


	var farePriceDiv = document.getElementById('farePrice');
	var singleFareString = "";
	var returnFareString = "";
	
	// deal with single fare
	if (!isNaN(x)) // single fares value is a number
	{
		x = x/100;
		x = x.toFixed(2);
		singleFareString = '<p>The fare from <strong>' + arrayOfStops[startOptionSelectedValue] + '</strong> to <strong>' + arrayOfStops[endOptionSelectedValue] + '</strong> is: <span style="font-size:16px; color: F78B1E;" ><strong>&pound;' + x + '</strong></span></p>';
	}
	else // single fare value is not a number
	{
		if (x=='B')
		{
			singleFareString = '<p>The single fare from <strong>' + arrayOfStops[startOptionSelectedValue] + '</strong> to <strong>' + arrayOfStops[endOptionSelectedValue] + '</strong> is not applicable.</p>';
		}
		if (x.startsWith('SE1-')) // should recommend the Skylink easycard 1 - Derby to East Midlands Airport £21 
		{
			var normalPrice = parseInt(x.substring(4));
			normalPrice = normalPrice/100;
			normalPrice = normalPrice.toFixed(2);
			singleFareString = '<p>The single fare from <strong>' + arrayOfStops[startOptionSelectedValue] + '</strong> to <strong>' + arrayOfStops[endOptionSelectedValue] + '</strong>  is: <span style="font-size:16px; color: F78B1E;" ><strong>&pound;' + normalPrice + '</strong></span></p><p>However the best value fare is the <strong>skylink Derby to East Midlands Airport easycard at &pound;21.00 for 10 journeys</strong>.';
		}
		if (x.startsWith('SE2-')) // should recommend the Skylink easycard 2 - Loughborough to East Midlands Airport £16.50 
		{
			var normalPrice = parseInt(x.substring(4));
			normalPrice = normalPrice/100;
			normalPrice = normalPrice.toFixed(2);
			singleFareString = '<p>The single fare from <strong>' + arrayOfStops[startOptionSelectedValue] + '</strong> to <strong>' + arrayOfStops[endOptionSelectedValue] + '</strong>  is: <span style="font-size:16px; color: F78B1E;" ><strong>&pound;' + normalPrice + '</strong></span></p><p>However the best value fare is the <strong>skylink Loughborough to East Midlands Airport easycard at &pound;16.50 for 10 journeys</strong>.';
		}
		if (x.startsWith('SE3-')) // should recommend the Skylink easycard 3 - Leicester to East Midlands Airport £21.00 
		{
			var normalPrice = parseInt(x.substring(4));
			normalPrice = normalPrice/100;
			normalPrice = normalPrice.toFixed(2);
			singleFareString = '<p>The single fare from <strong>' + arrayOfStops[startOptionSelectedValue] + '</strong> to <strong>' + arrayOfStops[endOptionSelectedValue] + '</strong>  is: <span style="font-size:16px; color: F78B1E;" ><strong>&pound;' + normalPrice + '</strong></span></p><p>However the best value fare is the <strong>skylink Leicester to East Midlands Airport easycard at &pound;21.00 for 10 journeys</strong>.';
		}		
	}
	
	
	if (x!='B') // only deal with return values if single value is not equal to 'B'
	{
	
		// deal with return fares
		if (!isNaN(y))  // return fares is a number
		{
			y = y/100;
			y = y.toFixed(2);	
			returnFareString = '<p>The return fare is <span style="font-size:16px; color: F78B1E;" ><strong>&pound;' + y + '</strong></span>.</p><p>And don\'t forget the &pound;10 family ticket for up to 2 adults and 3 kids.</p>';
		}
		else // return fare value is not  number
		{
			if (y=='X')
			{
				returnFareString = '';
			}
			if (y=='TE')
			{
				returnFareString = '<p>For a return journey please use the town easycard.</p><p>And don\'t forget the &pound;10 family ticket for up to 2 adults and 3 kids.</p>';
			}	
		}
	}
	
	farePriceDiv.innerHTML = singleFareString + returnFareString;

}




function setDropDowns(serviceSelectObj)
{
	var selectedServiceName = serviceSelectObj.options[serviceSelectObj.selectedIndex].value;
	stopsArrayName = selectedServiceName+"_stops";
	singleArrayName = selectedServiceName+"_single";
	returnArrayName = selectedServiceName+"_return";
	
	populateDropDown("startPoint", stopsArrayName);
	populateDropDown("endPoint", stopsArrayName);
}


function populateDropDown(selectElementId, arrayOfStopsName)
{
	eval("var arrayOfStops = " + arrayOfStopsName + ";");
	
	clearSelectOptions(selectElementId);
	for (var i=0; i<arrayOfStops.length; i++)
	{
		addOptionToEndOfSelect(selectElementId, arrayOfStops[i], i);
	}
}


function clearSelectOptions(selectElementId)
{
	var selectObj = $(selectElementId);
	while (selectObj.options.length > 0) 
	{
		if(Prototype.Browser.IE) 
		{
	       		selectObj.options.remove(0);
	        }
	        else
	        {
	        	selectObj.remove(0);
	        	// selectObj.remove(selectObj.options[0], null);  DOESNT WORK
	        }
	}	
}


function addOptionToEndOfSelect(selectElementId, strOptionText, strOptionValue)
{
	var selectObj = $(selectElementId);
	
	var optn = document.createElement("OPTION");
	optn.text = strOptionText;
	optn.value = strOptionValue;

	
	/*
	if(Prototype.Browser.IE) {
	    selectObj.options.add(optn, selectObj.options.length+1);
	}
	*/
	
	
	  try {
	    selectObj.add(optn, null); // standards compliant; doesn't work in IE
	  }
	  catch(ex) {
	    selectObj.add(optn); // IE only
  	}
	
	
	//else new Insertion.After(selectObj.options[0], '<option value="' + strOptionValue + '">' + strOptionText + '</option>');
}
