//----------------------- Prototype -----------------------------------------//
//  resize(x,y): Resize la fenetre a la taile x, y			     //
//  									     //
//---------------------------------------------------------------------------//


//function resize(x,y) 
//{
	
//	window.moveTo('0','0');
//	window.resizeTo(x, y);
//}

function resize(w, h)
{
	window.resizeTo(500, 500);
	if (self.innerWidth)
	{
		dif1	= 500 - self.innerWidth;
		dif2	= 500 - self.innerHeight;
		w	+= dif1;
		h	+= dif2;
		window.resizeTo(w, h);
	}
	else
	{
		dif1	= 500 - document.body.clientWidth;
		dif2	= 500 - document.body.clientHeight;
		w	+= dif1;
		h	+= dif2;
		window.resizeTo(w, h);
	}
		
}

function resize2(w, h)
{
	window.parent.resizeTo(500, 500);
	if (parent.self.innerWidth)
	{
		dif1	= 500 - parent.self.innerWidth;
		dif2	= 500 - parent.self.innerHeight;
		w	+= dif1;
		h	+= dif2;
		window.parent.resizeTo(w, h);
	}
	else
	{
		dif1	= 500 - window.parent.document.body.clientWidth;
		dif2	= 500 - window.parent.document.body.clientHeight;
		w	+= dif1;
		h	+= dif2;
		window.parent.resizeTo(w, h);
	}
}

function timeFormat(t) 
{
         return ((h=Math.floor(t/1000/60/60))<10?"0"+h:h)+":"+
                ((m=Math.floor(t/1000/60)%60)<10?"0"+m:m)+":"+
                ((s=Math.floor(t/1000)%60)<10?"0"+s:s);
}

function dynamic(layer, html)
{
  if (document.getElementById(layer).innerHTML != html) {
    document.getElementById(layer).innerHTML = html;
  }
}

function CreateXMLHTTPRequestObject() {
	// Proprietes
	
	this.xhr_object    = null;
	this.response      = null;
	this.ready         = true;
	this.asynchronous  = false;

	// Creation de l objet XMLHTTpRequest
	if(window.XMLHttpRequest) // Firefox
		this.xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
		this.xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else // XMLHttpRequest non supporte par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");



	// Methodes

	// Appelle la fonction censee indiquer qu'une communication est en cours
	// La fonction doit etre fournie par l'utilisateur de la classe et doit prendre un booleen en parametre :
	//  - true  : la communication commence
	//  - false : la communication est terminee
	this.indicatorFunction = null;
	
	// Permet de definir la fonction qui servira d'indicateur de communication
	this.setIndicatorFunction = function(func) {
		if(typeof(func) == "function") this.indicatorFunction = func;
	}
	
	// Passe en mode synchrone
	this.setSynchronous = function() {
		this.asynchronous = false;
	}

	// Passe en mode asynchrone
	this.setAsynchronous = function() {
		this.asynchronous = true;
	}

	// Lance une requete sur un fichier du serveur en passant eventuellement des parametres, avec la methode GET
	this.getFileGet = function(url, data) {
		return this.doRequest(url, "GET", data);
	}

	// Alias de this.getFileGet
	this.getFile = this.getFileGet;
	
	// Lance une requete sur un fichier du serveur en passant eventuellement des parametres, avec la methode POST
	this.getFilePost = function(url, data) {
		return this.doRequest(url, "POST", data);
	}

	// Recupere tous les header associes a l'URL passee en parametre, ou juste le header passe en parametre s'il est precise
	this.getFileHeader = function(url, header) {
		return this.doRequest(url, "HEAD", header);
	}

	// Effectue la requete proprement dite
	//  - method : GET, POST ou HEAD
	//  - url    : chemin vers un fichier
	//  - data   : donnees a transmettre (ex : a=5&foo=bar)
	this.doRequest = function(url, method, data) {
		if(!this.ready || !this.xhr_object) return false;

		// Recherche header_name dans tous les headers et retourne la valeur correspondante
		// ou "Header inconnu..." si header_name n'a pas ete trouve
		function _getResponseHeader(headers, header_name) {
			var tmp = headers.split("\n");
			for(var i=0, n=tmp.length, t=[]; i<n-1; ++i) {
				t = tmp[i].split(": ");
				if(t[0].toLowerCase() == header_name.toLowerCase()) return t[1];
			}
			return "Header inconnu...";
		}

		if(this.indicatorFunction) this.indicatorFunction(true);
		this.ready = false;

		// On copie la reference a l'objet courant car il ne sera plus "dans le contexte"
		// au moment oł la fonction onreadystatechange sera executee
		var obj = this;
		function onreadystatechangeFunction() {
			if(obj.xhr_object.readyState != 4) return;
			
			if(obj.indicatorFunction) obj.indicatorFunction(false);

			var all_headers = obj.xhr_object.getAllResponseHeaders();
			if(method == "HEAD") {
				obj.response = data ? _getResponseHeader(all_headers, data) : all_headers;
			}
			else {
				var content_type = _getResponseHeader(all_headers, "Content-Type");
				if (content_type != "Header inconnu..." && (new RegExp("^text/xml.*$", "gi")).test(content_type))
					obj.response = obj.xhr_object.responseXML;
				else
					obj.response = obj.xhr_object.responseText;
			}
		}

		if(method == "GET" && typeof(data) != "undefined" && data != "") url += "?"+data;
		this.xhr_object.open(method, url, this.asynchronous);

		if(this.asynchronous)
			this.xhr_object.onreadystatechange = onreadystatechangeFunction;
		
		if(data) this.xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		else     data = null;
		this.xhr_object.send(data);

		if(!this.asynchronous)
			onreadystatechangeFunction();

		return true;
	}

	// Retourne true si la reponse est arrivee, false sinon
	this.hasResponse = function() {
		return this.response != null;
	}

	// Retourne la reponse a la derniere requete
	this.getResponse = function() {
		return this.response;
	}

	// Valide la requete, une nouvelle requete peut etre faite avec ce meme objet
	this.validateRequest = function() {
		this.ready    = true;
		this.response = null;
	}

	// Annule la requete en cours
	this.cancelRequest = function() {
		this.xhr_object.abort();
		if(this.indicatorFunction) this.indicatorFunction(false);
		this.validateRequest();
	}
}

