var ajax_lock = 0;

// appel dynamique d'un fichier (ajax) avec retour de plusieurs valeurs pour plusieurs objects html
// format de la reponse php :
// <valueset><id_tag>id_champ_formulaire</id_tag><exec_method>methode</exec_method><value_tag>valeur_champ_formulaire</value_tag></valueset>
// methode peut tre value, innerHTML, etc...
function ajax_query_html_list_value(pageload, param)
{
	var xmlHttpReqlv = false;
	var self = this;
	var id_tags = new Array();
	var exec_methods = new Array();
	var value_tags = new Array();
	
	// apppel ajax en fonction du navigateur 
	if (window.XMLHttpRequest)
	{ // Mozilla/Safari 
		self.xmlHttpReqlv = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE 5.5+
		self.xmlHttpReqlv = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
	self.xmlHttpReqlv.open('POST', 'ajax.php', true);
	self.xmlHttpReqlv.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReqlv.onreadystatechange = function()
	{
		if (self.xmlHttpReqlv.readyState == 4)
		{
			var response_lines = self.xmlHttpReqlv.responseText.split('</value_tag></valueset>');
			for(var i = 0; i < response_lines.length; i ++)
			{
				if(response_lines[i] != null && response_lines[i] != '')
				{
					var response_values = response_lines[i].split('</exec_method><value_tag>');
					value_tags[i] = response_values[1];
					
					var response_exec = response_values[0].split('</id_tag><exec_method>');
					exec_methods[i] = response_exec[1];
						
					var response_id = response_exec[0].split('<valueset><id_tag>');
					id_tags[i] = response_id[1];
				} // end of if
			} // end of for
			
			for(var j = 0; j < exec_methods.length; j ++)
			{
				switch(exec_methods[j])
				{
					case 'value':
						document.getElementById(id_tags[j]).value = value_tags[j];
						break;
					case 'innerHTML':
						document.getElementById(id_tags[j]).innerHTML = value_tags[j];
						break;
					case 'class':
						$('#'+id_tags[j]).addClass(value_tags[j]);
						break;	
					default:
						break;
				} // end of switch
			} // end of for
			
		} // end of readystate
	} // end of function
	
	if(param != '' || param != null)
	{
		qstr = 'pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}
	else
	{
		qstr = 'pageload=' + pageload;
	}
	
	self.xmlHttpReqlv.send(qstr);
	
	return(self.xmlHttpReqlv);
}

// appel dynamique d'un fichier (ajax) avec retour de code HTML
function ajax_query_html(pageload, param, id_tag) {
	
    var xmlHttpReq = false;
    var self = this;
	
	// apppel ajax en fonction du navigateur 
    if (window.XMLHttpRequest) { // Mozilla/Safari 
		self.xmlHttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 5.5+
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
    self.xmlHttpReq.open('POST', 'ajax.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
    	if (self.xmlHttpReq.readyState == 4) {
    		document.getElementById(id_tag).innerHTML = self.xmlHttpReq.responseText;    	
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    self.xmlHttpReq.send(qstr);
}

// appel dynamique d'un fichier (ajax) avec retour de valeur pour object html
function ajax_query_html_value(pageload, param, id_tag, callback_func) {
	function ajaxObject(){
		if (document.all && !window.opera) obj = new ActiveXObject("Microsoft.XMLHTTP");
		else obj = new XMLHttpRequest();
		return obj;
	}
	var ajaxHttp = ajaxObject();
	
	// @param
    ajaxHttp.open('POST', 'ajax.php', true);
    ajaxHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxHttp.onreadystatechange = function() {
		if (ajaxHttp.readyState == 4) {
        	document.getElementById(id_tag).value = ajaxHttp.responseText;
			
			eval(callback_func);
			
			if(document.getElementById(id_tag).onchange){
				document.getElementById(id_tag).onchange();
			}
        }
    }
	
	if(param != '' || param != null){	
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    ajaxHttp.send(qstr);
	
	return(ajaxHttp);
}

// appel dynamique d'un fichier (ajax)
function ajax_query(pageload, param, callback_func) {
	function ajaxObject(){
		if (document.all && !window.opera) obj = new ActiveXObject("Microsoft.XMLHTTP");
		else obj = new XMLHttpRequest();
		return obj;
	}
	var ajaxHttp = ajaxObject();
		
	// @param
    ajaxHttp.open('POST', 'ajax.php', true);
    ajaxHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxHttp.onreadystatechange = function() {
		if (ajaxHttp.readyState == 4) {
			if(callback_func){
				eval(callback_func);
			}
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    ajaxHttp.send(qstr);
}
