function getXMLHttpRequest() {
	var xhr = null;
	
	if(window.XMLHttpRequest || window.ActiveXObject) {
		if(window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}

/*LA CONNEXION ADMIN*/
function connecter() {
	var bool = true;
	
	var login = $("#login").val();
	var mdp = $("#mdp").val();
	
	if (!verifChaine(login)) {
		$("#erreurLogin").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else{
		$("#erreurLogin").html("");
	}
	
	if (!verifChaine(mdp)) {
		$("#erreurMdp").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else{
		$("#erreurMdp").html("");
	}
	
	if (bool) {
		charger(true);
		
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				charger(false);
				if (xhr.responseText=="ok") {
					location.reload(false)
				}else{
					$("#res").html(xhr.responseText);
					$("#erreurLogin").val("");
					$("#erreurMdp").val("");
				}
			}
		}
		
		xhr.open("GET", "scripts/php/connecter.php?type=0&login="+login+"&mdp="+mdp, true);	
		xhr.send(null);
	}
}

function deconnecter() {
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			charger(false);
			location.reload(false);
		}
	}
	
	xhr.open("GET", "scripts/php/connecter.php?type=1", true);	
	xhr.send(null);
}

/*LES EQUIPES*/
function ajouterEquipe() {
	var bool = true;
	
	var emailLeader = $("#emailLeader").val();
	var joueur1 = $("#joueur1").val();
	var joueur2 = $("#joueur2").val();
	var joueur3 = $("#joueur3").val();
	var remplacant = $("#remplacant").val();
	var urlSite = $("#urlSite").val();
	
	if (!verifChaine(emailLeader)) {
		$("#erreurMail").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else if (!verifMail(emailLeader)){
		$("#erreurMail").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Email incorect");
		bool = false;
	}else{
		$("#erreurMail").html("")
	}
	
	if (!verifChaine(joueur1)) {
		$("#erreurJoueur1").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else{
		$("#erreurJoueur1").html("");
	}
	
	if (!verifChaine(joueur2)) {
		$("#erreurJoueur2").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else{
		$("#erreurJoueur2").html("");
	}
	
	if (!verifChaine(joueur3)) {
		$("#erreurJoueur3").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Obligatoire");
		bool = false;
	}else{
		$("#erreurJoueur3").html("");
	}
	
	if (verifChaine(urlSite) && !verifUrl(urlSite)) {
		$("#erreurUrl").html("<img src='images/nook.png' style='margin-bottom:-4px'/> Url non valide (v&eacute;rifiez que l'url commence par http:// ou https://)");
		bool = false;
	}else{
		$("#erreurUrl").html("");
	}
	
	return bool;
}

function validerEquipe(idEquipe) {
	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionEquipe.php?type=2&idEquipe="+idEquipe, true);	
	xhr.send(null);
}

function refuserEquipe(idEquipe) {
	if (confirm("Voulez-vous refuser cette équipe ?")) {
		var motif = prompt("Motif du refus ?");
		
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				if(xhr.responseText=="deco") {
					deconnecter();
				}else{
					location.reload(false);
				}
				
			}
		}
		
		xhr.open("GET", "scripts/php/gestionEquipe.php?type=3&idEquipe="+idEquipe+"&motif="+motif, true);	
		xhr.send(null);
	}
}

function supprimerEquipe(idEquipe) {
	if (confirm("Voulez-vous supprimer définitivement cette équipe ?")) {
	
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				if(xhr.responseText=="deco") {
					deconnecter();
				}else{
					location.reload(false);
				}
				
			}
		}
		
		xhr.open("GET", "scripts/php/gestionEquipe.php?type=6&idEquipe="+idEquipe, true);	
		xhr.send(null);
	}
}

function majResultats(idEquipe) {
	
	var nbV = $("#nbV"+idEquipe).val();
	var nbD = $("#nbD"+idEquipe).val();
	var nbN = $("#nbN"+idEquipe).val();
	var idPoule = $("#lstPoule"+idEquipe).val();
	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionEquipe.php?type=4&idEquipe="+idEquipe+"&nbV="+nbV+"&nbD="+nbD+"&nbN="+nbN+"&idPoule="+idPoule, true);	
	xhr.send(null);
}

function envoyerMailPoule() {
	if (confirm("Attention, cela va envoyer un mail a tous les leaders !")) {
		charger(true);
		
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				if(xhr.responseText=="deco") {
					deconnecter();
				}else{
					document.getElementById("content").innerHTML = xhr.responseText;
				}
				
			}
		}
		
		xhr.open("GET", "scripts/php/gestionEquipe.php?type=5", true);	
		xhr.send(null);
	}
}

/*LES POULES*/
function ajouterPoule() {
	var nbPoules = prompt("Nombre de poules");
	var nbMatchs = prompt("Nombre de matchs par poule");
	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionPoule.php?type=1&nbPoules="+nbPoules+"&nbMatchs="+nbMatchs, true);	
	xhr.send(null);
}

function supprimerPoule(idPoule) {
	if (confirm("Etes-vous sur de vouloir supprimer cette Poule (cette action supprimera egalement tous les matchs de la poule)?")) {
		charger(true);
		
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				if(xhr.responseText=="deco") {
					deconnecter();
				}else{
					location.reload(false);
				}
				
			}
		}
		
		xhr.open("GET", "scripts/php/gestionPoule.php?type=2&idPoule="+idPoule, true);	
		xhr.send(null);
	}
}

/*LES MATCHS*/
function ajouterMatch(idPoule) {	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionMatchs.php?type=1&idPoule="+idPoule, true);	
	xhr.send(null);
}

function majMatch(idMatch) {	
	var idTeam1 = $("#lstEq1"+idMatch).val();
	var idTeam2 = $("#lstEq2"+idMatch).val();
	var scoreTeam1Map = $("#scoreTeam1Map"+idMatch).val();
	var scoreTeam1Round = $("#scoreTeam1Round"+idMatch).val();
	var scoreTeam2Map = $("#scoreTeam2Map"+idMatch).val();
	var scoreTeam2Round = $("#scoreTeam2Round"+idMatch).val();
	var date = $("#date"+idMatch).val();
	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionMatchs.php?type=3&idMatch="+idMatch+"&idTeam1="+idTeam1+"&idTeam2="+idTeam2+"&scoreTeam1Map="+scoreTeam1Map+"&scoreTeam1Round="+scoreTeam1Round+"&scoreTeam2Map="+scoreTeam2Map+"&scoreTeam2Round="+scoreTeam2Round+"&date="+date, true);	
	xhr.send(null);
}

function supprimerMatch(idMatch) {
	if (confirm("Etes-vous sur de vouloir supprimer ce match ?")) {
		charger(true);
		
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				if(xhr.responseText=="deco") {
					deconnecter();
				}else{
					location.reload(false);
				}
				
			}
		}
		
		xhr.open("GET", "scripts/php/gestionMatchs.php?type=2&idMatch="+idMatch, true);	
		xhr.send(null);
	}
}

/* LE TOURNOI */
function creerTournoi() {	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionTournoi.php?type=1", true);	
	xhr.send(null);
	
}

function majTournoi(table,id) {	
	charger(true);

	var idTeam1 = $("#lstEq1"+table+id).val();
	var idTeam2 = $("#lstEq2"+table+id).val();
	var scoreTeam1 = $("#scoreTeam1"+table+id).val();
	var scoreTeam2 = $("#scoreTeam2"+table+id).val();
	var date = $("#date"+table+id).val();
	
	var xhr = getXMLHttpRequest();

	if(xhr && xhr.readyState != 0) {
		xhr.abort(); 
	}

	xhr.onreadystatechange = function() { 
		if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
			if(xhr.responseText=="deco") {
				deconnecter();
			}else{
				location.reload(false);
			}
			
		}
	}
	
	xhr.open("GET", "scripts/php/gestionTournoi.php?type=2&table="+table+"&id="+id+"&idTeam1="+idTeam1+"&idTeam2="+idTeam2+"&scoreTeam1="+scoreTeam1+"&scoreTeam2="+scoreTeam2+"&date="+date, true);	
	xhr.send(null);
	
}

/*LES NEWS*/
function supprimerNews(id) {
	if (confirm("Voulez-vous vraiment supprimer cette news ?")) {
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				location.reload(false);
			}
		}
		
		xhr.open("GET", "scripts/php/gestionNews.php?type=3&id="+id, true);
		xhr.send(null);
	}
}

function actualiserNews() {	
	nb = $("select[name='listeNb'] option:selected").val();
	
	window.location.href="index.php?page=accueil&nb="+nb;
}

/*LES MAPS*/
function supprimerMap(id,table) {
	if (confirm("Voulez-vous vraiment supprimer cette map ?")) {
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				location.reload(false);
			}
		}
		
		xhr.open("GET", "scripts/php/gestionMaps.php?type=3&id="+id+"&table="+table, true);
		xhr.send(null);
	}
}

/*LES SAISONS*/
function supprimerSaison(id) {
	if (confirm("Voulez-vous vraiment supprimer cette Saison ?")) {
		var xhr = getXMLHttpRequest();

		if(xhr && xhr.readyState != 0) {
			xhr.abort(); 
		}

		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
				location.reload(false);
			}
		}
		
		xhr.open("GET", "scripts/php/gestionSaisons.php?type=3&id="+id, true);
		xhr.send(null);
	}
}

function validation(idChamps,idRes,type) {   
    var texte = $("#"+idChamps).val();
   
    if (verifChaine(texte)) {
   
        var xhr = getXMLHttpRequest();

        if(xhr && xhr.readyState != 0) {
            xhr.abort();
        }

        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                $("#"+idRes).html(xhr.responseText);
            }
        }
       
        xhr.open("GET", "scripts/php/validation.php?type="+type+"&texte="+texte, true);
        xhr.send(null);
    }else{
        $("#"+idRes).html("");
    }
}
