var ajax = new Array();

function getSuburbs(stateId, pcodeId, suburbId) {
  var sel = document.getElementById(stateId);
  var stateCode = sel.options[sel.selectedIndex].value;

  document.getElementById(suburbId).options.length = 0; // Empty suburb select box
  if (stateCode.length>0) {
    ajax[0] = new sack();
    
    var pCode = document.getElementById(pcodeId).value;
    ajax[0].requestFile = 'ajax-suburbs.php?getSuburbsByState=1&state='+stateCode+'&pcode='+pCode;
    ajax[0].onCompletion = function(){ createSuburbs(stateId, pcodeId, suburbId) }; // callback
    ajax[0].runAJAX(); // Execute AJAX function
  }
}

function createSuburbs(stateId, pcodeId, suburbId) {
  if (ajax[0].response != "") {
    var suburbList = ajax[0].response.split("|");
    var sel = document.getElementById(suburbId);

    var newOption = new Option("", "");

    sel.options.add(newOption, sel.options.length);

    for (var i=0;i<suburbList.length;i++) {
      if (suburbList[i] != "") {
	newOption = new Option(suburbList[i], suburbList[i]);
	sel.options.add(newOption, sel.options.length);
      }
    }
  }
}
