function getXhr()
{
	var xhr;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        xhr = new XMLHttpRequest();
    }
    return xhr;
}

function login()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './loginText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById('compte').innerHTML = xhr.responseText;
			callPageGet("./menu2Text.php", "menu");
			callPageGet("./connectesText.php", "connectes");
		}
	}
	xhr.send("pseudo="+document.formLogin.pseudo.value+"&password="+document.formLogin.password.value);
}

function verifEmail()
{
	var xhr;
	xhr = getXhr();
	xhr.open("GET", "./menuText.php?option=verifEmail&email="+document.getElementById('email').value, true); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
		{
			if (xhr.responseText == "true")
				return true;
			else
				return false;
		}
	}
	xhr.send(null);
}

function ecrireNews()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './adminText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("title="+document.formEcrireNews.title.value+"&tags="+document.formEcrireNews.tags.value+"&content="+document.getElementById("contentNews").value+"&menu=ecrire_news&action=publier");
}


function editerNews()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './adminText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("title="+document.getElementById('titre').value+"&content="+document.getElementById("contentNews").value+"&id_news="+document.getElementById("id_news").value+"&menu=editer_news&action=editer");
}

function inscriptionSite()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './menuText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("pseudonyme="+ document.getElementById('formPseudo').value +"&password="+ document.getElementById('pass1').value +"&prenom="+ document.getElementById('prenom').value +"&nom="+ document.getElementById('nom').value +"&email="+ document.getElementById('email').value +"&menu=inscription_site&action=go");
}

function changerPassword()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './membreText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("password_ancien="+ document.getElementById('ancien_pass').value +"&password_new="+ document.getElementById('pass1').value +"&menu=changer_password&action=go");	
}

function modifierProfil()
{
	var xhr;
	var sexe;
	if (document.formModifierProfil.sexe[0].checked)
		sexe = 2;
	if (document.formModifierProfil.sexe[1].checked)
		sexe = 1;
	if (document.formModifierProfil.sexe[2].checked)
		sexe = 0;		
	xhr = getXhr();
	xhr.open("POST", './membreText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("nom="+ document.getElementById('nom').value
		   +"&prenom="+ document.getElementById('prenom').value
		   +"&login_cersti="+ document.getElementById('login_cersti').value
		   +"&promo="+ document.getElementById('promo').value
		   +"&sexe="+ sexe
		   +"&avatar="+ document.getElementById('avatar').value
   		   +"&site_web="+ document.getElementById('site_web').value
		   +"&msn="+ document.getElementById('msn').value
   		   +"&occupation="+ document.getElementById('occupation').value
		   +"&menu=modifier_profil&action=go");		
}


function prendreContact()
{
	if (document.getElementById('nom').value != '' &&
		document.getElementById('prenom').value != '' &&
		document.getElementById('email').value != '' &&
		document.getElementById('objet').value != '' &&
		document.getElementById('message').value != '')
	{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './menuText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
	xhr.send("nom="+ document.getElementById('nom').value
		   +"&prenom="+ document.getElementById('prenom').value
   		   +"&email="+ document.getElementById('email').value
   		   +"&objet="+ document.getElementById('objet').value
   		   +"&message="+ document.getElementById('message').value		   
		   +"&menu=contact&action=go");		
	}
	else
	{
		document.getElementById('infos').innerHTML = "<strong>Informations manquantes</strong>";
	}
}


function editerEvent()
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", './eventsText.php', true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.send("title="+ document.getElementById('titre').value
		   +"&gd_aff="+ document.getElementById('gdaff').value
		   +"&mini_aff="+ document.getElementById('miniaff').value
		   +"&description="+ document.getElementById('descriptif').value
		   +"&event="+ document.getElementById('evenement').value
		   +"&status="+ document.getElementById('status').value
		   +"&menu=editer&action=go");
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('contenu').innerHTML = xhr.responseText;
	}
}


function callPagePost(arg, page, target)
{
	var xhr;
	xhr = getXhr();
	xhr.open("POST", page, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
	xhr.send(arg);
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById(target).innerHTML = xhr.responseText;
	}
}

function callPageGet(page, target)
{
	var xhr;
	xhr = getXhr();
	xhr.open("GET", page, true); 
	xhr.onreadystatechange = function()
	{
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById(target).innerHTML = xhr.responseText;
	}
	xhr.send(null);
}

function verifDispoPseudo()
{
		var xhr;
	xhr = getXhr();
	xhr.open("GET", "./menuText.php?option=verifDispo&pseudo="+document.getElementById('formPseudo').value, true); 
	xhr.onreadystatechange = function()
	{ 
   		if(xhr.readyState == 4 && xhr.status == 200)
			document.getElementById('disponibilite_pseudo').innerHTML = xhr.responseText;
	}
	xhr.send(null);
}

function verifPass()
{
	if (document.getElementById('pass1').value != document.getElementById('pass2').value)
		document.getElementById('verif_pass').innerHTML = "<strong>Les mots de passe ne correspondent pas</strong>";
	else
	{
		document.getElementById('verif_pass').innerHTML = "<em>Les mots de passe correspondent</em>";		
	}
}

function verifFormInscription()
{
	if (document.getElementById('disponibilite_pseudo').innerHTML == '<em>Pseudo disponible</em>' && document.getElementById('pass1').value == document.getElementById('pass2').value && document.getElementById('pass2').value != '' && document.getElementById('prenom').value != "" && document.getElementById('nom').value != "" && document.getElementById('email').value != "")
		document.formInscriptionSite.submitbutton.disabled=false;
	else
		document.formInscriptionSite.submitbutton.disabled=true;
}

function verifFormChangerPassword()
{
	if (document.getElementById('ancien_pass').value != "" && document.getElementById('pass1').value == document.getElementById('pass2').value && document.getElementById('pass1').value != "")
		document.getElementById('soumettre').disabled=false;
	else
		document.getElementById('soumettre').disabled=true;
}


callPageGet("./menuText.php", "contenu");
callPageGet("./menu2Text.php", "menu");
callPageGet("./connectesText.php", "connectes");
callPageGet("./loginText.php", "compte");
setInterval("callPageGet('./connectesText.php', 'connectes')", 60000);