function getXMLHTTPRequest()
{

  var request = false;

  try
  {

    request = new XMLHttpRequest();
  }
  catch(err1)
  {
    try
    {

      request = new ActiveXObject("Msxml2.XMLHTTP.5.0");

    }
    catch(err2)
    {
      try
      {

        request = new ActiveXObject("Msxml2.XMLHTTP.4.0");

      }
      catch(err3)
      {

        try
        {

          request = new ActiveXObject("Microsoft.XMLHTTP");

        }
        catch(err4)
        {

          return false;

        }

      }

    }

  }

  //alert('here');

  return request;

}

var myRequest = getXMLHTTPRequest();

function responseAjax()
{

  if ( myRequest.readyState == 4 )
  {

    if (myRequest.status == 200 || myRequest.status == 304 )
    {

      var distanceNode = myRequest.responseXML.getElementsByTagName("distancedata");
      var howfar = distanceNode[0].getElementsByTagName("distance")[0].firstChild.nodeValue;

      document.getElementById('distance').innerHTML = 'The distance between the two postcodes as the crow flies is approximately: ' + howfar  + " miles";

    }
    else
    {

      // put something here is the server returns and error code

    }

  }

}

function callAjax()
{

  var a = document.postcode.a.value;
  var b = document.postcode.b.value;
  var myRandom = parseInt( Math.random()*99999999 );
  var url = "/postcode.php?a=" + a + "&b=" + b + "&rand=" + myRandom;

  // build the get request
  myRequest.open( "GET", url, true );

  myRequest.onreadystatechange = responseAjax;

  myRequest.send(null);

}


