var minPriceValue    = new Array("0", "100", "150", "200", "300", "400", "500", "600", "700", "800", "1000", "1200", "1500");
var maxPriceValue    = new Array("100", "150", "200", "300", "400", "500", "600", "700", "800", "1000", "1200", "1500", "2147483647");

var minPriceValueSale    = new Array("0", "100000", "150000", "200000", "300000", "400000", "500000", "600000", "700000", "800000", "1000000", "1200000", "1500000");
var maxPriceValueSale    = new Array("100000", "150000", "200000", "300000", "400000", "500000", "600000", "700000", "800000", "1000000", "1200000", "1500000", "2147483647");


var saleMinPriceText     = new Array("$0.00", "$100,000.00", "$150,000.00", "$200,000.00", "$300,000.00", "$400,000.00", "$500,000.00", "$600,000.00", "$700,000.00", "$800,000.00", "$1,000,000.00", "$1,200,000.00", "$1,500,000.00");
var saleMaxPriceText     = new Array("$100,000.00", "$150,000.00", "$200,000.00", "$300,000.00", "$400,000.00", "$500,000.00", "$600,000.00", "$700,000.00", "$800,000.00", "$1,000,000.00", "$1,200,000.00", "$1,500,000.00", "> $1,500,000.00");
var rentMinPriceText     = new Array("$0.00", "$100.00", "$150.00", "$200.00", "$300.00", "$400.00", "$500.00", "$600.00", "$700.00", "$800.00", "$1,000.00", "$1,200.00", "$1,500.00");
var rentMaxPriceText     = new Array("$100.00", "$150.00", "$200.00", "$300.00", "$400.00", "$500.00", "$600.00", "$700.00", "$800.00", "$1,000.00", "$1,200.00", "$1,500.00", "> $1,500.00");

orderValuesForSales       = new Array("listed", "suburb", "price", "beds", "updated", "auction")
orderValuesForRent      = new Array("listed", "suburb", "rent", "beds", "updated")
orderDescForRent         = new Array("Most Recent", "Suburb", "Rent", "Bedrooms", "Updated");
orderDescForSales        = new Array("Most Recent", "Suburb", "Price", "Bedrooms", "Updated", "Auct. Date");

function updatePricesFromSelect(value) {
    //alert(value);
    if(value == 'sale')
        updatePrices('buy');
    else
        updatePrices('lease');

}

function updatePrices(selectedValue) {

	if(document.sForm == null)
		return;



    if(selectedValue == 'buy') {
        clearOpts(document.sForm.lowerBound);
        clearOpts(document.sForm.upperBound);
        //clearOpts(document.sForm.Order);
        updateField(document.sForm.lowerBound, minPriceValueSale, saleMinPriceText);
        updateField(document.sForm.upperBound, maxPriceValueSale, saleMaxPriceText);
        //updateField(document.sForm.Order, orderValuesForSales, orderDescForSales);
        document.sForm.action = "sales/ListingSearch.action";
    } else {
        clearOpts(document.sForm.lowerBound);
        clearOpts(document.sForm.upperBound);
       // clearOpts(document.sForm.Order);
        updateField(document.sForm.lowerBound, minPriceValue, rentMinPriceText);
        updateField(document.sForm.upperBound, maxPriceValue, rentMaxPriceText);
        //updateField(document.sForm.Order, orderValuesForRent, orderDescForRent);
        document.sForm.action = "rentals/ListingSearch.action";
    }
}

function clearOpts(field) {
    if(field == null) {
        alert("The passed field was null");
    }

    for(i=field.length; i>=0; i--) {
        field.options[i] = null;
    }
}

function updateField(field, sysValue, userDesc) {
    if(sysValue.length != userDesc.length) {
        alert("Internal Error: length of values not the same as length of descriptions");
    }

    for(i = 0; i < sysValue.length; i++) {
        //field.options[i] = new Option(userDesc[i], sysValue[i], false, false);
        var optValue = sysValue[i];
        var optText  = userDesc[i];

        if(optText.toString()=='$0.00' || optText.toString()== '> $1,500.00' || optText.toString() == '> $1,500,000.00') {
            opt = new Option(optText, optValue, true, true);
        } else {
            opt = new Option(optText, optValue, false, false);
        }

        field.options[i] = opt;
    }
}