/*
	Journey quote form.
	© Gareth Harding 11th August 2011.
	Permitted for use on nazminibushire.co.uk only.		
	May not be reporduced in anyway without written consent from author.
*/
	var fLocationChosen = 0;
	var tLocationChosen = 0;
	
	
$(document).ready(function() {
	

	

	// Address Lookup handler
	$('#btnSubmitFromAddress').click(function(ev) {
		ev.preventDefault();
		sendValueF($("#fAddr").val());
	});
	
	$('#aSubmitFromAddress').click(function(ev) {
		ev.preventDefault();
		sendValueF($("#fAddr").val());
	});	
	
	
	$('#btnSubmitToAddress').click(function(ev) {
		ev.preventDefault();
		sendValueT($("#tAddr").val());
	});

	$('#aSubmitToAddress').click(function(ev) {
		ev.preventDefault();
		sendValueT($("#tAddr").val());
	});

	
	$('#addrFromResults > a').live('click', function() {

		var fullAddress = $(this).text();
		$('#fAddr').val(
			
			fullAddress
		);
		$('#addrFromResults').text("");
		fLocationChosen = 1;
		
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();
			//fade them both out
		});

	});
	
	$('#addrToResults > a').live('click', function() {
		var fullAddress = $(this).text();
		$('#tAddr').val(fullAddress);
		$('#addrToResults').text("");
		tLocationChosen = 1;
		
		$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();
			//fade them both out
		});
	});	

	
	
});

function sendValueD(strF, strT) {
	$('#distanceCalculated').html("calc.");
	$.post("script/getDistance.php", {
		sendToValueF : strF,
		sendToValueT : strT
	}, function(data) {

		if (data.returnFromValue===null) {
			$('#distanceCalculated').html("Unable to retrieve distance.");
		} 
		else{
			$('#distanceCalculated').html(data.returnFromValue);			
		};
		
	}, "json");
}

function sendValueF(str) {
	$('#addrFromResults').html("Searching for address.");
	$.post("script/getFormattedAddress.php", {
		sendToValue : str
	}, function(data) {

		if (data.returnFromValue===null) {
			$('#addrFromResults').html("No matches found.");
		} 
		else{
			$('#addrFromResults').html("<h3>Please choose your location</h3>" + data.returnFromValue);			
		};
		
	}, "json");
	
	
	$('#fade , .popup_block').fadeOut(function() {
		$('#fade, a.close').remove();
		//fade them both out
	});
	
}

function sendValueT(str) {
	$('#addrToResults').html("Searching for address.");
	$.post("script/getFormattedAddress.php", {
		sendToValue : str
	}, function(data) {
		if (data.returnFromValue===null) {
			$('#addrToResults').html("No matches found.");
		} 
		else{
			$('#addrToResults').html("<h3>Please choose your location</h3>" + data.returnFromValue);			
		};
	}, "json");


}
