<!-- 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){	
	popup = $(o).attr("alt");
	if($(o).is(':checked')){
		ajax_query_html('ajax_enable_saving','&popup='+popup, 'cart_full');
	}else{
		ajax_query_html('ajax_disable_saving','&popup='+popup, 'cart_full');
	}
}

function updateFundingAgree(o){			
	if($(o).is(':checked')) v_i_agree = 1;
	else v_i_agree = 0;
	$('#div_funding').load('ajax.php?pageload=ajax_funding&action=updateFundingAgree&agree='+v_i_agree);
	v_i_dponly = $('#form_display_only').val();		
	refreshFullCart(v_i_dponly,0);		
}

function updateFundingTermType(o){			
	$('#div_fundingTerm').load('ajax.php?pageload=ajax_funding&action=updateFundingTermType&code='+$(o).val());
	$('#div_fundingDetail').load('ajax.php?pageload=ajax_funding&action=updateFundingDetail');
	v_i_dponly = $('#form_display_only').val();
	refreshFullCart(v_i_dponly,0);			
}

function updateFundingTerm(o){				
	$('#div_fundingDetail').load('ajax.php?pageload=ajax_funding&action=updateFundingTerm&code='+$(o).val());
	v_i_dponly = $('#form_display_only').val();
	refreshFullCart(v_i_dponly,0);			
}

function refreshFullCart(v_i_dponly,popup) {
	$('#cart_full').load('ajax.php?pageload=ajax_refreshFullCart&dponly='+v_i_dponly+'&popup='+popup);
}

function updateCreditUse(o,popup) {
	v_i_dponly = $('#form_display_only').val();
	$('#cart_full').load("ajax.php?pageload=ajax_credit&action=updateCreditUse&creditId="+$('#'+o.id).val()+"&value="+$('#'+o.id+':checked').length+'&dponly='+v_i_dponly+'&popup='+popup);
}

function updateDiscountCode(discountCode,popup) {
	$.post("ajax.php?pageload=ajax_discount&action=updateDiscountCode&discountCode="+discountCode);
	v_i_dponly = $('#form_display_only').val();
	refreshFullCart(v_i_dponly,popup);	
}

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;
}

function posterAvisProduit(code_product) {
	$('#div_avis_form').load('ajax.php?pageload=ajax_product&action=postComment',
		{
			form_s_nom:$("#form_s_nom").val(),
			form_s_text:$("#form_s_text").val(),
			form_i_note:$("#form_i_note").val(),
			form_s_antispam:$("#form_s_antispam").val(),
			code_product:code_product				
		});
}



/*** fonction d'ajout de produit dans le panier ***/
function add_cart(code_product, quantite_produit, available_qty, unavailable_msg) {
	if(typeof(available_qty) != 'undefined'){
		if(available_qty < $('#product_qty_'+code_product).val()){
			alert(unavailable_msg);
			$('#product_qty_'+code_product).val(available_qty);
			return false;
		}
	}
	$.post('ajax.php?pageload=ajax_add_cart&code_product='+code_product+'&quantite='+$('#product_qty_'+code_product).val(), function(data){  
		showSimpleModal('ajax.php?pageload=cart_popup&include_header=1', true);
		//$.nyroModalManual({	url: 'ajax.php?pageload=cart_popup&include_header=1' });
		
	});	
}

function add_cart_simple(code_product, quantite_produit, available_qty, unavailable_msg) {
	if(typeof(available_qty) != 'undefined'){
		if(available_qty < $('#product_qty_'+code_product).val()){
			alert(unavailable_msg);
			$('#product_qty_'+code_product).val(available_qty);
			return false;
		}
	}
	v_s_param = 'ajax.php?pageload=ajax_add_cart_simple&code_product='+code_product+'&quantite='+quantite_produit;
	if($('#cart_body')) {
		$('#cart_body').load(v_s_param);
	} else {
		$.post(v_s_param);
	}
	//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, available_qty, unavailable_msg) {
	if(typeof(available_qty) != 'undefined'){
		if(available_qty < $('#product_qty_'+code_product).val()){
			alert(unavailable_msg);
			$('#product_qty_'+code_product).val(available_qty);
			return false;
		}
	}
	ajax_query('ajax_add_cart','&code_product='+code_product+'&quantite='+$('#product_qty_'+code_product).val());
	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,available_qty,unavailable_msg) {
	quantity = document.getElementById('quantity_'+code_product).value;
	
	if(typeof(available_qty) != 'undefined'){
		if(available_qty < quantity){
			alert(unavailable_msg);
			$('#quantity_'+code_product).val(available_qty);
			quantity = available_qty;
		}
	}
	
	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');
}


var winAjax;

function show_popup_wishList(code_produit){
	showSimpleModal('ajax.php?pageload=product_wishlist&code_product='+code_produit+'&include_header=1');
}
function show_popup_askProduct(code_produit){
	showSimpleModal('ajax.php?pageload=product_ask&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('[#]');
  			alert(myRep[1]);
  			if (myRep[0] == 1){
  				self.parent.$.modal.close();				
			}		
		});
	}
}

function send_askProduct(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_askProduct&code_product="+code_product, { name: name, message: message, email: email  } ,function(data){ 	
  			myRep = data.split('[#]');
  			alert(myRep[1]);
  			if (myRep[0] == 1){
  				self.parent.$.modal.close();				
			}		
		});
	}
}

//---< 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){		
		$("#product_button").load("ajax.php?pageload=ajax_check_availability&"+str+params);
		/*$.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.reload();
}


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,marque){
	var codeCat = $(o).val();
	var codeMarque=0;
	if (typeof(marque)!='undefined')
		codeMarque = document.getElementById(marque).value;
	
	$.post("ajax.php?pageload=manageMenu&action=loadSousCat", { codeCat: codeCat, codeMarque: codeMarque } ,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 au moins une valeur...");
	}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;
	}
}