/* $VERSION: recherche.js 3.1.032.1.2011.02.09.09.11	 */
//déclaration d'assistant
var declarations_assistant = new Array();

function EcranRecherche(p_nom, p_from, p_where, p_orderby)
{
  this.nom          = p_nom;                 // nom de l'Recherche
  this.from         = p_from;                // table(s) de la sélection
  this.where        = p_where;               // clause where de la sélection
  this.orderby      = p_orderby;             // clause tri de la sélection

  this.libelles     = "";                    // libellés séparés par des ;
  this.champs_sql       = "";                    // champs séparés par des ;
  this.noms_input        = "";                    // zones séparés par des ;
  
  this.nbChamps     = 0;                     // nombre de champs dans l'Recherche
  this.nbChampsVisibles     = 0;             // nombre de champs visibles dans l'Recherche
  this.nom_input    = new Array(10);         // nom de la zone de retour
  this.maxLength    = new Array(10);        // taille max de la zone de retour
  this.size    		= new Array(10);            // taille de la zone de retour
  this.champ_sql    = new Array(10);         // champs (Oracle)
  this.libelle      = new Array(10);         // libellé des colonnes de l'Recherche
  this.taille       = new Array(10);         // taille de la colone
  this.valeur       = new Array(10);         // valeur de la zone de recherche
}

//déclaration d'un assistant
function declare_recherche()
{
	code_assistant = arguments[0];
	from = arguments[1];
	where = arguments[2];
	orderby = arguments[3];
	declarations_assistant[code_assistant] = new EcranRecherche(code_assistant, from, where, orderby);
}

//déclaration d'un champs de recherche
function declare_champ_recherche(code_assistant, nom_input, champ_sql, libelle, taille)
{
	nbChamps = declarations_assistant[code_assistant].nbChamps;
	
	declarations_assistant[code_assistant].nom_input[nbChamps]	= nom_input;
	declarations_assistant[code_assistant].noms_input			+= nom_input+"*;*";
	declarations_assistant[code_assistant].champ_sql[nbChamps]  = champ_sql;
	declarations_assistant[code_assistant].champs_sql  			+= champ_sql+"*;*";
	declarations_assistant[code_assistant].libelle[nbChamps]    = libelle;
	declarations_assistant[code_assistant].libelles    			+= libelle+"*;*";
	declarations_assistant[code_assistant].taille[nbChamps]     = taille;
	declarations_assistant[code_assistant].valeur[nbChamps]     = '';
	
	declarations_assistant[code_assistant].nbChamps++;
	
	if ( libelle )
		declarations_assistant[code_assistant].nbChampsVisibles++;
}


//affichage d'assistants
var fenetre_assistant = false;
var code_assistant_en_cours = false;
var session_name = false;
var session_id = false;
function show_Recherche()
{
	code_assistant_en_cours = arguments[0]||code_assistant_en_cours;
	session_name = arguments[1]||session_name;
	session_id = arguments[2]||session_id;
	automatic_search = (arguments[0]?true:false);
	
	hauteur = 50 + 50 * declarations_assistant[code_assistant_en_cours].nbChampsVisibles;
	if ( hauteur < 200 ) hauteur = 200;
	largeur = 500;

	//fermeture de l'éventuelle dernière fenetre assistant ouverte
	try
	{
		fenetre_assistant.close();
	}
	catch(e)
	{
	}

	//calcul d'un numéro unique identifiant la fenetre pour ne pas avoir de conflits
	var maintenant = new Date();
	var fenetre_assistant_num = maintenant.getHours()+""+maintenant.getMinutes()+""+maintenant.getSeconds();

	fenetre_assistant = window.open("", "assistant_"+fenetre_assistant_num,"width="+largeur+",height="+hauteur+",status=no,resizable=yes,scrollbar=yes,top=200,left=200,dependant=yes");
	
	var document_f_assistant = fenetre_assistant.document;

	document_f_assistant.open();
	
	document_f_assistant.writeln("<html>");
	document_f_assistant.writeln("<head><title>Assistant</title>");
	document_f_assistant.writeln("<link rel='stylesheet' href='/composants/style_ciril.css' type='text/css'>");
	document_f_assistant.writeln("<link rel='stylesheet' href='/composants/css/input.css' type='text/css'>");
	document_f_assistant.writeln("<style>");
	document_f_assistant.writeln("body {background-color:#FAFFEC;}");
	document_f_assistant.writeln("table {width:100%; height:100%;}");
	document_f_assistant.writeln("</style>");
	document_f_assistant.writeln("</head>");
	document_f_assistant.writeln("<body>");
	document_f_assistant.writeln("<table width='100%' height='100%' id='fenetre_attente' style='display:none;position:absolute;top:0;left:0;background-color:#CCC;'><tr><td align='center' valign='middle' style='vertical-align:middle;'><img src='../../composants/images/sablier.gif'/> Recherche en cours</td></tr></table>");
	document_f_assistant.writeln("<form name='recherche' method='POST' action='/composants/recherche.php'>");
	document_f_assistant.writeln("<input type='hidden' name='libelles' value=\""+declarations_assistant[code_assistant_en_cours].libelles+"\" />");
	document_f_assistant.writeln("<input type='hidden' name='champs_sql' value=\""+declarations_assistant[code_assistant_en_cours].champs_sql+"\" />");
	document_f_assistant.writeln("<input type='hidden' name='noms_input' value=\""+declarations_assistant[code_assistant_en_cours].noms_input+"\" />");
	document_f_assistant.writeln("<input type='hidden' name='from' value=\""+declarations_assistant[code_assistant_en_cours].from+"\" />");
	document_f_assistant.writeln("<input type='hidden' name='where' value=\""+declarations_assistant[code_assistant_en_cours].where+"\" />");
	document_f_assistant.writeln("<input type='hidden' name='orderby' value=\""+declarations_assistant[code_assistant_en_cours].orderby+"\" />");
	if (session_name!='')
	{
		document_f_assistant.writeln("<input type='hidden' name='"+session_name+"' value='"+session_id+"' />");
	}
	document_f_assistant.writeln("<table>");
	js_effacer_criteres = "function effacer_criteres(){";
	for (var i=0; i < declarations_assistant[code_assistant_en_cours].nbChamps ; i++)
	{
		if (declarations_assistant[code_assistant_en_cours].libelle[i]!='')
		{
			document_f_assistant.writeln("<tr>");
			document_f_assistant.writeln("<td style='text-align:right;'>"+declarations_assistant[code_assistant_en_cours].libelle[i]+" :</td>");
			document_f_assistant.writeln("<td><input name='"+declarations_assistant[code_assistant_en_cours].nom_input[i]+"' value='"+getValue(declarations_assistant[code_assistant_en_cours].nom_input[i])+"' /></td>");
			document_f_assistant.writeln("</tr>");
			js_effacer_criteres += "document.getElementsByName('"+declarations_assistant[code_assistant_en_cours].nom_input[i]+"')[0].value = '';";
		}
	}
	js_effacer_criteres += "}";
	document_f_assistant.writeln("<tr><td colspan='2' style='text-align:center;'>");
	document_f_assistant.writeln("<input type='submit' onclick=\"document.getElementById('fenetre_attente').style.display='block';\" class='bouton rechercher' value='Rechercher'>");
	document_f_assistant.writeln("<input type='button' class='bouton supprimer' value='Effacer les critères' onclick='effacer_criteres();'>");
	document_f_assistant.writeln("</td></tr>");
	document_f_assistant.writeln("</table>");
	document_f_assistant.writeln("</form>");
	document_f_assistant.writeln("<script>");
	document_f_assistant.writeln(js_effacer_criteres);
	if ( automatic_search )
	{
		for (var i=0; i < declarations_assistant[code_assistant_en_cours].nbChamps ; i++)
		{
			if ( getValue(declarations_assistant[code_assistant_en_cours].nom_input[i]) )
			{
				document_f_assistant.writeln("document.recherche.submit();");
				break;
			}
		}
	}
	document_f_assistant.writeln("</script>");
	document_f_assistant.writeln("</body>");
	document_f_assistant.writeln("</html>");
	
	document_f_assistant.close();
}

