//var maxart=12;

function addsub(id,type,qr,nblig,separateurDecimal,languages_id,enPromo){
var maxart=parseInt(document.forms[1].elements['maxart'].value);

	if(qr<document.forms[1].elements['qte_'+id].value){
		alert('Juste only '+qr+' items available.');
		document.forms[1].elements['qte_'+id].value=qr;
		return false;
	}	

	var mtlg=0;
	if(document.forms[1].elements['qte_'+id].value==''){
		document.forms[1].elements['qte_'+id].value=0;
	}
	document.forms[1].elements['qte_'+id].value=parseInt(document.forms[1].elements['qte_'+id].value);
	
	var qte = document.forms[1].elements['qte_'+id];
	
	switch(type){
	case 2:
		if(parseInt(qte.value)<maxart){
			qte.value ++;
		}
		else{
		
			if(languages_id==1){
			 alert('You can only order  '+maxart+' pieces of the selected product');
			}else{
			alert('Achat limite: '+maxart+' produits maximum.');
			}
			qte.value=maxart;
		}
		break;
	case 1:
		if(parseInt(qte.value)>1){
			qte.value --;
		}
	}

	document.forms[1].elements['qte_'+id].value = qte.value;
	recalc(id,qr,nblig,separateurDecimal,languages_id,enPromo);
	
	return true;
}

function recalc(id,qr,nblig,separateurDecimal,languages_id,enPromo){
var maxart=parseInt(document.forms[1].elements['maxart'].value);

	var qte = document.forms[1].elements['qte_'+id].value;
	
	
	if(qte > maxart){
		if(qr < qte){
			alert('Le maximum disponible n\'est plus que de '+qr+' articles.');
			document.forms[1].elements['qte_'+id].value=qr;
			qte = qr;
		}
		else{
			if(languages_id==1){
			 alert('You can only order  '+maxart+' pieces of the selected product');
			}else{
			alert('Achat limite: '+maxart+' produits maximum.');
			}
			document.forms[1].elements['qte_'+id].value = maxart;
			qte = maxart;
		}			
	}	
	else{
		if(qr < qte){
			alert('Le maximum disponible n\'est plus que de '+qr+' articles.');
			document.forms[1].elements['qte_'+id].value=qr;
			qte = qr;
		}
	}	
	
	//recalcgen(nblig);
	if (document.forms[1].elements['ProduitAssocie'].checked) {
		if(languages_id==1){
		
		document.forms[1].elements['pt_'+id].value= "\u00A3"+format((qte * (document.forms[1].elements['pu_'+id].value)).toFixed(2),2,'','.');
		}else{
			if(enPromo==1){
			var montantassoc="";
		montantassoc=document.forms[1].elements['puassoc_'+id].value
	
		montantassoc= montantassoc.replace('&nbsp;', '');
		 montantassoc= montantassoc.replace('€', '');
		 montantassoc= montantassoc.replace('\u20ac', '');
		 montantassoc= montantassoc.replace('&euro;', '');
		 montantassoc= montantassoc.replace(',', '.');
		montantassoc=parseFloat(montantassoc);
		//alert(montantassoc);
		{
		var var_Pu_Promo="";
		var var_Pu_Promo_2="";
		var_Pu_Promo=document.getElementById('CartProductPricePU_PricePromo').innerHTML;
	
		var_Pu_Promo= var_Pu_Promo.replace('&nbsp;', '');
		var_Pu_Promo= var_Pu_Promo.replace('€', '');
		var_Pu_Promo= var_Pu_Promo.replace('\u20ac', '');
		var_Pu_Promo= var_Pu_Promo.replace('&euro;', '');
		var_Pu_Promo_2=format((document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal)+"";
	
	
			if(var_Pu_Promo_2==var_Pu_Promo){
	
				document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal);
				var calculpu="";
					calculpu=document.forms[1].elements['pus_'+id].value;
					var totalcalcul=parseFloat(calculpu)+parseFloat(montantassoc);
					//alert(totalcalcul);
				document.forms[1].elements['pu_'+id].value=totalcalcul;
				//alert("test1 pu "+document.forms[1].elements['pu_'+id].value);
						document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pu_'+id].value),2,'',separateurDecimal);
			}else{
		//alert("test2");
			document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal);
					var calculpu="";
					calculpu=document.forms[1].elements['pus_'+id].value;
					var totalcalcul=parseFloat(calculpu)+parseFloat(montantassoc);
					//alert(totalcalcul);
					document.forms[1].elements['pu_'+id].value=totalcalcul;
					//alert("test2 pu "+document.forms[1].elements['pu_'+id].value);
							document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pu_'+id].value),2,'',separateurDecimal);
							document.getElementById('PromoHousseChaise').innerHTML='<img width="91" height="91" src="images/promo/hc/p20.png">';
				document.getElementById('CartProductPricePU_PricePromo').innerHTML =format((document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal)+"&nbsp;&euro;";
			}
		
		}

		}else{
		document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pu_'+id].value),2,'',separateurDecimal);
		}
		
		}
	} else {
		if(languages_id==1){
	
		document.forms[1].elements['pt_'+id].value= "\u00A3"+format((qte * (document.forms[1].elements['pus_'+id].value)).toFixed(2),2,'','.');
		}else{
		
		if(enPromo==1){
		
		{
		var var_Pu_Promo="";
		var var_Pu_Promo_2="";
		var_Pu_Promo=document.getElementById('CartProductPricePU_PricePromo').innerHTML;
	
		var_Pu_Promo= var_Pu_Promo.replace('&nbsp;', '');
		var_Pu_Promo= var_Pu_Promo.replace('€', '');
		var_Pu_Promo= var_Pu_Promo.replace('\u20ac', '');
		var_Pu_Promo= var_Pu_Promo.replace('&euro;', '');
		var_Pu_Promo_2=format((document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal)+"";
	
	
			if(var_Pu_Promo_2==var_Pu_Promo){
	
				document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal);
			
			}else{
		//alert("test2");
			document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal);
		
				document.getElementById('PromoHousseChaise').innerHTML='<img width="91" height="91" src="images/promo/hc/p20.png">';
				document.getElementById('CartProductPricePU_PricePromo').innerHTML =format((document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal)+"&nbsp;&euro;";
			}
		
		}
		
		}else{
		document.forms[1].elements['pt_'+id].value= format(qte * (document.forms[1].elements['pus_'+id].value),2,'',separateurDecimal);
		}
		
		
		}
	}
	
}

function recalcgen(nblig){
/*
	//ligne
	var nbArt=0
	var txTva=0
	var ratTva=0 //ratio
	var mtTva=0
	var mtTTC=0
	
	//cumul
	var totArt=0
	var totTva=0
	var totTTC=0
	
	//sortie
	var chCde=''

	var i //iter
	var nblig=3 //nb lignes d'articles a passer 
	
	//boucle sur les lignes d articles
	for (i = 1; i <= nblig;  i++){ 
		nbArt=parseInt(document.getElementById('qte_00'+i).value);
		
		if(nbArt>0){
			//ligne courante
			mtTTC=nbArt*parseFloat(document.getElementById('pu_00'+i).value);
			txTva=document.getElementById('tva_001').value; //taux de tva de l article courant   parseFloat
			
			ratTva=parseFloat(1+(txTva/100)); //ratio de tva courant (ie: 1.196)
			mtTva=parseFloat(mtTTC-(mtTTC/ratTva)); //mt tva courant
						
			//cumuls
			totArt+=nbArt;
			totTva+=mtTva;
			totTTC+=mtTTC;

			if(chCde>''){chCde+='~';}//sep
			
			chCde+=document.getElementById('ref_001').value+'#' + nbArt;//XA#4~DC#2
		}//fin if
	}//fin for
	
	//intancie les champs totaux
	document.forms[1].elements['nbitems'].value = totArt;
	document.forms[1].elements['tva'].value = format(totTva,2,' ');
	document.forms[1].elements['mtot'].value = format(totTTC,2,' ');	
	
	document.forms[1].elements['C392'].value =chCde;//sortie
	*/
}
/*
function afftxtva(){
	//document.forms[1].elements['txtva'].value =(format(txtva,2,' ')+'%');
}
*/

function format(valeur,decimal,separateur,separateurDecimal) {
	
	
	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val=Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}

	if (decimal>0) {
		var decim=""; 
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+separateurDecimal+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	if (deci == 0) {
		return val;}

	return val_format;
}
/*
function ctrl_form() {
	
	if(document.forms[0].elements['nbitems'].value < 1){
		alert("Votre panier est vide !");
		return false;
	}
	else {
		document.forms[0].elements['C390'].value ='1';
		document.forms[0].elements['C391'].value =document.forms[0].elements['mtot'].value;
		return true;
	}	
	return true;
}*/

