var ajaxRequestPostCode_Get;
var ajaxRequestPostCode_Post;
var httpAjaxRequest = getHTTPObject(); // defined in xmlhttp.js
var httpAjaxPostRequest = httpAjaxRequest;

var busy = 0;
var postBusy = 0;
var queue = new Array();
var postQueue = new Array();

function getRequestResponse () {
  return (httpAjaxRequest.responseText);
}

function handleAjaxRequestResponse (isPost) {
  if (isPost == true) {
    if (httpAjaxPostRequest.readyState == 4) {
      if (httpAjaxPostRequest.status == 200) {
        if (ajaxRequestPostCode_Post != null) {
//        alert("response:\n" + ajaxRequestPostCode_Post);
          eval(ajaxRequestPostCode_Post);
        }

        postBusy = 0;
        if (postQueue.length) {
          x = postQueue.shift();
          makeAjaxRequestPost(x['url'], x['postCode'], x['postVals']);
        }
      } else {
        alert('There was a problem with the request.');
      }
    }
  } else {
    if (httpAjaxRequest.readyState == 4) {
      if (ajaxRequestPostCode_Get != null) {
        eval(ajaxRequestPostCode_Get);
      }

      busy = 0;
      if (queue.length) {
        x = queue.shift();
        makeAjaxRequest(x['url'], x['postCode']);
      }
    }
  }
}

function handleAjaxRequestResponsePost () {
  handleAjaxRequestResponse (true);
}

function makeAjaxRequest (url, postCode) {
  if (busy != 1) {
    ajaxRequestPostCode_Get = postCode;

    httpAjaxRequest = getHTTPObject(); // defined in xmlhttp.js

    httpAjaxRequest.open("GET", url , true);
    httpAjaxRequest.onreadystatechange = handleAjaxRequestResponse;

    busy = 1;

    httpAjaxRequest.send(null);
  } else {
    x = new Array();
    x['url'] = url;
    x['postCode'] = postCode;

    queue.push(x);
  }
}

function makeAjaxRequestPost (url, postCode, assocArray) {
  if (postBusy != 1) {
    httpAjaxPostRequest = false;
    ajaxRequestPostCode_Post = postCode;

    httpAjaxPostRequest = getHTTPObject(); // defined in xmlhttp.js

    httpAjaxPostRequest.open("POST", url , true);
    httpAjaxPostRequest.onreadystatechange = handleAjaxRequestResponsePost;
    httpAjaxPostRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpAjaxPostRequest.setRequestHeader("Content-length", assocArray.length);
    httpAjaxPostRequest.setRequestHeader("Connection", "close");

    postBusy = 1;

    var sendString='';
    for (var i in assocArray ) {
      sendString += i + "=" + encodeURI(assocArray[i]) + "&"
    }

//  alert("sendString:\n" + sendString);
    httpAjaxPostRequest.send(sendString);
  } else {
    x = new Array();
    x['url'] = url;
    x['postCode'] = postCode;
    x['postVals'] = assocArray;

    postQueue.push(x);
  }
}
