function loadImages()
{
	btn_photo_off = new Image();
	btn_photo_off.src = '_image/btn_box_off.png';
	btn_photo_on = new Image();
	btn_photo_on.src = '_image/btn_box_on.png';
}

loadImages();

function parcourrir_famille(ref)
{
	PMP.com.liste.showLoader('listephotos');
	PMP.com.liste.refresh('listephotos', {data:{famille:ref}}, 'produit', 'famille', 1);
	var familles = PMP.dom.getElementByMaskId('menu-%s', 'li');
	for(i=0;i<familles.length;i++)
	{
		if(familles[i].id == 'menu-'+ref)
			familles[i].className = 'active';
		else
			familles[i].className = '';
	}
}

function ajouter_article(ref, qte)
{
	var chk = document.getElementById('produit_'+ref);
		
	if(chk.value == 1)
	{
		var reponse = modifier_produit(ref, 0);
		actualiser_nbarticles_ajax();
		
		document.getElementById('cadre_produit_'+ref).className = 'cadre';
		document.getElementById('btn_panier_'+ref).src = btn_photo_off.src;
	}
	else{
		if(!ajouter_produit(ref, 1))
		{
			return false;
		}
		else{
			
			document.getElementById('cadre_produit_'+ref).className += ' on';
			document.getElementById('btn_panier_'+ref).src = btn_photo_on.src;
		}
	}
	
	if(chk.value == 1)
	{
		chk.value = 0
	}
	else
		chk.value = 1;
}

function actualiser_nbarticles_ajax()
{
	var ajax = new PMP.util.pmpAjax("/_modules/boutique/commande/panier.ajax", "POST");
		
	if(!ajax)
	{
		var msgbox = new PMP.util.pmpMsgBox("L'instanciation de la classe AJAX a échouée", "erreur", titreBox);
		return;
	}

	ajax.appendData("action", "get_nb_items");

	ajax.send();
		
	document.getElementById('nbarticlespanier').innerHTML = ajax.reponse.message;
}

function ajouter_produit(ref, qte, code)
{
	var titreBox = "Ajout d'un produit";
		
	if( !PMP.common.isNumber(ref) || ref==0 )
	{
		pmpAlert("La référence du produit est invalide.", titreBox, "erreur");
		return;
	}
	
	if( !PMP.common.isNumber(qte) )
	{
		pmpAlert("La quantité est invalide.", titreBox, "erreur");
		return;
	}	
	
	// ajout du produit au panier
	var ajax = new PMP.util.pmpAjax("/_modules/boutique/commande/panier.ajax", "POST");
	
	if(!ajax)
	{
		var msgbox = new PMP.util.pmpMsgBox("L'instanciation de la classe AJAX a échouée", "erreur", titreBox);
		return;
	}
	
	ajax.appendData("action", "ajouter_produit");
	ajax.appendData("ref", ref);
	ajax.appendData("qte", qte);
	
	if( !PMP.common.isUndefined(code) )
		ajax.appendData("code_prix", code);
	
	ajax.send();
	
	if(!ajax.reponse.etat)
	{
		alert(ajax.reponse.message + " " + ajax.url);
		return false;
	}
	else{
		ajax.appendData("action", "get_nb_items");
	
		ajax.send();
		
		document.getElementById('nbarticlespanier').innerHTML = ajax.reponse.message;
	}
	
	return true;
}


function modifier_produit(ref, qte, ligne)
{
	var titreBox = "Modification d'un produit";
		
	if( PMP.common.isUndefined(ref) )
	{
		pmpAlert("La référence du produit est invalide.", titreBox, "erreur");
		return false;
	}
	
	if( !PMP.common.isNumber(qte) )
	{
		pmpAlert("La quantité est invalide.", titreBox, "erreur");
		return false;
	}
	
	
	// ajout du produit au panier
	var ajax = new PMP.util.pmpAjax("/_modules/boutique/commande/panier.ajax", "POST");
	
	if(!ajax)
	{
		var msgbox = new PMP.util.pmpMsgBox("L'instanciation de la classe AJAX a échouée", "erreur", titreBox);
		return false;
	}
	
	ajax.appendData("action", "modifier_quantite");
	ajax.appendData("ref", ref);
	ajax.appendData("qte", qte);
	if(!PMP.common.isUndefined(ligne))
		ajax.appendData("ligne", ligne);
	
	ajax.send({context:true});
	
	return ajax.reponse;
}

function modiPaysLiv(obj)
{
	// recuperation du code pays selectionné dans la combo
	var codePays = obj.options[obj.selectedIndex].value;
	// modif graphique du prix
	actualiser_fraisdeport(codePays);
}

function addQuantite(ref, qte)
{
	if( PMP.common.isUndefined(ref) )
		return false;
	
	if( !PMP.common.isNumber(qte) )
		return false;
	
	var inputQte = document.getElementById("qte["+ref+"]");
	if(inputQte)
	{
		qte	= ( PMP.common.isNumber(parseInt(inputQte.innerHTML)) ? parseInt(inputQte.innerHTML) : 0 )
			+ parseInt(qte);
		
		if( qte >= 1 )
			setQuantite(ref, qte);		
	}
}


function supprimer_produit(ref)
{
	setQuantite(ref, 0);
	
	//window.location.replace(window.location.href);
    
    document.getElementById('cadre_produit_'+ref).style.display='none';
}


function setQuantite(ref, qte)
{
	if( PMP.common.isUndefined(ref) )
		return false;
	
	if( !PMP.common.isNumber(qte) )
		return false;
	
	qte = parseInt(qte);
	if( qte < 0 )
		qte = 0;
	
	
	// mise à jour du panier
	var reponse = modifier_produit(ref, qte);
	if( !reponse.etat )
	{
		//NotificationManager.notify({text:reponse.message, className:"erreur", visibleDuration:0, target:"panierTitre"});
		return;
	}
		
	var inputQte = document.getElementById("qte["+ref+"]");
	if(inputQte)
		inputQte.innerHTML 	= parseInt(qte);
	
	var inputPrix = document.getElementById("prix["+ref+"]");
	if(!inputPrix)
	{
		NotificationManager.notify({text:"Prix de l'article non défini", className:"erreur", visibleDuration:0, target:"panierTitre"});
		return;
	}
	
	var divMontant = document.getElementById("montant["+ref+"]");
	if(divMontant)
	{
		var montant = parseFloat(qte) * parseFloat(inputPrix.value);
		
		//vérifie que le montant soit positif
		if(montant < 0)
			montant = 0;
		
		divMontant.montant = montant;
		
		// formate le montant selon les paramètres de langue;
		montant = String.format(montant, "number");
		divMontant.innerHTML = montant;
		
		refreshSousTotal();
		actualiser_fraisdeport();
		actualiser_nbarticles()
	}
}


function refreshSousTotal()
{
	var elements = PMP.dom.getElementsByMaskId("montant[%i]", "span");
	
	var sousTotal = 0;
	for(var i=0; i < elements.length; i++)
	{
		sousTotal += elements[i].montant ? parseFloat(elements[i].montant) : 0;
	}
	
	var divSousTotal = document.getElementById("soustotal");
	if(divSousTotal)
	{
		divSousTotal.montant = sousTotal;
		
		// formate le montant selon les paramètres de langue;
		sousTotal = String.format(sousTotal, "number");
		
		divSousTotal.innerHTML = sousTotal;
	}
}


function refreshTotal()
{
	var montant  = 0;
	
	var divSousTotal = document.getElementById("soustotal");
	
	if(divSousTotal)
	{
		if(!divSousTotal.montant)
			refreshSousTotal();
		
		montant += parseFloat(divSousTotal.montant);
	}

	var divLivraison = document.getElementById("livraison");
	if(divLivraison)
		montant += parseFloat(divLivraison.montant);
	
	var divTotal = document.getElementById("total");
	if(divTotal)
	{
		divTotal.montant = montant;
				
		// formate le montant selon les paramètres de langue;
		montant = String.format(montant, "number");
		
		divTotal.innerHTML = montant;
	}
}


function actualiser_fraisdeport(pays)
{
	var livraison = document.getElementById("livraison");
	
	if(!livraison)
		return true;

	if(  PMP.common.isUndefined(pays) || !PMP.common.isNumber(pays) )
	{
		var paysCB = document.getElementById("pays");
					
		var pays = paysCB ? paysCB.value : 0;
	}
	
	
	// ajout du produit au panier
	var ajax = new PMP.util.pmpAjax("/_modules/boutique/commande/panier.ajax", "POST");
	
	if(!ajax)
		return false;
	
	ajax.appendData("action", "calculer_fraisdeport");
	ajax.appendData("pays", pays);
	
	ajax.send({context:true});
	
	if(ajax.reponse.etat)
	{
		var montant = parseFloat(ajax.reponse.valeur);
		
		livraison.montant = montant;
				
		// formate le montant selon les paramètres de langue;
		montant = String.format(montant, "number");
			
		livraison.innerHTML = montant;
	}

	refreshTotal();
	
	return true;
}


function actualiser_nbarticles()
{
	var elements = PMP.dom.getElementsByMaskId("qte[%i]", "input");
	
	var qte = 0;
	for(var i=0; i < elements.length; i++)
	{
		qte += parseInt(elements[i].value);
	}
	
	var nbPanier = document.getElementById("nbarticlespanier");
	if(nbPanier)
		nbPanier.innerHTML = qte;
	
}

function changeModeLivraison(mode)
{
	var chkMagasin = document.getElementById('checkMagasin');
	var chkLivraison = document.getElementById('checkLivraison');
	if(mode == 'magasin')
	{
		//document.getElementById('livraisonPays').style.visibility = 'hidden';		
		document.getElementById('livraisonPays').style.display = 'none';
		document.getElementById('venirMagasin').className = '';
		document.getElementById('fraisdeLivraison').className = 'desactive';
		actualiser_fraisdeport(0);	
	}
	else if(mode == 'livraison')
	{
		if(document.all)
			document.getElementById('livraisonPays').style.display = 'block';
		else
			document.getElementById('livraisonPays').style.display = 'table-row';
		//document.getElementById('livraisonPays').style.visibility = 'visible';
		document.getElementById('venirMagasin').className = 'desactive';
		document.getElementById('fraisdeLivraison').className = '';		
		
		modiPaysLiv(document.getElementById('choixPays'));
	}
}
