/*---------------------------------------------
-- Fonction d'appel d'un objet XMLHTTPRequest
----------------------------------------------*/

// appel dynamique d'un fichier (ajax) avec retour de code HTML
function ajax_query_html(pageload, param, id_tag) {	
    var xmlHttpReqh= false;
    var self = this;
     	
	// apppel ajax en fonction du navigateur 
    if (window.XMLHttpRequest) { // Mozilla/Safari 
		self.xmlHttpReqh = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 5.5+
		self.xmlHttpReqh = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
    self.xmlHttpReqh.open('POST', 'ajax.php', true);
    self.xmlHttpReqh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReqh.onreadystatechange = function() {
		if (self.xmlHttpReqh.readyState == 4) {			
        	document.getElementById(id_tag).innerHTML = self.xmlHttpReqh.responseText; 
        	//myLytebox.updateLyteboxItems();
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}	
    self.xmlHttpReqh.send(qstr);
	    
	return(self.xmlHttpReqh);
}

// appel dynamique d'un fichier (ajax) avec appel fonction
function ajax_query_func(pageload, param, function_dec) {
	
    var xmlHttpReqf= false;
    var self = this; 
	
	// apppel ajax en fonction du navigateur 
    if (window.XMLHttpRequest) { // Mozilla/Safari 
		self.xmlHttpReqf = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 5.5+
		self.xmlHttpReqf = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
    self.xmlHttpReqf.open('POST', 'ajax.php', true);
    self.xmlHttpReqf.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReqf.onreadystatechange = function() {
		if (self.xmlHttpReqf.readyState == 4) {
        	eval(function_dec); 
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    self.xmlHttpReqf.send(qstr);
}

// appel dynamique d'un fichier (ajax)
function ajax_query(pageload, param) {
	
    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');
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    self.xmlHttpReq.send(qstr);
}

function ajax_linked_combo(p_s_ajaxload,p_o_masterCombo,p_s_linkedCombo,p_s_param) {
	linked = document.getElementById(p_s_linkedCombo).innerHTML;		
	v_i_pos_end_select = linked.indexOf("<OPTION");	// for IE
	if(v_i_pos_end_select == -1) v_i_pos_end_select = linked.indexOf("<option"); // for firefox
	if(v_i_pos_end_select == -1) v_i_pos_end_select = linked.indexOf("</SELECT>"); // for IE
	if(v_i_pos_end_select == -1) v_i_pos_end_select = linked.indexOf("</select>"); // for firefox
	linked =linked.substring(0, v_i_pos_end_select);
	linked = escape(linked);	
	ajax_query_html(p_s_ajaxload,'value='+p_o_masterCombo.value+'&linked_slt='+linked+p_s_param,p_s_linkedCombo);
		
}



function ajax_onchange_event(p_s_ajaxload,p_o_masterCombo,p_s_linkedCombo,p_s_param) {
	ajax_query_html(p_s_ajaxload,'value='+p_o_masterCombo.value+p_s_param,p_s_linkedCombo);
}



function ajax_filter_form(p_s_pageload,p_a_param,p_s_div_return,p_s_param) {
	
	param = '';	
	if(p_a_param.length != 0) {
		param_array = p_a_param.split('+');	
		for(i=0;i<param_array.length;i++) {		
			v_o_param_field = document.getElementById(param_array[i]);		
			param+= '&'+v_o_param_field.name+'='+v_o_param_field.value;
		}	
	}
	
	param_string = '';
	if(p_s_param.length != 0) {
		param_string = p_s_param;		
	}	
	
	ajax_query_html(p_s_pageload,param+param_string,p_s_div_return);
}





// -------> Function Ajax pour la phototheque <------------
function ajax_query_html_photo(pageload, param, id_tag) {
	
    var xmlHttpReqh= false;
    var self = this; 

	// apppel ajax en fonction du navigateur 
    if (window.XMLHttpRequest) { // Mozilla/Safari 
		self.xmlHttpReqh = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 5.5+
		self.xmlHttpReqh = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// @param
    self.xmlHttpReqh.open('POST', 'ajax.php', true);
    self.xmlHttpReqh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReqh.onreadystatechange = function() {
		if (self.xmlHttpReqh.readyState == 4) {
        	if(self.xmlHttpReqh.responseText != '##'){
        		document.getElementById(id_tag).innerHTML = self.xmlHttpReqh.responseText; 
        	   	new Effect.Appear(document.getElementById(id_tag), { duration: 1.0 });
        	}
        }
    }
	
	if(param != '' || param != null){
		
   		qstr = 'ajax=1&pageload=' + pageload + '&' + param;  // NOTE: no '?' before querystring
	}else{
		qstr = 'ajax=1&pageload=' + pageload;
	}
	
    self.xmlHttpReqh.send(qstr);
	
	return(self.xmlHttpReqh);
}


