function addArticle(id_p, libelle_p, prix_p){
	$.post("panier/panier.inc.php", { action: ((id_p == "ds1")? "addAchat" : "addLoc"), id: id_p, libelle: libelle_p, prix: prix_p },
		function(data){
			$.each(data.articles, function(i,item){ 
				if(item.code_article == id_p){
					if($("#panierTable > table > tbody").children().is("tr#" + item.code_article))
						$("#panierTable > table > tbody > tr#" + item.code_article).replaceWith("<tr id=" + item.code_article + "><td>"+ ((item.prix == 0)?"<font  color='red'>"+item.libelle+"</font></td><td><font  color='red'>"+item.prix+" €" + ((item.code_article == "ds1")? "" : " / mois*")+"</font>":item.libelle+"</td><td>"+item.prix+" €" + ((item.code_article == "ds1")? "" : " / mois*")) +"</td></tr>"); 
					else
						$("#panierTable > table > tbody").append("<tr id=" + item.code_article + "><td>"+ ((item.prix == 0)?"<font  color='red'>"+item.libelle+"</font></td><td><font  color='red'>"+item.prix+" €" + ((item.code_article == "ds1")? "" : " / mois*")+"</font>":item.libelle+"</td><td>"+item.prix+" €" + ((item.code_article == "ds1")? "" : " / mois*")) +"</td></tr>"); 
				}
			});
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
		}, "json");
		updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
}//addArticle

function delArticle(id_p, prix_p){
	$("#panierTable > table > tbody > tr#"+id_p).remove();
	$.post("panier/panier.inc.php", { action: ((id_p == "ds1")? "delAchat" : "delLoc"), id: id_p, prix: prix_p },
		function(data){
			
			if(data != "false" && data.svg_id != null){
				$("#panierTable > table > tbody > tr#"+data.svg_id).remove();
				$.post("panier/panier.inc.php", { action: "delLoc", id: data.svg_id, prix: data.svg_prix });
				updatePanier("updateLoc");
				updatePanier("updateLoc");
				updatePanier("updateLoc");
				updatePanier("updateLoc");
			}
			
			if(data != "false" && data.maj_id != null){
				$("#panierTable > table > tbody > tr#"+data.maj_id).remove();
				$.post("panier/panier.inc.php", { action: "delLoc", id: data.maj_id, prix: data.maj_prix });
				updatePanier("updateLoc");
				updatePanier("updateLoc");
				updatePanier("updateLoc");
				updatePanier("updateLoc");
			}
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
			updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
		}, "json");
	updatePanier((id_p == "ds1")? "updateAchat" : "updateLoc");
}

function initPanier(){
	$.post("panier/panier.inc.php", { action: "updateAchat" },
		function(data){
			var patt=new RegExp("false");
			if(!(patt.test(data)) && data.articles != null){			
				$.each(data.articles, function(i,item){ 
					if(item != null){
						$("#panierTable > table > tbody").append("<tr id=" + item.code_article + "><td>"+ item.libelle +"</td><td>"+ item.prix +" €</td></tr>"); 
						modeAchat("achetermoins");
						choixSauvegarde("showAll");
					}
				});	
				
				if(data.prix > 0){
					commande(true);
					recommencer(true);	
				}
			 	$("#resultat > table > tbody").append("<tr id='ht'><td>Total HT</td><td id='value'>" + data.prix + " €</td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='livraison'><td><font color='red'>Livraison gratuite</font></td><td id='value'><font color='red'>0 €</font></td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='tva'><td>TVA</td><td id='value'>" + Math.round(data.tva*100)/100 + " €</td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='tot'><td>Total TTC</td><td id='value'>" + Math.round((data.tva+data.prix)*100)/100 + " €</td></tr>");
		 	}else{
		 		$("#resultat > table > tbody").append("<tr id='ht'><td>Total HT</td><td id='value'>0 €</td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='livraison'><td><font color='red'>Livraison gratuite</font></td><td id='value'><font color='red'>0 €</font></td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='tva'><td>TVA</td><td id='value'>0 €</td></tr>");
			 	$("#resultat > table > tbody").append("<tr id='tot'><td>Total TTC</td><td id='value'>0 €</td></tr>");
		 	}//if
	}, "json");
	$.post("panier/panier.inc.php", { action: "updateLoc" },
		function(data){
			var patt=new RegExp("false");
			if(!(patt.test(data)) && data.articles != null){	
				var flag = false ;
				var signMaj = "majplus";	
				$.each(data.articles, function(i,item){
					if(item != null) {
						$("#panierTable > table > tbody").append("<tr id=" + item.code_article + "><td>"+((item.prix == 0)?"<font color='red'>"+item.libelle +"</font></td><td><font color='red'>"+ item.prix +" € / mois*</font>":item.libelle +"</td><td>"+ item.prix +" € / mois*") + "</td></tr>"); 
						switch (item.code_article) {
							case 'ds2': modeAchat("louermoins"); choixSauvegarde("showAll"); options("hideAll"); flag = true; break;
							case 'sv50': choixSauvegarde("G50moins"); break;
							case 'sv100': choixSauvegarde("G100moins"); break;
							case 'sv150': choixSauvegarde("G150moins"); break;
							case 'sv200': choixSauvegarde("G200moins"); break;
							case 'sv250': choixSauvegarde("G250moins"); break;
							case 'maj': signMaj = "majmoins"; break;
							
						}//switch
					}
					if(!flag) options(signMaj);
				});
				
				
					
				if(data.prix > 0){
					commande(true);
					recommencer(true);	
				}	
				$("#forfait > table > tbody").append("<tr id='ht'><td>Total HT</td><td id='value'>" + data.prix + " €</td></tr>");
			 	$("#forfait > table > tbody").append("<tr id='tva'><td>TVA</td><td id='value'>" + Math.round(data.tva*100)/100 + " €</td></tr>");
			 	$("#forfait > table > tbody").append("<tr id='tot'><td>Total TTC</td><td id='value'>" + Math.round((data.tva+data.prix)*100)/100 + " €</td></tr>");
		 	}else{
		 		$("#forfait > table > tbody").append("<tr id='ht'><td>Total HT</td><td id='value'>0 €</td></tr>");
			 	$("#forfait > table > tbody").append("<tr id='tva'><td>TVA</td><td id='value'>0 €</td></tr>");
			 	$("#forfait > table > tbody").append("<tr id='tot'><td>Total TTC</td><td id='value'>0 €</td></tr>");
	 		}//if
	}, "json");
	
}//initPanier

function updatePanier(action_p){
	$.post("panier/panier.inc.php", { action: action_p },
		function(data){	
		var patt=new RegExp("false");
			var total = (action_p == "updateLoc")? "forfait": "resultat" ;
			if(!(patt.test(data)) && data != null){		
				$("#" + total + " > table > tbody > #ht > #value").replaceWith("<td id='value'>" + data.prix + " €</td>");
				$("#" + total + " > table > tbody > #tva > #value").replaceWith("<td id='value'>" + Math.round(data.tva*100)/100 + " €</td>");
				$("#" + total + " > table > tbody > #tot > #value").replaceWith("<td id='value'>" + Math.round((data.tva+data.prix)*100)/100 + " €</td>");
			}else{
				$("#" + total + " > table > tbody > #ht > #value").replaceWith("<td id='value'>0 €</td>");
				$("#" + total + " > table > tbody > #tva > #value").replaceWith("<td id='value'>0 €</td>");
				$("#" + total + " > table > tbody > #tot > #value").replaceWith("<td id='value'>0 €</td>");
			}//if
		},  "json");
}//updatePanier


function modeAchat(active){
	if(active == "showAll"){
		$("#modeAchat > div > table > tbody > tr > td > a.plus").show();
		$("#modeAchat > div > table > tbody > tr > td > a.moins").hide();
	}else if(active == "hideAll"){
		$("#modeAchat > div > table > tbody > tr > td > a").hide();
	}else{
		$("#modeAchat > div > table > tbody > tr > td > a").hide();
		$("#modeAchat > div > table > tbody > tr > td > a#"+active).show();
	}
	
}//modeAchat

function choixSauvegarde(active){
	if(active == "showAll"){
		$("#choixSauvegarde > div > table > tbody > tr > td > a.plus").show();
		$("#choixSauvegarde > div > table > tbody > tr > td > a.moins").hide();
	}else if(active == "hideAll"){
		$("#choixSauvegarde > div > table > tbody > tr > td > a").hide();
	}else{
		$("#choixSauvegarde > div > table > tbody > tr > td > a").hide();
		$("#choixSauvegarde > div > table > tbody > tr > td > a#"+active).show();
	}
}//choixSauvegarde

function options(active){
	if(active == "showAll"){
		$("#options > div > table > tbody > tr > td > a.plus").show();
		$("#options > div > table > tbody > tr > td > a.moins").hide();
	}else if(active == "hideAll"){
		$("#options > div > table > tbody > tr > td > a").hide();
	}else{
		$("#options > div > table > tbody > tr > td > a").hide();
		$("#options > div > table > tbody > tr > td > a#"+active).show();
	}
}//options

function commande(active){
	if(active == true){
		$(".validerCommande").show();
	}else{
		$(".validerCommande").hide();
	}
}//commande

function recommencer(active){
	if(active == true){
		$("#recommencer").show();
	}else{
		$("#recommencer").hide();
	}
}//recommencer

$(document).ready(
	function()
	{
		
		/*modeAchat("hideAll");
		choixSauvegarde("hideAll");
		options("hideAll");
		addArticle("ds1", "DSI Box (services inclus)", 149);
		commande(true);
		recommencer(false);*/
		initPanier();
		
		 /*$("#acheterplus").bind(
		 'click',
		 function(){
		 	addArticle("ds1", "DSI Box (achat)", 5580);
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	//addArticle("maj", "Assistance, Télésurveillance et Mise à jour", 20);
		 	choixSauvegarde("showAll");
		 	options("showAll");
		 	modeAchat("achetermoins");
		 	commande(false);
		 	recommencer(true);
			return false;
		});
		
		$("#lireexplications").bind(
			'click',
			function(){
				if($("#explications").is(":hidden"))
		 			$("#explications").slideDown("slow");
		 		else
		 			$("#explications").slideUp();
		 		return false;
		});
		
		$("#recommencer").bind(
			'click',
			function(){
				$.post("panier/panier.inc.php", { action: "reset" }, function(data){
					updatePanier("updateAchat");
					updatePanier("updateLoc");
				});
				$("#panierTable > table > tbody > tr").remove();
				modeAchat("showAll");
				choixSauvegarde("hideAll");
				options("hideAll");
				commande(false);
				recommencer(false);
				
				updatePanier("updateAchat");
				updatePanier("updateLoc");
				updatePanier("updateAchat");
				updatePanier("updateLoc");
				//window.location.reload()
				return false;
		});
		
		$("#louerplus").bind(
		 'click',
		 function(){
		 	
		 	addArticle("ds2", "DSI Box", 149);
		 	addArticle("sv50", "Option sauvegarde 50 Go", 0);
		 	//addArticle("maj", "Assistance et télésurveillance 24h/24h", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	options("hideAll");
		 	modeAchat("louermoins");
		 	commande(false);
		 	recommencer(true);
			return false;
		});
		 
		 /*$("#G50plus").bind(
		 'click',
		 function(){
		 	addArticle("sv50", "Option sauvegarde 50 Go", 24);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("G50moins");
		 	commande(true);
			return false;
		});
		
		$("#G100plus").bind(
		 'click',
		 function(){
		 	delArticle("sv50", 0);
		 	addArticle("sv100", "Option sauvegarde 100 Go", 50);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("G100moins");
		 	commande(true);
			return false;
		});
		
		$("#G150plus").bind(
		 'click',
		 function(){
		 	delArticle("sv50", 0);
		 	addArticle("sv150", "Option sauvegarde 150 Go", 100);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("G150moins");
		 	commande(true);
			return false;
		});
		
		$("#G200plus").bind(
		 'click',
		 function(){
		 	delArticle("sv50", 0);
		 	addArticle("sv200", "Option sauvegarde 200 Go", 130);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("G200moins");
		 	commande(true);
			return false;
		});
		
		$("#G250plus").bind(
		 'click',
		 function(){
		 	delArticle("sv50", 0);
		 	addArticle("sv250", "Option sauvegarde 250 Go", 160);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("G250moins");
		 	commande(true);
			return false;
		});
		
		/*$("#majplus").bind(
		 'click',
		 function(){
		 	addArticle("maj", "Assistance et télésurveillance 24h/24h", 20);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	options("majmoins");
			return false;
		});*/
		
		/*$("#achetermoins").bind(
		 'click',
		 function(){
		 	delArticle("ds1", 664);
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	updatePanier("updateAchat");
		 	//delArticle("maj", 20);
		 	choixSauvegarde("hideAll");
		 	options("hideAll");
		 	modeAchat("showAll");
		 	commande(false);
		 	recommencer(false);
			return false;
		});
		
		$("#louermoins").bind(
		 'click',
		 function(){
		 	delArticle("ds2", 149);
		 	//delArticle("maj", 0);
		 	delArticle("sv50", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("hideAll");
		 	options("hideAll");
		 	modeAchat("showAll");
		 	commande(false);
		 	recommencer(false);
			return false;
		});
		 
		 /*$("#G50moins").bind(
		 'click',
		 function(){
		 	delArticle("sv50", 24);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	commande(false);
			return false;
		});
		
		$("#G100moins").bind(
		 'click',
		 function(){
		 	delArticle("sv100", 50);
		 	addArticle("sv50", "Option sauvegarde 50 Go", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	commande(false);
			return false;
		});
		
		$("#G150moins").bind(
		 'click',
		 function(){
		 	delArticle("sv150", 100);
		 	addArticle("sv50", "Option sauvegarde 50 Go", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	commande(false);
			return false;
		});
		
		$("#G200moins").bind(
		 'click',
		 function(){
		 	delArticle("sv200", 130);
		 	addArticle("sv50", "Option sauvegarde 50 Go", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	commande(false);
			return false;
		});
		
		$("#G250moins").bind(
		 'click',
		 function(){
		 	delArticle("sv250", 160);
		 	addArticle("sv50", "Option sauvegarde 50 Go", 0);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	choixSauvegarde("showAll");
		 	commande(false);
			return false;
		});
		
		/*$("#majmoins").bind(
		 'click',
		 function(){
		 	delArticle("maj", 20);
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	updatePanier("updateLoc");
		 	options("majplus");
			return false;
		});*/
		
	}
);