// JavaScript Document

var ID_ERROR_MESSAGE= 0;
var ID_ADDRESS_1 = 1;
var ID_ADDRESS_2 = 2;
var ID_TOWN = 3;
var ID_COUNTY = 4;
var ID_POSTCODE = 5;
var ID_GRIDE = 6;
var ID_GRIDN = 7;
var ID_LONG = 8;
var ID_LAT = 9;

function GetAddress() {
	document.getElementById('Message').innerHTML = '';
	var entered_postcode = document.getElementById("Postcode").value;
	if (entered_postcode == "") {
		alert('Please Enter A Postcode.');
		return false;
	}

	document.getElementById('ProcessingImage').style.display = "inline";
	document.getElementById('btnGetAddress').style.display = "none";

	var url = "/AjaxGetAddress.php";
	var params = "Postcode=" + entered_postcode;

	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("POST", url, true);

		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var res_array = xmlhttp.responseText.split("|");
				var address_count = parseInt(res_array.length);
			
				if (address_count == 10) {
					if ((res_array[ID_ERROR_MESSAGE] == "Error: Invalid Postcode") || (res_array[ID_ERROR_MESSAGE] == "Error: Postcode Not Found"))
						alert(res_array[ID_ERROR_MESSAGE]);
					else {
						if (document.getElementById('Address1'))			document.getElementById('Address1').value = res_array[ID_ADDRESS_1];
						if (document.getElementById('Address2'))			document.getElementById('Address2').value = res_array[ID_ADDRESS_2];
						if (document.getElementById('Town'))					document.getElementById('Town').value = res_array[ID_TOWN];
						if (document.getElementById('County'))				document.getElementById('County').value = res_array[ID_COUNTY];
						if (document.getElementById('Postcode'))			document.getElementById('Postcode').value = res_array[ID_POSTCODE];
					}
				}
				else
					alert("INVALID POSTCODE");
			
				document.getElementById('btnGetAddress').style.display = "inline";
				document.getElementById('ProcessingImage').style.display = "none";
			}
		}
		xmlhttp.send(params);
	} catch (e) {
	}
}

function get_address_success(res) {

	



}
