function trim(str) { 
    str = str.replace(/^\s*/, '').replace(/\s*$/, ''); 

   return str;
} 

function alertDisabled() {
	//if (document.getElementById(element).disabled = true) {
		alert('You must use the postcode validation field first')
	//}
}

// function unDisableFields() {
// 	fields = Array('f_address1','f_address1','f_address3','f_townCity','f_countyState','f_postcode'); // lets tidy the code up at some point... (Rich)
// 	
// 	for (i=fields.length-1;i>=0;i--) // un disable the fields
// 		{
// 			document.getElementById('')
// 			document.getElementById(fields[i]).disabled = false;
// 		}
// }

function alertAboutPostcode() {
	alert('You must ensure your postcode is valid');
}

// Script to fetch address details using xmlhttp request
//var url = "postcode/PHP/postcode_xml-backup.php?postcode="; // The server-side script
var url = "includes/lookup-postcode.php?fdelim=,&rdelim=,&postcode="; // The server-side script
function handleHttpResponse() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText.split(",");
    
	if (trim(results[0])=="ERROR")
		{
			alert('That postcode was not found, please either try another or enter your details manually');
			document.getElementById('f_postcodeLookup').value = '';
			enterAddressLookup();
			// document.getElementById('checkerLink').innerHTML = "<a href=\"javascript:lookupPostcode();\">Click here to find your address</a>";
		} else
		{
			
			var count = 0;
			var selectBox = "<select id=\"postcodeIdDropDown\" name=\"postcodeIdDropDown\" onChange=\"getAddressFromPostcode();\"><option value=\"\">Please select your address from below</option>";;
					
			i=0;
			
			while (i<results.length)
				{
					var postcode_id = i;
					var address = i+1;
					selectBox = selectBox + "<option value=\""+trim(results[postcode_id])+"\">"+results[address]+"</option>";
					i=i+2;
				}
			
			selectBox = selectBox + "</select>";
			
			
			// Show
			document.getElementById('addressOptionsDiv').style.display = 'block';
			document.getElementById('addressSelectField').innerHTML = selectBox;
			document.getElementById('postcodeLookupSelect').style.display = 'block';
			
			// Show
			document.getElementById('differentAddressText').style.display = 'block';
			// Hide
			document.getElementById('postcodeIdDropDown').focus();
			

		}
  }
}

function handleHttpResponse2() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results2 = http.responseText.split(",");
  
    	/// document.getElementById('addressFields').style.display = 'block';
		var re= /<\S[^>]*>/g; 
		if (results2[0] != null) { document.getElementById('f_address1').value = trim(results2[0].replace(re,'')); document.getElementById('f_address1').style.color = '#000000'; } else { document.getElementById('f_address1').value = ""; }
    	if (results2[1] != null) { document.getElementById('f_address2').value = results2[1]; document.getElementById('f_address2').style.color = '#000000'; } else { document.getElementById('f_address2').value = ""; }
		if (results2[2] != null) { document.getElementById('f_town').value = results2[2]; document.getElementById('f_town').style.color = '#000000';} else { document.getElementById('f_town').value = ""; }
		if (results2[3] != null) { document.getElementById('f_county').value = results2[3]; document.getElementById('f_county').style.color = '#000000';} else { document.getElementById('f_county').value = ""; }
		if (results2[4] != null) { document.getElementById('f_postcode').value = results2[4]; document.getElementById('f_postcode').style.color = '#000000';} else { document.getElementById('f_postcode').value = ""; }
	
		// Show
		document.getElementById('differentAddressEditText').style.display = 'block';
		
		//Hide
		document.getElementById('differentAddressText').style.display = 'none';
		
		var addressFields = new Array('f_address1', 'f_address2', 'f_address3', 'f_town', 'f_county', 'f_postcode');
		
		for(var i = 0; i < addressFields.length; i++) {
			unsetError(addressFields[i]);
		}
		
		showHideAddressFields('block')
 		showHideInactiveAddressFields(1);
		
  }
}

function lookupPostcode() 
	{
	 var postcode = document.getElementById("f_postcodeLookup").value;
	 document.getElementById('differentAddressEditText').style.display = 'none';
	  
	  if (postcode == "")
		{
			alert("You must enter a postcode for us to find your address");
		} else
		{
		  // Hide
		  document.getElementById('inputAddressText').style.display = 'none';
		  // document.getElementById('checkerLink').innerHTML = "Checking Postcode..."; //url + escape(postcode) + "&building_name=" + escape(building_name)
		  http.open("GET", url + escape(postcode), true);
		  http.onreadystatechange = handleHttpResponse;
		  http.send(null);
		  // document.getElementById('checkerLink').innerHTML = "<a href=\"javascript:lookupPostcode();\">Click here to find your address</a>";
	      
	      document.getElementById('postcodeLookupFields').style.display = 'none';
	      document.getElementById('editManualLink').style.display = 'none';
		}
	}

 
 function getAddressFromPostcode() {
 
  //var url2 = "postcode/PHP/address_xml.php?postcode_id=";
  var url2 = "includes/lookup-address.php?fdelim=,&fields=line1,line2,town,county,postcode&addressID=";
  var postcode_id = document.getElementById("postcodeIdDropDown").value;
 
  http.open("GET", url2 + escape(postcode_id), true);
 
  http.onreadystatechange = handleHttpResponse2;
  http.send(null);
 
 }
 
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
