var item_index = 0;


/**
 * ajouter_item
 * Ajouter un item dans le formulaire d'ajout d'items
 *
 * @author		Simon
 * @copyright	egzakt.com
 * @since		2008-09-02
 * @package		CLH-001
 *
 * @version		2008-09-02
 */
function ajouter_item() {
	item_index++;
	$('#item_0').clone().attr('id','item_'+item_index).appendTo('#liste');
	$('#item_'+item_index).children('td').children('input, select').each(function () {
		$(this).attr('id',$(this).attr('id').replace(/_0/,'_'+item_index));
		$(this).attr('name',$(this).attr('name').replace(/_0/,'_'+item_index));
		$(this).val('');
	});
	
	attacher_onchange(item_index);
	
	return false;
}


/**
 * attacher_onchange
 * A chaque changement, mettre a jour les totaux des items et le grand total
 *
 * @author		Simon
 * @copyright	egzakt.com
 * @since		2008-09-02
 * @package		CLH-001
 *
 * @param		li_index	int		l'ordre de l'item dans le formulaire
 *
 * @version		2008-09-02
 */
function attacher_onchange(li_index) {
	$('#item_'+li_index).children('td').children('input, select').change(function () {
	
		// Mettre a jour le total de l'item
		var montant =		0;
		var quantite =		parseInt($('#quantite_'+li_index).val());
		var prix =			parseFloat($('#prix_'+li_index).val().replace(/,/,'.'));
		var prix_vente =	parseFloat($('#prix_vente_'+li_index).val().replace(/,/,'.'));
		
		if (prix_vente > 0) {
			montant = prix_vente;
		} else if (prix > 0) {
			montant = prix;
		}
		if (montant > 0) {
			montant = formatNumber(montant * quantite);
			$('#total_'+li_index).val(montant);
			
			// Mettre a jour le grand total
			maj_grand_total();
		}
		
	});
}


/**
 * maj_grand_total
 * A chaque changement, mettre a jour le grand total des achats entrés
 *
 * @author		Simon
 * @copyright	egzakt.com
 * @since		2008-09-02
 * @package		CLH-001
 *
 * @version		2008-09-02
 */
function maj_grand_total() {
	var grand_total = 0;
	for (var i=0;i<=item_index;i++) {
		grand_total += parseFloat($('#total_'+i).val().replace(/,/,'.'));
	}
	$('#grand_total').val(formatNumber(grand_total));
}



// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
// http://javascript.about.com/library/blnumfmt.htm
// 2008-09-02 Simon : valeurs par defaut
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	if (!dec) dec = 2;
	if (!thou) thou = ' ';
	if (!pnt) pnt = ',';
	if (!curr1) curr1 = '';
	if (!curr2) curr2 = '';
	if (!n1) n1 = '-';
	if (!n2) n2 = '';
	var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;
}


/**
 * envoyer_bulletin
 * Appeler l'envoi du bulletin en Ajax, pour ne pas attendre la reponse du script php
 *
 * @author		Simon
 * @copyright	egzakt.com
 * @since		2008-07-01
 * @package		QIM-004
 *
 * @version		2008-09-03		CLH-001
 */
function envoyer_bulletin(bouton,str) {

	var params = "";

	if (confirm(str) && ($('#envoi_url').val() != "")) {
		$(bouton).attr("disabled", true); 
		var xhr = $.ajax({
			type: "POST",
			url: $('#envoi_url').val(),
			data: params,
			success: function(msg) {
				// ne pas attendre le reste du script PHP, on redirige a l page réponse
				window.open(url_reponse,'_self');
			}
		});
		
	}

	return true;
}


/**
 * stripVowelAccent
 * Remplacer les caracteres acentues
 *
 * @author		Stephen Chalmers
 * @copyright	http://bytes.com/forum/thread145532.html
 */
function stripVowelAccent(str) {
var s=str;
var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
/[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
/[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
/[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
/[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];
var repChar=['A','a','E','e','I','i','O','o','U','u'];
for(var i=0; i<rExps.length; i++)
s=s.replace(rExps[i],repChar[i]);
return s;
}
