function checkPostcode() {  // This function does the AJAX request
document.getElementById('postcodeMsg').innerHTML = '<img src="/images/indicator_arrows.gif" />';
http.open("GET", "/functions/f.checkPostcode.php?postcode="+document.getElementById('postcode').value, true);
  http.onreadystatechange = getHttpRes;
  http.send(null);
}

function getHttpRes() {
  if (http.readyState == 4) { 
    res = http.responseText;  // These following lines get the response and update the page
    
	if(res == "true") {
		// Show user confirmation tick		
		document.getElementById('errorMsg').innerHTML = "";
		document.quote.postcode.style.background="#FFFFFF";
		document.getElementById('postcodeMsg').innerHTML = '<img src=\"/images/tick.gif\" alt=\"OK!\" title=\"OK!\" />';
		document.getElementById('quantityMsg').innerHTML = '<img src=\"/images/required.gif\" alt=\"Required Field\" title=\"Required Field\" style=\"margin-top:3px;\" />';
		document.getElementById('fuelTypeMsg').innerHTML = '<img src=\"/images/required.gif\" alt=\"Required Field\" title=\"Required Field\" style=\"margin-top:5px;\" />';
//		document.getElementById('deliveryDateMsg').innerHTML = '<img src=\"/images/required.gif\" alt=\"Required Field\" title=\"Required Field\" style=\"margin-top:3px;\" />';
		document.getElementById('businessMsg').innerHTML = '<a href=\"javascript:customerType()\"><img src=\"/images/question.jpg\" style=\"margin-top:0px;" alt=\"Usage Explained\" title=\"Usage Explained - Click Here\" /></a>';
		// Display additional labels
		document.getElementById('quantityLabel').innerHTML = '<label for \"quantity\">Quantity:</label>';
		document.getElementById('fuelTypeLabel').innerHTML = '<label for \"fuelType\">Fuel Type:</label>';
		document.getElementById('deliveryDateLabel').innerHTML = '<label for \"deliveryDate\">Delivery Date:</label>';
		document.getElementById('businessLabel').innerHTML = '<label for \"business\">Usage:</label>';
		// Display corresponding fields
		document.getElementById('postcodeField').innerHTML = '<input name=\"postcode\" type=\"text\" id=\"postcode\" size=\"7\" maxlength=\"8\" disabled=\"disabled\" value=\"'+document.getElementById('postcode').value+'" />';
		document.getElementById('quantityField').innerHTML = '<input name=\"quantity\" type=\"text\" id=\"quantity\" size=\"7\" maxlength=\"5\" />';
		document.getElementById('errorMsg').innerHTML = '<p style=\"color:green; font-size:11px;\">Please enter the quantity in litres.</p>';
		document.forms.quote.quantity.focus();
		
		document.getElementById('fuelTypeField').innerHTML = '<select name=\"fuelType\" id=\"fuelType\" onfocus=\"checkQuantity();\" onchange=\"tickIt(\'fuelType\'); \"><option selected=\"selected\">Choose</option><option value=\"kerosene\" onclick=\"checkQuantity(); tickIt(\'fuelType\');\">Kerosene</option></select>';
		
		document.getElementById('deliveryDateField').innerHTML = getDates();
		
		document.getElementById('businessField').innerHTML = '<select name=\"business\" id=\"business\" onfocus=\"checkQuantity();\"><option selected=\"selected\" value=\"personal\" onclick=\"checkQuantity();\">Residential</option><option value=\"business\" onclick=\"checkQuantity(); \">Commercial</option></select>';
		
		document.getElementById('getQuoteField').innerHTML = '<input type=\"image\" src=\"/images/getQuoteBtn.gif\" alt=\"Get Quote\" onclick=\"businessConfirm(); return false;\" style=\"margin-bottom:10px;\" class=\"blueBtn\" /><p style=\"margin-left:10px;\"><a href=\"javascript:resetForm()\">Reset Form</a></p>';
		
	}
	if(res == "false") {
		document.getElementById('postcodeMsg').innerHTML = '<img src=\"/images/error.gif\" alt=\"Error\" title=\"Error: Sorry, we do not deliver to this postcode area.\" />';
		document.getElementById('errorMsg').innerHTML = "<p class=\"error\">Sorry, we do not currently deliver to this postcode area.</p>";
		document.quote.postcode.style.background="#FFA8B0";
		document.forms.quote.postcode.focus();
		return false;
	}
	if(res == "empty") {
		document.getElementById('postcodeMsg').innerHTML = '<img src=\"/images/error.gif\" alt=\"Error\" title=\"Error: Please enter your postcode in full.\" />';
		document.getElementById('errorMsg').innerHTML = "<p class=\"error\">Please enter your postcode in full.</p>";
		document.quote.postcode.style.background="#FFA8B0";
		document.forms.quote.postcode.focus();
		return false;
	}	
	if(res == "short") {
		document.getElementById('postcodeMsg').innerHTML = '<img src=\"/images/error.gif\" alt=\"Error\" title=\"Error: The postcode you entered is too short. Please enter your full postcode.\" />';
		document.getElementById('errorMsg').innerHTML = "<p class=\"error\">Please enter your postcode in full.</p>";
		document.quote.postcode.style.background="#FFA8B0";
		document.forms.quote.postcode.focus();
		return false;
	}	
	if(res == "partial") {
		document.getElementById('postcodeMsg').innerHTML = '<img src=\"/images/error.gif\" alt=\"Error\" title=\"We may be able to deliver to you...\" />';
		document.getElementById('errorMsg').innerHTML = "<p class=\"error\">We are able to deliver to some addresses in this postcode area.</p><p class=\"error\">Please call us on 01262 673346 to check if we can deliver to you.</p>";
		document.quote.postcode.style.background="#FFA8B0";
		document.forms.quote.postcode.focus();
		return false;
	}
  }
}

function getXHTTP() {
  var xhttp;
   try {   // The following "try" blocks get the XMLHTTP object for various browsers…
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
 		 // This block handles Mozilla/Firefox browsers...
	    try {
	      xhttp = new XMLHttpRequest();
	    } catch (e3) {
	      xhttp = false;
	    }
      }
    }
  return xhttp; // Return the XMLHTTP object
}

var http = getXHTTP(); // This executes when the page first loads.
