/* $VERSION: net_rh.js 3.1.032.1.2011.02.09.09.11	 */
/*
*	Temporairement mit ici, en attente de validation
*	Accepte en arguments :
*		- le nom de la fonction PHP qui va etre lancée
*		- le nom de la cible HTML
*		- autant d'argument que l'on veut passer à la fonction php en utilisant la syntaxe suivante : "ma_variable=mavaleur" / comme pour les URLHREF en php
*	Exemple : envoyerAJAX('ma_fonction_php','ma_cible_html','argument1=valeur1','argument2=valeur2')
*/


function envoyerAJAX(php_function_name,cible)
{
	//creation de l'objet pour IE
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
	xmllocal = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmllocal = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmllocal = false;
	}
	}
	@end @*/

	//creation de l'objet pour MOZILLA
	if (typeof XMLHttpRequest != 'undefined')
	{
		xmllocal = new XMLHttpRequest();
	}

	//gestion des arguments passés en paramètres
	var values = {};
	if ( envoyerAJAX.arguments[2] == "ALL_INPUTS" )
	{
		values = getAllsInputs();
	}
	else
	{
		var elements = "";
		for (indexARG=2; indexARG<envoyerAJAX.arguments.length; indexARG++)
		{
			pairString = envoyerAJAX.arguments[indexARG];
			var pair = pairString.split('=');
			values[pair[0]] = pair[1];
			elements += (pair[0] + ' ');
		}
		values['SENDDATA'] = elements;
		param = $H(values).toQueryString()
	}
	url = "?REQUETE_HTML=Y&PHPSESSID=" + getValue("PHPSESSID") + "&ACTION="+php_function_name+"&ENVOI_PARAMETRE="+param;
	if ( getObj("fenetre_attente") )
	{
		afficherFenetreAttente(true);
	}
	xmllocal.open("GET", url);
	xmllocal.onreadystatechange = function()
	{
		if (xmllocal.readyState == 4 && xmllocal.status == 200)
		{
			retour = xmllocal.responseText;
			retour = retour.replace(new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img'), '');
			try 
			{
				p = retour.search("OK");
				if(p > -1)
					retour =  retour.substr(p);
			}
			catch(e){};


			if ( retour.substr(0,2) != "OK" && retour.substr(2,2) != "OK" )
			{
				alert(retour);
			}
			else if (retour.substr(2))
			{
				if ( retour.substr(2,2) == "OK" )
				{
					donnees = retour.substr(4);
				}
				else
				{
					donnees = retour.substr(2);
				}
				retourAJAX(cible,donnees);
			}
			if ( getObj("fenetre_attente") )
			{
				afficherFenetreAttente(false);
			}
		}
	}
	xmllocal.send(null);
}

/*
*	Temporairement mit ici, en attente de validation
*	Cette fonction est appellé par envoyerAJAX lorsque PHP répond à la requete envoyée
*	Arguments :
*		- la cible html
*		- les données qui vont être affectées à cette cible (une valeur, plusieurs valeurs, ou du code html à lui injecter)
*/
function retourAJAX(cible,donnees)
{
	//suppression du code javascript généré en fin de retour
	if ( cible == "formulaire" ) tmp_objet = cible;
	else tmp_objet = (getObj(cible)?getObj(cible).tagName:false);
	if ( tmp_objet )
	{
		switch ( tmp_objet )
		{
			case "formulaire":
			tabparchamps = donnees.split("*;*");
			for (i=0; i<tabparchamps.length; i++)
			{
				tabvalchamps = tabparchamps[i].split("*,*");
				if ( tabvalchamps[0].substr(0,8) == "DISPLAY_" )
				{
					if ( monObjet=getObj(tabvalchamps[0].substr(8)) )
					{
						monObjet.style.display=tabvalchamps[1];
					}
					else
					{
						window.status="debug : l'objet '"+tabvalchamps[0].substr(8)+"' n'existe pas !";
					}
				}
				else if ( tabvalchamps[0].substr(0,11) == "VISIBILITY_" )
				{
					if ( monObjet=getObj(tabvalchamps[0].substr(11)) )
					{
						monObjet.style.visibility=tabvalchamps[1];
					}
					else
					{
						window.status="debug : l'objet '"+tabvalchamps[0].substr(11)+"' n'existe pas !";
					}
				}
				else if ( tabvalchamps[0].substr(0,9) == "DISABLED_" )
				{
					if ( monObjet=getObj(tabvalchamps[0].substr(9)) )
					{
						monObjet.disabled=(tabvalchamps[1]=="true"?true:false);
					}
					else
					{
						window.status="debug : l'objet '"+tabvalchamps[0].substr(9)+"' n'existe pas !";
					}
				}
				else if ( tabvalchamps[0].substr(0,10) == "CLASSNAME_" )
				{
					if ( monObjet=getObj(tabvalchamps[0].substr(10)) )
					{
						monObjet.className = tabvalchamps[1];
					}
					else
					{
						window.status="debug : l'objet '"+tabvalchamps[0].substr(10)+"' n'existe pas !";
					}
				}
				else
				{
					setValue(tabvalchamps[0],tabvalchamps[1]);
				}
			}
			break;

			case "TABLE":
			//on vide le tableau
			if ( document.getElementById(cible).nombre_ligne )
			{
				//alert(document.getElementById(cible).nombre_ligne);
				for (i=document.getElementById(cible).nombre_ligne; i>0; i--)
				{
					document.getElementById(cible).deleteRow(0);
				}
			}
			//document.getElementById(cible).deleteRow(0);
			//donnees = retour.substr(2);
			tabparchamps = donnees.split("*;*");
			var rangee = ((document.getElementById(cible).hasChildNodes() && document.getElementById(cible).childNodes[0].tagName == "THEAD")?1:0);
			for (i=0; i<tabparchamps.length; i++)
			{
				var TR = document.getElementById(cible).insertRow(rangee);
				rangee += 1;
				tabvalchamps = tabparchamps[i].split("*,*");
				for (j=0; j<tabvalchamps.length; j++)
				{
					var TD1 = document.createElement("td");
					var TD1text = document.createTextNode(tabvalchamps[j]);
					TD1.appendChild(TD1text);
					TR.appendChild(TD1);
				}
			}
			document.getElementById(cible).nombre_ligne = rangee;
			break;

			case "SELECT":
			//on vide la combo
			getObj(cible).innerHTML = "";

			//on ajoute un choix vide
			nouvel_element = new Option("","",false,true);
			getObj(cible).options[getObj(cible).length] = nouvel_element;

			donnees = retour.substr(2);
			tabparchamps = donnees.split("*;*");

			valeurs_combo = tabparchamps[0].split("*,*");
			libelle_combo = tabparchamps[1].split("*,*");

			for ( i=1; i<valeurs_combo.length; i++ )
			{
				nouvel_element = new Option(libelle_combo[i],valeurs_combo[i],false,true);
				getObj(cible).options[getObj(cible).length] = nouvel_element;
			}
			//positionement sur le premier pour MOZILLA
			getObj(cible).selectedIndex = 0;
			break;

			default:
			getObj(cible).innerHTML = donnees;
			//alert("type AJAX non pris en charge : "+tmp_objet.tagName);
			break;
		}
		if ( cible != "formulaire" && !isVisible(cible))
		{
			getObj(cible).style.display = "";
			getObj(cible).style.visibility = "visible";
		}
	}
	else
	{
		alert("la cible AJAX est introuvable !");
	}
}
/**
* Recupere toutes les valeurs des inputs de la page et les serializes
*
*/
function getAllsInputs()
{
	var values = {}
	var elements = '';
	inputs = document.getElementsByTagName("INPUT");
	for (i = 0; i < inputs.length; i++)
	{
		if (inputs[i].name == "ENVOI_PARAMETRE") continue;
		if (inputs[i].name == "ACTION")	continue;
		if (inputs[i].name == "HISTORY") continue;
		if (inputs[i].name == "PHPSESSID")continue;
		if (inputs[i].type == 'submit')	continue;
		if (inputs[i].name == "") continue;
		values[inputs[i].name] = getValue(inputs[i].name);
		elements += (inputs[i].name + ' ');
	}

	inputs = document.getElementsByTagName("SELECT");
	for (i = 0; i < inputs.length; i++)
	{
		values[inputs[i].name] = inputs[i].value;
		elements += (inputs[i].name + ' ');
	}
	inputs = document.getElementsByTagName("TEXTAREA");
	for (i = 0; i < inputs.length; i++)
	{
		values[inputs[i].name] = inputs[i].value;
		elements += (inputs[i].name + ' ');
	}
	values['SENDDATA'] = elements;
	return $H(values).toQueryString();
}



/*
*	Verifie que les inputs, select et textarea sont bien renseignés si obligatoire
*	Retourne True si OUI, sinon FALSE
*/
function verifier_formulaire()
{
	tab_input = document.getElementsByTagName('INPUT');
	tab_select = document.getElementsByTagName('SELECT');
	tab_textarea = document.getElementsByTagName('TEXTAREA');
	if ( verifier_tabObj(tab_input) && verifier_tabObj(tab_select) && verifier_tabObj(tab_textarea) )
	{
		return true;
	}
	return false;
}

/*
*	Verifie que les objets du tableau en entrée sont bien renseignés si obligatoire
*	Retourne True si OUI, sinon FALSE
*/
function verifier_tabObj(table_in, noalert)
{
	return true;
	for(i = 0; i < table_in.length; i++)
	{
		//table_in[i].value == '')
		if (gZonesEcrans && gZonesEcrans[table_in[i].name] &&  gZonesEcrans[table_in[i].name].isObligatoire && getValue(table_in[i].name) == '')
		{
			if ( !noalert )
			{
				if (gZonesEcrans[table_in[i].name].message != '')
				{
					alert(gZonesEcrans[table_in[i].name].message);
				}
				else
				{
					alert(table_in[i].name + ' est obligatoire');
				}
			}
			if (table_in[i].type != "hidden")
			{
				table_in[i].focus();
			}
			return false;
		}
	}
	return true;
}
/*
* Envoi d'un formulaire pour faire suite à un set_action
*
*/
function lanceFormulaire(target, action, extraparam)
{
	if ( verifier_formulaire() )
	{
		$("ACTION").form.action = target;
		$("ACTION").value = action;
		$("ENVOI_PARAMETRE").value = getAllsInputs();
		$("ENVOI_PARAMETRE").form.submit();
	}
}



/*
*	Cache ou affiche les combos de la page
*
*/
var ma_page_combo_tab = new Array();
function cacherCombosPage(MYtrueorfalse)
{
	//alert(trueorfalse);
	if ( !ma_page_combo_tab.length )
	{
		//alert("premier scan des combos, recup des combos visibles");
		mes_combos = document.getElementsByTagName("select");
		nb_combos = mes_combos.length;
		//alert("il y a "+nb_combos+" combos dans la page");
		for (i=0; i<nb_combos; i++)
		{
			if ( mes_combos[i].id != "")
			{
				//alert("ajout de "+mes_combos[i].id);
				if ( isVisible(mes_combos[i].id) )
				{
					ma_page_combo_tab[ma_page_combo_tab.length] = new Array("id",mes_combos[i].id);
				}
			}
			else if ( mes_combos[i].name != "")
			{
				//alert("ajout de "+mes_combos[i].name);
				if ( isVisible(mes_combos[i].name) )
				{
					ma_page_combo_tab[ma_page_combo_tab.length] = new Array("name",mes_combos[i].name);
				}
			}
		}
		//alert("il y a "+ma_page_combo_tab.length+" combos enregistrées comme visible");
	}

	for ( combo_ind=0; combo_ind<ma_page_combo_tab.length; combo_ind++ )
	{
		if ( ma_page_combo_tab[combo_ind][0] == "id" )
		{
			document.getElementById(ma_page_combo_tab[combo_ind][1]).style.visibility = (MYtrueorfalse==true?"visible":"hidden");
		}
		else
		{
			document.getElementsByName(ma_page_combo_tab[combo_ind][1])[0].style.visibility = (MYtrueorfalse==true?"visible":"hidden");
		}
	}
}

/*
*	Retourne TRUE si l'objet est visible
*
*/
function isVisible(nom_objet)
{
	mon_objet = getObj(nom_objet);
	if ( mon_objet )
	{
		if ( mon_objet.style.visibility != "hidden" && mon_objet.style.display != "none" )
		{
			//alert("l'objet "+nom_objet+" est visible ("+mon_objet.style.visibility+" - "+mon_objet.style.display+")");
			return true;
		}
	}
	//alert("l'objet "+nom_objet+" est pas visible ");
	return false;
}

/*
*	Permet de passer un objet d'un etat d'affichage inverse
*	Arguments :
*		- la cible html
*		- les données qui vont être affectées à cette cible (une valeur, plusieurs valeurs, ou du code html à lui injecter)
*/
function swapDisplayVisibility(nom_objet)
{
	if ( tmp_objet = getObj(nom_objet) )
	{
		if ( isVisible(nom_objet) )
		{
			//alert("je cache "+nom_objet);
			tmp_objet.style.visibility = "hidden";
			tmp_objet.style.display = "none";
			return false;
		}
		else
		{
			//alert("j'affiche "+nom_objet);
			tmp_objet.style.visibility = "visible";
			tmp_objet.style.display = "";
			return true;
		}
	}
	else
	{
		return -1;
	}
}



function swapImage(nomImage, ficimg1, ficimg2)
{
	//url actuelle de l'image
	srcimgactuel = getObj(nomImage).src ;
	//nom du fichier de l'image
	ficactuel = srcimgactuel.substr(srcimgactuel.lastIndexOf('/')+1);
	//nouvelle url de l'image
	srcimgnouveau = "";
	//inversement du nom du fichier de l'image
	if ( ficactuel == ficimg1 )
	{
		srcimgnouveau = srcimgactuel.substr(0,srcimgactuel.lastIndexOf('/')+1)+ficimg2;
	}
	else if ( ficactuel == ficimg2 )
	{
		srcimgnouveau = srcimgactuel.substr(0,srcimgactuel.lastIndexOf('/')+1)+ficimg1;
	}
	//mise a jour de la source de l'image
	if ( srcimgnouveau != "" )
	{
		getObj(nomImage).src = srcimgnouveau;
	}
}
