//Image de chargement
var Loading = '<table width="100%" height="100%"><tr><td align="center" valign="center"><img src="/images/loading_black.gif" border="0" /></td></tr></table>';

//Fonction d'initialisation de l'objet XHTTPRequest elle permet
//de renvoyer un objet de ce type en fonction du navigateur
function GetXhr()
{
	var xhr = null; 
	//Declaration pour Geko et autres moteurs
	if(window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	//declaration pour IE
	else if(window.ActiveXObject)
		xhr = new ActiveXObject('Microsoft.XMLHTTP');
	else 
	{
		alert('Votre navigateur ne supporte pas les objets XMLHTTPRequest.'); 
		xhr = false; 
	} 
	return xhr
}

/*Fonction qui charge via XHTTPrequest la requete (req)
Options:
	syntax : option:value,option:value...
	loading : true or false, default is true, display or not the loading image
	method : GET or POST , GET is default (on post mode you should give the form parameter
	form : in POST mode design the form to post to
	aSync : true or false, default is true, execute or not on async mode
	doAfterLoad : Handler execute after the request complete
	doBeforeLoad : Handler execute before sendig request
	idDest : DOM id of the object where display the result
	objDest : object where display the result
*/
function ChargeContenu(req,option)
{
	//split options
	var list_option = [];
	var tab_option = [];
	var data = null;
	tab_option['loading'] = true;
	tab_option['aSync'] = true;
	tab_option['method'] = 'GET';
	list_option = option.split(',');
	for(i=0;i<list_option.length;i++){
		var option = [];
		option = list_option[i].split(':');
		tab_option[option[0]] = option[1];
	}
	
	//Action before load
	if(tab_option['doBeforeLoad']){
		eval(tab_option['doBeforeLoad']);
	}
	//Xhr exec
	var Xhr = GetXhr();
	
	if(eval(tab_option['aSync']) == true){
		Xhr.onreadystatechange = function(){
			if(Xhr.readyState == 4){
				//retour asynchrone
				XhrRetrun(tab_option,Xhr);
			}
		}
	}
	Xhr.open(tab_option['method'],req,tab_option['aSync']);
	//in post mode get params to send to
	if(tab_option['method'] == 'POST' && tab_option['form']){
		var data = recupDataForm(tab_option['form']);
		Xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	}else{
		//Xhr.setRequestHeader("Content-type", "content=text/html;charset=iso-8859-1");
	}
	//Xhr.setRequestHeader("Cache-Control", "no-cache");
	
	//Loading
	if(eval(tab_option['loading'])){
		if(tab_option['idDest']){
			document.getElementById(tab_option['idDest']).innerHTML=Loading;
		}else if(tab_option['objDest']){
			exec_str = tab_option['objDest']+'.innerHTML=Loading';
			eval(exec_str);
		}
	}
	
	Xhr.send(data);
	if(eval(tab_option['aSync']) == false){
		//Retour synchrone
		if(Xhr.readyState == 4){
			XhrRetrun(tab_option,Xhr);
		}
	}
}

function XhrRetrun(tab_option,Xhr){
	if(tab_option['idDest']){
		document.getElementById(tab_option['idDest']).innerHTML=Xhr.responseText;
	}else if(tab_option['objDest']){
		
		exec_str = tab_option['objDest']+'innerHTML=Xhr.responseText';
		eval(exec_str);
	}
	if(tab_option['doAfterLoad']){
		eval(tab_option['doAfterLoad']);
	}
}


function recupDataForm(nameForm){
	var f = document.forms[nameForm];
	var data;
	for(i=0;i<f.elements.length;i++){
		if(f.elements[i].type == 'radio'){
			if(f.elements[i].checked){
				if(i == 0){
					data = f.elements[i].name+'='+f.elements[i].value;
				}else{
					data += '&'+f.elements[i].name+'='+f.elements[i].value;
				}
			}
		}else{
			var elem = f.elements[i];
			if(i == 0){
				data = elem.name+'='+elem.value;
			}else{
				data += '&'+elem.name+'='+elem.value;
			}
		}
	}
	return(data);
}