//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {

	if (window.XMLHttpRequest) {

		return new XMLHttpRequest();

	} else if(window.ActiveXObject) {

		return new ActiveXObject("Microsoft.XMLHTTP");

	} else {

		alert("Error");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();
var try_count = 3;

//Starts the AJAX request.
function getlocationandsizes() {

	ajax_path = "";
	timestamp = Number(new Date());
	outcode = document.getElementById("quote_postoutcode");

	if (outcode.value == "") {

		alert("Sorry, we missed the first part of your postcode!");
		outcode.focus();
		return false;
	}

	ajax_path = "http://www.storage-units.co.uk/process/quotation/getlocationandsizes.php?timestamp=" + timestamp + "&outcode=" + outcode.value;

	if (searchReq.readyState == 4 || searchReq.readyState == 0) {

		searchReq.open("GET", ajax_path, true);
		searchReq.onreadystatechange = RespondHandler; 
		searchReq.send(null);		
	}
}

//Called when the AJAX response is returned.
function RespondHandler() {

	if (searchReq.readyState == 4) {

		// get size box
		select = document.getElementById("quote_size");

		// clear out existing items
		select.options.length = 0;

		//get response data
		data = searchReq.responseText.split(":");

		select.options.add(new Option("Select...", "#"));

		// populate with location sizes
		for(var i=0; i < data.length-1; i++) {

    			var d = data[i];

    			select.options.add(new Option(d+" sq ft", d));
		}
	}
}


