function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');

    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function (key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function (key) {
    var value = this.params[key];
    return (value != null);
}
function Search() {
    var minbeds, minprice, maxprice, department, loc;
    minbeds = document.getElementById("ddBedsMin").value;
    minprice = document.getElementById("ddPriceMin").value;
    maxprice = document.getElementById("ddPriceMax").value;
    //department = document.getElementById("Department").value;
    loc = document.getElementById("ddArea").value;
    document.location = "buying.htm?minbeds=" + minbeds + "&minprice=" + minprice + "&maxprice=" + maxprice + "&districts=" + loc;
}
function Lettings_Prices() {
    var minPriceDD = document.getElementById("ddPriceMin")
    var maxPriceDD = document.getElementById("ddPriceMax")
    var priceValues = new Array(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1250, 1500, 1750, 2000)

    var oItem

    minPriceDD.options.length = 0

    oItem = document.createElement("Option");
    oItem.text = "No Minimum";  //Textbox's value
    oItem.value = "";  //Textbox's value
    minPriceDD.options.add(oItem);

    for (var i = 0; i < priceValues.length; i++) {
        oItem = document.createElement("Option");
        oItem.text = priceValues[i] + "PCM";
        oItem.value = priceValues[i];
        minPriceDD.options.add(oItem);
    }


    maxPriceDD.options.length = 0

    oItem = document.createElement("Option");
    oItem.text = "No Maximum";  //Textbox's value
    oItem.value = "";  //Textbox's value
    maxPriceDD.options.add(oItem);

    for (var i = 0; i < priceValues.length; i++) {
        oItem = document.createElement("Option");
        oItem.text = priceValues[i] + "PCM";
        oItem.value = priceValues[i];
        maxPriceDD.options.add(oItem);
    }

}
function Sales_Prices() {
    var minPriceDD = document.getElementById("ddPriceMin");
    var maxPriceDD = document.getElementById("ddPriceMax");
    var priceValues = new Array(25000, 50000, 75000, 100000, 125000, 150000, 175000, 225000, 250000, 275000, 300000, 350000, 400000, 450000, 500000, 550000, 600000, 700000, 800000, 900000, 1000000);

    var oItem;

    minPriceDD.options.length = 0;

    oItem = document.createElement("Option");
    oItem.text = "No Minimum";  //Textbox's value
    oItem.value = "";  //Textbox's value
    minPriceDD.options.add(oItem);

    for (var i = 0; i < priceValues.length; i++) {
        oItem = document.createElement("Option");
        oItem.text = formatSalePrice(priceValues[i]);
        oItem.value = priceValues[i];
        minPriceDD.options.add(oItem);
    }

    maxPriceDD.options.length = 0;

    oItem = document.createElement("Option");
    oItem.text = "No Maximum";  //Textbox's value
    oItem.value = "";  //Textbox's value
    maxPriceDD.options.add(oItem);

    for (var i = 0; i < priceValues.length; i++) {
        oItem = document.createElement("Option");
        oItem.text = formatSalePrice(priceValues[i]);
        oItem.value = priceValues[i];
        maxPriceDD.options.add(oItem);
    }
}

function formatSalePrice(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return "£" + x1 + x2;
}

function switchPrices(value) {
    if (value == "1") {
        //Sales Prices
        Sales_Prices()
    }
    else {
        //Lettings Prices
        Lettings_Prices()
    }
}

function init() {
    var qs = new Querystring();
    //var dep = qs.get("dep",1);
    var minPrice = qs.get("minprice", "");
    var maxPrice = qs.get("maxprice", "");
    var minBeds = qs.get("minbeds", 0);
    var districts = qs.get("districts", "");
    Sales_Prices();
    //var depDD = document.getElementById("Department")
    var minbedsDD = document.getElementById("ddBedsMin");
    var minpriceDD = document.getElementById("ddPriceMin");
    var maxpriceDD = document.getElementById("ddPriceMax");
    var departmentDD = document.getElementById("Department");
    var locDD = document.getElementById("ddArea");
    //depDD.selectedIndex = dep - 1;
    for (var i = 0; i < minpriceDD.length; i++) {
        if (minPrice == minpriceDD.options[i].value) {
            minpriceDD.selectedIndex = i;
            break;
        }
    }
    for (i = 0; i < minbedsDD.length; i++) {
        if (minBeds == minbedsDD.options[i].value) {
            minbedsDD.options[i].selected = true;
            break;
        }
    }
    for (i = 0; i < maxpriceDD.length; i++) {
        if (maxPrice == maxpriceDD.options[i].value) {
            maxpriceDD.options[i].selected = true;
            break;
        }
    }

    for (i = 0; i < locDD.length; i++) {
        if (districts == locDD.options[i].value) {
            locDD.options[i].selected = true;
            break;
        }
    }
}
