﻿var radBuy;
var radRentST;
var radRentLT;
var ddlPropertyType;
var ddlPriceMin;
var ddlPriceMax;
var hdnPriceMin;
var hdnPriceMax;

function InitPrice(initradBuy, initradRentST, initradRentLT, initHdnPriceMin, initHdnPriceMax) {
    radBuy = initradBuy;
    radRentST = initradRentST;
    radRentLT = initradRentLT;
    ddlPropertyType = document.getElementById('ddlPropertyType');
    hdnPriceMin = initHdnPriceMin;
    hdnPriceMax = initHdnPriceMax;
    ddlPriceMin = document.getElementById('ddlPriceMin');
    ddlPriceMax = document.getElementById('ddlPriceMax');

    
}


function UpdatePriceRanges(selectedMin, selectedMax) {
    var uidMandateTransaction = '';

    if (radBuy.checked == true) {
        uidMandateTransaction = '0ceabfad-8d7f-4304-a5c2-e88c2165adb2';
    }

    if (radRentST.checked == true) {
        uidMandateTransaction = '1c347514-c5bf-4836-8754-e1ee880d63fb';
    }

    if (radRentLT.checked == true) {
        uidMandateTransaction = '279d9747-4ccd-4d7d-9aeb-66aafdfa2abc';
    }

    var selectedPropertyType = ddlPropertyType.options[ddlPropertyType.selectedIndex].value;

    var propertyPrices = GetPropertyTypePrices();
    var priceArray;

    if (propertyPrices != null &&
        selectedPropertyType != null && 
        uidMandateTransaction != null &&
        propertyPrices[selectedPropertyType] != null
        ) {
        priceArray = propertyPrices[selectedPropertyType][uidMandateTransaction];
    }

    if (priceArray == null) {
        //alert('no prices defined');
    }
    else {
        //alert('updating prices');
        $(ddlPriceMax).empty();
        $(ddlPriceMin).empty();

        var oNull = null;
//--- uncomment this to add no price limit to the start of the max drop down list
//        oNull = document.createElement('option');
//        oNull.text = 'No price limit';
//        oNull.value = null;
//        
//        try {
//            ddlPriceMax.add(oNull, null); // standards compliant; doesn't work in IE
//        }
//        catch (ex) {
//            ddlPriceMax.add(oNull); // IE only
//        }
        

        oNull = document.createElement('option');
        oNull.text = 'All';
        oNull.value = 0;
        
        try {
            ddlPriceMin.add(oNull, null); // standards compliant; doesn't work in IE
        }
        catch (ex) {
            ddlPriceMin.add(oNull); // IE only
        }

        
        
        var i = 0;
        for (i = 0; i < priceArray.length; i++) {
            var value = parseInt(priceArray[i].substring(2).replace(/,/gi, ''));
            
            if (selectedMax == null || selectedMax == '' || selectedMax == 'null' || value < selectedMax) {
                var o = document.createElement('option');
                o.text = priceArray[i];
                o.value = value;

                try {
                    ddlPriceMin.add(o, null); // standards compliant; doesn't work in IE
                }
                catch (ex) {
                    ddlPriceMin.add(o); // IE only
                }
            }

            if (selectedMin == null || selectedMin == '' || selectedMin == 'null' || value > selectedMin) {

                var omax = document.createElement('option');
                omax.text = priceArray[i];
                omax.value = value;

                try {
                    ddlPriceMax.add(omax, null); // standards compliant; doesn't work in IE
                }
                catch (ex) {
                    ddlPriceMax.add(omax); // IE only
                }
            }
        }

        oNull = document.createElement('option');
        if (radBuy.checked == true) {
            oNull.text = 'R 50,000,000+'; //priceArray[priceArray.length - 1] + ' +';
        }
        else {
            if (radRentLT.checked == true) {
                oNull.text = 'R 50,000+'; //priceArray[priceArray.length - 1] + ' +';
            }
            else {
                oNull.text = 'R 40,000+'; //priceArray[priceArray.length - 1] + ' +';
            }
        }
        oNull.value = null;
        try {
            ddlPriceMax.add(oNull, null); // standards compliant; doesn't work in IE
        }
        catch (ex) {
            ddlPriceMax.add(oNull); // IE only
        }
//---- uncomment this to add "from cheapest" to the bottom of the ddl as well as the top      
//        oNull = document.createElement('option');
//        oNull.text = 'From cheapest';
//        oNull.value = null;
//        try {
//            ddlPriceMin.add(oNull, null); // standards compliant; doesn't work in IE
//        }
//        catch (ex) {
//            ddlPriceMin.add(oNull); // IE only
//        }

        if (selectedMax == null || selectedMax == '' || selectedMax == 'null' || selectedMax == NaN) {
            // if nothing was selected, select the highest number, i.e. no limit
            ddlPriceMax.selectedIndex = ddlPriceMax.options.length - 1;
        }
    }
}

function selectedPriceChanged() {
    var minSelectedIndex = ddlPriceMin.selectedIndex;
    var maxSelectedIndex = ddlPriceMax.selectedIndex;

    var minSelectedValue = 0;
    if (minSelectedIndex != 0 && minSelectedIndex != -1 && /*(minSelectedIndex != (ddlPriceMin.options.length -1)) &&*/ ddlPriceMin.options[minSelectedIndex].value != null) {
        minSelectedValue = ddlPriceMin.options[minSelectedIndex].value;
    }

    hdnPriceMin.value = minSelectedValue;


    var maxSelectedValue = 100000000;
    if (/*maxSelectedIndex != 0 && */maxSelectedIndex != -1 && (maxSelectedIndex != (ddlPriceMax.options.length - 1)) && ddlPriceMax.options[maxSelectedIndex].value != null) {
        maxSelectedValue = ddlPriceMax.options[maxSelectedIndex].value;
    }

    hdnPriceMax.value = maxSelectedValue;

    

    //alert(minSelectedValue + ' ' + maxSelectedValue);
    //alert(minSelectedIndex + ' ' + maxSelectedIndex);
    UpdatePriceRanges(minSelectedValue, maxSelectedValue);

    var i = 0;
    if (minSelectedIndex != 0/* && minSelectedIndex != ddlPriceMin.options.length -1*/)
    {
        for (i = 0; i < ddlPriceMin.options.length; i++) {
            if (minSelectedValue == ddlPriceMin.options[i].value) {
                ddlPriceMin.selectedIndex = i;
                break;
            }
        }
    }

    if (maxSelectedValue != 100000000) {
        for (i = 0; i < ddlPriceMax.options.length; i++) {
            if (maxSelectedValue == ddlPriceMax.options[i].value) {
                ddlPriceMax.selectedIndex = i;
                break;
            }
        }
    }
    else {
        ddlPriceMax.selectedIndex = ddlPriceMax.options.length - 1;
    }

    //alert(hdnPriceMax.value);
    if (hdnPriceMax.value == '100000000') {
        hdnPriceMax.value = '';
    }
    //alert(hdnPriceMax.value);

    if (hdnPriceMin.value == '0') {
        hdnPriceMin.value = '';
    }
}
