// personal functions E.MILY - 2008 ----------------------------------------------------------------------------------------
function change_class(id, class)
{
document.getElementById(id).className=class;
}


function verif_formu(id_form) 
{
// message displayed in case of error
var msg_err = new Array;
msg_err["minsize"] = "Veuillez remplir le champ";
msg_err["carmust"] = "le contenu du champ est invalide";

 	for (var l=0; l<document.getElementById(id_form).elements.length; l++) 
	{
	var nom_champ = document.getElementById(id_form).elements[l].name;
	
	if(document.getElementById(id_form).elements[l].type == "select-one" && document.getElementById(nom_champ).title != "")
	var regle = document.getElementById(nom_champ).title;
		else if(document.getElementById(id_form).elements[l].type == "text" && document.getElementById(nom_champ).alt != "")
		var regle = document.getElementById(nom_champ).alt;
			else regle = 0;

	if(regle != 0) {
   	
	var contenu_champ = document.getElementById(nom_champ).value;
	var tableau_regle = regle.split(';');


		// we check all the rules for the input
		for (var i=0; i < tableau_regle.length; ++i) 
		{
		
			var rule = tableau_regle[i].split('=');
			var attribut = rule[0];
			var valeur =  rule[1];
			
			switch (attribut) 
			{
			case "minsize":
			// verifying the size of the input
			if(document.getElementById(nom_champ).value.length < valeur) {
			alert(msg_err["minsize"] + " " + nom_champ);
			var err = 1;
			}
			
			break;
			
			case "carmust":
			// verifying the content of the input
			if(valeur == "@") var myregexp = /.@/;
			
			if(document.getElementById(nom_champ).value.search(myregexp) == -1 ) {
			alert(nom_champ + ' : ' + msg_err["carmust"]);
			var err = 1;
			}
			break;
		
		} // end for rules
		
		}
			
	} // end for input

   
   
}

// validation of the form if there is no error
if(err != 1) {
document.getElementById(id_form).submit();
error_form = 0;
}
else error_form = 1;

}

function load_movie(url, id_p, w, h, url_player)
{
	var FO = {	movie:""+ url_player +"",width:""+ w +"",height:""+ h +"",majorversion:"7",build:"0",bgcolor:"#FFFFFF",allowfullscreen:"true",
		flashvars:"file="+ url +"&autostart=true" };
	
	UFO.create(FO, id_p);
	
}


function replace_(type, src, dest)
{
	// verifyng the type of source, imput field or direct string char
	if(type == "string")
	var val = src;
		else if(type == "input")
		var val = document.getElementById(src).value;
	
	//replace, verifing if the field is empty or not
	if (!document.getElementById(dest).value)
	document.getElementById(dest).value = val;
	else document.getElementById(dest).value = document.getElementById(dest).value + ";" + val;


}

function afficheaide(description, id)
// display a help message
{
	if (description == 0)
	document.getElementById(id).innerHTML = "&nbsp;";
	else document.getElementById(id).innerHTML = description;
}


function change_size(id, taille) {
// change the size of the text
var taille_test = taille_texte + taille;

if(taille == 0) {
taille_texte = 0;
var style_taille = "texte" + taille_texte;
document.getElementById(id).className=style_taille;
}
else {

		if(taille_test >= -2 && taille_test <= 5) 
		{ 
			taille_texte = taille_texte + taille;
			var style_taille = "texte" + taille_texte;
			document.getElementById(id).className=style_taille;
		}
	}
}

function g_menu(id2cache, id2see) {
// keep in a global variable the id of the div which is on view
id2see_now = id2see;

cacher(id2cache); 
cacher(id2see);
}

function opened_menu(id_element) 
{
	
	if(element_opened != 0) 
	{
	var lvl1_opened = "element_lvl1_" + element_opened;
	document.getElementById(lvl1_opened).className="";
	}

	element_opened = id_element;
	document.getElementById("element_lvl1_" + element_opened).className='menu_lvl1_selected';

}


function retour() {
window.history.back();
}

function init_menu(id2cache) {
// keep in a global variable the id of the div which is on view
id2see_now = 'sm' + id2cache;
taille_texte = 0;
element_opened = id2cache;
news_selected = "list_news_1";
}

function select_news(id2show) 
{
document.getElementById(news_selected).style.backgroundImage='url(/images/gfx/list_news.png)';
document.getElementById(id2show).style.backgroundImage='url(/images/gfx/list_news_hover.png)';
news_selected = id2show;
}

function confirmation(nom, redirection) {
co = confirm('Voulez vous vraiment supprimer ' + nom + '?');
if (co == true) 
window.location.href = redirection;
}

function count_string(longeur_max, id_champ, id_replace){
// we count the numbers of chars in a field
var taille = document.getElementById(id_champ).value.length;
document.getElementById(id_replace).innerHTML = taille;
}

function ouverture_url(url)
{
window.location.href = url;
}


function cacher(num){
  // On ouvre
  
  if (document.getElementById(num).style.display == "")
  document.getElementById(num).style.display = "none";
  	else if (document.getElementById(num).style.display == "none")
  	document.getElementById(num).style.display = "";

}

function store(id) {

if(Element.visible(id))
new Effect.BlindUp(id);
else new Effect.BlindDown(id);

}

function show_div(id)
{
	//passage en gris du fond
	if (document.getElementById('body').className != "grised")
    document.getElementById('body').className = "grised";
	else document.getElementById('body').className = "";

	//affichage du module correspondant
	if(document.getElementById(id).style.visibility=="hidden" || document.getElementById(id).style.visibility=="")
	document.getElementById(id).style.visibility = "visible";
	else if(document.getElementById(id).style.visibility=="visible")
	document.getElementById(id).style.visibility = "hidden";  
}

function vide(id, defaut_val)
{
	if(document.getElementById(id).value == defaut_val)
	document.getElementById(id).value = '';
	
}

function init_research(event)
{
	// getting the key
	var key = recup_key(event);
	if(document.getElementById('search').value.length > 2 && key == 13) // more than 2 caracters and key press = Enter
	ajax_func('GET', 1, 'recherche', 'centre', 'div', '', 'search', '', '', '');
}

function recup_key(event) {
e= event;
// Booléen reconnaissant le navigateur 
  isIE = (document.all) 
  isNN6 = (!isIE) && (document.getElementById)

  // Compatibilité : l'objet menu est détecté selon le navigateur
  if (isIE) { var lettre = String.fromCharCode(event.keyCode); var code = event.keyCode; }
  if (isNN6) { var lettre = String.fromCharCode(event.which); var code = event.which; }

return code;
}

function progression() {

	if(error_form != 1) {
	ajax_func('GET', 0, 'recup_taille', 'upload_statut', 'div', '', '', '', '', '');
	setTimeout("progression();",100);
	}
}

function reload_webcam () {
	
ajax_func('GET', 1, 'reload_webcam', 'webcam_big', 'div', '', '', '', '', '');
setTimeout("reload_webcam();",12000);
}

function ajax_func(method, second, what, id, type, src_replace, id_val1, id_val2, id_val3, id_val4)
// AJAX FUNCTION - MIL - www.mil-orages.com

	{
		var req = null; 
		var method;
		var second;
		
		if (id_val1 != ""){
			if(document.getElementById(id_val1))
			var val1 = document.getElementById(id_val1).value;
			else var val1 = id_val1;
		}
		else var val = "";
		
		if (id_val2 != "") {
			if(document.getElementById(id_val2))
			var val2 = document.getElementById(id_val2).value;
			else var val2 = id_val2;
		}
		else var val2 = "";
		
		if (id_val3 != "") {
			if(document.getElementById(id_val3))
			var val3 = document.getElementById(id_val3).value;
			else var val3 = id_val3;
		}
		else var val3 = "";

		if (id_val4 != "") {
			if(document.getElementById(id_val4))
			var val4 = document.getElementById(id_val4).value;
			else var val4 = id_val4;
		}
		else var val4 = "";
		
		//ci dessous le test pour connaitre le navigateur et créer l'objet activex
		if (window.XMLHttpRequest)
		{
 			req = new XMLHttpRequest();
			if (req.overrideMimeType) 
			{
				req.overrideMimeType('text/html');
			}
		} 
		else if (window.ActiveXObject) 
		{
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
			{
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
        	}
			// fin du test

		req.onreadystatechange = function()
		{ 
			// message waiting
			if(second != 0)
			document.getElementById(id).innerHTML = "<span class='noir_petit'>Chargement...</span><br /><img src='images/gfx/loading.gif' alt='loading' />";
			
			if(req.readyState == 4) //signifie que la réponse est envoyée par le serveur et disponible
			{
				if(req.status == 200) //status signifie ok
				{
					//document.ajax.dyn.value= req.responseText;	
					if(type == 'champ')
					document.getElementById(id).value = req.responseText;
					else if(type == 'div')
					document.getElementById(id).innerHTML = req.responseText;
					
				}	
				else	
				{
					document.ajax.dyn.value="Error: returned status code " + req.status + " " + req.statusText;
				}	
			} 
		}; 
		
		if(method == "GET") {
		req.open("GET", "ajax_req.php?second=" + second +"&val1=" +val1 + "&val2=" +val2 + "&val3=" +val3 + "&val4=" +val4 +"&what=" +what, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		req.send(null); 
		}
		else if(method == "POST"){
		req.open("POST", "ajax_req.php", true);
		var corps = "second=" + second +"&val1=" +val1 + "&val2=" +val2 + "&val3=" +val3 + "&val4=" +val4 +"&what=" +what;
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
  		req.send(corps);
		}
		
	} 
