/*
	Evenement à effecter au lien XHR qui vont devoir mettre à jour le contenu d'un div
	Le lien doit contenir les attributs suivante
	--> target
	--> href 
	--> type = XHRLink
*/
function anchorEvent_xhrLink(self)
{
	var moreAction  = "";
	if(self.getAttribute("moreaction"))
	{moreAction  = self.getAttribute("moreaction");}
		
	if (self.target)
	{XHRTarget=self.target}
	else
	{XHRTarget="main"}
        
    var demain=new Date();
    var current_hash = self.href
    window.location.hash=current_hash;	
	    	
	setCookie("XHRcok_TAR_" + XHRTarget, self.href)
    check_hash (XHRTarget,moreAction,true) ;
	return false;
}

function anchorEvent_xhrLinkConfirm(self)
{
	var msg = "";
	
	if(self.getAttribute("moreaction"))
	{eval(self.getAttribute("moreaction"));}
	
	if(self.getAttribute("msgConfirm"))
	{msg = self.getAttribute("msgConfirm");}

	if (self.target)
	{XHRTarget=self.target}
	else
	{XHRTarget="content"}
	
	if (confirm(msg))
	{
		myXHR = new httpRequest(self.href, "", "POST");
		myXHR.LoadContent(XHRTarget,'Chargement de la page en cours...<br>Veuillez patienter','');
	}
	
		
	return false;
}

/* 
	Evenement à effecter au lien XHR qui vont devoir se charger dans un div de style pseudo-popUp 
	(Le fond s'obscurcit, et seul le div est clair) 
	Le lien doit contenir les attributs suivante
	--> target
	--> href 
	--> type = XHRPopUp
*/
function anchorEvent_xhrPopUp(self)
{
	if(self.getAttribute("moreaction"))
	{eval(self.getAttribute("moreaction"));}
	
	print_pseudoPopUp(self.href);
	
	return false;
}


/*
	Evenement à effecter au lien XHR qui vont devoir soumettre un formulaire 
	Le lien doit contenir les attributs suivante
	--> target
	--> formName
	--> href 
	--> type = XHRForm
*/
function anchorEvent_xhrForm (self)
{		
	var XHR_MOREACTION = ""
	if(self.getAttribute("moreaction"))
	{XHR_MOREACTION = self.getAttribute("moreaction");}

	
	if (self.target)
	{XHRTarget=self.target}
	else
	{XHRTarget="content"}

	setCookie("XHRcok_TAR_" + XHRTarget, self.href)
	
	myXHR = new httpRequest(self.href, "", "POST");
	myXHR.PostForm(self.getAttribute("formName"));
	

	myXHR.LoadContent(XHRTarget,'Formulaire en cours de validation',XHR_MOREACTION)

	return false;
}	

//=====================================================================================
// Cette fonction parse un contenu HTML, récupère tous les liens et ajoute un évènement onclick sur chacun d'eux
// Permet d'utiliser XHR sous forme d'un lien simple
function initXHR()
{
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	var selectsBox = document.getElementsByTagName("select");
	var xhrTarget;
	var morejs = "";
	


	
	
	this.xhrSelect = function()
	{		
		if(this.getAttribute("moreaction"))
		{eval(this.getAttribute("moreaction"));}
	
		XHRTarget=this.getAttribute("target");
		var xhrTo = document.getElementById(XHRTarget).getAttribute("src");
		
		for (var i=0; i<this.options.length; i++) 
		{
			if (this.options[i].selected)
			{
				xhrTo  = xhrTo  + "&id=" + this.options[i].value;
			}
		}
				
		document.getElementById(XHRTarget).disabled= false;
		myXHR = new httpRequest(xhrTo, "", "POST");
		myXHR.LoadContent(XHRTarget,'Chargement de la page en cours...<br>Veuillez patienter','');
	
		return false;
	}	
	
	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && anchor.getAttribute("type")) 
		{	
			if (anchor.getAttribute("type") == "XHRLink")
			{

				anchor.onclick = function(){return anchorEvent_xhrLink(this);};
			}
			else if (anchor.getAttribute("type") == "XHRLink_confirm")
			{
				anchor.onclick = function(){ this.onclick ; return anchorEvent_xhrLinkConfirm(this)};
			}
			else if (anchor.getAttribute("type") == "XHRForm")
			{
				anchor.onclick = function(){ this.onclick ; return anchorEvent_xhrForm(this)};
			}
			else if (anchor.getAttribute("type") == "XHRPopUp")
			{
				anchor.onclick = function(){ this.onclick ; return anchorEvent_xhrPopUp(this)};
			}
		}
	}
	
	// loop through all select tags
	for (var i=0; i<selectsBox.length; i++) 
	{
		var selectBox = selectsBox[i];

		if (selectBox.getAttribute("src") && (selectBox.getAttribute("type") == "XHRSelect") && (!selectBox.getAttribute("XHR_LOAD") ))
		{	
			selectBox.setAttribute("XHR_LOAD", "true");
			xhr_path = selectBox.getAttribute("src")
			lastId = ""
			if (selectBox.getAttribute("lastId"))
			{lastId = selectBox.getAttribute("lastId");xhr_path = xhr_path + '&lastId=' + lastId;}
			
			myXHR = new httpRequest(xhr_path , "", "POST");
			myXHR.LoadContent(selectBox.id,'Chargement de la page en cours...<br>Veuillez patienter','');
			
			if (selectBox.getAttribute("target"))
			{
				if (lastId.length > 0)
				{initSelectSousType(selectBox.getAttribute("target"), '&id=' + lastId);}
				selectBox.onchange = this.xhrSelect;
			}
		}
	}
}
