var timerAjax = new Array();
var timer_FIFO = new Array();
var obj_inError = new Array();
var obj_WriteError = new Array(); 
var tmpObjWrite	= new Array();
var tmpObj= new Array();

function setInError(thisObj, objToWrite,  strError)
{
	if (thisObj.className.length == 0)
	{
		thisObj.className = "objInError";
	}
	else if (thisObj.className.indexOf('objInSucces', 0) >= 0)
	{
		thisObj.className = thisObj.className.replace(/objInSucces/g, 'objInError');
	}
	else
	{
		thisObj.className = thisObj.className + " objInError";
	}
	
	if (objToWrite) {objToWrite.innerHTML = strError;};
	
	
	
	obj_inError[obj_inError.length] = thisObj;
	obj_WriteError[obj_WriteError.length] = objToWrite;
}

function xhrUpdate_lastPan()
{
	anchorEvent_xhrUpdatePan(document.getElementById(document.getElementById("cilp").value))
}

function writeSucces(thisObj, objToWrite,  strSucces)
{
	
	if (thisObj.className.length == 0)
	{
		thisObj.className = "objInSucces";
	}
	else if (thisObj.className.indexOf('objInError', 0) >= 0)
	{
		thisObj.className = thisObj.className.replace(/objInError/g, 'objInSucces');
	}
	else
	{	
		thisObj.className = thisObj.className + " objInSucces";
	}
	
	if (objToWrite) {objToWrite.innerHTML = strSucces;};
	
	if (obj_inError.length > 0)
	{
		for (var i=0; i<obj_inError.length; i++) 
		{
			if (obj_inError[i] == thisObj && i < obj_inError.length - 1)
			{
				obj_inError[i] = obj_inError[i + 1]
				obj_inError[i + 1]  = thisObj
			}
		}	
		obj_inError.pop();
	}

	tmpObj[tmpObj.length] = thisObj;
	tmpObjWrite[tmpObjWrite.length] = objToWrite;
	timer_FIFO[timer_FIFO.length] = setInterval("removeSuccesSkin()", 1500);
	
}
	
function removeSuccesSkin()
{
	clearInterval(timer_FIFO[0]);
	timer_FIFO.shift();
	 
	
	tmpObj[0].className = tmpObj[0].className.replace(/objInSucces/g, '');
	
	if (tmpObjWrite[0]){tmpObjWrite[0].innerHTML = "";};
	
	tmpObj.shift();
	tmpObjWrite.shift();
}


function turnOff_timer()
{
	// loop through all anchor tags
	if (timerAjax)
	{
		for (var i=0; i<timerAjax.length; i++) 
		{
			clearInterval(timerAjax[i]);
		}
	}

	var timerAjax = new Array();
}

//=====================================================================================
// 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()
{
	// on met sur off les appele via setInterval. tout les timer sont stoqué dans le tableau timerAjax
	turnOff_timer();
	
	// si il n'est pas possible d'utilisé la fonctionnalité "getElementsByTagName" on arrete la...
	if (!document.getElementsByTagName){ return; }

	
	var inputs = document.getElementsByTagName("input"); // récupération de tous les input
	var xhrTarget;
	var morejs = "";
		
	var anchors = document.getElementsByTagName("a"); // récupération de tous les A
	
	// on se fade tout les liens :) (4/5 des fonctionnalité ajax)
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
	
		if (anchor.getAttribute("href") && anchor.getAttribute("href") != "#" && anchor.getAttribute("type")) 
		{	
			var attrType = anchor.getAttribute("type");
			var curFunction ;
			
			if (attrType == CONST_xhr_link || attrType == CONST_xhr_linkNotSave)
			{curFunction = function(e){return anchorEvent_xhrLink(this)};}
			
			else if (attrType == CONST_xhr_confirm)
			{curFunction = function(e){return anchorEvent_xhrLinkConfirm(this)};}
			
			else if (attrType == CONST_xhr_form)
			{curFunction = function(e){return anchorEvent_xhrForm(this)};}
			
			else if (anchor.getAttribute("type") == "XHRForm_Exec")
			{curFunction = function(e){return anchorEvent_execFormLink(this);};}
			
			else if (attrType == CONST_xhr_popUp )
			{curFunction = function(e){ return anchorEvent_xhrPopUp(this, false)};}
			
			else if (attrType == CONST_xhr_popUp_withBG)
			{curFunction = function(e){ return anchorEvent_xhrPopUp(this, true)};}
			
			else if (attrType == CONST_xhr_popUp_widget)
			{curFunction = function(e){return anchorEvent_xhrPopUp_widget(this)};}
			
			else if (attrType == CONST_xhr_update_pan)
			{curFunction = function(e){ return anchorEvent_xhrUpdatePan(this)};}
			
			else if (attrType == CONST_xhr_update)
			{curFunction = function(e){ return anchorEvent_xhrUpdateAjax(this)};}

			else if (attrType == CONST_xhr_update_pan__textArea)
			{curFunction = function(e){ return anchorEvent_xhrUpdatePan_textArea(this)};}
			
			else if (attrType == CONST_xhr_timer)
			{curFunction= null;anchor.setAttribute("xhrTime_" + anchor.id, timerAjax.length) ;timerAjax[timerAjax.length] = setInterval("anchorTime_withId('" + anchor.id +"')", 1000);}
 

            if (curFunction && !anchor.getAttribute("XHR_ADD") &&  anchor.getAttribute("XHR_ADD") != "1")
            {
                anchor.setAttribute("XHR_ADD", "1")
	            connect(anchor, "click", curFunction, true);
             //   anchor.onclick =  function(){return false};    
            }

		}
	}
	
	function connect(a0, a1, a2, a3) 
	{
        var a = arguments; 
		a0.onclick = a2; 
	    // return document.addEventListener ? a[0].addEventListener(a[1], a[2], a[3] || false):a[0].attachEvent ?a[0].attachEvent('on' + a[1], a[2]):false;
    }

 
	var selectsBox = document.getElementsByTagName("select"); // récupération de tous les select
	
	// loop through all select tags
	for (var i=0; i<selectsBox.length; i++) 
	{
		var selectBox = selectsBox[i];

		if (selectBox.getAttribute("src") && (selectBox.getAttribute("type") == CONST_xhr_select) && (!selectBox.getAttribute(CONST_xhr_asLoad) ))
		{	
			var xhr_path = selectBox.getAttribute("src");
			var lastId_name = "lastId";
			var lastId = "";
			
			selectBox.setAttribute(CONST_xhr_asLoad, "true");
			
			if (selectBox.getAttribute(CONST_lastId_name))
			{lastId_name = selectBox.getAttribute(CONST_lastId_name);}
						
			if (selectBox.getAttribute(CONST_lastId))
			{lastId = selectBox.getAttribute(CONST_lastId);xhr_path = xhr_path + '&' + lastId_name + '=' + lastId;}
			myXHR = new httpRequest(xhr_path , "", "POST");
			myXHR.LoadContent(selectBox.id,'','');
			
			if (selectBox.getAttribute("target"))
			{
				if (lastId.length > 0)
				{initSelectSousType(selectBox.getAttribute("target"),  lastId);}
				
				selectBox.onchange = function(){return selectEvent_xhrLink(this)};
			}
		}
		else if(selectBox.getAttribute("type") == CONST_xhr_link)
		{selectBox.onchange = function() {return selectEvent_xhrLink(this);}}
		else if(selectBox.getAttribute("type") == CONST_xhr_form)
		{selectBox.onchange = function(){return selectEvent_xhrForm(this);}}
		
		
	}
	
	
	// loop through all input tags
	for (var i=0; i<inputs.length; i++) 
	{
		var curRdb = inputs[i];
		if (curRdb.getAttribute("src") && curRdb.getAttribute("typeInput"))
		{
			var attrType = curRdb.getAttribute("typeInput");

			
			if (attrType == CONST_xhr_link)
			{curRdb.onclick = function(){inputEvent_xhrLink(this);};}
			
			else if (attrType == CONST_xhr_form)
			{curRdb.onkeypress = function(event){return keyPress_xhrForm(this, event);};}
			
			else if (attrType == CONST_xhr_change__noAction)
			{curRdb.onchange = function(event){return onChange_sendContent(this);};}
			
			else if (attrType == CONST_xhr_onBlur)
			{curRdb.onblur = function(){return inputEvent_XhrBlur(this);};}
		}
	}
	
	
	
	// init lightbox	
	if (Lightbox) {myLightbox = new Lightbox();}
}
