var ABSOLUTE_PATH = "http://www.cartuse-ieftine.ro/";

var timeout = null;

$(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));
		return false;
    });

    $('#asoc_key').focus(movekey());

     $('.please_confirm').click(function(){
         var answer = confirm('Esti sigur?');
         return answer;
         });

});

    function maxCant(obj, m){
        if(obj.val()>m) obj.val(m);
        if(obj.val()<1) obj.val(1);
    }
    function minCant(obj, m){
        if(obj.val()==0)
            {   alert("Acea valoare nu poate fi 0");
                obj.val(m);
                return false;
            }

        if(obj.val()<0)
            {   alert("Acea valoare nu poate fi mai mica ca 0");
                obj.val(m);
                return false;
            }

}

     function maxPret(obj, m){
        if(obj.val()>m) $('#container_ver').html('<span style="color:#c00">Atentie ! Pretul pe care l-ati propus este mai mare decat pretul recomandat de noi. Pretul recomandat reprezinta ultimul pret cu care am vandut acet produs. Este posibil ca oferta dvs. sa nu fie aprobata.</span>');
        if(obj.val()<m) $('#container_ver').html('');
    }




    function updateDB(tabela, camp, val, id){
        $.post(ABSOLUTE_PATH+"index.php", { jax: "updateDB", tabela: tabela, camp: camp, val: val, id: id}, function(){}, "json");
    }

    function updateDBcant(tabela, camp, obj, id, pret){
        updateDB(tabela, camp, obj.val(), id);
        var total=(pret*obj.val());
        obj.parent().next().html(Math.round(total*100)/100);
    }

    function fadeMsg(){
        $('div.ok').fadeTo(3000, 1).fadeOut(2000);
    }

    function showMsg(){
       $('#msg').html('Datele au fost salvate').show();
        fadeMsg();
    }

function updateDBpret(tabela, camp, obj, id, cantitate, adaos, pret_recomandat){

        var adaosul=(obj.val()*(adaos/100));
        var pret_final=(adaosul-(-obj.val()));

        var total=(obj.parent().next().find('input').val()*obj.val());
        //alert(cantitate+" * "+obj.val()+" = "+total);

       // if (timeout) {
        //    clearTimeout(timeout);
        //    timeout = null;
       // }
      //  timeout = setTimeout(showMsg, 1000);

        updateDB(tabela, camp, obj.val(), id);
        updateDB(tabela, 'pret_final', pret_final, id);
        updateDB(tabela, 'stare', '0', id);
        updateDB(tabela, 'pending', '0', id);

        obj.parent().next().next().html(Math.round(total*100)/100);
        obj.parent().next().next().next().html('<strong style="color:grey">in asteptare</strong>');
        if(obj.val()>pret_recomandat) {obj.css("color", "#cc0000"); }
        if(obj.val()<pret_recomandat) {obj.css("color", "grey"); }

    }

       function updateDBcos(tabela, camp, id){
        updateDB(tabela, camp, '', id);
        //window.location.href=window.location.href;
    }

    function reload(){
        window.location.href=window.location.href;
    }

//////////////////////////////////////////////////////////////////
function getElem(elemID){
	var obj;
	if (document.all){
		obj = document.all(elemID)
	}else if (document.getElementById){
		obj = document.getElementById(elemID)
	}else if (document.layers){
		obj = document.layers[elemID]
	}
	return obj;
}

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


  function cautaProduse(obj){
        $('#rezultate').show();
        var key = $(obj).val();
        if(key){
        $.get("index.php", {jax: "cauta", key: key},
                function(data){
                    $("#rezultate").html(data);
                    stripe();
                });
        }
    }

    function movekey(){
        $('#asoc_key').bind('keyup', function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if(code == 13) {
              if($('#rezultate ul li a.selected').size()>0) window.location.href=$('#rezultate ul li a.selected').attr('href');
            }
            if(code == 38) {
                if($('#rezultate ul li a.selected').size()>0){
                    if(!$('#rezultate ul li a:first').hasClass("selected")){
                        $('#rezultate ul li a.selected').parent().prev().find('a').addClass('selected');
                        $('#rezultate ul li a.selected:last').removeClass('selected');
                    }
               }else{
                    $('#rezultate ul li a:last').addClass('selected');
               }
            }
            if(code == 40) {
                if($('#rezultate ul li a.selected').size()>0){
                    if(!$('#rezultate ul li a:last').hasClass("selected")){
                        $('#rezultate ul li a.selected').parent().next().find('a').addClass('selected');
                        $('#rezultate ul li a.selected:first').removeClass('selected');
                    }
               }else{
                    $('#rezultate ul li a:first').addClass('selected');
               }
            }
            if((code != 13 && code != 38 && code != 40) || code==8){

                var goto_ = $("#nume_functie").val();
                window[goto_]($('#asoc_key'));
            }
        });


    }

  function addProd(obj){
$('#rezultate').show();
            var key = $(obj).val();
            if(key.length>2){

            $.get("index.php", {jax: "addProd", key: key},
                    function(data){
                        $("#rezultate").html(data);
                        stripe();
                    });
            }else {$("#rezultate").html('');}

    }

  function addProdAlerta(obj){
$('#rezultate').show();
            var key = $(obj).val();
            if(key.length>2){

            $.get("index.php", {jax: "addProdAlerta", key: key},
                    function(data){
                        $("#rezultate").html(data);
                        stripe();
                    });
            }else {$("#rezultate").html('');}

    }


  function addProdIndex(obj){
$('#rezultate').show();
            var key = $(obj).val();
            if(key.length>2){
            $.get("index.php", {jax: "addProdIndex", key: key},
                    function(data){
                        $("#rezultate").html(data);
                        stripe();
                    });
            }else {$("#rezultate").html('');}

    }


  function addProdOferta(obj){
$('#rezultate').show();
            var key = $(obj).val();
            if(key.length>2){
            $.get("index.php", {jax: "addProdOferta", key: key},
                    function(data){
                        $("#rezultate").html(data);
                        stripe();
                    });
            }else {$("#rezultate").html('');}

    }

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

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().prev().prev().html());
        valoare=0+cant*pret;
        $(this).parent().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();
            }
            if(val=="livrare_firma")
            {
                $('#add_livrare').hide();
            }
            if(val=="livrare_add_new")
            {
                resetForm('add_livrare');
                $('#add_livrare').show();
            }
        if(val.split("_")[0]=="livrare" && val.split("_")[1]!="sediu" && val.split("_")[1]!="firma" && !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();
        }
        });
    }


    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(pf_json.adresa);
                    $('#pj_judet').val(pf_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();
}





