﻿// Utiles.js
// Révision 13/12/2005

// DomValue DVal renvoyer la propriété Value d'un noeud dom identifié.
function DVal(anid)
{
	return document.getElementById(anid).value;
}

// 20/12/2005 Effacer le contenu d'un noeud visé par anid
function clearTag(anid)
{
	document.getElementById(anid).innerHTML="";
}


// Utilitaire pour manipuler XHRConnection
function loadHtm(acible,afile) 
{
	var XHR = new XHRConnection();
	XHR.setRefreshArea(acible);
	XHR.sendAndLoad(afile, "GET");
	return true;
}


function popup(page,l,h) {
	// ouvre une fenetre au placement prédéfini, largeur et hauteur transmise en parametre
	// Révision 15/05/2004
  	var left=(screen.width)-(l+50);
	if (h < 10) {h = 400};
	f=window.open(page,"pn","top = 15,left="+left+",width= "+l+" ,height = "+h+", scrollbars = yes");	
	f.focus();
}

// petite fonction utilitaire pour faire des balises
function bal(s)
{
	return "<" + s + ">";
}


// Révision 22/11/2005 
// prt : Le noeud chargé de recevoir l'enregistrement
// rec : Le nom de l'ensemble des champs
// url : Le programme php devant faire le travail
function appendForm(nodename,prt,fieldset,url)
{
node=document.getElementById(nodename);

	xml=bal("root");	
		xml=xml+bal("append");	
			xml=xml+bal("parent");
				xml=xml+prt;
			xml=xml+bal("/parent");

			xml=xml+bal("data");
			
			xml=xml+bal(fieldset);

				// Tous les inputs
				inputnodes = node.getElementsByTagName('input');
				
				for (i=0; i < inputnodes.length; i++)
				{
				
					xml=xml + bal(inputnodes[i].id);								
						xml=xml + inputnodes[i].value ;
					xml=xml + bal("/"+inputnodes[i].id);					
				}
				
				
				textarea = node.getElementsByTagName('textarea');
				
				for (i=0; i < textarea.length; i++)
				{
				
					xml=xml+bal(textarea[i].id);
						xml=xml + textarea[i].value ;		
					xml=xml+bal("/"+textarea[i].id);					
				}	
			
			xml=xml+bal("/"+fieldset);
			
			
			xml=xml+bal("/data");
			
		xml=xml+bal("/append");
	xml=xml+bal("/root");		
			
	//alert(xml);
	
	var XHR = new XHRConnection();
	XHR.setRefreshArea('output');
	XHR.appendData('xml',xml);
	XHR.sendAndLoad(url, "POST");	
}



// Révision 20/11/2005 
// rec est le nom des enregistrements, comme concert, message, ou info...
// cible est le n° d'ordre de la fiche
// url est l'adresse du programme php chargé de faire le travail
// TODO :  Ce programme raite tous les champs du document DOM. Il faut réduire à la div enveloppante, comme dans append form
function sendForm(rec,cible,url)
{
//alert('cible',cible)
	xml=bal("root");
		// Requête UPDATE

		xml=xml+bal("update");
			
			xml=xml+bal("cible");
				xml=xml+cible;
			xml=xml+bal("/cible");

		
			xml=xml+bal("record");
				xml=xml+rec;
			xml=xml+bal("/record");
			
			// L'ensemble des champs
			xml=xml+bal("fields");
			
				// Tous les inputs
				inputnodes = document.getElementsByTagName('input');
				
				for (i=0; i < inputnodes.length; i++)
				{
				
					xml=xml + bal("field");
					
					xml=xml+bal("id");
						xml = xml + inputnodes[i].id ;
					xml=xml+bal("/id");
				
					xml=xml + bal("value");
					xml=xml + inputnodes[i].value ;
					xml=xml+bal("/value");	
					
					xml=xml + bal("/field");					
				}
				
				
				textarea = document.getElementsByTagName('textarea');
				
				for (i=0; i < textarea.length; i++)
				{
				
					xml=xml+bal("field");
					
					xml=xml + bal("id");
						xml = xml + textarea[i].id ;
					xml=xml+bal("/id");
				
					xml=xml + bal("value");
					xml=xml + textarea[i].value ;
					xml=xml+bal("/value");	
					
					xml=xml+bal("/field");					
				}			

			xml=xml+bal("/fields");
			
		xml=xml+bal("/update");

			
	xml=xml+bal("/root");
	//alert(xml);
	// Envoyer à php
	
	var XHR = new XHRConnection();
	XHR.setRefreshArea(null);
	XHR.appendData('xml',xml);
	XHR.sendAndLoad(url, "POST");
}



// 24/11/2005 Cette fonction va rendre une chaine xml contenant les donnes des champs contenus dans anodeset
// <nom>Toto Cutugno</nom><email>toto@cutugno</email>
function extractNode(anodeset)
{
	xml="";
	for (i=0; i < anodeset.length; i++)
	{
		xml=xml+bal(anodeset[i].id);					
		xml=xml + anodeset[i].value ;
		xml=xml+bal("/"+anodeset[i].id);					
	}	
	return xml;
}


// 24//11/2005
// Approche vers une unification des traitements de données issues de formulaires...
// Le container est un div identifié, qui contiens des champs.
// Compiler les données dans une chaîne xml avec le format : (par exemple)
// <fieldset>
//	<nom>Toto Cutugno</nom>
//	<email>toto@cutugno</email>
//	<msg>Le corps du message</msg> 
// </fieldset>
// La fonction renvoie la chaîne xml
// La fonction appelle l'utilitaire bal() et extractNode(anode)
function extractForm(acontainer) // Et rend un fieldset xml
{
	container=document.getElementById(acontainer);
	xml=bal("fieldset");
	xml=xml+extractNode(container.getElementsByTagName('input'));
	xml=xml+extractNode(container.getElementsByTagName('textarea'));
	xml=xml+bal("/fieldset");
	return xml;
}

// Révision 24/11/2005 : area optionnel
// Envoyer le fragment XML à l'ul par POST, zone de réponse optionnelle
function sendXml(xml,url,area)
{
	var XHR = new XHRConnection();
	if (area)
	{
		XHR.setRefreshArea(area);
	}
	XHR.appendData('xml',xml);
	XHR.sendAndLoad(url, "POST");
}






