// *************************************************************************************
// ********************************************** Actions génériques au chargement

// Pre onload code execution.
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", my_init, null);
}

function statechange() {
  if (document.readyState == "interactive")
    my_init();
}

if (document.readyState) {	
  if (document.readyState == "interactive" || document.readyState == "complete") {
    my_init();
  } else {
    document.onreadystatechange = statechange;
  }
}
else
	window.onload = my_init;

function my_init()
{ 
	setMenu(); 
	
	for(i=0;i<switchingBlockOff.length;i++)
	{
		setDisplay(switchingBlockOff[i],'none');
	}

	for(i=0;i<switchingBlockOn.length;i++)
	{
		setDisplay(switchingBlockOn[i],'block');
	}
	
	//Gestionnaire de message
	url=document.location.href;
	if(url.indexOf('?msg=')>0)
	{
		alert(URLDecode(url.substr(url.indexOf('?msg=')+5,url.length)));
	}
}

// *************************************************************************************
// ********************************************** Gestionnaire de menu
//var menuActive='hebergement';
if(menuActive == null) { var menuActive=""; }

function setMenu() {
	cible=document.getElementById('menu_'+menuActive);
	if(cible) { cible.className='menu1on'; }
}

/************************
 * On insère au début du body de chaque page le tag suivant
 * <script type="text/javascript"> var menuActive="CodeDeLaPage"; </script>
 ************************/
 
// *************************************************************************************
// ********************************************** Block ouvert/fermé
var switchingBlockOff = Array();
var switchingBlockOn = Array();

function setDisplay(cible_name,mode)
{
	cible   = document.getElementById(cible_name);
	cible_1 = document.getElementById(cible_name+'_OUVRIR')
	cible_2 = document.getElementById(cible_name+'_FERMER')

	if(cible!=null)
	{
		cible.style.display=mode;

		if(cible_1!=null && cible_2!=null)
		{
			if(mode=='block')
			{
				cible_1.style.display='none';
				cible_2.style.display='block';
			}
			else
			{
				cible_1.style.display='block';
				cible_2.style.display='none';
			}
		}
	
		return true;
	}
	return false;
}

function getDisplay(cible)
{
	cible=document.getElementById(cible);
	if(cible!=null)
	{
		return cible.style.display;
	}
	return false;
}

function switchDisplay(cible)
{
	if(getDisplay(cible)=='block')
		setDisplay(cible,'none');
	else
		setDisplay(cible,'block');
}

function displaySwitcher(cible)
{
	document.write('<div id="'+cible+'_OUVRIR">');
	document.write('<a href="javascript: switchDisplay(\''+cible+'\');">');
	document.write('<img src="../images/puce5.gif" width="25" height="7" hspace="4" border="0">');
	document.write(' En savoir plus ');
	document.write('<img src="../images/puce6.gif" width="11" height="11" hspace="1" border="0" align="absmiddle">');
	document.write('</a>');
	document.write('</div>');
	
	document.write('<div id="'+cible+'_FERMER" style="display:none;">');
	document.write('<a href="javascript: switchDisplay(\''+cible+'\');">');
	document.write('<img src="../images/puce5.gif" width="25" height="7" hspace="4" border="0">');
	document.write(' Fermer ');
	document.write('<img src="../images/puce6.gif" width="11" height="11" hspace="1" border="0" align="absmiddle">');
	document.write('</a>');
	document.write('</div>');
	
	switchingBlockOff.push(cible);
}

// *************************************************************************************
// *************************************************************************************
function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};