// Fonctions sur les jeux de caractères
// Eric Quillévéré 2009
// v. 1.0 - 11/02/2009
function SupprimeCommentairesHTML(texte)
	{
	var posDeb=texte.indexOf('<!--');
	var posFin=texte.indexOf('-->');
	while (posDeb!=-1 && posFin!=-1 && posFin>posDeb)
		{
		texte=texte.substring(1,posDeb-1) + texte.substring(posFin+3,texte.length);
		posDeb=texte.indexOf('<!--');
		posFin=texte.indexOf('-->');

		}
	return texte;
	}
	
	
// Supprime les caractères compris dans le tableau (au début et à la fin de la chaine en param)
function caract_SupprCaractDebFin(texte, tab_caract)
	{
	
	// début
	var i=0;
	while (i<tab_caract.length && texte.length>=1)
		{
		if (texte.charAt(0)==tab_caract[i] )
			{
			texte=texte.substring(1,texte.length);
			i=0;
			}
		else
			i++;
		}
	
	//	fin
	i=0;
	while (i<tab_caract.length && texte.length>=1)
		{
		if (texte.charAt(texte.length-1)==tab_caract[i] )
			{
			texte=texte.substring(0,texte.length-1);
			i=0;
			}
		else
			i++;
		}		
			
		
	return texte;
	}	


// Renvoie ne chaine contenant N fois le caractère demandé
function chaineDeCaracteres(caractere, nb)
	{
	var chaine='';
	while (nb>0 && chaine.length<nb) chaine+=caractere;
	return chaine;
	}
	

// Remplace les caractères accentués de type &#150; par les caractères réels	
function caract_UNICODEversISO(chaine)
	{
	for (var i=0;i<=255;i++)
		{
		chaine=remplacer(chaine, '&#' + i +';',String.fromCharCode(i));
		}
	return chaine;
	}
	
	
// Remplace les accents par leur valeur unicode	
function caract_ISOversUNICODE(texte)
	{
	var s= texte.substring(0, 255);
	var retour= s.replace(/(\w|\W)/g,
		function(s, n) 
			{
			var c= n.charCodeAt(0);
			return((c == 38)? '&' : ((c > 127)? '&#'+c+';' : n));
			}
		);
	return(retour);
	} 	
	
// Equivalent à la fonction PHP html_entity_decode : remplace & par &amp;
function caract_HTML_encode(chaine)
	{
	
	var tab=html_entity();
	for (var cle in tab)
		chaine=remplacer(chaine, String.fromCharCode(cle), tab[cle]);
	return chaine;	
	}
	

	
// Equivalent à la fonction PHP caract_HTML_decode : &amp; par &
function caract_HTML_decode(chaine)
	{
	var tab=html_entity();
	for (var cle in tab)
		chaine=remplacer(chaine, tab[cle], String.fromCharCode(cle));
	return chaine;	
	}	
	
	
	
function html_entity()	
	{
	return { 38:'&amp;', 60:'&lt;', 62:'&gt;', 34:'&quot;', 39:'&#039;',
		38:'&amp;', 60:'&lt;', 62:'&gt;', 128:'&euro;', 160:'&nbsp;', 161:'&iexcl;' , 162:'&cent;',
		163:'&pound;', 164:'&curren;', 165:'&yen;', 166:'&brvbar;', 167:'&sect;',
		168:'&uml;', 169:'&copy;', 170:'&ordf;', 171:'&laquo;', 172:'&not;', 173:'&shy;',
		174:'&reg;', 175:'&macr;', 176:'&deg;', 177:'&plusmn;', 178:'&sup2;', 179:'&sup3;',
		180:'&acute;', 181:'&micro;', 182:'&para;', 183:'&middot;', 184:'&cedil;', 185:'&sup1;',
		186:'&ordm;', 187:'&raquo;', 188:'&frac14;', 189:'&frac12;', 190:'&frac34;', 191:'&iquest;',
		192:'&Agrave;', 193:'&Aacute;', 194:'&Acirc;', 195:'&Atilde;', 196:'&Auml;', 197:'&Aring;',
		198:'&AElig;', 199:'&Ccedil;', 200:'&Egrave;', 201:'&Eacute;', 202:'&Ecirc;', 203:'&Euml;',
		204:'&Igrave;', 205:'&Iacute;', 206:'&Icirc;', 207:'&Iuml;', 208:'&ETH;', 209:'&Ntilde;',
		210:'&Ograve;', 211:'&Oacute;', 212:'&Ocirc;', 213:'&Otilde;', 214:'&Ouml;', 215:'&times;',
		216:'&Oslash;', 217:'&Ugrave;', 218:'&Uacute;', 219:'&Ucirc;', 220:'&Uuml;', 221:'&Yacute;',
		222:'&THORN;', 223:'&szlig;', 224:'&agrave;', 225:'&aacute;', 226:'&acirc;', 227:'&atilde;',
		228:'&auml;', 229:'&aring;', 230:'&aelig;', 231:'&ccedil;', 232:'&egrave;', 233:'&eacute;',
		234:'&ecirc;', 235:'&euml;', 236:'&igrave;', 237:'&iacute;', 238:'&icirc;', 239:'&iuml;',
		240:'&eth;', 241:'&ntilde;', 242:'&ograve;', 243:'&oacute;', 244:'&ocirc;', 245:'&otilde;',
		246:'&ouml;', 247:'&divide;', 248:'&oslash;', 249:'&ugrave;', 250:'&uacute;', 251:'&ucirc;',
		252:'&uuml;', 253:'&yacute;', 254:'&thorn;', 255:'&yuml;'};	
	}
	
	
	
function caract_ISOversUTF8(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;
    }
	

function caract_UTF8versISO(utftext) 
	{
	// Euro 
	//utftext=remplacer(utftext, String.fromCharCode(226) + String.fromCharCode(8218) + String.fromCharCode(172),'&euro;');
	var string = "";
	var i = 0;
	var c =0,c1 = 0, c2 = 0, c3=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;
    }
	
	
// Equivalent replace mais global et sans expressions régulières.
// (permet de passer des chaînes contenant une variable, contrairement au replace global)
//alert(remplacer('Bed & Breakfast, Les Îles Glénans & Groix.', '&','&amp;'));
function remplacer(source, cherche, remplace) 
	{
	var pos=0;
	var posTrouve = source.indexOf(cherche, pos);
	while (posTrouve > -1)
		{
		source=source.substring(0,posTrouve) + remplace + source.substring(posTrouve+cherche.length,source.length);
		pos+=posTrouve+remplace.length;
		posTrouve = source.indexOf(cherche, pos);
		
		}
    return source;
	}	

// Langue du navigateur
function langue_Renvoie() 
	{
	if (navigator.language)
		{
		if (typeof navigator.language!='undefined')
			return  navigator.language;
		}
		
	if (navigator.userLanguage)
		{
		if (typeof navigator.userLanguage!='undefined')
			return  navigator.userLanguage;
		}		
	return '';
	}
