var host = window.location.host;
path = (host == "localhost") ? "http://" + host + "/rusm2/" : "http://" + host + "/";

if (document.images)
{
  pic1 = new Image(16,11);
  pic1.src = path + "images/ajax-loader.gif";  
}
function getXhr()
{
    var xhr = null;
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest();
	else if(window.ActiveXObject)
	{ // Internet Explorer
	   try
	   {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else
	{ // XMLHttpRequest non supporté par le navigateur
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   xhr = false;
	}
       return xhr;
}
function menu(page, team)
{
	$("#" + page + "_" + team).addClass("selected");
}
function resultats(journee, numero, count, i)
{	
	var oldNumero = $("#num").html();
	var oldCurrent = $("#current").html();
	
	var current = (i > 2) ? i - 2 : 0;
	//if(document.getElementById('j_' + j + ']')) 
	//{
		$("#j_" + oldNumero).removeClass("today");
		$("#j_" + oldCurrent).removeClass("current");
	//}
	$("#num").html(i);
	$("#current").html(current);
	
	showwaiting("load");
	$.ajax({
		type: "POST", url: path + "team/resultats2.php", data: "journee=" + journee,
		complete: function(data)
		{	
			leselect = data.responseText;
			//document.getElementById('resultats').innerHTML = leselect;
			
			$("#resultats").hide();
			$("#resultats").html(leselect);
			$("#resultats").fadeIn();
			
			hidewaiting("load");
			
			$("#j_" + i).addClass("today");
			$("#j_" + current).addClass("current");
			
			$(function() {
				$('._main')
					.galerieEvol({
						gotoCurrent	: true						
					})
				$('[rel*=facebox]').facebox({
					loadingImage : '../../images/loading.gif',
					closeImage   : '../../images/closelabel.png'
				})
			});
						
			document.getElementById('journee2').innerHTML = numero;
			document.getElementById('j').innerHTML = (numero == 1) ? "ère" : "ème";	
		}
	 });
}
function classement(team, tri, ou)
{
	var ancien = $('#ancien').html();
	$('#class_' + ancien).removeClass("sel");
	$('#ancien').html(tri + ou);
	
	showwaiting2("load", "classement");
	$.ajax({
		type: "POST", url: path + "team/classement2.php", data: "team=" + team + "&ou=" + ou + "&tri=" + tri,
		complete: function(data)
		{	
			leselect = data.responseText;
			//$("#classement").hide();
			$("#classement").html(leselect);
			//$("#classement").fadeIn();
			
			$("#class_" + tri + ou).addClass("sel");
			oncomplete = hidewaiting2("load", "classement");
		}
	});
}
function matchs(path, mois, cat)
{
	if(mois == 8) document.getElementById('ch8').className = "active";
	else document.getElementById('ch8').className = "p";

	if(mois == 11) document.getElementById('ch11').className = "active";
	else document.getElementById('ch11').className = "p";
	
	if(mois == 1) document.getElementById('ch2').className = "active";
	else document.getElementById('ch2').className = "p";
	
	if(mois == 4) document.getElementById('ch5').className = "active";
	else document.getElementById('ch5').className = "p";

	var xhr = getXhr();
	xhr.onloading = showwaiting("load");
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			leselect = xhr.responseText;
			document.getElementById('matchs').innerHTML = leselect;
			xhr.oncomplete = hidewaiting("load");
		}
	}
	xhr.open("POST", path + "calendriercomplet2.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("mois=" + mois + "&cat=" + cat);
}
function showwaiting(div)
{
	document.getElementById(div).innerHTML = "<img src='" + path + "images/ajax-loader.gif' width='16' height='11' alt='' />";
}
function hidewaiting(div)
{
	document.getElementById(div).innerHTML = "";
}
function showwaiting2(div, bloc)
{
	document.getElementById(div).innerHTML = "<img src='" + path + "images/ajax-loader.gif' width='16' height='11' alt='' />";
	$("#" + bloc).addClass("opacity");
}
function hidewaiting2(div, bloc)
{
	document.getElementById(div).innerHTML = "";
	$("#" + bloc).removeClass("opacity");
}
function changeJournee(numero, count, sens)
{
	if(sens == "next" && numero < count - 2) 
	{
		if(numero < 4) numero = 3;
		numero += 5;
	}
	else if(sens == "prev" && numero > 0) 
	{
		if(numero > 26) numero = 28;
		numero -= 5;
	}

	var t = new Array();
	
	if(numero < 3)
	{
		for(i = 1; i < 6; i++)
		{
			t += journee[i];
		}
	}	
	else if(numero > 2 && numero <= count - 3)
	{
		for(i = numero - 2; i < numero + 3; i++)
		{
			t += journee[i];
		}
	}				
	else if(numero > count - 3)
	{
		for(i = count - 4; i < count + 1; i++)
		{
			t += journee[i];
		}
	}
	document.getElementById("journee").innerHTML = t;
	
	if(numero < 4)
	{
		document.getElementById("next").innerHTML = '<span onclick="changeJournee(' + numero + ', ' + count + ', \'next\')"></span>';
		document.getElementById("prev").className = "blanc";
		document.getElementById("next").className = "next";
	}
	else if(numero >= 4 && numero <= count - 3)
	{
		document.getElementById("next").innerHTML = '<span onclick="changeJournee(' + numero + ', ' + count + ', \'next\')"></span>';
		document.getElementById("prev").innerHTML = '<span onclick="changeJournee(' + numero + ', ' + count + ', \'prev\')"></span>';
		document.getElementById("next").className = "next";
		document.getElementById("prev").className = "prev";
	}
	else if(numero > count - 3) 
	{
		document.getElementById("prev").innerHTML = '<span onclick="changeJournee(' + numero + ', ' + count + ', \'prev\')"></span>';
		document.getElementById("next").className = "blanc";
		document.getElementById("prev").className = "prev";
	}
}

function noyau(team, tri, noyau)
{
	var ancien = $("#ancien").html();
	var ordre = $("#ordre").html();
	
	if(ancien == tri) ordre = (ordre == "desc") ? "asc" : "desc";
	else
	{
		if(tri == "prenom" || tri == "date_naissance") ordre = "asc";
		else ordre = "desc";
	}

	$("#n_" + ancien).addClass("tri");
	$("#n_" + tri).addClass("tri" + ordre);
	$("#ancien").html(tri);
	$("#ordre").html(ordre);
	showwaiting("load");
	$.ajax({
		type: "POST", url: path + "team/noyau2.php", data: "team=" + team + "&tri=" + tri + "&ordre=" + ordre + "&noyau=" + noyau,
		complete: function(data)
		{	
			leselect = data.responseText;	
			$("#noyau").html(leselect);			
			hidewaiting("load");
			$('[rel*=facebox]').facebox({});
		}
	});
}
function changerPhoto(path, idPhoto, folder, sens, year, month, day)
{
	if(day < 10) day = "0" + day;
	if(month < 10) month = "0" + month;
 	var num = document.getElementById("numPhoto").innerHTML;
	var xhr = getXhr();
	xhr.onloading = showwaiting("load2");
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			document.getElementById("load2").innerHTML = "";
			leselect = xhr.responseText;
			document.getElementById('photoo').innerHTML = leselect;
			document.getElementById("numPhoto").innerHTML = parseInt(document.getElementById("numPhoto").innerHTML) + parseInt(sens);
		}
	}
	xhr.open("POST", path + "gallery/album_photo_detail2.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("idPhoto=" + idPhoto + "&folder=" + folder + "&year=" + year + "&month=" + month + "&day=" + day);
}
function detailJoueur(id)
{
	window.location.href = path + "joueur/" + id + "/";
}

function modif()
{
	document.getElementById("blocModif").className = "modifpresence";
	document.getElementById("btnModif").className = "hidden";
}
function changePresence(idMatch, idJoueur, team)
{
	var comm = encodeURIComponent(document.getElementById("comm").value);
	var val = 10;
	if (document.getElementById("reponse[" + 0 + "]").checked) val = 0;
	else if (document.getElementById("reponse[" + 1 + "]").checked) val = 1;
	else if (document.getElementById("reponse[" + 2 + "]").checked) val = 2; 
	
	var tab = new Array("En attente", "Présence", "absent");
	
	if(val != 10)
	{	
		var xhr = getXhr();
		xhr.onloading = showwaiting("load");
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4 && xhr.status == 200)
			{
				leselect = xhr.responseText;
				document.getElementById("blocModif").innerHTML = leselect;
				
				document.getElementById("presence").innerHTML = tab[val];				
			}
		}
		xhr.open("POST", path + "presence/presence2.php", true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send("idJoueur=" + idJoueur + "&idMatch=" + idMatch + "&comm=" + comm + "&val=" + val + "&team=" + team);
	}
}

function hivernale()
{	
	$("#waiting").html("<img src='images/ajax-loader.gif' />");
	
	document.getElementById("hivernale").onclick = "";
	$.ajax({
		type: "POST", url: "hivernale/formulaire.php", data: "",
		complete: function(data)
		{	
			leselect = data.responseText;
			$("#hivernale").hide();			
			$('#hivernale').html(leselect);	
			$("#hivernale").fadeIn(500);
			$("#waiting").html("");
		}
	});
}
function hivernaleSend()
{				
	$(".err").each(function(){
		$(this).removeClass('err');
	});
	var error = true;
	
	var parcours = getSelectedValue("hiv_parcours"); 
	var sexe = getSelectedValue('hiv_sexe');
	var email = document.getElementById('hiv_email').value;
	var name = document.getElementById('hiv_name').value;
	var prenom = document.getElementById('hiv_prenom').value;
	var rue = document.getElementById('hiv_rue').value;
	var num = document.getElementById('hiv_num').value;
	var cp = document.getElementById('hiv_cp').value;
	var ville = document.getElementById('hiv_ville').value;
	var pays = document.getElementById('hiv_pays').value;
	var tel = document.getElementById('hiv_tel').value;
	var year = getSelectedValue('year');
	var month = (getSelectedValue('month') > 9) ? getSelectedValue('month') : "0" + getSelectedValue('month');
	var day = (getSelectedValue('day') > 9) ? getSelectedValue('day') : "0" + getSelectedValue('day');
	var dn = year + "-" + month + "-" + day;	
	
	if(trim(name) == "") {error = false; $("label[for='hiv_name']").addClass("err")};
	if(trim(prenom) == "") {error = false; $("label[for='hiv_prenom']").addClass("err")};
	if(sexe == null) {error = false; $("label[for='hiv_sexe']").addClass("err")};
	if(year == null) {error = false; $("label[for='hiv_dn']").addClass("err")};
	if(month == null) {error = false; $("label[for='hiv_dn']").addClass("err")};
	if(day == null) {error = false; $("label[for='hiv_dn']").addClass("err")};
	if(parcours == null) {error = false; $("label[for='parcours']").addClass("err")};
	if(trim(email) == "") {error = false; $("label[for='hiv_email']").addClass("err")}
	else if(Verifmail(email) == false) {error = false; $("label[for='hiv_email']").addClass("err")};
	
	if(error == false)	
	{
		$('#error').hide();		
		$('#error').html("ATTENTION : un ou plusieurs champs obligatoire doit être compléter !!!");	
		$('#error').addClass("error");
		$('#error').fadeIn(500, function(){
			$(this).css('filter','');
		})
	}
	else
	{
		$("#error").html("<img src='images/ajax-loader.gif' />");
		$.ajax({
			type: "POST", url: "hivernale/envoi.php", data: "dn=" + dn + "&name=" + name + "&email=" + email + "&prenom=" + prenom + "&parcours=" + parcours + "&rue=" + rue + "&num=" + num + "&cp=" + cp + "&ville=" + ville + "&num=" + num + "&sexe=" + sexe + "&tel=" + tel + "&pays=" + pays,
			complete: function(data)
			{	
				leselect = data.responseText;	
				$('#envoiHivernale').hide();
				$('#StyleMe').removeClass();						
				$('#StyleMe').addClass("opacity");						
				$('#error').addClass("error2");
				$('#error').html("Votre inscription a bien été enregistré. <br /><br />Nous vous remercions de votre participation.");	
				$('#error').fadeIn(500, function(){
					$(this).css('filter','');
				})			
			}
		});		
	}
}
function getSelectedValue(id) {
	return $("#" + id + " span.value").html();
}
function trim(aString)
{
    var regExpBeginning = /^\s+/;
    var regExpEnd       = /\s+$/;
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}
function Verifmail(adresse)
{
    var regExpr = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return regExpr.test(adresse);
}

