var prix=0
var qt_nom=0
var prx_nom=0
function Monetaire(x)  {
   var cents = Math.round(x*100) - Math.floor (x)*100;
   if (cents < 10) cents = "0" + cents;
   x = Math.floor (x).toString();
   var tmp ="";
   var cpt=0;
   for(i=x.length-1;i>=0;i--){
	   if(cpt%3==0 && cpt!=0){
	   tmp = " "+tmp;
	   }
	   tmp = x.charAt(i) +tmp;
	   cpt++;
   }
   x = tmp + "," + cents;// + "€";
   return x;
}

function total_quantite(bouteilles){
 	//var emballage=new Array("qt_","carton6_","carton12_");
 	var total_qty=0;
 	for(i=0;i<bouteilles.length;i++)
 	{
		var name_qt='qt_'+bouteilles[i][3];
		var name_c6='carton6_'+bouteilles[i][3];
		var name_c12='carton12_'+bouteilles[i][3];

		var qty_btl = !document.form.elements[name_qt].value ? 0 : parseInt(document.form.elements[name_qt].value);
		var qty_c6 =  !document.form.elements[name_c6].value ? 0 : parseInt(document.form.elements[name_c6].value);
		var qty_c12 = !document.form.elements[name_c12].value ? 0 :  parseInt(document.form.elements[name_c12].value);
		total_qty += qty_btl + (qty_c6*6) + (qty_c12*12);
 	}
 	
	for(j=0;j<bouteilles.length;j++)
	{
		var prx_nom="prx_"+bouteilles[j][3];
		if(total_qty >= 30)
		{
			var prix_vente=bouteilles[j][2];
		}
		else
		{
			var prix_vente=bouteilles[j][1];
		}
		calcul_ligne(bouteilles[j][3], prx_nom, prix_vente);
 	}

}

function calcul_ligne(qt_nom, prx_nom, prix_vente){
	var prx = 0;
	var name_qt='qt_'+qt_nom;
	var name_c6='carton6_'+qt_nom;
	var name_c12='carton12_'+qt_nom;
	var qty_btl = !document.form.elements[name_qt].value ? 0 : parseInt(document.form.elements[name_qt].value);
	var qty_c6 = !document.form.elements[name_c6].value ? 0 :parseInt(document.form.elements[name_c6].value);
	var qty_c12 = !document.form.elements[name_c12].value ? 0 :parseInt(document.form.elements[name_c12].value);
	var total_btls = qty_btl + (qty_c6*6) + (qty_c12*12);

	var total_ligne = total_btls*prix_vente;
	document.getElementById(prx_nom).value=Monetaire(total_ligne);
}

function calcultotal(){

var total=0
var blanc=0
var rouge=0
var rose=0
var maxblanc=0
var maxrouge=0

var reg = new RegExp(" ", "gi");
var reg1 = new RegExp(",", "gi");


/*Total*/
var blanc=document.form.prx_blanc.value;
var rouge=document.form.prx_rouge.value;
var rose=document.form.prx_rose.value;
var maxblanc=document.form.prx_maxblanc.value;
var maxrouge=document.form.prx_maxrouge.value;
blanc = parseFloat(blanc.replace(reg,"").replace(reg1,"."));
rouge = parseFloat(rouge.replace(reg,"").replace(reg1,"."));
rose = parseFloat(rose.replace(reg,"").replace(reg1,"."));
maxblanc = parseFloat(maxblanc.replace(reg,"").replace(reg1,"."));
maxrouge = parseFloat(maxrouge.replace(reg,"").replace(reg1,"."));

total=blanc+rouge+rose+maxblanc+maxrouge;

/*Total qty*/
var qty_blanc= !document.form.qt_blanc.value ? 0 : parseInt(document.form.qt_blanc.value);
var qty_rouge= !document.form.qt_rouge.value ? 0 : parseInt(document.form.qt_rouge.value);
var qty_rose= !document.form.qt_rose.value ? 0 : parseInt(document.form.qt_rose.value);
var qty_maxblanc= !document.form.qt_maxblanc.value ? 0 : parseInt(document.form.qt_maxblanc.value);
var qty_maxrouge= !document.form.qt_maxrouge.value ? 0 : parseInt(document.form.qt_maxrouge.value);
total_qty=qty_blanc+qty_rouge+qty_rose+qty_maxblanc+qty_maxrouge;



/*Total TTC*/
document.form.totalttc.value=Monetaire(total);


if((total_qty%6)!=0){
	document.getElementById('message_qty').innerHTML = "<b class=\"infos_qty\" >Vous devez avoir un nombre de bouteilles multiple de 6";
	document.getElementById('formulaire_livraison').style.display="none";
}
else{
	document.getElementById('message_qty').innerHTML = "";
	document.getElementById('formulaire_livraison').style.display="inline";
}
}
