var IE = false;
var FF = false;
var ie6 = false;

if(window.ActiveXObject) IE = true;
if(window.sidebar) FF = true;

var editeurs = new Array();

function ID(id)
{
	if(id)
		return document.getElementById(id);
	else
		alert('Element '+id+' introuvable.');
}
function get_formvars(zone_id,param)
{
	if (typeof(param)=='undefined')
		var param = new Array();
	var tab_input = ID(zone_id).getElementsByTagName('INPUT');
	for(var i=0;i<tab_input.length;i++)
	{
		if(tab_input[i].name.length > 0)
		{
			if(tab_input[i].type == 'text' || tab_input[i].type == 'hidden')
				param = addParam(param,tab_input[i].name,tab_input[i].value);
			else if(((tab_input[i].type == 'radio') || (tab_input[i].type == 'checkbox')) && tab_input[i].checked == true)
				param = addParam(param,tab_input[i].name,tab_input[i].value);
		}
	}
	var tab_textarea = ID(zone_id).getElementsByTagName('TEXTAREA');
	for(var i=0;i<tab_textarea.length;i++)
	{
		if(tab_textarea[i].name.length > 0)
			param = addParam(param,tab_textarea[i].name,tab_textarea[i].value);
	}
	var tab_select = ID(zone_id).getElementsByTagName('SELECT');
	for(var i=0;i<tab_select.length;i++)
	{
		if(tab_select[i].name.length > 0)
			param = addParam(param,tab_select[i].name,tab_select[i].value);
	}
	return param;
}
function hasClass(elem,classe)
{
	var classes = elem.className.split(' ');
	var found=false;
	if(classes.length > 0)
	{
		for(var i=0;i<classes.length;i++)
		{
			if(classe == classes[i])
			{
				found=true;
				break;
			}
		}
	}
	return found;
}
function addClass(elem,classe)
{
	var found = hasClass(elem,classe);
	if(!found){
		if(elem.className.length > 0)
			elem.className += ' ';
		elem.className += classe;
	}
}
function delClass(elem,classe)
{
	if(hasClass(elem,classe))
	{
		var tmp = new Array();
		var classes = elem.className.split(' ');
		for(var i=0;i<classes.length;i++)
		{
			if(classe != classes[i])
				tmp.push(classes[i]);
		}
		elem.className = tmp.join(' ');
	}
}
function str_replace(from,to,str)
{
	while(str.indexOf(from) >= 0)
		str = str.replace(from,to);
	return str;
}
function traite_date(madate)
{
	var sep = '/';
	var today = new Date()
	var jour = '' + today.getDate();
	var mois = '' + (today.getMonth()+1);
	var an = ''  +today.getFullYear();
	if(madate.length == 0)
		return '';
	madate = str_replace(" ","/",madate);
	madate = str_replace(".","/",madate);
	madate = str_replace("-","/",madate);
	
	var tab_madate;
	var monjour;
	var monmois;
	var monan;
	if(madate.indexOf("/") == -1)
	{
		monjour = '' + madate.substr(0,2);
		monmois = '' + madate.substr(2,2);
		monan = '' + madate.substr(4,4);
	}
	else
	{
		tab_madate = madate.split("/");
		monjour = tab_madate[0];
		monmois = '' + tab_madate[1];
		monan = '' + tab_madate[2];
	}
	if(monmois == 'undefined' || monmois == '')
		monmois = mois;
	if(monan == 'undefined' || monan == '')
		monan = an;
	if(monjour.length == 1)
		monjour = '0' + monjour;
	if(monmois.length == 1)
		monmois = '0' + monmois;
	if(monan.length < 4){
		if(monan.length == 2 && monan >= 70)
			monan = '19' + monan;
		else
			monan = an.substr(0,4-monan.length) + monan;
	}
	return monjour + sep + monmois + sep + monan;
}
function hauteur_ecran_disponible()
{
	if(navigator.appName=='Microsoft Internet Explorer')
		return document.body.clientHeight;
	else
		return window.innerHeight;
}
function largeur_ecran_disponible()
{
	if(navigator.appName=='Microsoft Internet Explorer')
		return document.body.clientWidth;
	else
		return window.innerWidth;
}
function centrer_div(div,horiz,verti)
{
	if(typeof(horiz)=='undefined')
		var horiz = true;
	if(typeof(verti)=='undefined')
		var verti = true;
	if(verti)
	{
		var h_dispo = hauteur_ecran_disponible();
		var h = get_height(div);
		div.style.top = Math.floor((h_dispo - h)/2) + 'px';	
	}
	if(horiz)
	{
		var w_dispo = largeur_ecran_disponible();
		var w = get_width(div);
		div.style.left = Math.floor((w_dispo - w)/2) + 'px';
	}
}
function get_width(element)
{
	return element.clientWidth;
}
function get_height(element)
{
	return element.clientHeight;
}
function getSousElementsByClassName(elem,className,tag)
{
	if(typeof(tag)=='undefined')
		var tags = elem.getElementsByTagName('*');
	else
		var tags = elem.getElementsByTagName(tag);
	var tab = new Array();
	for(var j=0;j<tags.length;++j)
	{
		if(hasClass(tags[j],className))
			tab.push(tags[j]);
	}
	return tab;
}
function js_replace(from,to,value)
{
	if(value.length > 0)
	{
		while(value.indexOf(from) >= 0)
			value = value.replace(from,to);
		return value;
	}
	return value;
}
function no_cache()
{
	var tab_input = document.getElementsByTagName('input');
	for(var i=0;i<tab_input.length;i++)
		tab_input[i].setAttribute('autocomplete','off');
	
	try{
		maj_multi_image_form_edition();
	}
	catch(eee){
		
	}
}
function checkall(value,classe)
{
	var tab_cb = getSousElementsByClassName(document,classe,'input');
	for(var i=0;i<tab_cb.length;i++)
		tab_cb[i].checked = value;
}
function montre_masque()
{
	if(ID('masque'))
		ID('masque').style.display = 'block';
}
function cache_masque()
{
	if(ID('masque'))
		ID('masque').style.display = 'none';
}
function clic_masque()
{
	if(filtre)
		filtre_fermer();
}
function ouvre_popup(url,titre,w,h)
{
	if(typeof(w)=='undefined')
		w = 500;
	if(typeof(h)=='undefined')
		h = 350;
	
	window.open(url,titre,config='height='+h+', width='+w+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}
function redirection(url)
{
	window.location.href = url;
}
function in_array(elem,tab)
{
	if(tab.length)
	{
		for(var i=0;i<tab.length;i++)
		{
			if(tab[i] == elem)
				return true;
		}
		return false;
	}
	return false;
}
function is_numeric(str)
{
	var exp = new RegExp("^[0-9]*$","g");
	return exp.test(str);
}
function display_block(elem)
{
	if(elem.tagName == 'TH' || elem.tagName == 'TD')
		elem.style.display = '';
	else
		elem.style.display = 'block';
}
// ------------------------------------------------
// Clavier ----------------------------------------
function clavier(e)
{
	if(window.event)
		e = window.event;
	
	var key = window.event?e.keyCode:e.which;
	if(key == 16 || key == 17 || key == 18)
		return true;
	
	return event_clavier(key);
}
function event_clavier(key)
{
	// ECHAP : Fermeture lightbox et videoplayer
	if(key == 27)
	{
		var stop = false;
		if(ID('lightbox'))
		{
			if(ID('lightbox').style.display != 'none')
			{
				myLightbox.end();
				stop = true;
			}
		}
		if(ID('videoplayer'))
		{
			if(ID('videoplayer').style.display != 'none')
			{
				videoplayer_fermer();
				stop = true;
			}
		}
		if(stop)
			return false;
	}
	return true;
}
function editeurs_init()
{
	var module = '';
	
	var tab_editeurs = getSousElementsByClassName(document.body,'editeur','DIV');
	if(tab_editeurs.length)
	{
		for(var i=0;i<tab_editeurs.length;i++)
		{
			try
			{
				var textarea = tab_editeurs[i].getElementsByTagName('TEXTAREA')[0];
				var id_textarea = textarea.id;
				
				if(ID('cle_module'))
					module = ID('cle_module').value;
				
				var editeur_type = js_replace('editeur yui-skin-sam ','',textarea.parentNode.className);
				
				YAHOO.widget.Logger.enableBrowserConsole();
				
				if(editeur_type == 'mini')
				{
					var myEditor = new YAHOO.widget.Editor(id_textarea,
					{	height:'200px',
						width:'650px',
						dompath:true,
						animate:true,
						toolbar: {
							buttons: 
							[
							    {group: 'textstyle', label: '&nbsp;',
							        buttons: 
									  [
							            { type: 'push', label: 'Gras', value: 'bold' },
							            { type: 'push', label: 'Italique', value: 'italic' },
							            { type: 'push', label: 'Souligné', value: 'underline' },
							            { type: 'separator' },
							            { type: 'select', label: 'Arial', value: 'fontname', disabled: true,
							                menu: [
							                    { text: 'Arial', checked: true },
							                    { text: 'Arial Black' },
							                    { text: 'Comic Sans MS' },
							                    { text: 'Courier New' },
							                    { text: 'Lucida Console' },
							                    { text: 'Tahoma' },
							                    { text: 'Times New Roman' },
							                    { text: 'Trebuchet MS' },
							                    { text: 'Verdana' }
							                ]
							            },
							            { type: 'spin', label: '13', value: 'fontsize', range: [ 9, 75 ], disabled: true },
							            { type: 'separator' },
							            { type: 'color', label: 'Couleur de la police', value: 'forecolor', disabled: true },
							            { type: 'color', label: 'Couleur de surlignage', value: 'backcolor', disabled: true }
							        ]
							    },
								 { group: 'alignment', label: '&nbsp;', 
							        buttons: [ 
							            { type: 'push', label: 'A gauche CTRL + SHIFT + [', value: 'justifyleft' }, 
							            { type: 'push', label: 'Centré CTRL + SHIFT + |', value: 'justifycenter' }, 
							            { type: 'push', label: 'A droite CTRL + SHIFT + ]', value: 'justifyright' }, 
						            { type: 'push', label: 'justifié', value: 'justifyfull' } 
							        ] 
							    },  
								{ group: 'indentlist', label: '&nbsp;', 
								   buttons: [ 
								    { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' }, 
								      { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' },
								      { type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true }
								   ] 
								}
							]
						}
					});
				}
				else
				{
					// Editeur standard
					var myEditor = new YAHOO.widget.Editor(id_textarea,{height:'300px',width:'650px',dompath:true,animate:true});
				}
					
				yuiImgUploader(myEditor,id_textarea,'index.php?upload=1&module='+module,'image','index.php?resize=1&module='+module);
				myEditor.render();
				
				editeurs.push(myEditor);
			}
			catch(e){}
		}
	}
}
function no_tabindex()
{
	var tab_no_tabindex = getSousElementsByClassName(document.body,'no_tabindex');
	for(var i=0;i<tab_no_tabindex.length;i++)
		tab_no_tabindex[i].setAttribute('tabindex','-1');
}
function recherche(str)
{
	indice = detect.indexOf(str) + 1;
	last_recherche = str;
	return indice;
}
function donne_focus()
{
	if(ID('form_login_admin'))
	{
		if(ID('admin_login').value.length)
			ID('admin_password').focus();
		else
			ID('admin_login').focus();
	}
		
}
function charge_onchange()
{
	var tab_onchange = getSousElementsByClassName(document.body,'onchange','div');
	for(var i=0;i<tab_onchange.length;i++)
	{
		var elem_select = tab_onchange[i].getElementsByTagName('SELECT')[0];
		elem_select.onchange();
	}
}
function garde(str,chars,length)
{
	if(!str.length)
		return '';
	
	if(typeof(chars)=='undefined')
		var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-';
	
	if(typeof(length)=='undefined')
		var length = -1;
	
	if(length >= 0)
		str = str.substring(0,length);
	
	var t = new Array;
	for(i=0;i<chars.length;i++)
		t.push(chars[i]);
	
	var ret = new Array;
	for(var i=0;i<str.length;i++)
	{
		if(in_array(str[i],t))
			ret.push(str[i]);
	}
	return ret.join('');
}
function no_accents(str)
{
	var from = 'à,â,ä,á,ã,å,î,ï,ì,í,ô,ö,ò,ó,õ,ø,ù,û,ü,ú,é,è,ê,ë,ç,ÿ,ñ,À,Â,Ä,Á,Ã,Å,Î,Ï,Ì,Í,Ô,Ö,Ò,Ó,Õ,Ø,Ù,Û,Ü,Ú,É,È,Ê,Ë,Ç,Ÿ,Ñ';
	from = from.split(',');
	
	var to = 'a,a,a,a,a,a,i,i,i,i,o,o,o,o,o,o,u,u,u,u,e,e,e,e,c,y,n,A,A,A,A,A,A,I,I,I,I,O,O,O,O,O,O,U,U,U,U,E,E,E,E,C,Y,N';
	to = to.split(',');
	
	for(var i=0;i<from.length;i++)
		str = js_replace(from[i],to[i],str);
	
	return str;
}
function espacetirets(str)
{
	str = js_replace('  ',' ',str);
	str = js_replace(' ','-',str);
	str = js_replace('--','-',str);
	
	return str;
}
function trim(val,carac)
{
	if(val.length)
	{
		while(val[0] == carac)
			val = val.subString(1);
	}	
	if(val.length)
	{
		while(val[val.length - 1] == carac)
			val = val.substring(0,val.length - 1);
	}
	
	return val;
}
function focus_recherche(champ)
{
	if(champ.value == val_champ_recherche)
		champ.value = '';
}
function blur_recherche(champ)
{
	if(champ.value == '')
		champ.value = val_champ_recherche;
}
function get_style(elem,attribut)
{
	if(typeof(elem)!='undefined' && elem != 0)
	{
		if(mon_navigateur=='IE')
			return eval('elem.currentStyle.'+attribut+';');
		else
			return eval('getComputedStyle(elem,null).'+attribut+';');
	}
	else
		return '';
}
function nopx(str)
{
	if(str.length > 2)
	{
		var fin = str.substring(str.length-2,str.length);
		if(fin == 'px' || fin == 'pt')
			return parseInt(str.substring(0,str.length-2));
		else
			return str;
	}
	else return str;
}
function open_admin_barre()
{
	ID('admin_barre_closed').style.display = 'none';
	ID('admin_barre_open').style.display = 'block';
}
function close_admin_barre()
{
	ID('admin_barre_open').style.display = 'none';
	ID('admin_barre_closed').style.display = 'block';
}
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// ---------------------------------------------------------------
function load()
{
	// Affiche le contenu de la page
	ID('affichage').style.display = 'block';
	
	// Vide le cache des formulaires
	no_cache();
	
	// Affiche les éditeurs
	editeurs_init();
	
	// Enlève les tabindex sur les éléments qui n'ont pas à en avoir
	no_tabindex();
	
	// Focus()
	donne_focus();
	
	// Execute le onchange des champs disposant d'une fonction onchange
	charge_onchange();
	
	// Instanciation d'un ajax.
	//obj_ajax = cree_ajax();
	
	// onload du projet
	projet_load();
}
function imprimer()
{
	try{
		window.print();
	}
	catch(e)
	{
		alert('Cette fonctionnalité n\'est pas utilisable directement avec votre navigateur.');
	}
}
