/**
 * @author Steve Grosbois
 * @version 0.1
 */

function total(id) {
	var nb_articles = $('nb_articles').value;
	var vtotal = 0;
	var v = 0;
	for(i=0; i<nb_articles; i++) {
		v = 0;
		if ($('v'+id+(i+1)).value != '') v = $('v'+id+(i+1)).value;
		vtotal += parseFloat(v);
	}
	$('total_'+id).update(vtotal);
}

function add_line() {
	var nb_articles = $('nb_articles').value;
	nb_articles++;
	var html = '<tr id="l'+nb_articles+'">';
	html += '<td id="article'+nb_articles+'">';
	html += '<img src="images/package.png" />&nbsp;&nbsp;<input id="varticle'+nb_articles+'" name="article'+nb_articles+'" type="text" size="40" value=""/>';
	html += '</td>';
	html += '<td id="quantite'+nb_articles+'" class="centered">';
	html += '<input id="vquantite'+nb_articles+'" name="quantite'+nb_articles+'" type="text" size="3" onkeyup="total(\'quantite\');" value="0"/>';
	html += '</td>';
	html += '<td id="cout_reel'+nb_articles+'" class="centered">';
	html += '<input id="vcout_reel'+nb_articles+'" name="cout_reel'+nb_articles+'" type="text" size="5" onkeyup="total(\'cout_reel\');" value="0"/> &euro;';
	html += '</td>';
	html += '<td id="prix_paye'+nb_articles+'" class="centered">';
	html += '<input id="vprix_paye'+nb_articles+'" name="prix_paye'+nb_articles+'" type="text" size="5" onkeyup="total(\'prix_paye\');" value="0"/> &euro;';
	html += '</td>';
	html += '<td class="action"><a href="javascript:delete_line('+nb_articles+');"><img src="images/package_delete.png" /></a></td>';
	html += '</tr>';
	new Insertion.Before('ladd', html);
	$('nb_articles').value = nb_articles;
}

function delete_line(pos) {
	var nb_articles = $('nb_articles').value;
	for(i=pos; i<nb_articles; i++) {
		$('varticle'+i).value = $('varticle'+(i+1)).value;
		$('vquantite'+i).value = $('vquantite'+(i+1)).value;
		$('vcout_reel'+i).value = $('vcout_reel'+(i+1)).value;
		$('vprix_paye'+i).value = $('vprix_paye'+(i+1)).value;
	}
	$('l'+nb_articles).remove();
	nb_articles--;
	$('nb_articles').value = nb_articles;
	total('quantite');
	total('cout_reel');
	total('prix_paye');
}
