var ATF = new Object();
ATF.version = function () {
	alert("5 !");
}

/* Appelle un template Smarty, pour le mettre dans un ou plusieurs div 
	@param url
	@param post Données à poster
	@param infos Paramètres supplémentaires
*/
ATF.tpl2div = function (url,post,infos) {
	if (!infos) {
		infos = new Object();	
	}
	if (!infos.onComplete) {
		infos.onComplete = __ajax_refresh;	
	}
	infos.url = url;		
	infos.data = post;		
	__ajax_generic(infos);
	if (url!='tpl2div.ajax') {
		location.hash = url;
	}
}


function ajax_make_xhr() {
	var xhr_object = null; 
	if(window.XMLHttpRequest) // Firefox 
	  xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	  return; 
	} 
	return xhr_object;
}

//Permet d'initialiser les valeurs des champs choisis.
function __ajax_set_value(obj) { 
	if (obj.result!='') {
		//Détection de div ou non.
		if (String(document.getElementById(obj.infos.target)).match("Div")) {
			document.getElementById(obj.infos.target).innerHTML=obj.result;
		} else {
			document.getElementById(obj.infos.target).value=obj.result;
		}
	} else {
		document.getElementById(obj.infos.target).value="";
	}
}

function ajax(f,libelle1,libelle2) {
	var index = libelle1.selectedIndex; 
	if (libelle2.type == 'select-one' || libelle2.type == 'select-multiple') {
		if(index < 1) 
			libelle2.options.length = 0; 
		else { 
			xhr = ajax_make_xhr();	
			xhr.open("POST", "ajax/ajax.php", true); 		 
			xhr.onreadystatechange = function() { 
				if(xhr.readyState == 4) {
					eval(xhr.responseText);
				}
			} 
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
			var data = libelle1.name+"="+escape(libelle1.options[index].value)+"&form="+f.name+"&element="+libelle2.name; 
			xhr.send(data); 
		} 
	} else {
		xhr = ajax_make_xhr();	
		xhr.open("POST", "ajax/ajax.php", true); 		 
		xhr.onreadystatechange = function() { 
			if(xhr.readyState == 4) {
				eval(xhr.responseText);
			}
		} 
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
		var data = libelle1.name+"="+escape(libelle1.options[index].value)+"&form="+f.name+"&element="+libelle2; 
		xhr.send(data); 
	}
}

function __ajax_left(obj) { /* Partie de gauche, récents, quick-insert et searchform */
	document.getElementById('left').innerHTML=obj.html;
}
function __ajax_left_update(table) {
	__ajax_generic({'url':'generic.php?event=template&template=left&method=ajax&table='+table,'onComplete':__ajax_left,'data':'rien=rien'});
}

function __ajax_template(obj) { /* Appeler une nouvelle rubrique */
	document.getElementById('left').innerHTML=obj.left;
	document.getElementById('top').innerHTML=obj.top;
	document.getElementById('main').innerHTML=obj.main;
}
function __ajax_template_cleodis(obj) { /* Appeler une nouvelle rubrique */
	document.getElementById(obj.infos.target).innerHTML=obj.html;
}
function __ajax_template_update(table) {
	__ajax_generic({'url':'generic.php?event=template&method=ajax&table='+table,'onComplete':__ajax_template,'data':'rien=rien'});
}

function __ajax_options(obj) {
	var e = document.getElementById(obj.infos.id_html_options);
	e.options.length = 0;
	e.options[e.options.length] = new Option("Choisissez : ");
	for (var key in obj) {
		if (key=="infos") continue;
		e.options[e.options.length] = new Option(unescape(obj[key]),key);	
	}
//	alert (e.options.length);
	if (e.options.length <= 1) {
		e.options.length = 0;
		e.options[e.options.length] = new Option("Aucun enregistrement");
	}
}

function __ajax_select_once(obj){
	for (var i=0;i<obj.infos.fields.length;i++) {
		document.getElementById(obj.infos.fields[i]).value = obj.data[obj.infos.fields[i]];	
	}
}

function __ajax_display(obj) {
	var e = document.getElementById(obj.infos.target);
	e.innerHTML = obj.html;
	//alert(obj.html.extractScripts()+'__ajax_display');
	return nd();
	obj.html.evalScripts();
}

function wait_on() {
	window.status = "veuillez_patienter";
	document.body.style.cursor = "wait";	
}

function wait_off() {
	window.status = "chargement_termine";
	document.body.style.cursor = "default";
}

function __ajax_generic(infos) {
	var xhr = ajax_make_xhr();
	if (infos.method) {
		var method = infos.method;
	} else {
		var method = 'POST';
	}
	xhr.open(method, infos.url, true);
	xhr.onreadystatechange = function() {
		if (!typeof(xhr)) return;
		if(xhr.readyState == 4) {
			wait_off();
			if (xhr.status == 200) {
				if (obj = eval(xhr.responseText)) {
					obj.infos=infos;
					if (infos.onComplete) {
						infos.onComplete(obj);
					}
				}
			} else {
				//alert("Une Erreur "+xhr.status+" s'est produite !");	
			}
		}
	}
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	wait_on();
	xhr.send(infos.data); 
}

function __ajax_autocomplete(obj) {
	var e = document.getElementById(obj.infos.id+"_options");
	if (obj.nb>0) {
		e.innerHTML = obj.html;
	} else {
		e.innerHTML = "";
	}
	__div_cloak(obj.infos.id+"_options",obj.nb==0);
}

function __ajax_autocomplete_countdowntimer(cmd, ms) {
    this.cmd = cmd;
    this.ms = ms;
    this.tp = 0;
}

__ajax_autocomplete_countdowntimer.prototype.start = function(prefix) {
    if (this.tp > 0) this.reset();
    this.tp = window.setTimeout(this.cmd.replace("this.value","'"+prefix+"'"), this.ms);
}

__ajax_autocomplete_countdowntimer.prototype.reset = function() {
    if (this.tp > 0) window.clearTimeout(this.tp);
    this.tp = 0;
}

// New code Optime
function __div_cloak(id_div,bool) {
	if (bool===true || bool===false) {
		if (bool) {
			document.getElementById(id_div).className='cloak';
		} else {
			document.getElementById(id_div).className='uncloak';
		}
	} else {
		if (document.getElementById(id_div).className=='uncloak') {
			document.getElementById(id_div).className='cloak';
		} else {
			document.getElementById(id_div).className='uncloak';
		}
	}
}

__ajax_refresh = function(obj) {	
	if (obj.explorer) {
		/* Execution d'une commande pour explorer */
		eval(obj.explorer);
	}
	
	if (obj.alert) {
		/* Si un message d'alerte est passé en retour */
		alert(obj.alert);
	}

	if (obj.infos.id) {
		/* Si demandé dans l'énoncé, on rafraichi l'un des cadre */
		cadre_refresh(obj.infos.id);
	}
	if (obj.cadre_refreshed) {
		for (var it in obj.cadre_refreshed) {
			if ($(it+"_data")) {
				$(it+"_data").update(obj.cadre_refreshed[it]);
				//document.getElementById(it+"_data").innerHTML=obj.cadre_refreshed[it];
//console.log(obj.cadre_refreshed[it].evalScripts());
			} else if ($(it)) {
				$(it).update(obj.cadre_refreshed[it]);
				//document.getElementById(it+"_data").innerHTML=obj.cadre_refreshed[it];
//console.log(obj.cadre_refreshed[it].evalScripts());
			}
		}
	}
	
	if (obj.infos.onComplete2) {
		obj.infos.onComplete2();
	}
}

__ajax_refresh_to_location = function(obj) {	
	if (obj.alert) {
		/* Si un message d'alerte est passé en retour */
		alert(obj.alert);
	}
	
	if (obj.infos.location) {
		/* Si demandé dans l'énoncé, on rafraichi vers l'url spécifiée en location */
		window.location = obj.infos.location;
		return;
	}
	
	if (obj.url) {
		/* Si demandé dans l'énoncé, on rafraichi vers l'url */
		window.location = obj.url;
		return;
	}
}

__toLocation = function(url) {	
	window.location = url;
}

function initMainTableHeight() {
	agt = navigator.userAgent.toLowerCase();
	document.getElementById('MainTable').style.height=''+((agt.indexOf("msie") != -1 && agt.indexOf("opera") == -1 ) ? document.body.clientHeight : window.innerHeight)+'px';
}

function include(fileName) {
	if (document.getElementsByTagName) {
		Script = document.createElement("script");
		Script.type = "text/javascript";

		Script.src = fileName;
		Body = document.getElementsByTagName("BODY");
		if (Body) {
			Body[0].appendChild(Script);
		}
	}
}

function verif_touche(e,o) {
	if (e.keyCode == 13) {
		o.rows = o.rows + 1;
	}	
}

function preg_replace (array_pattern, array_pattern_replace, my_string) {
	var new_string = String (my_string);
	for (i=0; i<array_pattern.length; i++) {
		var reg_exp= RegExp(array_pattern[i], "gi");
		var val_to_replace = array_pattern_replace[i];
		new_string = new_string.replace (reg_exp, val_to_replace);
	}
	return new_string;
}

function keepOnline() {
	include('accueil.php?keeponline=1');
	setTimeout('keepOnline()',120000);
}

/* Redirige vers une  */
function refreshToLoginDetails(jeu,id_user) {
	window.location = jeu+".php?event=stats&id_user="+id_user;
}

