var ABSOLUTE_PATH = "http://www.aerconditionat-aquarys.ro/";

$(document).ready(function() {
   $("#filtre li input").click(function(){
        window.location.href=$(this).next().attr("href");
    });

    //stripes
    stripe();

    //cos total
    $('#detalii_cos tbody tr input').bind('keypress', function(e) {
      return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
    })
    $('#detalii_cos tbody tr input').keyup(function(){
        cant=0+parseFloat($(this).val());
        var wishlist = $('#wishlist').val();
        var wval=null;
        if(wishlist==1) wval=1;
        $.get(ABSOLUTE_PATH+"index.php", {jax: 'updateCos', cant: cant, id : $(this).attr('id'), wishlist: wval},
        function(data){
            totalCos();
        });
    });


   $('a.sterge_din_cos').click(function(){
        $(this).parent().parent().remove();
        wishlist = $('#wishlist').val();
        $.get(ABSOLUTE_PATH+"index.php", {jax: 'delFromCart',  id : $(this).attr('id'), wishlist: wishlist},
        function(data){
            stripe();
            totalCos()
            if($('#detalii_cos tbody tr input').size()==0) location.reload();
        });
    });

    observeLivrare();
    observePersoana()

    $('.msg').fadeTo(3000, 1).fadeOut(2000);

     $("#tablist a").bind("click", function(){
      	setTab($(this).get(0).id.substr(1));
        if($(this).attr('href')!="#") window.location.href=$(this).attr('href');
		return false;
    });


});

/////////////////////////////////////////////////////////////////////////////////////////////////   dom loaded observers  above


function setListing(setting, val){
        $.get(ABSOLUTE_PATH+"index.php", {jax: 'setListing', setting: setting, val: val },
        function(data){
            location.reload();
        });
}

function stergeFiltrele(url){
    window.location.href=url;
}

function stripe(){$('table.tabular').each(function(){
        $(this).find('tr').removeClass('even');
        $(this).find('tr:even').addClass('even');
    });
}

function totalCos(){
    var total= 0;
    $('#detalii_cos tbody tr input').each(function(){
        cant=0+parseFloat($(this).val());
        pret=0+parseFloat($(this).parent().next().html());
        valoare=0+cant*pret;
        $(this).parent().next().next().html(Math.round(valoare*100)/100)
        total=total+valoare;
    });
    $('#total_cos, #top_total_cos').html(Math.round(total*100)/100);
    $('#top_nr_produse').html($('#detalii_cos tbody tr input').size());
}

function addAllToCart(id){
    var str = id+',';
        $('#produse_complementare input[@name="prod[]"]:checked').each(function(i, field){
            str+=field.value + ",";
        });
        str=str.substr(0, str.length - 1);
     window.location.href=ABSOLUTE_PATH+'cos/adauga-'+str+'/';
}

function inapoi(){
    history.go(-1);
    return false;
}

function resetForm(id){
    $(':input','#'+id).val('').removeAttr('checked').removeAttr('selected');
}

     function observeLivrare(){
        $("input[name$='livrare']").click(function(){
            var val = $(this).val();
            if(val=="livrare_sediu")
            {
                //$('#add_livrare').hide();
                $('#adrese_livrare').hide();
                //$("input[name='livrare']").removeAttr('checked');
            }
            if(val=="livrare_firma")
            {
                $('#adrese_livrare').show();
            }
            if(val=="livrare_curier")
            {
                $('#adrese_livrare').show();
            }
            if(val=="livrare_add_new")
            {
                resetForm('add_livrare');
                $('#add_livrare').show();
            }
        if(val.split("_")[0]=="livrare" && val.split("_")[1]!="sediu" && !val.split("_")[2])
        {
            var client_id=val.split("_")[1];
            $.post(ABSOLUTE_PATH+"index.php", { jax: "getLivrare", id: client_id },
                function(livrare_json){
                    $('#livrare_adresa').val(livrare_json.adresa);
                    $('#livrare_judet').val(livrare_json.judet);
                    $('#livrare_localitate').val(livrare_json.localitate);
                    $('#livrare_cod_postal').val(livrare_json.cod_postal);
                }, "json");
            $('#add_livrare').show();
			$('#livrare_add_new').attr('checked');
        }
        });
    }


    function observePersoana(){
        $("input[name$='persoana']").click(function(){
        var val = $(this).val();
        if(val.split("_")[0]=="pf" && !val.split("_")[2])
        {
            var client_id=val.split("_")[1];
            $.post(ABSOLUTE_PATH+"index.php", { jax: "getPf", id: client_id },
                function(pf_json){
                    $('#pf_nume').val(pf_json.nume);
                    $('#pf_prenume').val(pf_json.prenume);
                    $('#pf_cnp').val(pf_json.cnp);
                    $('#pf_adresa').val(pf_json.adresa);
                    $('#pf_judet').val(pf_json.judet);
                    $('#pf_localitate').val(pf_json.localitate);
                    $('#pf_cod_postal').val(pf_json.cod_postal);
                    $('#pf_telefon').val(pf_json.telefon);
                }, "json");
            $('#add_pf').show();
            $('#add_pj').hide();
        }
        if(val.split("_")[0]=="pj" && !val.split("_")[2])
        {
            var client_id=val.split("_")[1];
            $.post(ABSOLUTE_PATH+"index.php", { jax: "getPj", id: client_id },
                function(pj_json){
                    $('#pj_denumire').val(pj_json.denumire);
                    $('#pj_cui').val(pj_json.cui);
                    $('#pj_reg_com').val(pj_json.reg_com);
                    $('#pj_banca').val(pj_json.banca);
                    $('#pj_cont').val(pj_json.cont);
                    $('#pj_adresa').val(pj_json.adresa);
                    $('#pj_judet').val(pj_json.judet);
                    $('#pj_localitate').val(pj_json.localitate);
                    $('#pj_cod_postal').val(pj_json.cod_postal);
                    $('#pj_telefon').val(pj_json.telefon);
                    $('#pj_fax').val(pj_json.fax);
                }, "json");
            $('#add_pj').show();
            $('#add_pf').hide();
        }
        if(val=="pf_add_new")
        {
            resetForm('add_pf');
            $('#add_pf').show();
            $('#add_pj').hide();
        }
        if(val=="pj_add_new")
        {
            resetForm('add_pj');
            $('#add_pj').show();
            $('#add_pf').hide();
        }
    });
    }

    function setTab(tab_id){
        $('#tablist a').removeClass('tab_on');
        $('#t'+tab_id).addClass('tab_on');
        $('#tabContent div').hide();
        $('#c'+tab_id).show();
}

function open_imagini(IDProdus, IDImagine){
	window.open(ABSOLUTE_PATH+'index.php?p=popimg&id='+IDProdus+'&IDImagine='+IDImagine,'pop_img','scrollbars=no,height=250,width=350,resizable=yes');
}




