function ConsumoAgua(){
    this.b6 = 0;
    this.b7 = 0;
    this.b8 = 0;
    this.b9 = 0;
    this.b10 = 0;

    this.b12 = 0;
    this.b13 = 0;
    this.b14 = 0;
    this.b15 = 0;
    
    this.b17 = 0;
    this.b18 = 0;
    
    this.b22 = 1;
    this.b23 = 0;
    this.b24 = 0;
    
    this.b28 = 0;
    this.b29 = 0;
    this.b30 = 0;
    
    this.b34 = 0;
    this.b35 = 0;
    
    this.b37 = 0;
    this.b38 = 0;
    
    this.b40 = 0;
    this.b41 = 0;
    
    this.b43 = 0  
    
    this.TotalDucha = TotalDucha;
    this.TotalCisterna = TotalCisterna;
    this.TotalAseo = TotalAseo;
    this.totalCocina = totalCocina;
    this.totalLavadora = totalLavadora;
    this.totalVajilla = totalVajilla;
    
    this.totalLimpieza = totalLimpieza;
    this.totalRiego = totalRiego;

    this.consumoTotal= consumoTotal;    
}

function TotalDucha(){
    var res = 0;
    
    res=((this.b12*this.b13*10)*(1-0.35*this.b15)) + (this.b14*150);
    
    return Math.round(res);
}

function TotalCisterna(){
    var res = 0;
    
    if(this.b18 == 0){
        res = this.b17*9;
    }else{
        res = this.b17*6;
    }
    
    return Math.round(res);
}

function TotalAseo(){
    var res = 0;
    
    res = (this.b7*2+this.b8*2+this.b6*3+this.b9*3)*(1-this.b10*0.35);
    
    return Math.round(res);
}

function totalCocina(){
    var res = 0;
   
    res = (this.b23*0.25)+(this.b24/this.b22);
    
    return Math.round(res);
}

function totalLavadora(){
    var res = 0;

    if(this.b35 == 0){
        res = this.b34*90/(7*this.b22);
    }else{
        res = this.b34*60/(7*this.b22);
    }
    
    return Math.round(res);
}

function totalVajilla(){
    var res = 0;

    if(this.b38 == 0){
        res = this.b37*30/(7*this.b22);
    }else{
        res = this.b37*17/(7*this.b22);
    }

    return Math.round(res + this.b43*8/this.b22);

}

function totalLimpieza(){
    var res = 0;
    
    res = this.b40*5*this.b41/(7*this.b22);
    
    return Math.round(res);
}

function totalRiego(){
    var res = 0;
    
    res = (this.b28*5/(7*this.b22)) + (this.b29*8*(1-0.35*this.b30)/(7*this.b22));
    
    return Math.round(res);
}

function consumoTotal(){
    var res = 0;
    
    res = this.TotalDucha() + this.TotalCisterna() + this.TotalAseo() + this.totalCocina() + this.totalLavadora() + this.totalVajilla() + this.totalLimpieza() + this.totalRiego();
    
    return Math.round(res);
}

$(document).ready(function (){
    
    var consumo = new ConsumoAgua();   
 
    $('#b6').change(function() {
        consumo.b6 = $("#b6").val();
    });
        
        
    $('#b7').change(function() {
        consumo.b7 = $("#b7").val();
    });
    $('#b8').change(function() {
        consumo.b8 = $("#b8").val();
    });
    $('#b9').change(function() {
        consumo.b9 = $("#b9").val();
    });
    $('#b10si').click(function() {
        consumo.b10 = 1;
    });
        
    $('#b10no').click(function() {
        consumo.b10 = 0;
    });

    $('#b12').change(function() {
        consumo.b12 = $("#b12").val();
    });
    $('#b13').change(function() {
        consumo.b13 = $("#b13").val();
    });
    $('#b14').change(function() {
        consumo.b14 = $("#b14").val();
    });
    $('#b15si').click(function() {
        consumo.b15 = 1;
    });
    $('#b15no').click(function() {
        consumo.b15 = 0;
    });
    
    $('#b17').change(function() {
        consumo.b17 = $("#b17").val();
    });
    $('#b18si').click(function() {
        consumo.b18 = 1;
    });
    $('#b18no').click(function() {
        consumo.b18 = 0;
    });
    
    $('#b22').change(function() {
        consumo.b22 = $("#b22").val();
    });
    $('#b23').change(function() {
        consumo.b23 = $("#b23").val();
    });
    $('# b24').change(function() {
        consumo.b24 = $("#24").val();
    });
    
    $('#b28').change(function() {
        consumo.b28 = $("#b28").val();
    });
    $('#b29').change(function() {
        consumo.b29 = $("#b29").val();
    });
    $('#b30si').click(function() {
        consumo.b30 = 1;
    });
    $('#b30no').click(function() {
        consumo.b30 = 0;
    });
    
    $('#b34').change(function() {
        consumo.b34 = $("#b34").val();
    });
    $('#b35si').click(function() {
        consumo.b35 = 1;
    });
    $('#b35no').click(function() {
        consumo.b35 = 0;
    });
    
    $('#b37').change(function() {
        consumo.b37 = $("#b37").val();
    });
    $('#b38si').click(function() {
        consumo.b38 = 1;
    });
    $('#b38no').click(function() {
        consumo.b38 = 0;
    });
    
    $('#b40').change(function() {
        consumo.b40 = $("#b40").val();
    });
    $('#b41').change(function() {
        consumo.b41 = $("#b41").val();
    });
    
    $('#b43').change(function() {
        consumo.b43 = $("#b43").val();
    });
    
    $('.respsi').click(function() {

        var aux = $(this).parent().find('.selectrespno');
        aux.removeClass("selectrespno");
        aux.addClass("selectrespsi");
        $(this).addClass("selectrespno");
    });

    $('.respno').click(function() {

        var aux = $(this).parent().find('.selectrespno');
        aux.removeClass("selectrespno");
        aux.addClass("selectrespsi");
        $(this).addClass("selectrespno");
    });

    $('#botoncalcularAhorro').click(function() {

        if(consumo.consumoTotal()<131){
          $('#totcalper').html('BAJO');
        }else if(consumo.consumoTotal()<165){
          $('#totcalper').html('MEDIO');
        }else{
          $('#totcalper').html('ALTO');
        }

        $('#totlitrosper').html(consumo.consumoTotal()+'&#32;');
        $('.totalducha').html(consumo.TotalDucha()+'&#32;litros');
        $('.totalcisterna').html(consumo.TotalCisterna()+'&#32;litros');
        $('.totalaseo').html(consumo.TotalAseo()+'&#32;litros');
        $('.totalcocina').html(consumo.totalCocina()+'&#32;litros');
        $('.totallavadora').html(consumo.totalLavadora()+'&#32;litros');
        $('.totalvajilla').html(consumo.totalVajilla()+'&#32;litros');
        $('.totallimpieza').html(consumo.totalLimpieza()+'&#32;litros');
        $('.totalriego').html(consumo.totalRiego()+'&#32;litros');

        $('html, body').animate({
            scrollTop:0
        }, 'fast');
        $('.views-field-title span').html("Resultado");
        $('#formcalculoAgua').hide();
        $('#resformcalculoAgua').show();

        arrayOfData = new Array(
            [consumo.TotalDucha()],
            [consumo.TotalCisterna()],
            [consumo.TotalAseo()],
            [consumo.totalCocina()],
            [consumo.totalLavadora()],
            [consumo.totalVajilla()],
            [consumo.totalLimpieza()],
            [consumo.totalRiego()]
            );

        $('#graficoconsumoAgua').jqbargraph({
            data: arrayOfData ,
            height: 220,
            width: 320,
            colors: ['#005380','#007BB5','#00A6DE','#24B0A2', '#67B14C', '#009036', '#C9D100', '#FDCF00']
        });


    });

});

