
// Permet de recalculer automatiquement le montant total en cours de saisie de la quantité

function sub(event){ 
	//TouchKeyPress = (event.keyCode) ? event.keyCode : event.keyPress;
	TouchKeyPress = event.keyCode;
	
	if(TouchKeyPress == 13){
		event.keyCode == 0; 
		window.event.returnValue = false; 
		event.stop();
		return false;
	}
	
	return true;
}


function func_KeyPress(event, id, value){ 
	//TouchKeyPress = (window.Event) ? event.keyCode : event.keyPress;
	TouchKeyPress = event.keyCode;
	
	if(TouchKeyPress == 27){ // echap
		return false;
	}
	else if(TouchKeyPress == 13){ //Entrer
		event.keyCode = 9; 	
		cumuler();
		//var doc =  $('#quantite'+id).next(".corps").val();
		//alert(doc);	
		return false;
	}
} 

function recalculer (id) {

	var quantite = document.getElementById('quantite'+id).value;
	var prixunit = document.getElementById('prixunit'+id).value;	//var prixunit_pub = document.getElementById('prixunit_pub'+id).value;
	
	var montant = quantite * prixunit;	//var montant_pub = quantite * prixunit_pub;

	//arrondit le résultat et affichage forcé à 2 décimales
	montant = Math.round(montant*100)/100;	//montant_pub = Math.round(montant_pub*100)/100;
	montant = montant.toFixed(2);	//montant_pub = montant_pub.toFixed(2);

	document.getElementById('montant'+id).innerHTML = montant+' €'	//document.getElementById('montant_pub'+id).innerHTML = montant_pub+' €'
	if (isNaN(montant))	document.getElementById('montant'+id).innerHTML = '<font color="#FF0000">Erreur !</font>'	//if (isNaN(montant))	document.getElementById('montant_pub'+id).innerHTML = '<font color="#FF0000">Erreur !</font>'
		//cumuler();
	
	
	return false;
}
	//effectue un cumul de tous les montants

function cumuler() {
	
	var total = 0;	var total_pub = 0;	var total_epuise = 0;		$('.lig').each( function( index ){		var id = $(this).attr("id");		var p_id = id.substring(3, id.length);		if (typeof($('#quantite'+p_id)) != "undefined") {						var tempquantite = $('#quantite'+p_id).val();			var tempprixunit = $('#prixunit'+p_id).val();			var tempprixunit_pub = $('#prixunit_pub'+p_id).val();						var tempmontant = tempquantite * tempprixunit;			var tempmontant_pub = tempquantite * tempprixunit_pub;						total = total + tempmontant;				total_pub = total_pub + tempmontant_pub;			   if( $('#status'+p_id).val() >= 3){				total_epuise = total_epuise + tempmontant;			}		}				});		total = Math.round(total*100)/100;	total = total.toFixed(2);		total_pub = Math.round(total_pub*100)/100;	total_pub = total_pub.toFixed(2);		total_epuise = Math.round(total_epuise*100)/100;	total_epuise = total_epuise.toFixed(2);		if (isNaN(total)) $('#total').html('<font color="#FF0000">Erreur !</font>');	else $('#total').html(total+' €');		if (!isNaN(total_pub)) $('#total_public').html(total_pub+' €'); else $('#total_public').html('<font color="#FF0000">Erreur !</font>');	if (!isNaN(total_epuise)) $('#total_epuise').html(total_epuise+' €');
}

