//Fonction pour afficher, masquer des champs,boutons de formulaire :
function affiche(form,btn_nom,champ_nom){
	var bouton = form.elements[btn_nom];
	var champ = form.elements[champ_nom];
	
	if(champ.value == '')
	{
		bouton.style.display='none';
	}
	else
	{
		bouton.style.display='block';
	}
}

//Fonction pour les select liées :
function boites(form,nom_liste_1,nom_liste_2){
	var liste1    = form.elements[nom_liste_1];
	var liste2    = form.elements[nom_liste_2];
	var index = liste1.selectedIndex;
	if(index < 1)
	{
	   liste2.options.length = 0;
	   liste2.options[liste2.options.length] = new Option("Site...","");
	   affiche(form,"btn_modif","site_id");
	   affiche(form,"btn_suppr","site_id");
	}
	else 
	{
	   var xhr_object = null;
	
	   if(window.XMLHttpRequest) // Firefox
	   {
	      xhr_object = new XMLHttpRequest();
	   }
	   else if(window.ActiveXObject) // Internet Explorer
	   {
	      xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   else { // XMLHttpRequest non supporté par le navigateur
	      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	      return;
	   }
	
	   xhr_object.open("POST", "index.php", true);
	
	   xhr_object.onreadystatechange = function() {
	      if(xhr_object.readyState == 4)
	         eval(xhr_object.responseText);
	         affiche(form,"btn_modif","site_id");
	         affiche(form,"btn_suppr","site_id");
	   }
	
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   var data = "categorie="+escape(liste1.options[index].value)+"&form="+form.name+"&select="+nom_liste_2;
	   xhr_object.send(data);
	}
}
//Fonction de redirection :
function redirection(page) {
	location.href = page;
}

function onload()	{

	chargement();
}

function chargement()	{

	for (id=1; id<30; id++) {
		nid = "onglet_"+id;
		if(document.getElementById(nid)!=null)	{
			cookie_value = getCookie("cookie_"+id);
			//alert(id+' : '+cookie_value);
			bid = 'bmasquer_' + id;
			idr = 'onglet_' + id;
			if(cookie_value == 'i')	{
				document.getElementById(bid).src='img/bouton_afficher.gif';
				document.getElementById(idr).style.display='none';
			}
			else if(cookie_value == 'v')	{
				document.getElementById(bid).src='img/bouton_masquer.gif';
				document.getElementById(idr).style.display='block';
			}
		}
	}
}

function change(id)	{

	bid = 'bmasquer_' + id;
	idr = 'onglet_' + id;

	//alert(bid + " / " + document.getElementById(bid));

	date= new Date;
	date.setMonth(date.getMonth()+6);

	if(document.getElementById(idr).style.display==='none')	{
		document.getElementById(bid).src='img/bouton_masquer.gif';
		document.getElementById(idr).style.display='block';
		setCookie("cookie_"+id, 'v',date);
	}
	else	{
		document.getElementById(bid).src='img/bouton_afficher.gif';
		document.getElementById(idr).style.display='none';
		setCookie("cookie_"+id, 'i',date);
	}
}

/**
	GESTION DE COOKIES, pris sur http://www.netspade.com/articles/2005/11/16/javascript-cookies/
*/
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}