// 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));
				myRegExp = /\+/gi;
				s1=s.replace(myRegExp,' ');
				s1 = Utf8.decode(s1);
				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='';
	civilite=getCookie('civilite');		if(civilite==null)civilite='';
	pseudo	=getCookie('pseudo');		if(pseudo==null)pseudo='';
	id_sso 	= getCookie('id_sso'); 		if(id_sso == null) id_sso = ''; 

	//alert("id_sso="+id_sso);
	if (id_sso == '')	affiche_bandeau_non_identifie(contexte);
	// Patch au cas où banni
	else if (id_sso != '' && nom == '' && prenom == '' && civilite == '' && pseudo == '')	affiche_bandeau_non_identifie(contexte);
	// Fin Patch
	else			affiche_bandeau_identifie(civilite,nom,prenom,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";
        }
}

//fonction prise à l'EstRepu pour les test
function affiche_bandeau_non_identifie(contexte)
{
if(contexte=='bandeau')
	{
  document.write("");
  document.write("<a href=\"/acces/espaceclient/connexion.php?placeValuesBeforeTB_=savedValues&amp;TB_iframe=true&amp;height=444&amp;width=610&amp;height=590\" class=\"thickbox\" title=\"Se connecter / S'inscrire\" >Se connecter | S'inscrire</a>");
  	}
else if(contexte=='espaceabo')
	{
  document.write("<a href=\"/acces/espaceclient/connexion.php?placeValuesBeforeTB_=savedValues&amp;TB_iframe=true&amp;height=444&amp;width=610&amp;height=590\" class=\"thickbox\" title=\"Se connecter / S'inscrire\" >Espace Abonnés</a>");
	}
else if(contexte=='newsletter')
{
  //document.write("<a href=\"/acces/espaceclient/connexion.php?placeValuesBeforeTB_=savedValues&amp;TB_iframe=true&amp;height=444&amp;width=610\" class=\"thickbox\" title=\"Se connecter / S'inscrire\" >News</a>");
  document.write("<a href=\"/acces/espaceclient/abonnements/newsletter/newsletter.php?placeValuesBeforeTB_=savedValues&amp;TB_iframe=true&amp;height=520&amp;width=610\" class=\"thickbox\" title=\"Modifier mon abonnement\">Newsletter</a>");
}	
}




//"anciennes" fonction d'affichage du bandeau
function affiche_bandeau_identifie(civilite,nom,prenom,contexte)
{
if(contexte=='bandeau')
	{
  document.write("<a href=\"/acces/espaceclient/abonnements/abonnements.php\">Bonjour "+pseudo+"</a> | ");
  document.write("<a href=\"/acces/espaceclient/abonnements/abonnements.php\">Mon compte</a> | ");  
  document.write("<a href=\"/acces/deconnexion.php?nocache=true\">Se déconnecter</a>");
  	}
else if(contexte=='espaceabo')
	{
  document.write("<a href=\"/acces/espaceclient/abonnements/abonnements.php\">Espace Abonnés</a>");  
	}
else if(contexte=='newsletter')
{
  document.write("<a href=\"/acces/espaceclient/abonnements/newsletter/newsletter.php?placeValuesBeforeTB_=savedValues&amp;TB_iframe=true&amp;height=520&amp;width=610\" class=\"thickbox\" title=\"Modifier mon abonnement\">Newsletter</a>");
}
}





var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
