﻿function HouseTypeSelection() {

    this.Init = Init;
    this.setContractType = setContractType;
    this.setBuildingType = setBuildingType;

    ContractType = document.getElementById(lbContractType);
    BuildingType = document.getElementById(lbBuildingType);
    MinPrice = document.getElementById(lbMinPrice);
    MaxPrice = document.getElementById(lbMaxPrice);
    MinMq = document.getElementById(lbMinMq);
    MaxMq = document.getElementById(lbMaxMq);
    HouseTipology = document.getElementById(lbHouseTipology);

    function Init() {
        var ct = 2;
        var bt = "RES";
        
        loadMinPrice(ct);
        loadMaxPrice(ct);
        loadMinMQ(bt);
        loadMaxMq(bt);
        loadHouseTipology(bt);
        
        ContractType.onchange = function() { 
            loadMinPrice(this.options[this.selectedIndex].value); 
            loadMaxPrice(this.options[this.selectedIndex].value)  
        };
        
        BuildingType.onchange = function() { 
            loadMinMQ(this.options[this.selectedIndex].value); 
            loadMaxMq(this.options[this.selectedIndex].value);
            loadHouseTipology(this.options[this.selectedIndex].value);
        };
    }
    
    
    function loadMinPrice(ct)
    {
        var minPrices;
        if (ct == "1") //AFFITTO
            minPrices = prices.priceForRent;
        else //VENDITA
            minPrices = prices.priceForSell;

        MinPrice.options.length = 0;
        
        for (var mPrice in minPrices)
        {
            var value = minPrices[mPrice];
            
            if (Number(value) || value==0)
            {
                var option = document.createElement("option");
                option.setAttribute("value", minPrices[mPrice]);
                option.innerHTML = "€ " + formatValuta(minPrices[mPrice],false);
                
                MinPrice.appendChild(option);
            }
        }
    }

    function loadMaxPrice(ct)
    {
        var maxPrices;
        if (ct == "1") //AFFITTO
            maxPrices = prices.priceForRent;
        else //VENDITA
            maxPrices = prices.priceForSell;
        
        MaxPrice.options.length = 0;
        
        for (var xPrice in maxPrices)
        {
            var value = maxPrices[xPrice];
            
            if (Number(value) || value==0)
            {
                var option = document.createElement("option");
                option.setAttribute("value", maxPrices[xPrice]);
                option.innerHTML = "€ " + formatValuta(maxPrices[xPrice],false);
                
                MaxPrice.appendChild(option);
            }
        }
        
        var lastOpt = document.createElement("option"); 
        lastOpt.setAttribute("value", "2147483647");
        lastOpt.innerHTML = "nessun limite";

        MaxPrice.appendChild(lastOpt);
        MaxPrice.selectedIndex = MaxPrice.options.length - 1;
    }

    function loadMinMQ(bt)
    {
        var minMQs;
        switch (bt)
        {
            case "RES":
                minMQs = sizes.ResidentialSizes;
            break;
            
            case "COM":
                minMQs = sizes.CommercialSizes;
            break;
            
            case "VAC":
                minMQs = sizes.VacationSizes;
            break;
        }
        
        MinMq.options.length = 0;
        
        for (var mMq in minMQs)
        {
            var value = minMQs[mMq];
            
            if (Number(value) || value==0)
            {
                var option = document.createElement("option");
                option.setAttribute("value", minMQs[mMq]);
                option.innerHTML = minMQs[mMq];
                
                MinMq.appendChild(option);
            }
        }
    }

    function loadMaxMq(bt)
    {
        var maxMQs;
        switch (bt)
        {
            case "RES":
                maxMQs = sizes.ResidentialSizes;
            break;
            
            case "COM":
                maxMQs = sizes.CommercialSizes;
            break;
            
            case "VAC":
                maxMQs = sizes.VacationSizes;
            break;
        }
        
        MaxMq.options.length = 0;
        
        for (var xMq in maxMQs)
        {
            var value = maxMQs[xMq];
            
            if (Number(value) || value==0)
            {
                var option = document.createElement("option");
                option.setAttribute("value", maxMQs[xMq]);
                option.innerHTML = maxMQs[xMq];
                
                MaxMq.appendChild(option);
            }
        }
        
        var lastOpt = document.createElement("option"); 
        lastOpt.setAttribute("value", "2147483647");
        lastOpt.innerHTML = "nessun limite";
        
        MaxMq.appendChild(lastOpt);
        MaxMq.selectedIndex = MaxMq.options.length - 1;
    }
    
    function loadHouseTipology(bt)
    {
        var HouseTypes;
        switch (bt)
        {
            case "RES":
                HouseTypes = tipologies.ResidentialTipologies;
            break;
            
            case "COM":
                HouseTypes = tipologies.CommercialTipologies;
            break;
            
            case "VAC":
                HouseTypes = tipologies.VacationTipologies;
            break;
        }
        
        HouseTipology.options.length = 0;
        
        var option = document.createElement("option");
        option.setAttribute("value", "");
        option.innerHTML = "qualsiasi";

        HouseTipology.appendChild(option);
                
        for (var ht in HouseTypes)
        {
            var value = HouseTypes[ht].value;
            
            if (Number(value) || value==0)
            {
                var option = document.createElement("option");
                option.setAttribute("value", HouseTypes[ht].value);
                option.innerHTML = HouseTypes[ht].text;
                
                HouseTipology.appendChild(option);
            }
        }
    }
    
    function formatValuta(num,alwaysCents) {
        num = num.toString().replace(/\$|\,/g,'');
        
        if(isNaN(num))
            num = "0";
        
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        
        if(cents<10)
            cents = "0" + cents;
        
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3));
        
        if (alwaysCents)
            return (((sign)?'':'-') + num + ',' + cents);
        else
        {
            if (cents == "00")
                return (((sign)?'':'-') + num);
            else
                return (((sign)?'':'-') + num + ',' + cents);
        }
    }
    
    function setContractType(ct)
    {
        for (var i=0; i < ContractType.length; i++) {
            if (ContractType[i].value == ct)
                ContractType[i].selected = true;
        }
        
        loadMinPrice(ct); 
        loadMaxPrice(ct);
    }
    
    function setBuildingType(bt)
    {
        bt = bt.toUpperCase();
        
        for (var i=0; i < BuildingType.length; i++) {
            if (BuildingType[i].value == bt)
                BuildingType[i].selected = true;
        }
        
        loadMinMQ(bt); 
        loadMaxMq(bt);
        loadHouseTipology(bt);
    }
    
}



//Initialize object
var hts = new HouseTypeSelection();
hts.Init();