var blocage_panier = 0;
var _CAT_ATTENTE = "";
var _CONFIRM_SUPP_PANIER = "";

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}


function nav_ajout_panier(id_article, chemin) {
 //recuperation de la quantite
 var options = "";
 var id_option ;
 var nom_option;
 var quantite = $("#quantite_art_"+id_article).attr("value");

 $(':regex(name,value_opt_'+id_article+')').each(function(i){
 	id_option = $(this).attr('name').split('_')[3];
 	nom_option = $(':regex(id,name_opt_'+id_article+'_'+id_option+')').val();
 	nom_option = nom_option.replace(/ /g, '%20');
 	valeur =$(this).val();
 	if (options != "")
 	options = options + "|" + id_option + ";" + valeur;
 	else
 	options = id_option + ";" + valeur;
 });
 //affichage infos panier
 	tb_show('', chemin+'admin/link.php?mod=catalogue|v1.0|themes|theme_02|actions|preview_panier.php&height=150&width=600&id_article='+id_article+'&quantite='+quantite+'&options='+options, 'false');
}

function element_maj_panier(element,niveau,chemin, options) {
		
		//recuperation de lelement
		tab_element = element.split("_");
		id_element = tab_element[2];
		debut_suffixe_classe = tab_element[0].length + tab_element[1].length + 1 + 1;

		suffixe_classe = element.substring(debut_suffixe_classe);
		
		nombre_ajout = 1;
		img_loading = "<img src='"+chemin+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|images|loading.gif' border='0'>"
		
		
		//recuperation de la quantite actuel
		quantite = $("#quantite_"+suffixe_classe).attr("value");
		
		if(niveau == "ajout") {
			
			if(quantite*1 <99)
				new_quantite = (quantite*1)+nombre_ajout*1;
			else
				return false;
		}//fif
		else {
			
			if(quantite*1 >1)
				new_quantite = (quantite*1)-nombre_ajout*1;
			else
				return false;
		}//felse
			
			if(blocage_panier == 0) {
				blocage_panier = 1;
			
			//mise en attente
			$("#total_ht").html(img_loading);
			$("#total_ttc").html(img_loading);
			$("#total_tva").html(img_loading);
			$("#montant_ttc_"+suffixe_classe).html(img_loading);
			
			//mise a jour du montant
			   $.ajax({
				   type: "POST",
				   url: chemin+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|scripts|maj_commande.php",
				   data: "id_article="+id_element+"&niveau="+niveau+"&quantite="+nombre_ajout+"&total_quantite="+new_quantite+"&options="+options,
				   success: function(msg){
							 tab_resultat = msg.split("|");
							 
							 montant_ttc_ligne = tab_resultat[0];
							 totalht = tab_resultat[1];
							 totaltva = tab_resultat[2];
							 totalttc = tab_resultat[3];
							 aff_stock = tab_resultat[4];
							 aff_bouton_valide = tab_resultat[5];
							 
						//mise a jour quantite
						$("#quantite_"+suffixe_classe).attr("value",new_quantite);
						
						//mise a jour montant article
						$("#montant_ttc_"+suffixe_classe).html(montant_ttc_ligne);
						
						//mise a jour total ht
						$("#total_ht").html(totalht);
						//mise a jour total tva
						$("#total_tva").html(totaltva);
						//mise a jour total ttc
						$("#total_ttc").html(totalttc);
						
						//mise a jours infos stock
						$("#infos_stock_"+id_element).html("");
						$("#infos_stock_"+id_element).html(aff_stock);
						
						//mise a jour bouton
						$("#aff_bouton_valid").html(aff_bouton_valide);
						
						//Deblocage
						blocage_panier = 0;
					}//fsucces
			  });
			}//fif
			else {
				
				alert(_CAT_ATTENTE);
			}//felse
			
}//ffunc

function element_moins_panier(element) {
	
		//recuperation de lelement
		tab_element = element.split("_");
		id_element = tab_element[2];
		
		quantite = $("#quantite_"+id_element).attr("value");
		
		if(quantite*1 >1) {
			$("#quantite_"+id_element).attr("value",(quantite*1)-1);
		
		}//fif
}//ffunc

//chargement XML des messages d'erreurs
function cat_lecture_xml_catalogue(chemin) {
	
		$.ajax({
			   type: "POST",
			   url: chemin,
			   dataType: "xml",
			   success: function(xml){
				   
					$("language", xml).each(function(){
					
						
						_CAT_ATTENTE = $("attente_save_panier", this).text();
						_CONFIRM_SUPP_PANIER = $("confirm_supp_panier", this).text();
						
						
					});
				  
				}
		});
}//ffun

//function permettant la suppression d'un article dans le panier
	function suppression_element(element,chemin,position_langue, options) {
		
		//recuperation de lelement
		tab_element = element.split("_");
		id_element = tab_element[1];
		retour = confirm(_CONFIRM_SUPP_PANIER);
		if(retour) {
			
			//mise a jour du montant
			   $.ajax({
				   type: "POST",
				   url: position_langue+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|scripts|supp_element_panier.php",
				   data: "id_article="+id_element+'&options='+options,
				   success: function(msg){
							
							window.open(chemin,"_parent");
					}//fsucces
			  });
		}//fif
		
	}//ffunc
	
function cat_infos_complementaire(element) {

	$(element).slideToggle('normal');
}

function cat_modif_frais_port(frais_port,total,device,trad,id_transporteur,chemin) {
	
	img_loading = "<img src='"+chemin+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|images|loading.gif' border='0'>";
	$("#frais_port").html(img_loading);
	$("#total_ttc_avc_frais").html(img_loading);
	
	//mise a jour du montant
			   $.ajax({
				   type: "POST",
				   url: chemin+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|scripts|maj_frais_port.php",
				   data: "frais_port="+frais_port+"&total="+total+"&id_transporteur="+id_transporteur,
				   success: function(msg){
														
							if(frais_port*1 > 0) {
								//mise a jour frais de port
								$("#frais_port").html(frais_port+" "+device);
							}
							else {
								$("#frais_port").html(trad);
							}
							//retour via php pour arrondir correctemetnt
							montant_total_avc_frais = msg;
							
							$("#total_ttc_avc_frais").html(montant_total_avc_frais+" "+device); 
					}//fsucces
			  });
}//ffunc

//function permettant mise a jour paiement
function cat_modif_mode_paiement(id_paiement,chemin) {
	
	$.ajax({
				   type: "POST",
				   url: chemin+"admin/link.php?mod=catalogue|v1.0|themes|theme_02|scripts|maj_mode_paiement.php",
				   data: "id_mode_paiement="+id_paiement,
				   success: function(msg){
							
					}//fsucces
			  });	
}
