function $(id) {
	return document.getElementById(id);	
}

// Stocke les fonctions qui s'executent au chargement de page
function ajoutLoadEvent(func) 
{ 
   var oldonload = window.onload; 
   if(typeof window.onload != 'function') 
   { 
      window.onload = func; 
   } 
   else 
   { 
      window.onload = function() 
      { 
         oldonload(); 
         func(); 
      } 
   } 
}

// verifie si une variable javascript existe ou non
function isset(variable) {
    var undefined;
    return ( variable == undefined ? false : true );
}

// Affiche une boite de dialogue avant d'aller a l'url souhaitee
function confirmation(url) { 
  if(confirm(message_suppression)) {
   document.location.href=url; 
  }  
}

// Affiche la boite de dialogie pour choisir un sport
function choisir_sport(lien) {		
	afficher_elt('autre_sport','block');
	if($('autre_sport').childNodes[0].nodeName!="SPAN") {	
		sendData("fen=ajax",lien,"autre_sport");
	}	
}

// Ajoute ou supprime un sport favori selectionné par un utilisateur
function update_sport_selection(sport,mode,contenant) {
 sendData("sport="+sport+"&mode="+mode,lien_membre_sport,"sport_selection_"+sport); 
 if(contenant!="") sendData("sport="+sport+"&mode="+mode,lien_membre_sport,"sport_"+contenant+"_selection_"+sport); 
}

// used by tinymce to open the file manager
function fileBrowserCallBack(field_name, url, type, win) {
    var file_type;
    switch (type) {
        case "image": file_type = "image"; break;
        case "flash": file_type = "flash"; break;
        case "file": file_type = "file"; break;
    }    
    if (file_type) {
        file_connector += "&file_type=" + file_type;
    }
    file_connector += "&parent_form=tinymce&field_name="+field_name;
	
    window.open(file_connector, "FileManager", "modal,width=750,height=600,toolbar=no,location=no, directories=no, menuBar=no, status=no, scrollbars=yes, resizable=yes, copyhistory=no");
}
 
 
// information pour les cookies
//var pathname=location.pathname;
//var myDomain='/';
var c_pathname='/';
var c_myDomain='';

var c_date_exp = new Date();
c_date_exp.setTime(c_date_exp.getTime()+(30*24*3600*1000));	// Ici on définit une durée de vie de 30 jours


// Gestion des cookies	
function SetCookie (name, value) {	
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}   

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1)
      		endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal (j);
        i=document.cookie.indexOf(" ",i)+1;
        if (i==0) break;
	}
	return null;
}

// Augmente la largeur de la page
function augmenter_largeur() {
  $('page').style.width='980px';
  $('contenu').style.width='518px';
  if(isset($('droite'))) $('droite').style.display='block';
  $('augmenter_largeur').style.display='none';
  $('diminuer_largeur').style.display='inline';    
  SetCookie('largeur','1',c_date_exp,c_pathname,c_myDomain);
}

// Diminue la largeur de la page
function diminuer_largeur() {
 $('page').style.width='760px';
 $('contenu').style.width='518px';
 if(isset($('droite'))) $('droite').style.display='none';
 $('augmenter_largeur').style.display='inline';
 $('diminuer_largeur').style.display='none';
 SetCookie('largeur','0',c_date_exp,c_pathname,c_myDomain);
}

// Augmente la taille du texte
taille_texte=0.8;
function augmenter_texte() {
 taille_texte = taille_texte + 0.1;
 if(taille_texte > 0.7 && isset(document.getElementsByTagName("body")[0])) {
  document.getElementsByTagName("body")[0].style.fontSize = taille_texte + "em";
  SetCookie('taille_texte',taille_texte+'em',c_date_exp,c_pathname,c_myDomain);
 }
}

// Diminue la taille du texte
function diminuer_texte() {
 taille_texte = taille_texte - 0.1;
 if(taille_texte > 0.7 && isset(document.getElementsByTagName("body")[0])) {
  document.getElementsByTagName("body")[0].style.fontSize = taille_texte + "em";
  SetCookie('taille_texte',taille_texte+'em',c_date_exp,c_pathname,c_myDomain);
 }
}


// Gestion de la feuille de styles CSS active
var defaultStyle = 'orange';
var currentStyle ='';
var skin = GetCookie('skin');
var bgStyle = ((skin == "bleu") || (skin == "orange") || (skin == "vert") || (skin == "rouge") || (skin == "violet")) ? skin : defaultStyle;
	
function setActiveStyleSheet(title) {
	var i, a, main;
	currentStyle = title;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
	SetCookie('skin',title,c_date_exp,c_pathname,c_myDomain);
}


// initialisation de la page (largeur et taille du texte)
function initialiser_page()
{
 setActiveStyleSheet(bgStyle);
 //if(top.frames.length!=0) top.location=self.document.location;
 if(check_largeur()==false) { diminuer_largeur(); }
 if(isset(document.getElementsByTagName("body")[0])) {
	 document.getElementsByTagName("body")[0].style.fontSize=get_taille_texte();
 }
}


function check_largeur() {
  if(GetCookie('largeur')==null) { return true; }
  else if(GetCookie('largeur')=="1") { return true;  }
  else  { return false; }
}

function get_taille_texte() {
  if(GetCookie('taille_texte')==null) { return '0.8em'; } // par defaut la taille est petite
  else { return GetCookie('taille_texte'); }
}



function rollover(nom,src) {
  document.images[nom].src = src;
}

function str2url(str,encoding)
{
	str = str.toUpperCase();
	str = str.toLowerCase();
	
	if (encoding == 'UTF-8') {
		str = str.replace(/[Ã Ã¡Ã¢Ã£Ã¤Ã¥]/g,'a');
		str = str.replace(/[Ã§]/g,'c');
		str = str.replace(/[Ã²Ã³Ã´ÃµÃ¶Ã¸]/g,'o');
		str = str.replace(/[Ã¨Ã©ÃªÃ«]/g,'e');
		str = str.replace(/[Ã¬Ã­Ã®Ã¯]/g,'i');
		str = str.replace(/[Ã¹ÃºÃ»Ã¼Ã¿]/g,'u');
		str = str.replace(/[Ã±]/g,'n');
	} else {
		str = str.replace(/[àáâãäå]/g,'a');
		str = str.replace(/[ç]/g,'c');
		str = str.replace(/[òóôõöø]/g,'o');
		str = str.replace(/[èéêë]/g,'e');
		str = str.replace(/[ìíîï]/g,'i');
		str = str.replace(/[ùúûüÿ]/g,'u');
		str = str.replace(/[ñ]/g,'n');
	}
	
	str = str.replace(/[^a-z0-9_\s-]/g,'');
	str = str.replace(/[\s]+/g,' ');
	str = str.replace(/[ ]/g,'-');
	return str;
	//return str.substr(0,1).toUpperCase()+str.substr(1);
}

function pop(url,width,height)
{
 var nb = parseInt('40');
 var w = parseInt(width)+nb;
 var h = parseInt(height)+nb;
 window.open(url,'_blank','toolbar=no, location=no, directories=no, menuBar=no, status=1, scrollbars=yes, resizable=yes, copyhistory=no, width='+w+', height='+h+', left=100, top=100');
}


function clic(lien){
  	if(document.images)
  		(new Image()).src=lien;
   return true;
}

function pop_mail(nom,domaine)
{
    var txt = 'location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,width=10,height=10,screenY=100,screenX=100';
    window.open('mailto:'+ nom + '@' + domaine,'Mail',txt);
}


function cocherTous(checked, f, champ)
{
    for (i = 0; i < document.forms[f].elements.length; i++) {
        if (document.forms[f].elements[i].name.indexOf(champ) >= 0) {
            document.forms[f].elements[i].checked = checked;
        }
    }
}

function add_bookmark(url,titre) {
       if ( navigator.appName != 'Microsoft Internet Explorer' ){
           window.sidebar.addPanel(titre,url,"");
       }
       else {
           window.external.AddFavorite(url,titre);
       }
}

//affiche ou rend invisible un element html
function afficher_elt(id,value)
{
 // we check that the element exists
 if(isset(document.getElementById(id)))
 {
  var div = document.getElementById(id);  
  if(value==0) {
	 var etat = 'block';
	 var etat_inverse = 'none';	
	 if( div.style.display != 'none')
	 {
	 	etat_inverse = etat;
		etat = 'none';
	 }	 
	 div.style.display=etat;
  }
  else
  {
	if(value=="block") {
     var etat = 'block';
	 var etat_inverse = 'none';			
	}
	else {
     var etat = 'none';
	 var etat_inverse = 'block';
	}    
  }
  div.style.display=etat;

  if(isset(document.getElementById("ImgDown"+id))) document.getElementById("ImgDown"+id).style.display=etat_inverse;
  if(isset(document.getElementById("ImgUp"+id))) document.getElementById("ImgUp"+id).style.display=etat;
 }	
}

// show the image "url" in the div "div" and the image "image"
function show_image (div,image,url) {
 if(url=="") {
  document.getElementById(div).style.display="none";
 }
 else {
  document.getElementById(image).src=url;
  document.getElementById(div).style.display="block";
 }
}

// show the tab "Num" of the tabs array "tablist"
function show_tab(Num, tablist)
{
  var tmp;
  var nb_tab = tablist.length;
	// on rend invisible toutes les div
	for(var i=0; i <  nb_tab; i++) {
	 tmp = tablist[i];	 
	 if(isset(document.getElementById(tmp))) document.getElementById(tmp).style.display = 'none';
	 if(isset(document.getElementById('tab_'+tmp))) document.getElementById('tab_'+tmp).className = '';
	}
	// on affiche la div 
	if(isset(document.getElementById(Num))) document.getElementById(Num).style.display = 'block';
	if(isset(document.getElementById('tab_'+Num))) document.getElementById('tab_'+Num).className = 'on';	
}

/**
 * AJAX : Permet d'envoyer des données en GET ou POST en utilisant les XmlHttpRequest
 */
 
 
function getXMLHTTP() {
	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) {
			try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e1) {
				xhr = null;
			}
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	}
	return xhr;
}


function sendData(param, page, div)
{	
	var XhrObj = getXMLHTTP();
	// définition de l'endroit d'affichage:
	var content = $(div);	
	
	XhrObj.open("POST", page);	
	// Ok pour la page cible
	XhrObj.onreadystatechange = function()
	{
		if (XhrObj.readyState == 4 && XhrObj.status == 200) {
			var Ndiv = document.createElement("span");
			Ndiv.innerHTML = XhrObj.responseText;
			content.innerHTML="";
			content.appendChild(Ndiv);
   			//content.innerHTML=XhrObj.responseText;
		}
	}
	XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	XhrObj.send(param);
}//end position SendData


/* Met en majuscule la première lettre de chaque mot d'une phrase  */
function ucWords(chaine) {
	var valeur="";
	if ((chaine) && (chaine!=""))
	{
		valeur = chaine.toLowerCase();
		
		// Analyse des mots séparés par des espaces
		var mots = valeur.split(' ');
		for(var i = 0; i < mots.length; i++)
		{
			if(mots[i].length > 1)
			{
				carac = mots[i].split('');
				carac[0] = carac[0].toUpperCase();
				mots[i] = carac.join('');
			}
			else
			{
				mots[i] = mots[i].toUpperCase();
			}
		}
		valeur = mots.join(' ');
	}
	return valeur;
}

// fonction qui bloque les boutons submit pendant plusieurs secondes pour eviter des doubles soumissions.
function anti_dblclic(id) {
	document.getElementById(id).disabled='disabled';	
	var t=setTimeout("document.getElementById('"+id+"').disabled='';",10000);
	return false;
}

// fonction qui compte le nombre de caracteres restant dans un champs texte
function limit(input,limit)
{
	var field = document.getElementById(input);
	var field_car = document.getElementById(input+'_nb_caractere');
	var dlen = field.value.length;
	var rem = limit - dlen;
	
	if( rem > 0) 
	{
		field_car.innerHTML= rem;
		return true;
	}
	else
	{
		field_car.innerHTML=0;
		field.value = field.value.substr(0,limit);
		return false;
	}
}


// Cette fonction permet de vérifier la validité d'une date au format jj/mm/aa ou jj/mm/aaaa
function isDate(d) {	
	if (d == "") {
		return false;
	}
	else {	
		e = new RegExp("^[0-9]{1,2}\-[0-9]{1,2}\-([0-9]{4})$");
		if (!e.test(d)) {
			return false;
		}
		else {
			// On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier
			j = parseInt(d.split("-")[0], 10); // jour
			m = parseInt(d.split("-")[1], 10); // mois
			a = parseInt(d.split("-")[2], 10); // année
			
			// Définition du dernier jour de février
			// Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
			if (a%4 == 0 && a%100 !=0 || a%400 == 0) fev = 29;
			else fev = 28;
			
			// Nombre de jours pour chaque mois
			nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);
			
			// Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
			return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] );
		}
	}
}


// format les numeros de telephone pour 00 00 00 00 00
function format_telephone(telephone) {
 $(telephone).value=$(telephone).value.replace(/[^0-9]/g,'');
 
 if($(telephone).value.substring(2,3)!=' ') {
	 $(telephone).value=$(telephone).value.substring(0,2)+' '+$(telephone).value.substring(2,4)+' '+$(telephone).value.substring(4,6)+' '+$(telephone).value.substring(6,8)+' '+$(telephone).value.substring(8,10);
 }
}

// fonction permettant a un membre d'ajouter un autre membre a sa liste d'ami (ou de le supprimer)
function update_ami(membre,lien,action,conteneur) {
  param='ami='+membre+'&mode='+action;
  sendData(param,lien,conteneur); 
}