var xhr=new Array();
var action='';
var ndds='';
var session='';
var suppression='';
 
function getXhr(name){
	if(window.XMLHttpRequest){
		xhr[name]=new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			xhr[name]=new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch(e){
			xhr[name]=new ActiveXObject('Microsoft.XMLHTTP');
		}
	}else{
		window.alert('Votre navigateur ne supporte pas les objets XMLHTTPRequest, veuillez le mettre à jour');
		xhr[name]=false;
	} 
}

function ajax_check(name){
	xhr[name].onreadystatechange=function(){
		if(xhr[name].readyState==4&&xhr[name].status==200){
			eval(xhr[name].responseText);
		}
	}
}

function ajax_faq(categorie,question){
	getXhr('af1');
	ajax_check('af1');
	xhr['af1'].open('POST','ajax-faq.html',true);
	xhr['af1'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['af1'].send('categorie='+escape(categorie)+'&question='+question);
}

function ajax_fillin(type,profil,maj){
	getXhr('af2');
	ajax_check('af2');
	xhr['af2'].open('POST','ajax-fillin.html',true);
	xhr['af2'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['af2'].send('type='+type+'&profil='+profil+'&maj='+maj);
}

function ajax_login(page){
	getXhr('al');
	ajax_check('al');
	xhr['al'].open('POST','ajax-login.html',true);
	xhr['al'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['al'].send('email='+escape(document.getElementById('email').value)+'&password='+escape(document.getElementById('password').value)+'&page='+escape(page));
	return(false);
}

function ajax_modif_login(){
	getXhr('aml');
	ajax_check('aml');
	xhr['aml'].open('POST','ajax-modif-login.html',true);
	xhr['aml'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['aml'].send('email='+escape(document.getElementById('email').value)+'&password='+escape(document.getElementById('password').value));
	return(false);
}

function ajax_renvoi_password(){
	getXhr('arp');
	ajax_check('arp');
	xhr['arp'].open('POST','ajax-renvoi-password.html',true);
	xhr['arp'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['arp'].send('email='+escape(document.getElementById('email').value));
	return(false);
}

function ajax_newsletter(){
	getXhr('an');
	ajax_check('an');
	xhr['an'].open('POST','ajax-newsletter.html',true);
	xhr['an'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['an'].send('newsletter='+escape(document.getElementById('newsletter').value));
	return(false);
}

function ajax_maj_ndds() {
	ndds='';
	flag='';
	for(i=1;i<=document.getElementById('champs').value;i++){
		if(document.getElementById('champ'+i).checked==true){
			ndds+=document.getElementById('champ'+i).value+'|';
			flag=1;
		}
	}
	return(flag);
}

function ajax_client_menu(produit){
	getXhr('acm');
	ajax_check('acm');
	xhr['acm'].open('POST','ajax-client-menu.html',true);
	xhr['acm'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['acm'].send('produit='+escape(produit));
	document.getElementById('bloqueur').innerHTML='';
}

function ajax_client_produit_menu(produit,action){
	flag=ajax_maj_ndds();
	if(flag!=1){
		action='inactif';
	}
	getXhr('aczpm');
	ajax_check('aczpm');
	xhr['aczpm'].open('POST','ajax-client-'+produit+'-menu.html',true);
	xhr['aczpm'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['aczpm'].send('action='+escape(action));
	ajax_client_zone(produit,action,ndds);
	if(flag==1){
		document.getElementById('log_action').value=action;
	}
	ajax_check_suppression(suppression);
}

function ajax_client_zone(produit,action,parametre){
	getXhr('acz');
	ajax_check('acz');
	xhr['acz'].open('POST','ajax-client-'+produit+'-zone.html',true);
	xhr['acz'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['acz'].send('action='+escape(action)+'&parametre='+escape(parametre));
	ajax_check_suppression(suppression);
}

function ajax_client_action(produit,action,champs){
	request='action='+escape(action);
	if(produit!='xdir-value'){
		ajax_maj_ndds();
		request+='&ndds='+escape(ndds);
	}
	if(typeof(champs)!='undefined'){
		var champsArray=champs.split('|');
		for(i=0;i<champsArray.length;i++){
			text=escape(document.getElementById(champsArray[i]).value);
			text=text.replace(/\+/g,'%2B');
			request+='&'+champsArray[i]+'='+text;
		}
	}
	getXhr('aca');
	ajax_check('aca');
	xhr['aca'].open('POST','ajax-client-'+produit+'-action.html',true);
	xhr['aca'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['aca'].send(request);
	if(action!='suppression'){
		ajax_check_suppression(suppression);
	}
	return(false);
}

function ajax_client_zone_patience(produit,action,parametre){
	document.getElementById('bloqueur').innerHTML='<div class="cover"></div>';
	ajax_client_zone(produit,action,parametre);
}

function ajax_check_suppression(produit){
	if(suppression!=''){
		suppression='';
		getXhr('acs');
		ajax_check('acs');
		xhr['acs'].open('POST','ajax-client-'+produit+'-encart-zone.html',true);
		xhr['acs'].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr['acs'].send('action=suppression&parametre=accueil');
	}
}

function ajax_bloc(bloc,get,form){
	randomid=Math.random();
	request='dummy=1';
	if(form!=false){
		for(i=0;i<document.getElementById(form).length;i++){
			if(document.getElementById(form).elements[i].name!=''){
				text=escape(document.getElementById(form).elements[i].value);
				text=text.replace(/\+/g,'%2B');
				request+='&'+document.getElementById(form).elements[i].name+'='+text;
			}
		}
	}
	getXhr('loadbloc_'+randomid);
	ajax_check('loadbloc_'+randomid);
	if(get==false){
		var get='';
	}else{
		get=get.replace(/\+/g,'%2B');
	}
	xhr['loadbloc_'+randomid].open('POST','ajax-'+bloc+'.html?'+get,true);
	xhr['loadbloc_'+randomid].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr['loadbloc_'+randomid].send(request);
	return(false);
}

function selectall(self,produit,champ,champs){
	for(i=1;i<=champs;i++){
		if(document.getElementById(self).checked==true){
			document.getElementById(champ+i).checked=true;
		}else document.getElementById(champ+i).checked=false;
	}
	if(document.getElementById('log_action').value!=''){
		ajax_client_produit_menu(produit,document.getElementById('log_action').value);
	} else ajax_client_produit_menu(produit,'accueil');
}

function display_combobox(type){
	document.getElementById('combobox-mails').innerHTML=document.getElementById('combobox-mails-'+type).innerHTML;
	ajax_check_suppression(suppression);
}
