/***********************************************************************
	On fully loaded document
************************************************************************/
$(document).ready(function(){

	/***********************************************************************
		Book and Quote component
	************************************************************************/
	
	
	setQuoteAndBookTab($("#journey_type").val()); // set initial tabs
	
	$("#btnQuoteBook").click(function(){
		$("#form_action").val("book_quote");
		$("#mainForm").submit();
	});
	$("#btnQuoteBookNow").click(function(){
		$("#form_action").val("book_quote");
		$("#mainForm").submit();
	});
	
	
	$("#tabTo").click(function(){
		//
		setQuoteAndBookTab("to_airport");
	});
	
	$("#tabFrom").click(function(){
		//
		setQuoteAndBookTab("from_airport");		
	});
	
	$("#tabGeneral").click(function(){
		//
		setQuoteAndBookTab("general");		
	});
	
	/**
	Quote Page Booking
	**/
	$("#btnBook").click(function(){
		$("#form_action").val("book");
		$("#mainForm").submit();
	});
	
    // collection address lookup
    $("#btnCollPostcodeLookup").click(function(){
    	//
    	var pc = $("#book_coll_postcode").val();
    	if (pc == "") return;
    	//
		$.getJSON('../ajax/ajax_premises_list.php', {'pc': pc}, function(data) {
			if (data['error'] == "")
			{
				var premisesArray = data['premises'];
				var list = '<option id="-1">--- addresses ---</option>';
				for (key in premisesArray)
				{
					list += '<option value="' + key  + '">' + premisesArray[key]  + "</option>";
				}
				$("#coll_address_list").html (list); 
				$("#coll_address_list").show();
				//
				$("#coll_postcode_lookup").hide();
			}
			else
			{
				alert ("Unable to find postcode. A full postcode is required.");
			}
    	});
    });
    
    // destination address lookup
    $("#btnDestPostcodeLookup").click(function(){
    	//
    	alert ("here");
    	var pc = $("#book_dest_postcode").val();
    	if (pc == "") return;
    	//
		$.getJSON('../ajax/ajax_premises_list.php', {'pc': pc}, function(data) {
			if (data['error'] == "")
			{
				var premisesArray = data['premises'];
				var list = '<option id="-1">--- addresses ---</option>';
				for (key in premisesArray)
				{
					list += '<option value="' + key  + '">' + premisesArray[key]  + "</option>";
				}
				$("#dest_address_list").html (list); 
				$("#dest_address_list").show();
				//
				$("#dest_postcode_lookup").hide();
			}
			else
			{
				alert ("Unable to find postcode. A full postcode is required.");
				//alert (data['error']);
			}
    	});
    });
    
    // login option
	$("#btnLogin").click(function(){
		$("#form_action").val("login");
		$("#mainForm").submit();
	});
    // logout option
	$("#btnLogout").click(function(){
		$("#form_action").val("logout");
		$("#mainForm").submit();
	});	
});

/***
Additional functionality
**/
function setQuoteAndBookTab (theJourneyType)
{
	if (theJourneyType == "to_airport")
	{
		$("#coll_airport_container").hide();
		$("#coll_postcode_container").show();
		$("#drop_airport_container").show();
		$("#drop_postcode_container").hide();
		//
		$("#tabTo").addClass("selected");
		$("#tabFrom").removeClass("selected");
		$("#tabGeneral").removeClass("selected")
	}
	else if (theJourneyType == "from_airport")
	{
		$("#coll_airport_container").show();
		$("#coll_postcode_container").hide();
		$("#drop_airport_container").hide();
		$("#drop_postcode_container").show();
		//
		$("#tabTo").removeClass("selected");
		$("#tabFrom").addClass("selected");
		$("#tabGeneral").removeClass("selected")
	}
	else
	{
		$("#coll_airport_container").hide();
		$("#coll_postcode_container").show();
		$("#drop_airport_container").hide();
		$("#drop_postcode_container").show();
		//
		$("#tabTo").removeClass("selected");
		$("#tabFrom").removeClass("selected");
		$("#tabGeneral").addClass("selected")
	}
	// set the journey type
	$("#journey_type").val(theJourneyType);
}

