
var url_base = "http://www.eti-construction.fr/wp-content/themes/eti";


function add_CodePromo(ht)
{
        var code_promo = jQuery('#code_promo_input').val();
	jQuery.ajax({
		type: "POST",
		url: url_base+"/code_promo.php",
		data: "code_promo="+code_promo+"&ht="+ht,
		success: function(msg){
                        result = jQuery.parseJSON(msg);
			jQuery('#flash_notice_inscription').html(result.msg);
                        if(result.valid == "1"){
                            jQuery('#flash_notice_inscription').removeClass( "nvalid" );
                            jQuery('#flash_notice_inscription').addClass( "valid" );
                            jQuery('#strike_ht').addClass( "strike" );
                            jQuery('#strike_ttc').addClass( "strike" );
                            jQuery('#champs_promo_ht').html("<br/><span class='new_price'>"+result.ht+" &euro; HT</span>");
                            jQuery('#champs_promo_ttc').html("<br/><span class='new_price'>"+result.ttc+" &euro; TTC</span>");
                            //jQuery('#addcode').removeAttr('onclick');
                            jQuery('#tarif_formation_promo').val(result.ht+" &euro; HT");
                            jQuery('#tarif_ttc_promo').val(result.ttc+" &euro; TTC");
                            jQuery('#scode_promo').val(code_promo);
                        }else{
                            jQuery('#flash_notice_inscription').removeClass( "valid" );
                            jQuery('#flash_notice_inscription').addClass( "nvalid" );
                            jQuery('#champs_promo_ht').html("");
                            jQuery('#champs_promo_ttc').html("");
                            jQuery('#strike_ht').removeClass( "strike" );
                            jQuery('#strike_ttc').removeClass( "strike" );
                            jQuery('#tarif_formation_promo').val("");
                            jQuery('#tarif_ttc_promo').val("");
                            jQuery('#scode_promo').val("");
                        }
			jQuery('#flash_notice_inscription').slideDown();
		}
	});

}

function add_CodePromoBoutique(display_err, id, type){
    if (display_err==undefined) display_err = true;

    var code_promo = jQuery('#code_promo').val();
	jQuery.ajax({
		type: "POST",
		url: url_base+"/code_promo_boutique.php",
		data: "code_promo="+code_promo,
		success: function(msg){
                        result = jQuery.parseJSON(msg);
                        jQuery('#code_remise_confirm').hide();
                        jQuery('#code_remise_error').hide();
                        if(result.valid == "1"){
                            if(display_err){
                                jQuery('#code_remise_confirm').html(result.msg);
                                jQuery('#code_remise_confirm').slideDown();
                            }
                            var gtotal = 0.0;
                            var gttctotal = 0.0;
                            if(id==undefined){
                                jQuery('.item_prix_ht').each(function(index) {
                                        var tv = $(this).text();
                                        var hv = $(this).html();
                                        var fv = tv.replace(" ", "");
                                        fv = parseFloat(fv.replace(",", "."));
                                        var nvalue = fv - (result.value * fv)/100;
                                        $(this).html(nvalue.toFixed(2)+ " &euro; <br/><span class='old_value'>au lieu de <br/><s>"+hv+"</s></span>" );
                                  });

                                  jQuery('.item_ss_total').each(function(index) {
                                    var tv = $(this).text();
                                    var hv = $(this).html();
                                    var fv = tv.replace(" ", "");
                                    fv = parseFloat(fv.replace(",", "."));
                                    var nvalue = fv - (result.value * fv)/100;
                                    gtotal = gtotal + nvalue;
                                    $(this).html(nvalue.toFixed(2)+ " &euro; <br/><span class='old_value'>au lieu de <br/><s>"+hv+"</s></span>" );
                                  });
                            }else{
                                var tv = jQuery('#prix_ht_'+id).text();
                                var hv = jQuery('#prix_ht_'+id).html();
                                var fv = tv.replace(" ", "");
                                fv = parseFloat(fv.replace(",", "."));
                                var nvalue = fv - (result.value * fv)/100;
                                jQuery('#prix_ht_'+id).html(nvalue.toFixed(2)+ " &euro; <br/><span class='old_value'>au lieu de <br/><s>"+hv+"</s></span>" );

                                tv = jQuery('#sous_total_ht_'+id+'_'+type).text();
                                hv = jQuery('#sous_total_ht_'+id+'_'+type).html();
                                fv = tv.replace(" ", "");
                                fv = parseFloat(fv.replace(",", "."));
                                nvalue = fv - (result.value * fv)/100;
                                jQuery('#sous_total_ht_'+id+'_'+type).html(nvalue.toFixed(2)+ " &euro; <br/><span class='old_value'>au lieu de <br/><s>"+hv+"</s></span>" );
                                

                                jQuery('.item_ss_total').each(function(index) {
                                 var tv = $(this).text();
                                 var fv = tv.replace(" ", "");
                                 fv = parseFloat(fv.replace(",", "."));
                                //var nvalue = fv - (result.value * fv)/100;
                                 gtotal = gtotal + fv;
                                });
                            }

                              var fdp = 0.0;
                              var fa = 0.0;
                              if(jQuery('#frais_de_port_value').length){
                                  fdp = parseFloat(jQuery('#frais_de_port_value').text().replace(" ",""));
                              }
                              if(jQuery('#frais_abonnement_value').length){
                                  fa = parseFloat(jQuery('#frais_abonnement_value').text().replace(" ",""));
                              }
                              var total_ht = gtotal + fdp + fa;
                              jQuery('#totaux_ht').html(total_ht.toFixed(2)+ " &euro;");
                              //
                              jQuery('.infos_ttc').each(function(index) {
                                var ttc = $(this).find('.item_prix_ttc').first().text();
                                var fv = ttc.replace(" ", "");
                                fv = parseFloat(fv.replace(",", "."));
                                var nvalue = fv - (result.value * fv)/100;
                                var qty = $(this).find('.nombre_quantite').first().text();
                                gttctotal = gttctotal + nvalue*parseFloat(qty);
                              });
                              var total_ttc = gttctotal + fdp*1.196 + fa*1.196;
                              
                              jQuery('#total_ttc').html(total_ttc.toFixed(2) + " &euro;");

                              var total_tva = total_ttc - total_ht;
                              jQuery('#total_tva').html(total_tva.toFixed(2)+ " &euro;");

                              $('#code_promo').attr('readonly', true);
                              $('#addcode').removeAttr('onclick');

                        }else{
                            if(display_err){
                                jQuery('#code_remise_error').html(result.msg);
                                jQuery('#code_remise_error').show();
                            }

                        }

		}
	});
}

function modifier_quantite(id,type,quantite,bouton){



    jQuery.ajax({
		type: "POST",
		url: url_base+"/ajax.php",
		data: "id="+id+"&type="+type+"&quantite="+quantite+"&bouton="+bouton,
		success: function(msg){
                    result = jQuery.parseJSON(msg);
           // alert("quantite=" + result['quantite'] + " ht="+ result['ht'] + " sous_total_item=" + result['sous_total_item'] + " sous_total_ht="+ result['sous_total_ht']+" sous_total_ttc="+result['sous_total_ttc'] );
            jQuery("#cellule_quantite_"+id+"_"+type).html(result['quantite']);
            jQuery("#sous_total_ht_"+id+"_"+type).html(result['sous_total_item'] + " &euro;");
            jQuery("#totaux_ht").html(result['sous_total_ht'] + " &euro;");
            jQuery("#total_tva").html(result['tva'] + " &euro;");
            jQuery("#total_ttc").html(result['sous_total_ttc'] + " &euro;");
            jQuery("#prix_ht_"+id).html(result['ht'].toFixed(2)+ " &euro;");
            jQuery("#iditem_"+id+"_"+type).html(" <a href=\"#\" class=\"bouton_increase\" onclick=\"modifier_quantite("+id+","+type+","+result['quantite']+",'plus');return false;\"/><a href=\"#\" class=\"bouton_decrease\" onclick=\"modifier_quantite("+id+","+type+","+result['quantite']+",'moins');return false;\"/>");
            jQuery("#prix_abo_"+id+"_"+type).html(result['prix_abo']);
            jQuery("#nb_item_sidebar").html = result['nb_item_sidebar'];
            add_CodePromoBoutique(false, id, type);
                    }

                });
    
}

function effacer_ligne(id,type,bouton){
     jQuery.ajax({
		type: "POST",
		url: url_base+"/ajax.php",
		data: "id="+id+"&type="+type+"&bouton="+bouton,
		success: function(msg){
                    result = jQuery.parseJSON(msg);
                      // if(result['effacer']!=null) window.location=window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;

            jQuery("#cellule_quantite_"+id+"_"+type).html(result['quantite']);
            jQuery("#sous_total_ht_"+id+"_"+type).html(result['sous_total_item'] + " &euro;");
            jQuery("#totaux_ht").html(result['sous_total_ht'] + " &euro;");
            jQuery("#total_tva").html(result['tva'] + " &euro;");
            jQuery("#total_ttc").html(result['sous_total_ttc'] + " &euro;");
            jQuery("#iditem_"+id+"_"+type).html(" <a href=\"#\" class=\"bouton_increase\" onclick=\"modifier_quantite("+id+","+type+","+result['quantite']+",'plus');return false;\"/><a href=\"#\" class=\"bouton_decrease\" onclick=\"modifier_quantite("+id+","+type+","+result['quantite']+",'moins');return false;\"/>");
            jQuery("#prix_abo_"+id+"_"+type).html(result['prix_abo']);
            

                      jQuery("#tr_"+result['effacer']+"_"+result['type']).remove();
                      jQuery("#tr_abo_"+result['effacer']+"_"+result['type']).remove();
                      if (result['prix_abo']==0) jQuery('#tr_abo_'+result['effacer']+'_'+result['type']).hide();
                      jQuery("#nb_item_sidebar").html(result['nb_item_sidebar']);
                if(result['nb_item_sidebar']==0){jQuery("#form_panier").html("Votre panier est vide")}
                if (result['flag_abo']) jQuery('tr#frais_abonnement').hide();
                if (result['flag_port']) jQuery('tr#frais_de_port').hide();
                add_CodePromoBoutique(false, id, type);
                if( $('tr[id^="tr_abo"]').length==0 ) jQuery('#abonnements').hide();
                }
                });
    
}


