//===========================================================================================================================================================================================================
//Fonction création d'un cookie

function setCookie(nom, valeur, jours)
{ 
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000));
	document.cookie = nom + '=' + escape(valeur) + ';expires=' + expDate.toGMTString();
}

//===========================================================================================================================================================================================================
//Fonction destruction d'un cookie

function delCookie(nom) 
{ 
	setCookie(nom, '', -1);
}


//===========================================================================================================================================================================================================
//Fonction destruction d'un cookie

function readCookie(nom) 
{ 
	var start, end;
	start = document.cookie.indexOf(nom + '=');
	if(start >= 0)
	{
		start += nom.length + 1;
		end = document.cookie.indexOf(';', start);
		if(end < 0) end = document.cookie.length;
		return unescape(document.cookie.substring(start, end));
	}
	return '';
}

//===========================================================================================================================================================================================================
//Fonction équivalente  trim()

function trim (myString) 
{ 
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'') 
}

//===========================================================================================================================================================================================================
//Fonction de scroll pour les matches
function scrollBottom()
{
	var objDiv = document.getElementById('matchcomment');
	objDiv.scrollTop = objDiv.scrollHeight;
}

//===========================================================================================================================================================================================================
//Fonction permettant de créer un objet XMLHttpRequest
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;
}

//===========================================================================================================================================================================================================
//Fonction envoyant une requête XMLHttpRequest au serveur

//Création d'un objet global
var oXHR = null;
function request(callback, page, variable, id)
{
	//Si une requête est déjà en cours, on n'exécute pas la nouvelle
	if(oXHR)
	{
		if(oXHR.readyState != 0 && oXHR.readyState != 4)
		{
			return;
		}
	}
	
	//Création de l'objet XMLHttpRequest
	oXHR = getXMLHttpRequest();
	
	//Détection des changements d'état de l'objet XMLHttpRequest
	oXHR.onreadystatechange = function()
	{
		//Si le serveur a renvoyer toutes les données (readystate 4) et qu'elles ne contiennent pas d'erreur (exemple: 404 ou 500)
		if(oXHR.readyState == 4 && (oXHR.status == 200 || oXHR.status == 0))
		{
			callback(oXHR.responseText, id);
			document.getElementById('loader').style.display = 'none';
		}
		//Si le serveur est en train de traiter les données
		else if(oXHR.readyState < 4)
		{
			document.getElementById('loader').style.display = 'block';
		}
	};
	
	//Définition des modalités d'envoi
	oXHR.open('POST', page, true);
	//On defini le header (utillisation de la méthode POST)
	oXHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//Envoie de la requête
	oXHR.send(variable);
}

//===========================================================================================================================================================================================================
//Fonction retournant en text/plain les données d'un objet XMLHttpRequest
function writeData(data, id)
{
	document.getElementById(id).value = '';
	/*document.getElementById(id).style.backgroundImage = 'url(images/icons/ok.png)';
	document.getElementById(id).style.backgroundPosition = 'bottom left';
	document.getElementById(id).style.backgroundRepeat = 'no-repeat';*/
	commentState = 0;
	viewComment('commentaireAJAX', document.getElementById('idnews').value, startCom);
}

function writeDataMatch(data, id)
{
	document.getElementById(id).value = '';
	/*document.getElementById(id).style.backgroundImage = 'url(images/icons/ok.png)';
	document.getElementById(id).style.backgroundPosition = 'bottom left';
	document.getElementById(id).style.backgroundRepeat = 'no-repeat';*/
	commentState = 0;
	viewCommentMatch('commentaireAJAX', document.getElementById('idnews').value, startCom);
}

function writeComment(data, id)
{
	var texte = document.createTextNode(data);
	var node = document.getElementById(id);
	node.innerHTML = data;
	/*node.appendChild(texte);
	node.replaceChild(texte, node.firstChild);*/
}

function writeUpdate(data, id)
{
	var texte = document.createTextNode(data);
	var node = document.getElementById(id);
	node.innerHTML = data;
	scrollBottom();
	//Pour ie qui est bien trop long à la réaction ...
	setTimeout('scrollBottom()', 100);
}

function writeTmr(data, id)
{
	var texte = document.createTextNode(data);
	var node = document.getElementById(id);
	node.innerHTML = data;
}

function writeLiveMatch(data, id)
{
	var texte = document.createTextNode(data);
	var node = document.getElementById(id);
	node.innerHTML = data;
}

function writeEdit(data, id)
{
	commentState = 0;
	viewComment('commentaireAJAX', document.getElementById('idnews').value, startCom);
}

function writeEditMatch(data, id)
{
	commentState = 0;
	viewCommentMatch('commentaireAJAX', document.getElementById('idnews').value, startCom);
}

//Fonction ne renvoyant rien
function writeVoid(data, id)
{
}

//Fonction affichant le résultat
function writeDebug(data, id)
{
	alert(data);
}

//===========================================================================================================================================================================================================
//Fonction pour afficher ou cacher les commentaires de news
var commentState = 0;
var startCom = 0;
function viewComment(id, idNews, commentPage)
{
	if(commentState == 0)
	{
		document.getElementById('viewcomment').src = 'images/icons/cachercom.png';
		request(writeComment, 'ajax/commentaire/commentaire.php', 'idNews=' + idNews + '&startCom=' + commentPage, id);
		commentState = 1;
		startCom = commentPage;
	}
	else
	{
		document.getElementById('viewcomment').src = 'images/icons/voircom.png';
		writeComment('', id);
		commentState = 0;
	}
}

//===========================================================================================================================================================================================================
//Fonction d'envoi de commentaire de news
function sendComment(id, id2)
{
	//Récupération du contenu du formulaire de commentaire
	commentValue = encodeURIComponent(document.getElementById(id).value);
	idnews = encodeURIComponent(document.getElementById(id2).value);
	variable = 'commentaire=' + commentValue + '&idnews=' + idnews;
	
	if(trim(document.getElementById(id).value) == '' || trim(document.getElementById(id2).value) == '')
	{
		document.getElementById(id).value = '';
		return;
	}
	
	request(writeData, 'ajax/commentaire/traitcommentaire.php', variable, id);
}

//===========================================================================================================================================================================================================
//Fonction  d'édition d'un commentaire de news
function editComment(comID)
{
	commentValue = encodeURIComponent(document.getElementById('comtext'+comID).value);
	variable = 'commentaire=' + commentValue + '&idcom=' + comID;
	
	request(writeEdit, 'ajax/commentaire/editcommentaire.php', variable, '');
}

//Afficher/cacher édition
function spoilerComment(comID)
{
	if(document.getElementById('com'+comID).style.display == 'block')
	{
		document.getElementById('com'+comID).style.display = 'none';
	}
	else
	{
		document.getElementById('com'+comID).style.display = 'block';
	}
}

//===========================================================================================================================================================================================================
//Fonction  pour effacer un commentaire de news
function deleteComment(comID, newsID)
{
	if(confirm('Voulez-vous vraiment effacer ce commentaire ?'))
	{
		variable = 'idcom=' + comID + '&idnews=' + newsID;
		request(writeEdit, 'ajax/commentaire/deletecommentaire.php', variable, '');
	}
}

//===========================================================================================================================================================================================================
//Fonction pour afficher ou cacher les commentaires de news
var commentState = 0;
var startCom = 0;
function viewCommentMatch(id, idNews, commentPage)
{
	if(commentState == 0)
	{
		document.getElementById('viewcomment').src = 'images/icons/cachercom.png';
		request(writeComment, 'ajax/commentaire/commentairematch.php', 'idNews=' + idNews + '&startCom=' + commentPage, id);
		commentState = 1;
		startCom = commentPage;
	}
	else
	{
		document.getElementById('viewcomment').src = 'images/icons/voircom.png';
		writeComment('', id);
		commentState = 0;
	}
}

//===========================================================================================================================================================================================================
//Fonction d'envoi de commentaire de match
function sendCommentMatch(id, id2)
{
	//Récupération du contenu du formulaire de commentaire
	commentValue2 = encodeURIComponent(document.getElementById(id).value);
	idnews = encodeURIComponent(document.getElementById(id2).value);
	variable = 'commentaire=' + commentValue2 + '&idnews=' + idnews;
	
	if(trim(document.getElementById(id).value) == '' || trim(document.getElementById(id2).value) == '')
	{
		document.getElementById(id).value = '';
		return;
	}
	
	request(writeDataMatch, 'ajax/commentaire/traitcommentairematch.php', variable, id);
}

//===========================================================================================================================================================================================================
//Fonction  d'édition d'un commentaire de match
function editCommentMatch(comID)
{
	commentValue2 = encodeURIComponent(document.getElementById('comtext'+comID).value);
	variable = 'commentaire=' + commentValue2 + '&idcom=' + comID;
	
	request(writeEditMatch, 'ajax/commentaire/editcommentairematch.php', variable, '');
}

//===========================================================================================================================================================================================================
//Fonction  pour effacer un commentaire de match
function deleteCommentMatch(comID, newsID)
{
	if(confirm('Voulez-vous vraiment effacer ce commentaire ?'))
	{
		variable = 'idcom=' + comID + '&idnews=' + newsID;
		request(writeEditMatch, 'ajax/commentaire/deletecommentairematch.php', variable, '');
	}
}

//===========================================================================================================================================================================================================
//Fonction  pour actualiser les commentaire de match
function updateMatch(id, idMatch)
{
	request(writeUpdate, 'ajax/match/update.php', 'idMatch=' + idMatch, id);
}
//===========================================================================================================================================================================================================
//Fonction  permettant d'afficher ou de cacher une div
function spoiler(id)
{
	if(document.getElementById('spoilerdiv'+id).style.display == 'block')
	{
		document.getElementById('spoilerdiv'+id).style.display = 'none';
		document.getElementById('spoilerbutton'+id).innerHTML = ' + ';
	}
	else
	{
		document.getElementById('spoilerdiv'+id).style.display = 'block';
		document.getElementById('spoilerbutton'+id).innerHTML = ' - ';
	}
}

//===========================================================================================================================================================================================================
//Fonction d'update des donnees du live dans l'administration
function updateLive(matchID)
{
	scoreA = document.getElementById('scoreA').value;
	scoreB = document.getElementById('scoreB').value;
	statut = document.getElementById('statut').value;
	commentValue3 = document.getElementById('live').value;
	variable = 'matchID=' + matchID + '&scoreA=' + scoreA + '&scoreB=' + scoreB + '&statut=' + statut + '&comment=' + commentValue3;
	request(writeVoid, 'updatelive.php', variable, '')
}

//===========================================================================================================================================================================================================
//Fonction de changement de page depuis le sommaire
function changePage(sommaire)
{
	document.location.href = sommaire.options[sommaire.selectedIndex].value;
}

//===========================================================================================================================================================================================================
//Fonction de changement de classement du module tmranking
var page = 0;
function selectTMR(action)
{
	document.getElementById('tmrdata').innerHTML = '';
	if(action == '-')
	{
		page--;
		if(page < 0)
		{
			page = 3;
		}
	}
	else if(action == '+')
	{
		page++;
		if(page > 3)
		{
			page = 0;
		}
	}
	
	if(page == 0)
	{
		document.getElementById('tmrsection').innerHTML = '<a href="http://www.tm-rankings.com/teams/tmnf/">TMN/TEAMS</a>';
	}
	else if(page == 1)
	{
		document.getElementById('tmrsection').innerHTML = '<a href="http://www.tm-rankings.com/teams/tmuf/">TMU/TEAMS</a>';
	}
	else if(page == 2)
	{
		document.getElementById('tmrsection').innerHTML = '<a href="http://www.tm-rankings.com/players/tmnf/">TMN/SOLO</a>';
	}
	else if(page == 3)
	{
		document.getElementById('tmrsection').innerHTML = '<a href="http://www.tm-rankings.com/players/tmuf/">TMU/SOLO</a>';
	}
	else
	{
		document.getElementById('tmrsection').innerHTML = 'Inconnu';
	}
	
	variable = 'section=' + page;
	request(writeTmr, 'ajax/tmranking/parsertmranking.php', variable, 'tmrdata')
}

//===========================================================================================================================================================================================================
//Fonction de changement de classement du module live
var pageLive = 0;
	
var oXHR2 = null;
function request2(callback, page, variable, id)
{
	//Si une requête est déjà en cours, on n'exécute pas la nouvelle
	if(oXHR2)
	{
		if(oXHR2.readyState != 0 && oXHR2.readyState != 4)
		{
			return;
		}
	}
	
	//Création de l'objet XMLHttpRequest
	oXHR2 = getXMLHttpRequest();
	
	//Détection des changements d'état de l'objet XMLHttpRequest
	oXHR2.onreadystatechange = function()
	{
		//Si le serveur a renvoyer toutes les données (readystate 4) et qu'elles ne contiennent pas d'erreur (exemple: 404 ou 500)
		if(oXHR2.readyState == 4 && (oXHR2.status == 200 || oXHR2.status == 0))
		{
			callback(oXHR2.responseText, id);
			document.getElementById('loader2').style.display = 'none';
		}
		//Si le serveur est en train de traiter les données
		else if(oXHR2.readyState < 4)
		{
			document.getElementById('loader2').style.display = 'block';
		}
	};
	
	//Définition des modalités d'envoi
	oXHR2.open('POST', page, true);
	//On defini le header (utillisation de la méthode POST)
	oXHR2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//Envoie de la requête
	oXHR2.send(variable);
}
	
function selectLive(action, rootpath)
{
	pageLiveTmp = readCookie('pageLive');
	if(pageLiveTmp != '')
	{
		pageLive = pageLiveTmp;
	}
	else
	{
		pageLive = 0;
	}
		
	document.getElementById('livedata').innerHTML = '';
	if(action == '-')
	{
		pageLive--;
		if(pageLive < 0)
		{
			pageLive = 1;
		}
	}
	else if(action == '+')
	{
		pageLive++;
		if(pageLive > 1)
		{
			pageLive = 0;
		}
	}
	
	setCookie('pageLive', pageLive, 30);
	
	if(pageLive == 0)
	{
		document.getElementById('livesection').innerHTML = '<img src="' + rootpath + '/images/template/tmnmini.png" alt="section du match : tmn" />';
		variable = 'section=tmn';
	}
	else if(pageLive == 1)
	{
		document.getElementById('livesection').innerHTML = '<img src="' + rootpath + '/images/template/tmumini.png" alt="section du match : tmu" />';
		variable = 'section=tmu';
	}

	request2(writeLiveMatch, rootpath + '/ajax/match/show.php', variable, 'livedata')
}


//===========================================================================================================================================================================================================
//Fonction de changement de mois du calendrier
var pageLive = 0;
	
var oXHR3 = null;
function request3(callback, page, variable, id)
{
	//Si une requête est déjà en cours, on n'exécute pas la nouvelle
	if(oXHR3)
	{
		if(oXHR3.readyState != 0 && oXHR3.readyState != 4)
		{
			return;
		}
	}
	
	//Création de l'objet XMLHttpRequest
	oXHR3 = getXMLHttpRequest();
	
	//Détection des changements d'état de l'objet XMLHttpRequest
	oXHR3.onreadystatechange = function()
	{
		//Si le serveur a renvoyer toutes les données (readystate 4) et qu'elles ne contiennent pas d'erreur (exemple: 404 ou 500)
		if(oXHR3.readyState == 4 && (oXHR3.status == 200 || oXHR3.status == 0))
		{
			callback(oXHR3.responseText, id);
			document.getElementById('loader3').style.display = 'none';
		}
		//Si le serveur est en train de traiter les données
		else if(oXHR3.readyState < 4)
		{
			document.getElementById('loader3').style.display = 'block';
		}
	};
	
	//Définition des modalités d'envoi
	oXHR3.open('POST', page, true);
	//On defini le header (utillisation de la méthode POST)
	oXHR3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//Envoie de la requête
	oXHR3.send(variable);
}
	
function selectCalendar(month, year, action, rootpath)
{
	document.getElementById('calendar').innerHTML = '';
	variable = 'month=' + month + '&year=' + year + '&action=' + action;
	request3(writeCalendar, rootpath + '/ajax/calendar/viewcalendar.php', variable, 'calendar');
}

function writeCalendar(data, id)
{
	var texte = document.createTextNode(data);
	var node = document.getElementById(id);
	node.innerHTML = data;
}

//===========================================================================================================================================================================================================
//Fonction d'effacement et de restauration du contenu d'un input
function delInput(input, value)
{
	if(input.value == value)
	{
		input.value = '';
	}
}

function resInput(input, value)
{
	if(input.value == '')
	{
		input.value = value;
	}
}
