function get_root_url()
{
	var root_url = document.URL.split('/');
	return root_url[0] + '//' + root_url[2] + '/';
}

var ROOT_URL = get_root_url();

function marquer_menu_actif(menu_id)
{
	menu = document.getElementById('a'+menu_id);
	//alert('[a'+menu_id+']');
	if (menu == null) return;
	classe = menu.className;
	menu.className = 'menu_actif';
	if (classe != '')
	{
		menu.className += ' ' + classe;
	}
	//alert('['+menu.innerHTML+']['+menu.className+']');
}

//fonction pour le controle de saisie de l'email
function valider(formulaire)
{
	var txtEmail=formulaire.elements['email'];
	//si le champ Email n'est pas renseigné ! ! !
	if (txtEmail.value=="")
	{
		alert('Le champ Email doit être renseigné (saisissez votre email) !');
		txtEmail.focus();
		return false;
	}
	else
	{
		//si le champs Email est renseigné sans arobase...
		if (txtEmail.value.indexOf('@')==(-1))
		{
			alert('L\'e-mail doit contenir un @ (par exemple : toto@laposte.net) !');
			txtEmail.focus();
			txtEmail.select();
			return false;
		}
	}
	return true;
}

function selectionner(bRadio) 
{
    bRadio.checked = !bRadio.checked;
}

function getSelectedRadioValue(radioObj)
{
	if (!radioObj)
	{
		return "";
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
	for(var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].checked)
		{
			return radioObj[i].value;
		}
	}
	return "";
}

function getSelectedRadioId(radioObj)
{
	if (!radioObj)
	{
		return "";
	}
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.id;
		}
		else
		{
			return "";
		}
	}
	for(var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].checked)
		{
			return radioObj[i].id;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function valider_choix(type,destination,nom_form)
{
	var val = "";
	var alerte = "Vous devez sélectionner ";
	
	switch (type)
	{
		case 'image':
			val = getSelectedRadioValue(document.forms['choix'].elements['photo_id']);
			alerte += "une image";

			if (val != "")
			{
				var image_uri = 'personnalisation/img/'+val+'.'+document.getElementById(val+'ext').innerHTML;
				var image_url = ROOT_URL + image_uri;
				var image_minia_url = ROOT_URL + 'personnalisation/img/'+val+'minia.jpg';
				var hauteur = parseInt(document.getElementById(val+'wid').innerHTML)+20;
				var largeur = parseInt(document.getElementById(val+'hei').innerHTML)+20;
				var image_desc = document.getElementById(val+'desc').innerHTML;
							
				switch (destination)
				{
					case 'menu':
						window.opener.document.forms['choix_image'].elements['photo_id'].value = val;
						window.opener.document.getElementById('ic').className = 'disp';
						window.opener.document.getElementById('nom_image').innerHTML = document.getElementById(val + 'desc').innerHTML;
						window.opener.document.getElementById('ai').className = 'disp';
						break;
					
					case 'tiny_mce':
						window.opener.document.forms['formlien'].elements['src'].value = ROOT_URL + 'personnalisation/img/' + val + '.' + document.getElementById(val + 'ext').innerHTML;
						window.opener.document.forms['formlien'].elements['alt'].value = document.getElementById(val + 'legende').innerHTML;
						window.opener.document.forms['formlien'].elements['width'].value = document.getElementById(val + 'wid').innerHTML;
						window.opener.document.forms['formlien'].elements['height'].value = document.getElementById(val + 'hei').innerHTML;
						break;
						
					case 'evenement':
						window.opener.document.forms['form_evenement_photo'].elements['photo_id'].value = val;
						window.opener.document.getElementById('nouvelle_photo').src = image_minia_url;
window.opener.document.getElementById('nouvelle_photo_popup').href="javascript:popup('"+image_url+"','popup','"+hauteur+"','"+largeur+"')";
						window.opener.document.getElementById('evenement_photo').className = 'disp';
						window.opener.document.getElementById('evenement_photo_submit').className = 'disp';
						break;
						
					case 'evenement_lien':
						window.opener.document.getElementById('evenement_lien_image_id').value = val;
						window.opener.document.getElementById('evenement_lien_image').src = image_minia_url;
						window.opener.document.getElementById('evenement_lien_image_popup').href = "javascript:popup('"+image_url+"','popup','"+hauteur+"','"+largeur+"')";
						window.opener.document.getElementById('evenement_lien_texte').value = image_desc;
						window.opener.document.getElementById('evenement_lien_uri').value = image_uri;
						window.opener.document.getElementById('evenement_lien_submit').className = 'disp';
						break;
						
					default: return;
				}
			}
			break;
		
		case 'flash':
			alerte += "une animation";
			val = getSelectedRadioValue(document.forms['choix'].elements['flash_id']);

			if (val != "")
			{
				var flash_nom = document.getElementById(val + 'nom').innerHTML;
				var flash_uri = 'personnalisation/flash/'+val+'.swf';
				var flash_url = ROOT_URL + flash_uri;
				var hauteur = parseInt(document.getElementById('largeur_'+val).innerHTML)+20;
				var largeur = parseInt(document.getElementById('hauteur_'+val).innerHTML)+20;			
				switch (destination)
				{
					case 'menu':
						window.opener.document.forms['choix_flash'].elements['flash_id'].value = val;
						window.opener.document.getElementById('fc').className = 'disp';
						window.opener.document.getElementById('nom_flash').innerHTML = flash_nom;
						window.opener.document.getElementById('afl').className = 'disp';
						break;
					
					case 'evenement_lien':
						window.opener.document.getElementById('evenement_lien_flash_id').value = val;
						window.opener.document.getElementById('evenement_lien_flash_popup').href = "javascript:popup('"+flash_url+"','popup','"+hauteur+"','"+largeur+"')";
						window.opener.document.getElementById('evenement_lien_flash_popup').innerHTML = flash_nom;
						window.opener.document.getElementById('evenement_lien_texte').value = flash_nom;
						window.opener.document.getElementById('evenement_lien_uri').value = flash_uri;
						window.opener.document.getElementById('evenement_lien_submit').className = 'disp';
						break;
						
					default: return;
				}
			}
			break;
			
		case 'document':
			alerte += "un document";
			val = getSelectedRadioValue(document.forms['choix'].elements['document_id']);
			if (val != "")
			{
				var document_uri = 'personnalisation/doc/' + document.getElementById(val + 'url').innerHTML;
				var document_url = ROOT_URL + document_uri;
				var document_desc = document.getElementById(val + 'desc').innerHTML;
				switch (destination)
				{
					case 'tiny_mce':
						window.opener.document.forms['formlien'].elements['href'].value = document_url;
						window.opener.document.forms['formlien'].elements['linktitle'].value = document_desc;
						break;
					
					case 'evenement_lien':
						window.opener.document.getElementById('evenement_lien_document_id').value = val;
						window.opener.document.getElementById('evenement_lien_document_popup').href = document_url;
						window.opener.document.getElementById('evenement_lien_document_popup').innerHTML = document_desc;
						window.opener.document.getElementById('evenement_lien_texte').value = document_desc;
						window.opener.document.getElementById('evenement_lien_uri').value = document_uri;
						window.opener.document.getElementById('evenement_lien_submit').className = 'disp';
						break;
				}
			}
			break;
			
		case 'menu':
			alerte += "un menu";
			val = getSelectedRadioValue(document.forms['choix'].elements['menu_id']);
			if (val != "")
			{
				var menu_uri = 'index.php?m=' + val;
				var menu_url = ROOT_URL + menu_uri;
				var menu_nom = document.getElementById(val + 'l').innerHTML;			
				switch (destination)
				{
					case 'actualite':
						// #TODO l'alerte est particulière, voir comment le gérer
						var id = getSelectedRadioId(document.forms['choix'].elements['menu_id']);
						var menu_nom = document.getElementById(id + 'l').innerHTML;
						id = id.substring(0,id.length-1);
						switch (nom_form)
						{
							case 'formlien':
								window.opener.document.forms['formlien'].elements['menu_id'].value = val;
								window.opener.document.getElementById('mc').className = 'disp';
								window.opener.document.getElementById('nom_menu').innerHTML = document.getElementById(id + 'l').innerHTML;
								window.opener.document.getElementById('ar').className = 'disp';
								break;
							
							case 'calform':
								window.opener.document.forms['calform'].elements['menu_id_lien'].value = id;
								window.opener.document.forms['calform'].elements['lien'].value = ROOT_URL + 'index.php?m='+val;
								window.opener.document.getElementById('mc_lien').className = 'disp';
								window.opener.document.getElementById('mc_lien').innerHTML += document.getElementById(id + 'l').innerHTML;
								break;
		
							default: return;
						}
						break;
									
					case 'utilisateur':
						window.opener.document.forms['connect'].elements['menu_id'].value = val;
						window.opener.document.getElementById('mc').className = 'disp';
						window.opener.document.getElementById('nom_menu').innerHTML = document.getElementById(val + 'l').innerHTML;
						window.opener.document.getElementById('ar').className = 'disp';
						break;
						
					case 'tiny_mce':
						window.opener.document.forms['formlien'].elements['href'].value = ROOT_URL + 'index.php?m=' + val;
						break;
						
					case 'evenement_lien':
						window.opener.document.getElementById('evenement_lien_menu_id').value = val;
						window.opener.document.getElementById('evenement_lien_menu_popup').href = menu_url;
						window.opener.document.getElementById('evenement_lien_menu_popup').innerHTML = menu_nom;
						window.opener.document.getElementById('evenement_lien_texte').value = menu_nom;
						window.opener.document.getElementById('evenement_lien_uri').value = menu_uri;
						window.opener.document.getElementById('evenement_lien_submit').className = 'disp';
						break;
	
					default: return;
				}
			}
			break;
		
		case 'contenu': // uniquement dans tiny_mce actuellement
			alerte += "un contenu";
			val = getSelectedRadioValue(document.forms['nl_choix'].elements['contenuid']);
			if (val != "")
			{
				window.opener.document.forms['formlien'].elements['href'].value = ROOT_URL + 'index.php?c=' + val;
			}
			break;
		
		default: return;
	}
	
	if (val == "")
	{
		alert(alerte+" !");
		return;
	}
	
	window.opener.focus();
	window.close();
}

// regarde si la chaîne str comporte plus de len caractères
function string_len(str, len)
{
	str = str.replace(/ /g, "");	//enlève les blancs
	str = str.replace(/\n/g, "");	//enlève les nouvelles lignes
	str = str.replace(/\r/g, "");	//enlève les retours chariots
	str = str.replace(/\t/g, "");	//enlève les tabulations
	return (str.length >= len);
}

//Ouvre une pop-up au centre de l'écran, SANS scrollbars
function popup(page,titre,larg,haut)
{
	window.open(page, titre, "width=" + larg + ",height=" + haut +",status=no,toolbar=no,scrollbars=no,resizable=yes,left=" + ((screen.width - larg)/2) + ",top=" + ((screen.height - haut)/2));
}

//Ouvre une pop-up au centre de l'écran, AVEC scrollbars
function popup_s(page,titre,larg,haut)
{
	window.open(page, titre, "width=" + larg + ",height=" + haut +",status=no,toolbar=no,scrollbars=yes,resizable=yes,left=" + ((screen.width - larg)/2) + ",top=" + ((screen.height - haut)/2));
}

function popup_choix_image(type)
{
	var width = 450;
	var height = 600;
	window.open(ROOT_URL + 'admin/choix_image.php?type='+type, 'choixmenu', 'location=no, width='+width+', height='+height+', menubar=no, status=yes, resizable=yes, scrollbars=yes');
}


function changeStyle(img,id)
{
	if (document.getElementById(id).className == 'hide')
	{
		document.getElementById(id).className = 'txt';
		img.src = ROOT_URL + 'img/plusm.png';
	}
	else
	{
		document.getElementById(id).className = 'hide';
		img.src = ROOT_URL + 'img/plus.png';
	}
}

function changeStyleArbre(obj,img)
{
	if (obj.className == 'hide')
	{
		obj.className = 'lc';
		img.src = ROOT_URL + 'img/plusm.png';
	}
	else
	{
		obj.className = 'hide';
		img.src = ROOT_URL + 'img/plus.png';
	}
}

function changeStyleBlocCouleur(cc,hex)
{
	if (hex.className == 'hide')
	{
		cc.disabled = false;
		hex.className = 'disp';
	}
	else
	{
		cc.disabled = true;
		hex.className = 'hide';
	}
}


function changeStyleMessage(obj,img,id)
{
	if (obj.className == 'hide')
	{
		obj.className = 'disp';
		document.getElementById('txt_' + id).className = 'txt';
		img.src = ROOT_URL + 'img/plusm.png';
	}
	else
	{
		obj.className = 'hide';
		document.getElementById('txt_' + id).className = 'hide';
		img.src = ROOT_URL + 'img/plus.png';
	}
}

// un message d'archive
function changeStyleArchive(obj,img,id)
{
	if (obj.className == 'hide')
	{
		obj.className = 'disp';
		document.getElementById('archive_txt_' + id).className = 'txt';
		img.src = ROOT_URL + 'personnalisation/img/plusm.png';
	}
	else
	{
		obj.className = 'hide';
		document.getElementById('archive_txt_' + id).className = 'hide';
		img.src = ROOT_URL + 'personnalisation/img/plus.png';
	}
}

// un bloc archive
function changeStyleArchiveBloc(obj,img)
{
	if (obj.className == 'hide')
	{
		obj.className = 'disp';
		//document.getElementById('archive_txt_' + id).className = 'txt';
		img.src = ROOT_URL + 'personnalisation/img/plusm.png';
	}
	else
	{
		obj.className = 'hide';
		//document.getElementById('archive_txt_' + id).className = 'hide';
		img.src = ROOT_URL + 'personnalisation/img/plus.png';
	}
}

function changeStylePlan(obj,img)
{
	if (obj.className == 'hide')
		deplierPlan(obj,img);
	else
		replierPlan(obj,img);
}

function replierPlan(obj,img)
{
	obj.className = 'hide';
	img.src = 'img/plus.png';
}

function deplierPlan(obj,img)
{
	obj.className = 'lc';
	img.src = 'img/plusm.png';	
}

function replierToutPlan()
{
	for(i = 0; i < document.getElementsByTagName("ul").length ; i++)
	{
		if (parseInt(document.getElementsByTagName("ul")[i].id) >= 0)
		{
			var imgid = "img"+document.getElementsByTagName("ul")[i].id;
			debug(document.getElementsByTagName("ul")[i].id+":"+document.getElementById(imgid).id);
			replierPlan(document.getElementsByTagName("ul")[i],document.getElementById(imgid));
		}
	}
}

function deplierToutPlan()
{
	for(i = 0; i < document.getElementsByTagName("ul").length ; i++)
	{
		if (parseInt(document.getElementsByTagName("ul")[i].id) >= 0)
		{
			var imgid = "img"+document.getElementsByTagName("ul")[i].id;
			debug(document.getElementsByTagName("ul")[i].id+":"+document.getElementById(imgid).id);
			deplierPlan(document.getElementsByTagName("ul")[i],document.getElementById(imgid));
		}
	}	
}

function replierLC()
{
	for (i = 0 ; i < document.getElementsByTagName("ul").length ; i++)
	{
		if (parseInt(document.getElementsByTagName("ul")[i].id) >= 0) document.getElementsByTagName("ul")[i].className = 'hide';
	}
}

function deplierLC()
{
	for (i = 0 ; i < document.getElementsByTagName("ul").length ; i++)
	{
		if (parseInt(document.getElementsByTagName("ul")[i].id) >= 0) document.getElementsByTagName("ul")[i].className = 'lc';
	}
}

function replier()
{
	for (i = 0 ; i < document.getElementsByTagName("div").length ; i++)
	{
		if (document.getElementsByTagName("div")[i].id.substr(0,4) == 'grp_' || document.getElementsByTagName("div")[i].id.substr(0,4) == 'txt_'){
			document.getElementsByTagName("div")[i].className = 'hide';
		}
	}
}

function deplier()
{
	for (i = 0 ; i < document.getElementsByTagName("div").length ; i++)
	{
		if (document.getElementsByTagName("div")[i].id.substr(0,4) == 'grp_')
		{
			document.getElementsByTagName("div")[i].className = 'disp';
		}
		if (document.getElementsByTagName("div")[i].id.substr(0,4) == 'txt_')
		{
			document.getElementsByTagName("div")[i].className = 'txt';
		}
	}
}

function popup_color_picker(name_w)
{
	var width = 400;
	var height = 300;
	window.open('color_picker/color_picker.html', name_w, 'resizable=no, location=no, width='+width+', height='+height+', menubar=no, status=yes, scrollbars=no, menubar=no');
}

function remplacerTexte( elementID, text )
{
	var obj = document.getElementById( elementID );
	if( !obj )
		throw "Element " + elementID + " not found!";

	// Netscape, Mozilla, Firefox
	if( typeof(obj.selectionStart) != "undefined" ){
		
		obj.focus();
		var start_selection = obj.selectionStart;
		var end_selection = obj.selectionEnd;
		if (end_selection < start_selection){
			
			var temp = end_selection;
			end_selection = start_selection;
			start_selection = temp;
		}

		// Sad, but true: You actually have to replace *all* of the text in 
		// the text input to replace the selection in Mozilla browsers!
		var startText = (obj.value).substring(0, start_selection);
		var endText = (obj.value).substring(end_selection, obj.textLength);

		obj.value = startText + text + endText;
		obj.selectionStart = start_selection;
		obj.selectionEnd = start_selection + text.length;
		
	}

	// Internet Explorer
	if( obj.createTextRange ){
	
		obj.focus( obj.caretPos );
		obj.caretPos = document.selection.createRange().duplicate();
		obj.caretPos.text = text;
		obj.caretPos.moveStart( "character", text.length * -1 );
		obj.caretPos.select();
		
	}

	obj.focus();
}

function validateAndSubmitForum(form)
{
	if (string_len(form.titre.value,2) && string_len(form.auteur.value,2) && string_len(form.texte.value,2))
		return true;
	else
	{
		alert("Vous devez rentrer au moins 2 caractères alphanumériques pour les champs Titre, Auteur et Texte !");
		return false;
	}
}

function validateAndSubmit(form)
{
	if (string_len(form.nom.value,2))
		return true;
	else
	{
		alert("Vous devez rentrer au moins 2 caractères pour le nom");
		return false;
	}
}

function validateAndSubmitOption(form)
{
	if (string_len(form.description.value,255))
	{
		alert("La description ne doit pas dépasser 255 caractères !");
		return false;
	}
	else
	{
		if (string_len(form.titre.value,2))
		{
			return true;
		}
		else
		{
			alert("Le titre doit avoir 2 caractères minimum.");
			return false;
		}
	}
}

function popupchoixlien()
{
	var width = 500;
	var height = 500;
	window.open(ROOT_URL + 'tiny_mce/themes/advanced/link_choix.php', 'choixmenu', 'location=no, width='+width+', height='+height+', menubar=no, status=yes, resizable=yes, scrollbars=yes');
}

function toggle_activation(groupe)
{
	for(i = 0 ; i != groupe.childNodes.length ; i++)
	{
		toggle_activation_champ(groupe.childNodes[i]);
	}
}

function toggle_activation_champ(champ)
{
	champ.disabled = !champ.disabled;
}
