var G_id, G_action;
var listId=	'liste';

const ARGS = 		window.arguments[0].inn;
const NOMS =		ARGS.noms;
const TABLE =		' '+ARGS.table+' ';
const PREFIX =	ARGS.prefix;
const ZONE =		ARGS.zone;
const VERIF =		ARGS.verif;

function Init()
{
	var sql = "SELECT * FROM" + TABLE +
						"ORDER BY 2";
	
	_listboxRemplir(listId, sql);

	G_id=0;
	G_action="insert";
	
	$("btnSubmit").label = "Ajouter";
	$('titre').value=NOMS;
	
	with($("libelle"))
	{
		reset();
		focus();
	}
}

function Reset()
{
	G_id = 0;
	G_action="insert";

	setVal($("btnSubmit"), "label", "Ajouter");
	
	with($("liste"))
	{
		selectedIndex = -1;
		scrollToIndex(0);
	}
	
	$("message").value="";
	
	with($("libelle"))
	{
		reset();
		focus();
	}
}

function Delete()
{
	var liste = $("liste");

	if (liste.selectedIndex>-1)
	{
		if (eval("window.opener."+VERIF+"("+liste.value+")"))
		{
			if(Confirm('Suppression', "Voulez-vous vraiment supprimer le libellé ?\n>> "+liste.getSelectedItem(0).firstChild.getAttribute("label")))
			{
				var sql = "DELETE FROM"+ TABLE +
									"WHERE "+PREFIX+"id=?1";
	
				_exec_sql(sql, liste.value);
	
				Init();
			}
		}
		else
		{
			Alert("Information", "Impossible de supprimer !!!");
		}
	}
}

function Encodage()
{
	var Inputs= submitForm();
	
	var msg_erreur=	"";
	var focus=			"";
	var verif=			true;
	
	if (Inputs['libelle']=="")
	{
		msg_erreur+=	"Veuillez remplir le libellé !\n";
		focus=				verif ? "libelle" : focus;
		verif=				false;
	}
	else
	{
		var sql = "SELECT "+PREFIX+"id FROM" + TABLE +
							"WHERE "+PREFIX+"libelle LIKE ?1";
				sql+= G_action=="modif" ? " AND "+PREFIX+"id <> "+G_id : "";
				sql+= " LIMIT 1";
		
		var req=_exec_sql(sql, Inputs['libelle']);
		
		if (req.executeStep())
		{
			msg_erreur+=	"Ce libellé existe déjà !\n";
			focus=				verif ? "libelle" : focus;
			verif=				false;
		}
		
		req.reset();
	}
	
	if (!verif)
	{
		Alert("Problème d'encodage", msg_erreur);
		$(focus).focus();
	}
	else	
	{
		if (G_action=="insert")
		{
			var sql = "INSERT INTO" + TABLE +
								"("+PREFIX+"libelle) " +
								"VALUES (?1)";
			
			_exec_sql(sql, Inputs['libelle']);
		}
		else
		{
			var sql = "UPDATE" + TABLE +
								"SET "+PREFIX+"libelle=?1 " +
								"WHERE "+PREFIX+"id=?2";
			
			_exec_sql(sql, Inputs['libelle'], G_id);
		}
			
		Init();
	}
}

function listebox_onselect(liste)
{
	if (liste.selectedIndex > -1)
	{
		setVal($("btnSubmit"), "label", "Modifier");
		
		G_id=liste.value;
		G_action="modif";
		
		$("libelle").value =liste.getSelectedItem(0).firstChild.getAttribute("label");
	}
}