// Fonction de gestion de l'acces a "Mon Compte"

//Get Cookie Function --------------------------------------------------------------------------
function getCookie(name) {
	var cname = '; ' + name + "=";
	var dc = document.cookie;
	//alert(dc);
	dc = '; '+document.cookie;
	if (dc.length > 0) {
		begin = dc.indexOf(cname);
		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
				s=new String("");
				//s=unescape(dc.substring(begin, end));
				s=decodeURI(dc.substring(begin, end));
				myRegExp = /\+/gi;
				s1=s.replace(myRegExp,' ');
				return(s1);
				//return unescape(dc.substring(begin, end));
		}
	}
	return null;
}

//Set Cookie Function ---------------------------------------------------------------------------
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
	((path == null) ? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

//Delete Cookie Function ------------------------------------------------------------------------
function delCookie (name,path,domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path == null) ? "" : "; path=" + path) +
		((domain == null) ? "" : "; domain=" + domain) +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}





// Fonction principale, appelee depuis les pages HTML -------------------------------------------

function affiche_bandeau(contexte){
	nom	=getCookie('nom');				if(nom==null)nom='';
	prenom	=getCookie('prenom');		if(prenom==null)prenom='';
	id_sso = getCookie('id_sso'); 		if(id_sso == null) id_sso = '';
	mes_favoris = getCookie('mes_favoris'); 		if(id_sso == null) id_sso = '0';

	//alert("id_sso="+id_sso);
	if (id_sso == '')	affiche_bandeau_non_identifie(contexte);
	else			affiche_bandeau_identifie(nom,prenom,mes_favoris,contexte);
}


//Fonction pour la fonction affiche_bandeau_non_identifie
function toPass(obj)
{
        if(obj.value=="Mot de passe" || obj.value=="")
        {
                obj.value="";
                obj.type="password";
        }

}

function toText(obj)
{
        if(obj.value=="")
        {
                obj.value="Mot de passe";
                obj.type="text";
        }
}

function affiche_bandeau_non_identifie(contexte)
{
	if(contexte=="menu")
		$("div.membre").replaceWith('<div class="bloc membre">'+$("div.membre div.non_identifie").html()+'</div>');
}

function affiche_bandeau_identifie(nom,prenom,mes_favoris,contexte)
{
	if(contexte=="menu")
	{
		$("div.membre").replaceWith('<div class="bloc membre">'+$("div.membre div.identifie").html()+'</div>');
		if(mes_favoris>0)
			$("div.membre .mes_favoris").html($("div.membre .mes_favoris").html()+" ("+mes_favoris+")");
		$("div.membre li").each(function(){
			if( $(this).children("a").attr("href")==document.location.pathname || $(this).children("a").attr("href")==document.location.href)
				$(this).html('<span class="fred rouge">'+$(this).children("a").html()+'</span>');
		});
	}
	else if(contexte=="bandeau")
	{
		document.write('<div class="right">Bienvenue '+prenom+' '+nom+'<a href="/acces/deconnexion.php"><img class="bt_close" src="/img/bt_close.png"/></a></div>');
	}
}