function ChangePage(pageName, pageNumber) {	

	if(document.getElementById('paging_form') != null) {
		document.getElementById('paging_form').action = pageName + '&pf=' + pageNumber;
		document.getElementById('paging_form').submit();
	}
	else {
		if(pageName.indexOf('?') == -1) pageName += '?'
		else pageName += '&'
		
		document.location.href = pageName + 'pf=' + pageNumber;
	}
}

function go(c){
	if(!c.data.replace(/(\s|\n)/g,''))
		c.parentNode.removeChild(c);
}

function clean(d){
	var bal=d.getElementsByTagName("*");
	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
}

function RemoveChildNodes(elem)
{
	
	if(elem.hasChildNodes()) {
		
		var nbElems = elem.childNodes.length;
		for(var i=0;i<nbElems;i++)
		{
			elem.removeChild(elem.childNodes[elem.childNodes.length - 1]);
		}
	}
	return elem;
}

function couperTexte(texte, longueur)
{
	if(texte.length <= longueur)
		return texte;
	else 
		return texte.substr(0,longueur-3)+"...";
}

function PopUp2(idPopup, idFond, width, height)
{
	fond.style.height = document.body.scrollHeight+'px';
	fond.style.width = document.body.scrollWidth+'px';
	
	//popupTitre.innerHTML = titre;
	//popupContenu.innerHTML = contenu;
	
	if (width != undefined)
		popup.style.width = width+'px';
	if (height != undefined)
		popup.style.height = height+'px';	

	popup.style.top = ((window.innerHeight-popup.offsetHeight)/2+window.pageYOffset)+"px";
	popup.style.left = ((document.body.scrollWidth-popup.offsetWidth)/2)+"px"; 
	
	fond.style.visibility = '';
	popup.style.visibility = '';
}

function PopUp(titre, contenu, width, height)
{
	fond.style.height = document.body.scrollHeight+'px';
	fond.style.width = document.body.scrollWidth+'px';
	
	popupTitre.innerHTML = titre;
	popupContenu.innerHTML = contenu;
	
	if (width != undefined)
		popup.style.width = width+'px';
	if (height != undefined)
		popup.style.height = height+'px';	

	popup.style.top = ((window.innerHeight-popup.offsetHeight)/2+window.pageYOffset)+"px";
	popup.style.left = ((document.body.scrollWidth-popup.offsetWidth)/2)+"px"; 
	
	fond.style.visibility = '';
	popup.style.visibility = '';
}

function FermerPopUp()
{
	popup.style.visibility='hidden';
	fond.style.visibility='hidden';
}

function submitForm(formId, callBack, typeResponse)
{
	var form = document.getElementById(formId);
	
	var inputs = form.getElementsByTagName('input');
	var selects = form.getElementsByTagName('select');
	var textareas = form.getElementsByTagName('textarea');
	var parameters = "";	
	
	for(var i=0; i<inputs.length; i++)
	{
		if(inputs[i].type == "checkbox" || inputs[i].type == "radio")
		{
			if(inputs[i].checked == true)
				parameters += "&" + inputs[i].name + "=" + encodeURIComponent(inputs[i].value); 
		}
		else
		{
			if(inputs[i].value != "")
				parameters += "&" + inputs[i].name + "=" + encodeURIComponent(inputs[i].value); 
		}
	}
	
	for(var i=0; i<selects.length; i++)
	{
		parameters += "&" + selects[i].name + "=" + encodeURIComponent(selects[i].options[selects[i].selectedIndex].value);
	}
	
	for(var i=0; i<textareas.length; i++)
	{
		parameters += "&" + textareas[i].name + "=" + encodeURIComponent(textareas[i].value);
	}
	
	getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200) 
		{						
			if(typeResponse == "XML")
				callBack(xhr.responseXML)
			else
				callBack(xhr.responseText)
		}
	}
	
	xhr.open('POST',form.action,true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
	xhr.send(parameters);
}
/*
** Cette fonction retourne n'importe quel texte sans caractere spéciaux'
*/
function formatUrlRw(texte_brut)
{
	texte_brut = texte_brut.toLowerCase(); 
	texte_brut = texte_brut.replace(/[é,è,ê,ë,Ë,Ê,È]/ig, 'e');  // remplacement des e
	texte_brut = texte_brut.replace(/[ô,ö,Ö,Ô]/ig, 'o'); // remplacement des o
	texte_brut = texte_brut.replace(/[ï,î,Î,ï]/ig, 'i'); // remplacement des i
	texte_brut = texte_brut.replace(/[à]/ig, 'a'); // remplacement des a
	texte_brut = texte_brut.replace(/[û,ü,ù]/ig, 'u'); // remplacement des u
	texte_brut = texte_brut.replace(/[^a-z0-9]/ig,'-'); // remplacement de tous les caractere autre qu'alphanumerique par des -'
	while(texte_brut.match(/--/) == "--")
	{
		texte_brut = texte_brut.replace(/--/ig, '-');
	}	
	return texte_brut;
}

function LoadSousCats(listeCat, key) 
{
	//alert("ssCat");
	var idCategorie = listeCat.options[listeCat.selectedIndex].value;
	var niveau = parseInt(listeCat.id.split('_')[1]);
	
	if(key != null)
		var listes_categories = document.getElementById('listes_categories_' + key);
	else
		var listes_categories = document.getElementById('listes_categories');
	
	//Suppression des listes de niveau supérieur
	var selects = listes_categories.getElementsByTagName('select');
	var nbSelects = selects.length-1;
	for(var i=nbSelects; i>=0; i--)
	{
		//alert(niveau + " / " + selects[i].id.split('_')[1])
		if(parseInt(selects[i].id.split('_')[1]) > niveau)
			listes_categories.removeChild(selects[i]);
	}
	
	getXhr();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200) 
		{	
			reponse = clean(xhr.responseXML.documentElement);
			//alert(reponse);
			var motcle = reponse.getElementsByTagName('motCle');
			var ponderation = reponse.getElementsByTagName('ponderation');
			var ecoPart = reponse.getElementsByTagName('ecoPart');
			var souscats = reponse.getElementsByTagName('souscategorie');
			var listeSousCats = document.createElement('select');
			
			// Mots clé de la catégorie repercuté sur l'input correspondant
			if(motcle.length > 0 && document.getElementById('SeoMotCle') != null)
			{	
				if (motcle[0].firstChild != null)
				{	
					var InputmotCle = document.getElementById('SeoMotCle');
					InputmotCle.value = motcle[0].firstChild.nodeValue;
				}
			}
			// Pondération défini dans la categorie repercuté sur l'input correspondant
			if(ponderation.length > 0 && document.getElementById('ponderation') != null)
			{	
				if (ponderation[0].firstChild != null)
				{	
					document.getElementById('ponderation').value = ponderation[0].firstChild.nodeValue;
				}
			}
			// eco Participation défini dans la categorie
			if(ecoPart.length > 0 && document.getElementById('eco_participation') != null)
			{	
				if (ecoPart[0].firstChild != null)
				{	
					document.getElementById('eco_participation').value = ecoPart[0].firstChild.nodeValue;
				}
			}
			
			if(key != null)
			{
				listeSousCats.name = 'categorisation_' + (niveau + 1) + '_' + key;
				listeSousCats.id = 'categorisation_' + (niveau + 1 + '_' + key);
				listeSousCats.onchange = function() { LoadSousCats(this, key); }
			}
			else
			{
				listeSousCats.name = 'categorisation_' + (niveau + 1);
				listeSousCats.id = 'categorisation_' + (niveau + 1);
				listeSousCats.onchange = function() { LoadSousCats(this); }
			}
			
			listeSousCats.className = 'inputSelect';
			listeSousCats.style.display = 'block';
			listeSousCats.style.marginBottom = '5px';
			//alert(souscats.length);
			if(souscats.length > 0) 
			{
				listeSousCats.options.length = souscats.length + 1;
				
				listeSousCats.options[0].value = '';
				listeSousCats.options[0].text = 'Choisir une sous categorie';
				
				for(var i = 0; i < souscats.length; i++) 
				{
					listeSousCats.options[i+1].value = souscats[i].childNodes[0].firstChild.nodeValue;
					listeSousCats.options[i+1].text = souscats[i].childNodes[1].firstChild.nodeValue;
				}
				listes_categories.appendChild(listeSousCats);
			}
			else 
			{
				listeSousCats.length = 2;
				listeSousCats.options[1].value = '0';
				listeSousCats.options[1].text = 'Aucune sous catégorie';
			}
		}		
		//if(iniVal != null) SelectListValue('scat',iniVal);
	}
	
	xhr.open('POST','/ajax/ajax_appels.php',true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
	xhr.send('n=4&idcat=' + idCategorie);
}