// UK Postcode Lookup JavaScript Document

function postcodeSearch() {
	$('#postcodeSearch').html(''); // reset field
	$('#postcodeError').html('');
	
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "building=" + $('input#building').val() + "&postcode=" + $('input#postcode').val(),
		success: function(response){
			$('#postcodeSearch').html(response);
			// change the please wait
			$('#locate_button').show();
			$('#locate_wait').hide();
			/*
			if (response.indexOf('valid') <= 0) { // Not invalid hide the building input
				$('#building').parent('div').parent('div').hide();
				$('#building').val('');
			}
			else {
				$('#building').parent('div').parent('div').show();	
			}
			*/
		},
		error: function(response) {
			$('#postcodeSearch').html(response);
			// change the please wait
			$('#locate_button').show();
			$('#locate_wait').hide();
			/*
			if (response.indexOf('valid') <= 0) { // Not invalid hide the building input
				$('#building').parent('div').parent('div').hide();
				$('#building').val('');
			}
			else {
				$('#building').parent('div').parent('div').show();	
			}
			*/
		}
	});
}

function addressInput(moniker) {
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "moniker=" + moniker,
		success: function(response){
			$('#postcodeSearch').html(response);
		},
		error: function(response) {
			$('#postcodeSearch').html(response);
		}
	});

}

function notlisted(error) {
	/*
	if (error == 'error') {
		// Hide the building input
		$('#building').parent('div').parent('div').hide();
		$('#building').val('');	
	}
	*/
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "notlisted=true&error="+error,
		success: function(response){
			$('#postcodeSearch').html(response);
		},
		error: function(response) {
			$('#postcodeSearch').html(response);
		}
	});

}

// delivery functions 
function shippostcodeSearch() {
	$('#shippostcodeSearch').html(''); // reset field
	$('#shippostcodeError').html('');
	
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "building=" + $('input#shipbuilding').val() + "&postcode=" + $('input#shippostcode').val() + "&shipping=true",
		success: function(response){
			$('#shippostcodeSearch').html(response);
			// change the please wait
			$('#shiplocate_button').show();
			$('#shiplocate_wait').hide();
			/*
			if (response.indexOf('valid') <= 0) { // Not invalid hide the building input
				$('#shipbuilding').parent('div').parent('div').hide();
				$('#shipbuilding').val('');
			}
			else {
				$('#shipbuilding').parent('div').parent('div').show();	
			};
			*/
		},
		error: function(response) {
			$('#shippostcodeSearch').html(response);
			// change the please wait
			$('#shiplocate_button').show();
			$('#shiplocate_wait').hide();
			/*
			if (response.indexOf('valid') <= 0) { // Not invalid hide the building input
				$('#shipbuilding').parent('div').parent('div').hide();
				$('#shipbuilding').val('');
			}
			else {
				$('#shipbuilding').parent('div').parent('div').show();	
			};
			*/
		}
	});
}

function shipaddressInput(moniker) {
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "moniker=" + moniker + "&shipping=true",
		success: function(response){
			$('#shippostcodeSearch').html(response);
		},
		error: function(response) {
			$('#shippostcodeSearch').html(response);
		}
	});

}

function shipnotlisted(error) {
	/*
	if (error == 'error') {
		// Hide the building input
		$('#shipbuilding').parent('div').parent('div').hide();
		$('#shipbuilding').val('');	
	}
	*/
	$.ajax({
		type: "POST",
		url: "/inc/postcodeSearch.php",
		data: "notlisted=true&error=" + error + "&shipping=true",
		success: function(response){
			$('#shippostcodeSearch').html(response);
		},
		error: function(response) {
			$('#shippostcodeSearch').html(response);
		}
	});

}

function addressOK() {	
	if ( ($('#street_address').length <= 0) && (window.notlisted) ) {
		notlisted('error');
		passback = false;
	}
	else {
		passback = true;
	}
	
	if ($("input[@name='copy']:checked").val() != 'Y') {
		if ( ($('#ShipAddress').length <= 0) && (window.shipnotlisted) ) {
			shipnotlisted('error');
			passback = false;
		}
		else {
			passback = true;
		}	
	}
	
	return passback;
}

$(document).ready(function() {
	
	$('#locate_button').click(function() {
		// change the find address
		$('#locate_button').hide();
		$('#locate_wait').show();
		
		postcodeSearch();
		return false;
	});
	
	$('#shiplocate_button').click(function() {
		// change the find address
		$('#shiplocate_button').hide();
		$('#shiplocate_wait').show();
		
		shippostcodeSearch();
		return false;
	});
	
	$('#country').change(function() {
		if ( ($('#country').val() != 222) && ($('#country').val() != 'United Kingdom') ) {
			notlisted();
			$('#postcode').val('');	
			// Hide the building input
			$('#building').parent('div').parent('div').hide();
			$('#building').val('');	
			// Hide the button
			$('#locate_button').hide();
			$('#street_address').focus();
		}
		else {
			$('#building').parent('div').parent('div').show();
			$('#locate_button').show();
			$('#postcodeSearch').html(''); // reset field
		}
	});
	
	$('#shipcountry').change(function() {
		if ($('#shipcountry').val() != 222) {
			shipnotlisted();
			$('#shippostcode').val('');
			// Hide the building input
			$('#shipbuilding').parent('div').parent('div').hide();
			$('#shipbuilding').val('');	
			// Hide the button
			$('#shiplocate_button').hide();
			$('#ShipAddress').focus();
		}
		else {
			$('#shipbuilding').parent('div').parent('div').show();
			$('#shiplocate_button').show();
			$('#shippostcodeSearch').html(''); // reset field
		}
	});
	
	$('#shiplocate_wait').hide();
	$('#locate_wait').hide();
});

