// Librairie de fonctions javascript - Ajax en France 2008

// Note : on pourrait fusionner les fonctions Ajax, mais il faudrait plus de paramètres pour ObjetXHR

// Ajax -> Vue d'ailleurs **************************************************************************************************
function lext_ajax(data,id)
{
	if(!id)
		{id = "lext_div";}
	
	var xhr_object = null;
	
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return;
	}		

	var method   = "GET";
	var filename = "/actualite/lext/lext.php";
	
	if(method == "GET" && data != null) {
	   filename += "?"+data;
	   data      = null;
	}
	
	document.getElementsByTagName('body')[0].style.cursor = 'wait';
	
	
	xhr_object.open(method, filename, true);
	
	xhr_object.onreadystatechange = function() {
	   if(xhr_object.readyState == 4) {
		document.getElementById(id).innerHTML = xhr_object.responseText;
		document.getElementsByTagName('body')[0].style.cursor = 'default';
	   }
	}
	xhr_object.send(data);
}

function deadlink(data)
{	
	var xhr_object = null;
	
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return;
	}		

	var method   = "GET";
	var filename = "/site/deadlink.php";
	
	if(method == "GET" && data != null) {
	   filename += "?"+data;
	   data      = null;
	}
	
	document.getElementsByTagName('body')[0].style.cursor = 'wait';
	
	
	xhr_object.open(method, filename, true);
	
	xhr_object.onreadystatechange = function() {
	   if(xhr_object.readyState == 4) {
	    	alert(xhr_object.responseText);
			document.getElementsByTagName('body')[0].style.cursor = 'default';
	   }
	}
	xhr_object.send(data);
}



// Ajax -> Sélection d'un club *******************************************************************

var Ld1Id='';
var Ld2Id='';
var id_liste='';

function ValideLd2(val,champ) {
	Ld1Id=val; //id_département
	Ld2Id='';
	Liste = champ;
	id_liste='2'+Liste;//Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '/resultats/dyn_div.php?Ld1='+Ld1Id+'&champ='+Liste;
	ObjetXHR(LD_URL)
}

function ValideLd3(val,champ) {
	Ld2Id=val; //id_commune
	Liste = champ;
	id_liste='3'+Liste; //Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '/resultats/dyn_club.php?Ld1='+Ld1Id+'&Ld2='+Ld2Id+'&champ='+Liste;
	// Désactivé car pose problème avec remplissage auto
	//if (Ld2Id=='') {var LD_URL = '/resultats/dyn_club.php?champ='+champ;}	
	ObjetXHR(LD_URL)
}
/*
function ValideLd2(val,champ) {
	Ld1Id=val; //id_département
	Liste = champ;
	id_liste='2'+Liste;//Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '/resultats/dyn_div.php?Ld1='+Ld1Id+'&champ='+Liste;
	ObjetXHR(LD_URL)
	// Réinitialisation de Ld3 si modification de LD1 après passage en Ld2
	if (Ld2Id!='') {ValideLd3('',champ);	}
}

function ValideLd3(val,champ) {
	Ld2Id=val; //id_commune
	Liste = champ;
	id_liste='3'+Liste; //Utilisé dans la fonction ChargeLd() pour identifier la liste déroulante
	var LD_URL = '/resultats/dyn_club.php?Ld1='+Ld1Id+'&Ld2='+Ld2Id+'&champ='+Liste;
	if (Ld2Id=='') {var LD_URL = '/resultats/dyn_club.php?champ='+champ;}	
	ObjetXHR(LD_URL)
}
*/

function ObjetXHR(LD_URL) {
	//creation de l'objet XMLHttpRequest
	if (window.XMLHttpRequest) { // Mozilla,...
		xmlhttp=new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}	
		xmlhttp.onreadystatechange=ChargeLd;
		xmlhttp.open("GET", LD_URL, true);
		xmlhttp.send(null);
	} else if (window.ActiveXObject) { //IE 
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
		if (xmlhttp) {
			xmlhttp.onreadystatechange=ChargeLd;
			xmlhttp.open('GET', LD_URL, false);
			xmlhttp.send();
		}
	}
	// Bouton non apparent car modification de LD1 ou Ld2
	document.getElementById('buttons').style.display='none';
}

// fonction pour manipuler l'appel asynchrone
function ChargeLd() {
	if (xmlhttp.readyState==4) { 
		if (xmlhttp.status==200) { 
			//span id="niv2" ou "niv3"
			document.getElementById('niv'+id_liste).innerHTML=xmlhttp.responseText; 
			if (xmlhttp.responseText.indexOf('disabled')<=0) {
				//focus sur liste déroulante 2 ou 3
				document.getElementById('Liste'+id_liste).focus(); 
			}	
		}
	}
}

// Affichage du bouton de validation
function Affiche_Btn(element) {
	document.getElementById('buttons').style.display='inline';

	if(document.getElementById('Liste1b'))
	{
		if(element.id == 'Liste3a') { champ = 'b'; }
		else { champ = 'a'; }
	
		// Si le club sélectionné est différent de l'Ajax, on remplie l'autre ligne automatiquement
		if(element.value != '1')
			{
				document.getElementById('Liste1'+champ).selectedIndex = '1'; // Sélection du Pays			
				ValideLd2(0,champ); // Affichage des divisions
				
				window.setTimeout(Pause1, 500, champ);	// Sélection de la division
				window.setTimeout(Pause2, 700, champ);  // Affichage des clubs
				window.setTimeout(Pause3, 1000, champ);  // Sélection du club
							
			}
		else if(element.value == '1' && champ == 'b')
		{
			document.getElementById('lieu').value = 'Amsterdam ArenA';	
		}

	}
}

function Pause1(champ) {
	document.getElementById('Liste2'+champ).selectedIndex = '1';
}

function Pause2(champ) {
	ValideLd3(0,champ);
}
function Pause3(champ) {
	
	var textToFind = 'Ajax';

	var dd = document.getElementById('Liste3'+champ);
	for (var i = 0; i < dd.options.length; i++) {
    	if (dd.options[i].text === textToFind) {
        	dd.selectedIndex = i;
       	 break;
   	 	}	
	}
	
	document.getElementById('buttons').style.display='inline';
	
	if(champ == 'a')
	{
		document.getElementById('lieu').value = 'Amsterdam ArenA';	
	}
}

// Récupération de l'id du club adverse
function resultats_adv() {
		adversaire = document.getElementById('Liste3a')[document.getElementById('Liste3a').selectedIndex].value;
		document.location.href = 'page-resultats-'+adversaire+'.html';
}










/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: http://www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
//http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(popUpElement){
//loads popup only if it is disabled
if(popupStatus==0){
jQuery("#backgroundPopup").css({
"opacity": "0.7"
});
jQuery("#backgroundPopup").fadeIn("slow");
jQuery(popUpElement).fadeIn("slow");
popupStatus = 1;
}
}

//disabling popup with jQuery magic!
function disablePopup(popUpElement){
//disables popup only if it is enabled
//if(popupStatus==1){ ->>>>>>>>>>> Vérification désactivée car cause un bug pour la deuxième pop-up si clique extérieur
jQuery("#backgroundPopup").fadeOut("slow");
jQuery(popUpElement).fadeOut("slow");
popupStatus = 0;
//}
}

//centering popup
function centerPopup(popUpElement){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = jQuery(popUpElement).height();
var popupWidth = jQuery(popUpElement).width();
//var popupHeight = jQuery("#popupContact").height();
//var popupWidth = jQuery("#popupContact").width();
//centering
jQuery(popUpElement).css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6

jQuery("#backgroundPopup").css({
"height": windowHeight
});

}

function myPopUp(triggerButton, targetElement, closingElement)
{
	jQuery(triggerButton).click(function()
		{
		//centering with css
		centerPopup(targetElement);
		//load popup
		loadPopup(targetElement);
		});

//CLOSING POPUP
//Click the x event!
jQuery(closingElement).click(function(){
disablePopup(targetElement);
});
//Click out event!
jQuery("#backgroundPopup").click(function(){
disablePopup(targetElement);
});
//Press Escape event!
jQuery(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup(targetElement);
}
});
}

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){

//These functions initiate each popUp. myPopUp takes has three options: the button, the popUP-element and the popUp-closebutton

myPopUp("#VALien", "#popupVA", "#popupVAClose");
myPopUp("#GoogleActuLien", "#popupGoogleActu", "#popupGoogleActuClose");
});
