var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName = "Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName = "MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName = "UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName = "Linux";

var SEPARATORS = Array();
SEPARATORS["Windows"] = "\\";
SEPARATORS["MacOS"] = "/";
SEPARATORS["UNIX"] = "/";
SEPARATORS["Linux"] = "/";
var DIR_SEP = SEPARATORS[OSName];

function docload(){MM_findObj('WaitLayer').style.visibility = 'hidden';}
function docunload(){ MM_findObj('WaitLayer').style.visibility = 'visible';}

/*********************************/
/* FONCTIONS JAVASCRIPT COMMUNES */
/*********************************/


function checkFields(form){
	if(!checkString(form.mylog.value)) {
		alert("Veuillez saisir votre nom");
	}
	else {
		if(!checkString(form.mypass.value)){
			alert("Veuillez saisir votre mot de passe");
		}
		else {
			form.submit();
		}
	}
}
    
function checkString(entry) 
{
   for(var i = 0; i < entry.length; i++)
   {if (entry.charAt(i) != " ") {return true;}}
   return false;
}

//* Fonctions de pop up

function pop(url, param)
{
	if (url){
		var jour = new Date();
		var name = 'pop_'+jour.getHours()+''+jour.getMinutes()+''+jour.getSeconds();
	
		eval(name+" = window.open('"+url+"','"+name+"','"+param+"');");

  		eval("setTimeout('"+name+".focus();',250);");
  		if(pop.arguments.length == 2){return eval(name);}
	}
}

function popExpo(){
	pop('../approfondir/expo-photo.asp', 'width='+screen.width+', height='+(screen.height*0.94)+', top=0, left=0');
}


// Object renvoi l'object n du document
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MyClose(){
	if (window.opener != null){
		var ifra = GetVal('ifraRef');
		if(ifra != false){
			MM_findObj(ifra, window.opener).document.location.reload();
		}else{
			window.opener.document.location.reload();
		}
		window.opener.focus();
	}
	self.close();
}


function popAssoMul(url, w, h, Ifra){
	url = BuildUrl(url, 'vals', getLcheck( eval(Ifra+ ".document.forms[0]") ));
	newpop = pop('../../libs/popAssoMul.asp?'+ url + '&popw='+ w +'&poph='+h, 'width='+ w +', height='+ h);
	
}

function popAssoMulReturn(DistIfra){
	vals = getLcheck(ifra.document.forms[0]);
	var url = ifra.document.location + '';
	

	var act_url = window.top.opener.MM_findObj("i"+ DistIfra, window.top.opener).document.location + '';
	var act_vals = getLcheck(window.top.opener.MM_findObj("i"+ DistIfra, window.top.opener).document.forms[0]);

	var full_vals = vals;

	if (act_vals != ''){
		full_vals = act_vals +':'+ vals;
	}


	if(GetVal('type_media', url) != GetVal('type_media', act_url)){
		act_url = BuildUrl(url, 'type_media', 0);
	}

	url = BuildUrl(act_url, 'vals', full_vals);
	url = BuildUrl(url, 'pop', 0);
	window.top.opener.MM_findObj("i"+ DistIfra, window.top.opener).document.location.href = url ;

	window.top.opener.focus();
	self.close();

}

// Fonctions des trims
function strim(val, crt){
  var txt = val;
  while (txt.substr(0, 1) == crt) {txt = txt.substr(1, txt.length);}
  while (txt.substr(txt.length-1, 1) == crt) {txt = txt.substr(0, txt.length-1);}

  return txt;
}
function trim(val){return  strim(val, " ");} // un simple alias ?



var TR_COLOR_BUFFER = Array;
function TRhl(obj, p){
	if(p){
		TR_COLOR_BUFFER[obj.id] = obj.style.backgroundColor;
		obj.style.backgroundColor = p;
	}
	else{
		obj.style.backgroundColor = TR_COLOR_BUFFER[obj.id];
	}

}


/**** FONCTIONS POUR LES TABLEAUX *****/

// Booleen : Vérifit si la valeur [val] est présente dans le tableau [l]
function inList(l, val){
  for (var i=0; i < l.length; i++)
   {
	  if (l[i].toUpperCase()==val.toUpperCase()) {return true; break;}
   }
  return false;
}

//  ?
function isExist(id, li, nval, oval){
	inl = inList(li, nval);

	if ((inl) && (id == 0)) {return true;}
	if ((inl) && (id > 0) && ((nval).toUpperCase() != (oval).toUpperCase())) {return true;}
	return false;
}

// Cherche la valeur val dans le tableau tab et renvoi son index, renvoi false si la valeur n'est pas trouvée
function in_array(val, tab){
	for(var i=0; i<tab.length; i++){
		if(tab[i] == val){
			return i;
		}
	}
return false;
}

// booleen : Vérifit que la clef key existe dans le tableau tab
function is_array_key(key, tab){
	if(tab[key] != undefined) {
		return true;
	}
	else{
		return false;
	}
}
/*********************************/


function nospam(adresse){
	adresse = adresse.replace(SPAM_AT, '@');
	while(adresse.indexOf(SPAM_DOT) > -1){	adresse = adresse.replace(SPAM_DOT, '.'); }

//	adresse_aff = truncateBR(adresse, 40);
	adresse_aff = adresse;

	document.open();
	document.write(adresse_aff.link('mailto:'+adresse));
	document.close();
}



/**************** URLs ***********/

// Retourne l'url [Qstr] en attribuant [val] à [champ]
function BuildUrl(Qstr, champ, val){
	if(typeof(param = GetVal(champ, Qstr)) != 'boolean'){
		return Qstr.replace(champ+'='+param, champ+'='+val);
	}
	else{
		var sep = '?';
		if(Qstr.indexOf(sep)>-1){
			sep = '&';
		}
	return Qstr+=sep+champ+'='+val;
	}
}


/*
Retourne la valeur de [val] passé en GET
False si la variable n'existe pas
*/
function GetVal(val){
	args = GetVal.arguments;
	if(args.length > 1){
		var Turl = args[1];
	}
	else{
		var Turl = window.location+'';
	}
	var deb = Turl.indexOf('&'+val+'=');
	if(deb == -1){
		var deb = Turl.indexOf('?'+val+'=');
	}


	var fin = 0;
	if(deb > -1)	{
		var sub1 = Turl.substring(deb+1,Turl.length+1)
		fin	 = sub1.indexOf('&');
		if (fin == -1)	fin = sub1.length+1;
		sub2 = sub1.substring(0,fin)
		return sub2.replace(val+'=','');
	}
	else {
		return false;
	}
}

function getLcheck(ifra){
	tth = "";
	for (var i = 0; i < ifra.elements.length; i++){
	  if ( (ifra.elements[i].type == 'checkbox') && (ifra.elements[i].name == 'ccase') )
	   {if (ifra.elements[i].checked) {tth += ifra.elements[i].value + ":";}}
	}
	
	if ((tth).length > 1) {tth = (tth).substr(0, (tth).length-1);}

	return tth;
}



function getLcheck2(ifra){
	tth = "";
	for (var i = 0; i < ifra.elements.length; i++){
	  if ( (ifra.elements[i].type == 'checkbox') && (ifra.elements[i].name == 'ccase') )
	   {if (ifra.elements[i].checked) {tth += ifra.elements[i].value + "," + ifra.elements[i].id + ":";}}
	}
	
	if ((tth).length > 1) {tth = (tth).substr(0, (tth).length-1);}

	return tth;
}


function getLcheck_Niveau(ifra){
	tth = '';
	ttn = '';
	sep = '';
	for (var i = 0; i < ifra.elements.length; i++){
	  if ( (ifra.elements[i].type == 'checkbox') && (ifra.elements[i].name == 'ccase') ){
		  if (ifra.elements[i].checked) {
			  tth += sep + ifra.elements[i].value;
			  if (radio = eval('ifra.id_xtra_statut_'+ ifra.elements[i].value)){
				 for (k=0; k<radio.length; k++){
					  if (radio[k].checked){
						  ttn += sep + radio[k].value;
					  }
				  }
			  }else
			  {
				  ttn += sep + 0;
			  }
			  sep = ':';
			  }
		  }
	}
	MM_findObj('id_xtra_statut').value = ttn;


	return tth;
}

/*********************************/



/******* CALQUES DHTML **********/

var CALQUE_UTILISE = false;
function rempli_calque(obj_name, statut){
	obj = MM_findObj(obj_name);
	display = MM_findObj('langFields');
	
	if(CALQUE_UTILISE){
		backup = MM_findObj(CALQUE_UTILISE);
		backup.innerHTML = display.innerHTML;
		display.innerHTML = '';
	}
	
	display.innerHTML = obj.innerHTML;
	obj.innerHTML = '';
	
	highlilght_onglet(obj.id, statut);
	masque_autres_calques(obj_name, statut, obj.className);
	
	CALQUE_UTILISE = obj.id;

}


function coche_fils(obj){
	var id = obj.value;
	var idz = Array(id);
	var parent = obj.id;
	
	for(var i=0; i<document.forms[0].elements.length; i++){
		elem = document.forms[0].elements[i];
		if(inList(idz, elem.id)){
			idz[idz.length] = elem.value;
			elem.checked = obj.checked;
		}
	}
}

function groupes_onlclick(obj){
	calque_id = 'droit_'+ obj.value;
	if (obj.checked){
		GenShow(calque_id);
	}
	else{
		GenHide(calque_id);
	}
}

function GenShow(calque_id){
	MM_findObj(calque_id).style.display = 'block';
}

function GenHide(calque_id){
	MM_findObj(calque_id).style.display = 'none';
}

function affiche_calque(obj_name, statut){

	obj = MM_findObj(obj_name)

	if(statut){
		obj.style.visibility = 'visible';
		obj.style.height = 'auto';
		highlilght_onglet(obj.id, statut);
	}
	else{
		obj.style.visibility = 'hidden';
		obj.style.height = '0px';
		highlilght_onglet(obj.id, statut);
	}
	masque_autres_calques(obj_name, statut, obj.className);

}

var	contName = 'conteneur_';
var ongName = 'onglet_';

function masque_autres_calques(obj_name, statut, className){
	if(statut){
		Mobj = MM_findObj(obj_name)
		oType = Mobj.tagName;
		Trubrique = Mobj.id.split('_');
		rubrique = Trubrique[1];
		Wanted_indexOf = contName.length;

		similars = document.getElementsByTagName(oType);

		for(i=0; i<similars.length; i++){
			if(similars[i].className == className && similars[i].id != Mobj.id && similars[i].id.indexOf(rubrique) == Wanted_indexOf){
				affiche_calque(similars[i].id, !statut);
			}
		}
	
	}
}

highlight_colors = Array('FFEEEE', 'FFFFF0');
function highlilght_onglet(id, statut){
	id = id.replace(contName, ongName);
	onglet = MM_findObj(id);
	color = (statut)? 0 : 1 ;
	onglet.style.backgroundColor = '#'+ highlight_colors[color];
}


function OpenPrevIfra(ifra, url){
		mIfra = MM_findObj(ifra);
		if(mIfra.src == ''){
			mIfra.src = url;
		}
		mIfra.style.height = '300px';
}


function ClosePrevIfra(ifra){
		mIfra = MM_findObj(ifra);
		mIfra.style.height = '0px';
}


function affiche_calque_dyn(objClick, obj_name){

	statut = (objClick.alt == 'ouvrir')? true : false;
	affiche_calque(obj_name, statut);
	if (statut){
		Nalt = 'fermer'
		Nsrc = objClick.src.replace('_0', '_1');
	}
	else{
		Nalt = 'ouvrir';
		Nsrc = objClick.src.replace('_1', '_0');
	}

	objClick.src = Nsrc;
	objClick.alt = Nalt;
}

/*********************************/
