function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
	    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	    catch(f) { xmlhttp=null; }
	}
	if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	  	xmlhttp=new XMLHttpRequest();
	}
	return  xmlhttp;
}

function postcoderGetThrfareAddress(postcode_el,county_el,city_el,address2_el) {
    
    var req= new XMLHttpRequest();
    var url='xml_postcoder_getthraddress.php?postcode='+postcode_el.value;
    if (arguments.length > 4) {
	div = arguments[4];
    	div.style.display="";
    }
    req.open("GET", url, true);
    req.onreadystatechange = function () {
	if (req.readyState == 4) {
	    if (req.status == 200) {
		xml= req.responseXML;
		nodes=xml.childNodes.item(0).childNodes;
		if(nodes.length >= 1) {
			county_el.value=nodes.item(0).attributes.getNamedItem('county').textContent;
			city_el.value=nodes.item(0).attributes.getNamedItem('post_town').textContent;
			address2_el.value=nodes.item(0).attributes.getNamedItem('street').textContent;
		}
		if (div)
    			div.style.display="none";
	
	    }
	}
    }
    
    req.send(null);
}

function postcoderFillDetails(ref) {
    city = document.getElementById('city['+ref+']');
    county = document.getElementById('addr_home_county['+ref+']');
    post_code = document.getElementById('addr_home_pc['+ref+']');
    country = document.getElementById('addr_home_country['+ref+']');
    address2 = document.getElementById('address2['+ref+']');
    if(post_code.value == '') {
	alert("Please select a postal code.");
	return false;
    }
    div = document.getElementById('making_request['+ref+']');
    postcoderGetThrfareAddress(post_code,county,city,address2,div);
    country.value="United Kingdom";
    return true;
}

function postcoderGetAddressesList(callback) {
    var req= createRequestObject();
    postcode_el = document.getElementById('postal_code');
    address1_el = document.getElementById('street');
    //address2_el = document.getElementById('address2');
    //county_el = document.getElementById('county');
    city_el = document.getElementById('town');
    
    var url='xml_postcoder_getpremiseslist.php?postcode='+postcode_el.value;
    if (arguments.length > 1) {
	div = arguments[1];
    	div.style.display="";
    }
    //req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.open("GET", url, true);
    req.onreadystatechange = function () {
	if (req.readyState == 4) {
	    if (req.status == 200) {
		xml= req.responseXML;
		var list=new Array;
		var ie;
		if (document.all) ie=true;
		else ie=false;
		if (ie) nodes=xml.documentElement.childNodes;
		else nodes=xml.childNodes.item(0).childNodes;
		if(nodes.length>=1) {
				for(i=0;i<nodes.length;i++) {
					list[i]=new Object;
					if (ie) {
						list[i].address1=nodes(i).getAttribute('organisation');
						list[i].address2=nodes(i).getAttribute('premise');
						list[i].address3=nodes(i).getAttribute('street');
						list[i].county=nodes(i).getAttribute('county');
						list[i].city=nodes(i).getAttribute('post_town');
						list[i].dependent_locality=nodes(i).getAttribute('dependent_locality');
	
					} else {
						list[i].address1=nodes.item(i).attributes.getNamedItem('organisation').textContent;
						list[i].address2=nodes.item(i).attributes.getNamedItem('premise').textContent;
						list[i].address3=nodes.item(i).attributes.getNamedItem('street').textContent;
						list[i].county=nodes.item(i).attributes.getNamedItem('county').textContent;
						list[i].city=nodes.item(i).attributes.getNamedItem('post_town').textContent;
						list[i].dependent_locality=nodes.item(i).attributes.getNamedItem('dependent_locality').textContent;
					}
				}
				callback(list);
		}
		if (div)
    			div.style.display="none";
	
	    }
	}
    }
    
    req.send(null);
}

function openHotelChoices(list) {
	el = document.getElementById('street');
	el.choices = list;
	win = window.open("/postcoder_address_choice_pop.php",'choice_address','width=800,height=550,toolbar=no,locationbar=no,statusbar=no,menubar=no,scrollbars=yes,dependent=yes,alwaysRaised=yes');
}

function postcoderGetAddresses() { 
   post_code = document.getElementById('postal_code');
   country = document.getElementById('country');
   if(post_code.value == '') {
	alert("Please enter a postal code.");
	return false;
    }
    div = document.getElementById('making_request');
    postcoderGetAddressesList(openHotelChoices,div);    
    country.value="United Kingdom";
}
