var debug = false;


function CreerSelect(tableau, nom)
{
	var resultat = "<select name="+nom+">";
	
	resultat += "<option value=''></option>";
	for(var i=0; i<tableau.length; i++)
	{
		resultat += "<option value='"+tableau[i][0]+"'>"+tableau[i][1]+"</option>";
	}
	resultat += "</select>";
	return resultat;
}

function FloatFormat(nombre, decimal)
{
	nombre = nombre.toString();
	if(!decimal) decimal = 2;
	
	var position = nombre.indexOf('.', 0);
	var partieEntiere  = nombre;
	var partieDecimale = "";
	
	if(position > -1)
	{
		partieEntiere  = nombre.substr(0, position);
		partieDecimale = nombre.substr(position+1, nombre.length-position-1);
	}
	
	if(partieDecimale.length > decimal) partieDecimale = partieDecimale.substr(0, decimal);
	
	for(var i=partieDecimale.length; i<decimal; i++)
	{
		partieDecimale += "0";
	}
	
	return partieEntiere+"."+partieDecimale;
}

function IntFormat(nombre, chiffres)
{
	nombre = nombre.toString();
	
	var position = nombre.indexOf('.', 0);
	if(position > -1) nombre = nombre.substr(0, position);
	
	if(!chiffres) chiffres = 2;
	for(var i=nombre.length; i<2; i++)
	{
		nombre = "0"+nombre;
	}
	return nombre;
}

function SecToHeure(seconde)
{
	var temps, j, h, m, s;
	
	if((typeof seconde == "number") || (seconde = parseInt(seconde)))
	{
		s = seconde;
		m = Math.floor(s/60);     s -= m *60;
		h = Math.floor(m/60);     m -= h *60;
		j = Math.floor(h/24);     h -= j *24;
		
		temps = "";
		if(j == 1)     temps += j+" jour ";
		else if(j > 1) temps += j+" jours ";
		
		temps += IntFormat(h)+":";
		temps += IntFormat(m)+":";
		temps += IntFormat(s);
		
		return temps;
	}
	return "00:00:00 !";
}


Array.prototype.toString = function()
{
	var chaine = "[";
	
	for(var i in this)
	{
		if(this.hasOwnProperty(i))
		{
			if     (typeof this[i] == 'object')   chaine += i+" => "+this[i].toString()+",";
			else if(typeof this[i] != 'function') chaine += i+" => "+this[i]+",";
		}
		
	}
	if(chaine[chaine.length-1] == ',') chaine = chaine.substr(0, chaine.length-1);
	
	return chaine+"]";
}

Array.prototype.Dupliquer = function()
{
	var copie = new Array(this.length);
	
	for(var i in this)
	{
		if(typeof this[i] == 'object') copie[i] = this[i].Dupliquer();
		else                           copie[i] = this[i];
	}
	
	return copie;
}


/* ******************************************* */
/* ******************* Ajax ****************** */
/* ******************************************* */
function Ajax(action, id, parametres, methode, assynchrone, succes, echec)
{
	if(!methode) methode = 'POST';
	if(!succes)  succes = null;
	if(!echec)   echec  = null;
	if(!assynchrone)
	{
		assynchrone = false;
		if(!succes) succes = null;
		if(!echec)  echec  = null;
	}
	
	var chemin = "Interface/ajax_interface.php";
	var xhr    =  new XMLHttpRequest();
	parametres = "action="+action+"&id="+id+"&"+parametres;
	
	if(methode.toUpperCase() == 'POST')
	{
		xhr.open(methode, chemin, assynchrone);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(parametres);
	}
	else
	{
		chemin += "?"+parametres;
		xhr.open(methode, chemin, assynchrone);
		xhr.send(null);
	}
	
	if(!assynchrone)
	{
		if(xhr.readyState == 4) return xhr.responseText;
		else                    return "erreur";
	}
	else
	{
		xhr.onreadystatechange = function ()
		{
			if(xhr.readyState == 4)
			{
				if (debug) alert("Donnees Ajax : "+xhr.responseText);
				
				if(xhr.status  == 200)
				{
					if (succes != null) succes(xhr.responseText);
				}
				else
				{
					if(debug)	      alert("Erreur : "+xhr.status);
					if(echec != null) echec(xhr.status);
				}
			}
		}
	}
	
}
