// JavaScript Document
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
//var http = getHTTPObject(); // We create the HTTP Object
var http;

var ih='';

var url = "loclookup.php?postcode="; // The server-side script
function handleHttpResponse() {
if (http.readyState == 4) {  
	  
document.getElementById('pcdropdiv').style.display='block';	  
	  
if (http.responseText.indexOf('invalid') == -1) {
	  document.getElementById('postcodediv').style.display='none';
      // Split the comma delimited response into an array
	  //alert(http.responseText);
	  var oneormul = http.responseText.split("|");	  
	  if(oneormul[0]=='1'){	  
      results = oneormul[1].split(",");	  
      document.getElementById('location').value = results[0]+','+results[1]+','+results[2]+','+results[3]+','+results[4];
      document.getElementById('pc').value = results[0]+' '+results[3]+', '+results[4];
	  document.getElementById('location').style.visibility='visible';
	  document.getElementById('pc').style.visibility='visible';
	  document.getElementById('loclabel').style.visibility='visible';	  	  
	  if(ih==null || ih==''){		  
	  }else{
	  document.getElementById('pcdropdiv').style.display='none';
	  }
	  }else{		  
		  document.getElementById('loclabel').style.visibility='visible';
		  document.getElementById('location').style.visibility='visible';		  
		  results1 = oneormul[1].split(",");		  
		  document.getElementById('location').value = results1[0]+','+results1[1]+','+results1[2]+','+results1[3]+','+results1[4];
		  ih = "<select name='pcdrop' id='pcdrop' onchange='locationload();'><option value='"+results1[0]+","+results1[1]+","+results1[2]+","+results1[3]+","+results1[4]+"'>"+results1[0]+" "+results1[3]+", "+results1[4]+"</option>"
		  for(i=2;i<=oneormul.length-1;i++){
			 //alert(oneormul[i]);
		  resultsx = oneormul[i].split(",");
		  ih = ih+"<option value='"+resultsx[0]+","+resultsx[1]+","+resultsx[2]+","+resultsx[3]+","+resultsx[4]+"'>"+resultsx[0]+" "+resultsx[3]+", "+resultsx[4]+"</option>";
		  }
		  ih = ih+"</select>";
		  document.getElementById('pcdropdiv').innerHTML = ih;
		  document.getElementById('pcdropdiv').style.display='block';
		  document.getElementById('pc').style.visibility='hidden';
	  }
	  //alert(document.getElementById('location').value);
      isWorking = false;
	  http.readyState = null;
	  http.responseText = null;
	  http = null;
    }
  }
}
var isWorking = false;

function updateLoc(pcdrop) {
	
	    http = getHTTPObject(); // We create the HTTP Object
		//if (!isWorking && (http != false)) {
		//alert('TEST');
			
		//if (!(isWorking)) {
		var locValue = document.getElementById("postcodeput").value;
	
		//document.getElementById('postcodediv').style.display='block';
		document.getElementById('pcdropdiv').style.display='none';	
		document.getElementById('postcodediv').innerHTML = '<img src="images/ajax-loader.gif" alt="Loading...">';						
		
		http.open("GET", url + escape(locValue), true);
		http.onreadystatechange = handleHttpResponse;
		
		if (http.readyState==4 || http.readyState=="complete"){
		document.getElementById('pcdrop').value=pcdrop;	
		}
		
		//alert('TEST');
		
		isWorking = true;
		http.send(null);		
		
		//}else{
			
		//}
}

function locationload(){
	var si = document.getElementById('pcdrop').options[document.getElementById('pcdrop').selectedIndex].value;
	//alert(si);
	document.getElementById('location').value = document.getElementById('pcdrop').options[document.getElementById('pcdrop').selectedIndex].value;	
}

