//  These two variables store the previous values for Suburb and Region.
//  These are checked to prevent unecassary trips to the server.
var tempSuburb = "";
var tempRegion = "";

function getPostcode(region, suburb)
{
    if (tempRegion != region || tempSuburb != suburb)
    {
        if (region.toUpperCase() != "NULL" && suburb != "")
            getHTMLViaXML(XML_POSTCODE, region + cGlobalDelim + suburb);
    }

    tempRegion = region;
    tempSuburb = suburb;
}

function fillInPostcode(postcodes)
{
    var values;

    if (postcodes == "")
    {
        document.getElementById("Postcode").value = "Not Found";
    }
    else
    {
        var postcodeList = postcodes.split(cGlobalDelim);

        if (postcodeList.length == 1)
        {
            // A broader search may still only return 1 row,
            // so we still need to handle this.
            
            values = postcodeList[0].split(cColDelim);

            if (values.length > 1)
                processMultiplePostcodes(postcodeList);
            else
                fillInOnePostcode(postcodeList[0]);
        }
        else
        {
            processMultiplePostcodes(postcodeList);
        }
    }
}

function processMultiplePostcodes(list)
{
    fillInMultiplePostcodes(list);
    setFooterPosition(PUSHED_FOOTER_TOP);
}

function fillInOnePostcode(postcode)
{
    document.getElementById("Postcode").value = postcode;
    displaySection("divPostcodeSelectPlace", false);
}

function fillInMultiplePostcodes(postcodes)
{
    var values;

    displaySection("divPostcodeSelectPlace", true);
    displaySection("divAddressSelectPlace", false);

    clearSelect("SuburbList");
    
    for (var index = 0; index < postcodes.length; index++)
    {
        values = postcodes[index].split(cColDelim);
        createOption("SuburbList", values[0] + cColDelim + values[1], values[0] + " " + values[1] + ", " + document.getElementById("Region").value);
    }
}

function fillInSuburbAndPostcode(value)
{
    var values = value.split(cColDelim);
    
    document.getElementById("Suburb").value = values[0];
    document.getElementById("Postcode").value = values[1];
    document.getElementById("Suburb").className = "valid";
    tempSuburb = values[0];

    clearSelect("SuburbList");
    displaySection("divPostcodeSelectPlace", false);
    setFooterPosition(FOOTER_TOP);
    document.getElementById("Postcode").focus();
}