// JavaScript Document
function ajout_panier2(id, qte){
	var pan=new Panier();
		pan.ajout_panier2(id, qte);

}
function ajout_panier(id, qte, form){
	var pan=new Panier();
	if(qte>0){
		pan.ajout_panier(id, qte, function(){
			if(pan.msg_ajout!=''){
				alert(pan.msg_ajout);
				pan.get_stock(id, function(){
					jsLib.getElementsByClassName('quantite',"input",form)[0].value=Math.round(pan.stock);
					calcul_montant(form, pan.stock);
					affiche_panier();
				})
			}
			else{calcul_montant(form, qte); affiche_panier();}
		});
	}
	else{pan.suppr_panier(id, function(){calcul_montant(form, 0);}); affiche_panier();}
}

function calcul_montant(form, qte){
	var qte_unite=jsLib.getElementsByClassName('qte_unite',"input",form)[0].value;
	var montant=jsLib.getElementsByClassName('montant',"input",form)[0].value;
	var prix=qte*qte_unite*montant;
	prix=prix.toFixed(2);
	
	var strong=jsLib.getElementsByClassName("montant","div",form)[0].getElementsByTagName("strong")[0];
	strong.innerHTML=prix+"";	
}

function affiche_panier(){
	var pan=new Panier();
	pan.compte_panier(function(){
		pan.prix_panier(function(){
				document.getElementById("nb_articles_panier").innerHTML=pan.nbPanier;
				document.getElementById("total_prix_panier").innerHTML=pan.prix_panier;
				
				if(document.getElementById("nb_articles_panier2"))document.getElementById("nb_articles_panier2").innerHTML=pan.nbPanier;
				if(document.getElementById("total_prix_panier2"))document.getElementById("total_prix_panier2").innerHTML=pan.prix_panier;
				if(document.getElementById("montant_global"))document.getElementById("montant_global").getElementsByTagName("strong")[0].innerHTML=pan.prix_panier;
				if(document.getElementById("montant_global")){if(document.getElementById("montant_global").getElementsByTagName("strong")[1]){
					pan.prix_panierTTC(function(){document.getElementById("montant_global").getElementsByTagName("strong")[1].innerHTML=pan.prix_panierTTC;});
				}}
				
		});
	});
}
function affiche_panier2(){
	if(document.getElementById("ifs_nb")!=null){
		var nb = document.getElementById("ifs_nb").value;
		var px = document.getElementById("ifs_px").value;
		var txt = "<strong>"+nb+" article(s)</strong> soit "+px+" &euro;";
		if(document.getElementById("infos_panier").childNodes[3].innerHTML!=undefined){
			document.getElementById("infos_panier").childNodes[3].innerHTML = txt;
		}else{
			document.getElementById("infos_panier").childNodes[1].innerHTML = txt;
		}
	}
}
function Panier(){this.nbPanier;this.stock;this.msg_ajout='';this.prix_panier;this.prix_panierTTC;}

Panier.prototype.compte_panier = function(callback){
	var localThis=this;
	var req = new HTTP_request("inc/ajax/compte_panier.php",
		function(){
			if(req.isSuccess()){
				localThis.nbPanier = req.getText();
				if(callback){callback();}
			}
		}		   
	,'get');// nouvelle requete
	
	req.sendRequest();
}

Panier.prototype.get_stock=function(id, callback){
	var localThis=this;
	var req = new HTTP_request("inc/ajax/get_stock.php",
		function(){
			if(req.isSuccess()){
				localThis.stock = req.getText();
				if(callback){callback();}
			}
		}		   
	,'get');// nouvelle requete
	req.sendRequest("id_prod="+id);
}

Panier.prototype.ajout_panier=function(id, qte, callback){
	var localThis=this;
	var req = new HTTP_request("inc/ajax/ajout_panier.php",
		function(){
			if(req.isSuccess()){
				localThis.msg_ajout = req.getText();
				if(callback){callback();}
			}
		}		   
	,'get');// nouvelle requete
	req.sendRequest("id_prod="+id+"&quantite="+qte);
}
///////////////////////////////////////////////////////////
Panier.prototype.ajout_panier2=function(id, qte){
	var req = new HTTP_request("inc/ajax/ajout_panier2.php",
		function(){
			if(req.isSuccess()){
				document.getElementById('panier_liste').innerHTML = req.getText();
					construct_panier2();
					init_suppr_panier();
					
			}
		}		   
	,'get');// nouvelle requete
	req.sendRequest("id_prod="+id+"&quantite="+qte);
}

////////////////////////////////////////////////////////////
Panier.prototype.suppr_panier = function(id){
	var req = new HTTP_request("inc/ajax/suppr_panier.php",	function(){
				if(req.isSuccess()){
					document.getElementById('panier_liste').innerHTML = req.getText();
					construct_panier2();
					init_suppr_panier();
					
				}
			}	,'get');// nouvelle requete
	req.sendRequest("id_prod="+id);
}

Panier.prototype.prix_panier = function(callback){
	var localThis=this;
	var req = new HTTP_request("inc/ajax/prix_panier.php",
		function(){
			if(req.isSuccess()){
				localThis.prix_panier = req.getText();
				if(callback){callback();}
			}
		}		   
	,'get');// nouvelle requete
	req.sendRequest();
}

Panier.prototype.prix_panierTTC = function(callback){
	var localThis=this;
	var req = new HTTP_request("inc/ajax/prix_panierTTC.php",
		function(){
			if(req.isSuccess()){
				localThis.prix_panierTTC = req.getText();
				if(callback){callback();}
			}
		}		   
	,'get');// nouvelle requete
	req.sendRequest();
}
