<!-- patronage -->

function jqPatronage(o){	
	if($(o).is(':checked')){
		ajax_query_html('ajax_add_patronage','&code='+$(o).attr('rel'), 'cart_full');
	}else{
		ajax_query_html('ajax_remove_patronage','&code='+$(o).attr('rel'), 'cart_full');
	}
}

function jqSaving(o){	
	if($(o).is(':checked')){
		ajax_query_html('ajax_enable_saving','', 'cart_full');
	}else{
		ajax_query_html('ajax_disable_saving','', 'cart_full');
	}
}

function verif_qty ()
{
	var valid_qty = true;
	var nb_qty_ko_products = 0;
	nb_qty_ko_products = $('.qty_min_ko').length;
	
	if(nb_qty_ko_products > 0)
	{
		valid_qty = false;
		alert('Certains produits nécessitent une quantité minimale de commande.');
	}
	
	return valid_qty;
}

/*** fonction d'ajout de produit dans le panier ***/
function add_cart(code_product, quantite_produit) {
	$.post('ajax.php?pageload=ajax_add_cart&code_product='+code_product+'&quantite='+quantite_produit, function(data){  
		showSimpleModal('ajax.php?pageload=cart_popup&include_header=1', true);
	});	
}

function add_cart_simple(code_product, quantite_produit) {
	ajax_query_html('ajax_add_cart_simple','&code_product='+code_product+'&quantite='+quantite_produit,'cart_body');
}

/*** fonction d'ajout de produit dans le panier ***/
function add_cart_popup(code_product, quantite_produit) {
	ajax_query('ajax_add_cart','&code_product='+code_product+'&quantite='+quantite_produit);
	ajax_query_html('cart_popup','&include_header=1','product_popup_body');
}

/*** fonction de modification de produit dans le panier ***/
function maj_cart(code_product, quantite_produit) {
	ajax_query_html('ajax_maj_cart','&code_product='+code_product+'&quantite='+quantite_produit,'cart_body');
}


/*** fonction de mise à jour de la quantité ***/
function maj_quantite(code_product,popup) {

	quantity = document.getElementById('quantity_'+code_product).value;
	pageload = 'ajax_maj_quantity';
	param = 'code_product='+code_product+'&quantity='+quantity+'&popup='+popup;

	var xmlHttpReqh= false;
    var self = this;
     	
	// apppel ajax en fonction du navigateur 
    if (window.XMLHttpRequest) { // Mozilla/Safari 
		self.xmlHttpReqh = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 5.5+
		self.xmlHttpReqh = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
    self.xmlHttpReqh.open('POST', 'ajax.php', true);
    self.xmlHttpReqh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReqh.onreadystatechange = function() {
		if (self.xmlHttpReqh.readyState == 4) {			
        	document.getElementById('cart_full').innerHTML = self.xmlHttpReqh.responseText; 
        	document.getElementById('quantity_'+code_product).focus();
        	//myLytebox.updateLyteboxItems();
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}	
    self.xmlHttpReqh.send(qstr);
	    
	return(self.xmlHttpReqh);
}

/*** fonction vide pour timeout ***/
function vide() {
}
//
/*** Fonction pour l'affichage des popup trop belle ***/
function show_popup_cart(code_produit){
	showSimpleModal('ajax.php?pageload=product_popup&code_product='+code_produit+'&include_header=1', true);
}


var winAjax;

function show_popup_wishList(code_produit){
	showSimpleModal('ajax.php?pageload=product_wishlist&code_product='+code_produit+'&include_header=1');
}

function send_wishList(code_product){	
	var name = document.getElementById("wishListName").value;
	var message = document.getElementById("wishListMessage").value;
	var email = document.getElementById("wishListEmail").value;
	if(verifMail(email)){
		$.post("ajax.php?pageload=send_wishlist&code_product="+code_product, { name: name, message: message, email: email  } ,function(data){  			
  			myRep = data.split('[#]');
  			if (myRep[0] == 1){				
				$.modal.close();
				alert(myRep[1]);				
			}else{
				alert(myRep[1]);
			}			
		});
	}
}

//---< Fonction pour la verification des declinaison >---\\
function refreshProdctDecel(code_product, popup){
	//---< Recuperation des champs des select >---\\
	var isFormComplet = true;
	
	$('#infoDecli select').each(function() {	     	
     	if(this.value == 0) isFormComplet = false;
   	});
	
	var str = $("#infoDecli").serialize();
	
	var params = '';
	if (popup == "1")
	{
		params = '&popup=1';
	}
	
	if(isFormComplet == true){		
		$.post("ajax.php?pageload=ajax_check_availability&"+str+params, function(data){  			
  			myRep = data.split('[#]');
  			$('#productPrice_'+code_product).html(myRep[0]);
			$('#productAddCart').html(myRep[1]);
		});
		$.post("ajax.php?pageload=product_refreshImage&"+str, function(data){  			  			
  			$('#productImage_'+code_product).html(data);
		});
	} else {
		//---< nettoyage des informations >---\\
		$('#productPrice_'+code_product).html("");
		$('#productAddCart').html("");
	}
}

function showSimpleModal(url, reloadPage) {
	var containerHeight=540; //px
	var containerTop=15; //percent
	var windowH=(window.innerHeight?window.innerHeight:document.documentElement.clientHeight); //height of inner window
	var closeH=15; // buttom height
	var marginH=10; // total margin top and down
	
	//resize modal thanks CSS
	if (containerHeight+closeH+marginH>windowH){
		containerHeight=(windowH-closeH-marginH)+'px';
		containerTop=Math.floor(closeH-marginH/2)+'px';
	}else{
	 	if ((containerTop*windowH/100+containerHeight)>windowH)
	 	{
	 		containerTop=Math.floor((windowH-containerHeight)/2*100/windowH);
	 	}
	 	containerTop+='%';
 		containerHeight+='px';
	}
	var containerStyle={height:containerHeight, top:containerTop};

	//show modal
	if (typeof reloadPage != "undefined"){
		$('<iframe class="iFrameContainer" src="'+url+'"></iframe>').modal({
			containerCss:containerStyle,
			onClose: close_modal
		});
	}else{
		$('<iframe class="iFrameContainer" src="'+url+'"></iframe>').modal({
			containerCss:containerStyle
		});
	}
}

function close_modal(){
    /*refreshCart();
    $.modal.close();*/
    document.location.href='index.php?to=home';
}

function refreshCart(){
	if(document.getElementById('cart_body')){
		ajax_query_html('ajax_refresh_cart','','cart_body');
	}
}

//---< Fonction de chargement des marques en fonctions de la nomenclature >---\\
function loadSsCatNW(o){
	var codeCat = $(o).val();
	
	$.post("ajax.php?pageload=manageMenu&action=loadSousCat", { codeCat: codeCat } ,function(data){  			
 		myRep = data.split('[#]');
 		$('#sousCat_nom_web').html(myRep[0]);
 		$('#marque_nom_web').html(myRep[1]);
	});
}

function loadMarqueNW(o){
	var codeSsCat = $(o).val();
	var codeCat = $('#select_nom_web').val();
	
	$.post("ajax.php?pageload=manageMenu&action=loadMarque", { codeCat: codeCat, codeSsCat:codeSsCat } ,function(data){  			
 		$('#marque_nom_web').html(data);
	});
}

function loadResult(){
	var codeCat = $('#select_nom_web').val();
	var codeSsCat = $('#select_ssCat_nom_web').val();
	var codeMarque = $('#select_marque_nom_web').val();
	
	if(codeCat==0 & codeSsCat==0 & codeMarque==0){
		alert("Vous devez choisir une categorie...");
	}else{
		var url = "index.php?to=product_list&u_i_nomenclature="+codeCat;
		if(codeSsCat!=0) url += "&codeSsCat="+codeSsCat;
		if(codeMarque!=0) url += "&codeMarque="+codeMarque;
		
		// Redirection 
		document.location.href=url;
	}
}

function search_ref(){
	var ref_prod = $('#ref_product').val();
	
	if(ref_prod.length>0){
		var url = "index.php?to=search&ref_product="+ref_prod;	
		
		document.location.href=url;
	}else{
		alert("Ref. Incorrecte...");
	}
}

function add_product_cart(ref_product){
	var qte = $('#qte_'+ref_product).val();
	
	if(qte > 0){
		$.post("ajax.php?pageload=add_product_cart", { qte: qte, ref_product:ref_product } ,function(data){  			
			alert("Le produit a \351t\351 ajout\351 au panier.");
		});
	}else{
		alert("La quantit\351 est nulle...");
	}
}

function supp_product_cart(id_prod){
	$.post("ajax.php?pageload=supp_product_cart", { id_prod: id_prod } ,function(data){  			
		document.location.reload();
	});
}

function showCommande(o){
	n_commande = $(o).val();
	$.post("ajax.php?pageload=load_commande", { n_commande: n_commande  } ,function(data){  			
			$('#data_info').html(data);
		}			
	);
}

function check(o){
	if($(o).val() == '' | $(o).val() == 0){
		if($(o).parent().parent().prev().children().children().val() != ''){
			$(o).parent().animate({ backgroundColor: "red" }, 2000);
		}	
	}else{
		$(o).parent().animate({ backgroundColor: "white" }, 1000);
	}
}

function add_line(o){
	nb_ligne = $('#hidden_line').val();
	nb_ligne ++;
	nb_ligne_2 = nb_ligne + 1;
	tr = $(o).parents('tr:first').prev();
	$(tr).after('<tr><td><span id="nb_'+nb_ligne+'">'+nb_ligne+')</span></td><td><span id="ref_'+nb_ligne+'"><input type="text" name="SearchRef_'+nb_ligne+'" id="SearchRef_'+nb_ligne+'" size="28" /></span></td><td><span id="qte_'+nb_ligne+'" class="required"><input type="text" name="QtyRef_'+nb_ligne+'" id="QtyRef_'+nb_ligne+'" size="5" maxlength="5" onblur="check(this);"/></span></td><td style="padding-left:20px;"><span id="nb_'+nb_ligne_2+'">'+nb_ligne_2+')</span></td><td><span id="ref_'+nb_ligne_2+'"><input type="text" name="SearchRef_'+nb_ligne_2+'" id="SearchRef_'+nb_ligne_2+'" size="28" /></span></td><td><span id="qte_'+nb_ligne_2+'" class="required"><input type="text" name="QtyRef_'+nb_ligne_2+'" id="QtyRef_'+nb_ligne_2+'" size="5" maxlength="5" onblur="check(this);"/></span></td></tr>');
	$('#hidden_line').val(nb_ligne_2);
}

function check_form(){
	var $obj_required = $('#MultiSearch').find(".qte_required");
	var verif_result = true;
	
	$obj_required.each(
		function(i){
			var is_empty = false;
			switch(this.type){
				case 'text':
					if($(this).val() == ''){
						if($(this).parent().parent().prev().children().children().val() != ''){
							is_empty = true;
						}
					}
				break;
				default:
				break;
			}
			//---< Gestion input >---\\
			if(is_empty){
				$(this).parent().animate({ backgroundColor: "red" }, 2000);
				$(this).focus();
				verif_result = false;
			}else{
				$(this).parent().animate({ backgroundColor: "white" }, 1000);
			}
		}
	);
	if(verif_result){
		$('#MultiSearch').submit();
	}else{
		alert("Les Champs rouges sont obligatoires...");
	}
}

function suppProduct(id_prod){
	$.post("ajax.php?pageload=supp_product", { id_prod: id_prod } ,function(data){  			
		$('#resultArea').html(data);
	});
}

function select_ambigious(obj){
	//alert($(this).attr('name'));
	//alert($(obj).attr('value'));
	
	if(confirm('Choisir cette référence et supprimer les autres références équivalentes?')){
		
		id_list = '';
		$('input[type=radio][name="'+$(obj).attr('name')+'"]').each(function(){ 
		
			if(!$(this).attr('checked')){
				if(id_list == ''){
					id_list += $(this).attr('value');
				}else{
					id_list += ','+$(this).attr('value');
				}
			} 
		});
		
		$.post("ajax.php?pageload=supp_product", { id_prod: id_list, id_ambigious: $(obj).attr('value')} ,function(data){  			
			$('#resultArea').html(data);
			//alert($(obj).parents().parents().attr('tagName'));
			$(obj).parents().parents().find('.ref_ambigious').removeClass('ref_ambigious');
			//
		});
		
	}else{
		$(obj).attr('checked', false);
	}
}

function addToCart(){
	//type = $("input[@type=radio][@checked]").val();
	type = $('#typeChoix').val();
	if( type == 0 ){
	 	//type!=1 & type!=2 & type != 3){
		alert('Vous devez selectionner une des options disponibles');
	}else{
		$.post("ajax.php?pageload=addToCart", { type:type } ,function(data){  			
			location.href= "index.php?to=panier";
			//showSimpleModal('index.php?to=panier',false);
		});
	}
}

function check_commande(){
	var $obj_required = $('#send_cart').find(".n_required");
	var verif_result = true;
	
	$obj_required.each(
		function(i){
			var is_empty = false;
			switch(this.type){
				case 'text':
					if($(this).val() == ''){
						is_empty = true;
					}
				break;
				default:
				break;
			}
			//---< Gestion input >---\\
			if(is_empty){
				verif_result = false;
			}
		}
	);
	if(verif_result){
		$('#send_cart').submit();
	}else{
		alert("La référence de la commande est obligatoire.");
		$obj_required.focus();
		$obj_required.fadeTo(300, 0).fadeTo(300, 100).fadeTo(300, 0).fadeTo(300, 100);
		
	}
}

function activeChoice(choice){
	$('#typeChoix').val(choice);
	
	// remise a zero
	for(i=1;i<=3;i++){
		if($('#choice_' + i).length > 0){
			$('#choice_' + i).attr('src','_specific/erp_automax/pic/radio_0.jpg');
		}
	}
	
	$('#choice_' + choice).attr('src','_specific/erp_automax/pic/radio_1.jpg');
	
}
