// --------------------------------------------------------
// Fonction de gestion des inputs textes pour les variables
// --------------------------------------------------------

var objInput = function () {}

objInput.prototype.create = function (
			noeud,			// ID de la cellule ou doit se placer le champ
			identifier,		// identifierifiant du champ
			defaultValue,	// Valeur par défaut
			minValue,		// Valeur mini
			maxValue,		// Valeur maxi
			interval,		// Pas des +/-
			minMax			// Affichage du minMax
			) 
{
	// ----------------------------------
	// On s'assure que les variables passées sont bien reconues comme des chiffres
	var defaultValue = parseFloat(defaultValue);
	var minValue = parseFloat(minValue);
	var maxValue = parseFloat(maxValue);
	var interval = parseFloat(interval);
	//-----------------------------------
	// Création d'un input text
	this.champ=document.createElement('input');
	this.champ.setAttribute('type','text');
	this.champ.setAttribute('name',identifier);
	this.champ.setAttribute('value',defaultValue);
	var leChamp = this.champ;

	//-----------------------------------
	// Vérifie les valeur soumise au champ
	this.champ.onchange = function()
	{
		if (this.value == '' || !parseFloat(this.value) || parseFloat(this.value) < minValue || parseFloat(this.value) > maxValue)
		{
			this.style.borderColor = 'red';
			this.className = 'error';
			tips(document.getElementById('tips'),"Type a value between "+minValue+" and "+maxValue);
			comp=(setTimeout("tips(document.getElementById('tips'))",3500));
		}
		else
		{
			this.style.borderColor = 'black';
			this.className = 'edited';
			this.value = around(parseFloat(this.value));
		}
	}

	//-----------------------------------
	// Applique le style au focus
	this.champ.onfocus = function()
	{
		if (this.value != '' && parseFloat(this.value) && parseFloat(this.value) >= minValue && parseFloat(this.value) <= maxValue)
		{
			// this.style.borderColor = 'black';
		}
		else
		{
			this.style.borderColor = 'red';
			this.className = 'error';
			tips(document.getElementById('tips'),"Type a value between "+minValue+" and "+maxValue);
			comp=(setTimeout("tips(document.getElementById('tips'))",3500));
		}
	}

	//-----------------------------------
	// Gestion des touches fléchées
	var IsIE=!!document.all;

	if(window.event) // If = IE
	{
		this.champ.onkeypress = function(event){
			if (event.keyCode == "40")
			{
				if (parseFloat(leChamp.value)-interval >= minValue)
				{
					leChamp.value = parseFloat(leChamp.value) - interval;
					leChamp.value = around(leChamp.value);
				}
				else
					leChamp.value = minValue;
			}
			else if (event.keyCode == "38")
			{
				if (parseFloat(leChamp.value)+interval <= maxValue)
				{
					leChamp.value = parseFloat(leChamp.value) + interval;
					leChamp.value = around(leChamp.value);
				}
				else
					leChamp.value = maxValue;
			}

			leChamp.champ.style.borderColor = 'black';
			leChamp.champ.className = 'edited';
		}
	}
	else if(!IsIE) // If != IE
	{
		this.champ.onkeypress = function(event){
			if (event.keyCode == "40")
			{
				if (parseFloat(leChamp.value)-interval >= minValue)
				{
					leChamp.value -= interval;
					leChamp.value = around(leChamp.value);
				}
				else
					leChamp.value = minValue;
			}
			else if (event.keyCode == "38")
			{
				if (parseFloat(leChamp.value)+interval <= maxValue)
				{
					leChamp.value = parseFloat(leChamp.value)+interval;
					leChamp.value = around(leChamp.value);
				}
				else
					leChamp.value = maxValue;
			}
			leChamp.style.borderColor = 'black';
			leChamp.className = 'edited';
		}
	}

	//-----------------------------------
	// Création d'un bouton moins
	this.btMoins=document.createElement('img');
	this.btMoins.setAttribute('src','images/less.gif');
	this.btMoins.setAttribute('alt','decrease value');
	// Gestion du clic
	this.btMoins.onclick = function()
	{
		if (parseFloat(leChamp.value-interval) >= minValue)
		{
			leChamp.value -= interval;
			leChamp.value = around(leChamp.value);
			leChamp.style.borderColor = 'black';
			leChamp.className = 'edited';
		}
		else
			leChamp.value = minValue;
	}

	//-----------------------------------
	// Création d'un bouton plus
	this.btPlus=document.createElement('img');
	this.btPlus.setAttribute('src','images/more.gif');
	this.btPlus.setAttribute('alt','increase value');
	// Gestion du clic
	this.btPlus.onclick = function()
	{
		if (parseFloat(leChamp.value)+interval <= maxValue)
		{
			leChamp.style.borderColor = 'black';
			leChamp.value = parseFloat(leChamp.value) + interval;
			leChamp.value = around(leChamp.value);
			leChamp.className = 'edited';
		}
		else
			leChamp.value = maxValue;
	}

	//-----------------------------------
	// Création des labels min et max
	this.mini=document.createElement('span');
	this.maxi=document.createElement('span');
	this.mini.setAttribute('class','grey');
	this.maxi.setAttribute('class','grey');
	this.mini.innerHTML = minValue+" <";
	this.maxi.innerHTML = "< "+maxValue;

	//-----------------------------------
	// Ajout des éléments précédents dans le code HTML
	document.getElementById(noeud).style.textAlign = 'center';
	if (minMax != "false")
		document.getElementById(noeud).appendChild(this.mini);
	document.getElementById(noeud).appendChild(this.btMoins);
	document.getElementById(noeud).appendChild(this.champ);
	document.getElementById(noeud).appendChild(this.btPlus);
	if (minMax != "false")
		document.getElementById(noeud).appendChild(this.maxi);
	//-----------------------------------
}

insertInput = function (
			noeud,			// ID de la cellule ou doit se placer le champ
			identifier,		// identifierifiant du champ
			defaultValue,	// Valeur par défaut
			minValue,		// Valeur mini
			maxValue,		// Valeur maxi
			interval,		// Pas des +/-
			minMax
			)
{
	var o = new objInput();
	o.create(noeud,identifier,defaultValue,minValue,maxValue,interval,minMax);
}

// ------------------------------------------
function toogleCustomTab(tab,bt)
{
	var el = document.getElementById(tab);
	var el2 = bt;
	var el3 = this.customTab;
	var el4 = this.customBtTab;
	if (el != el3)
	{
		el.className = "selected";
		el2.className = "selected";
		el3.className = "";
		el4.className = "";
		this.customTab = el;
		this.customBtTab = el2;
	}
}

// ------------------------------------------
function toogleCustomMenu(tab,bt)
{
	var el = document.getElementById(tab);
	var el2 = bt;
	var el3 = this.customMenu;
	var el4 = this.customBtMenu;
	if (el != el3)
	{
		el.className = "subTabSelected";
		el2.className = "selected";
		el3.className = "subTab";
		el4.className = "";
		this.customMenu = el;
		this.customBtMenu = el2;
	}
}

// ------------------------------------------
function toogleHydro(bt)
{
	if (bt.className != "selected")
	{
		bt.className = "selected";
		hydroSelected.className = "";
		hydroSelected = bt;
	}
}

// ------------------------------------------
function verifCustoms()
{
	var monbloc = document.getElementById("custom" );
	var mesinputs = monbloc.getElementsByTagName("input" );
	var erreurListe = 0;
	var liste = 0;
	var elementsList = Array();
	for (var i=0; i<mesinputs.length; i++) {

		elementsList.push(Array(mesinputs[i].name, mesinputs[i].value));

		if (mesinputs[i].className == "edited")
			liste += 1;
		if (mesinputs[i].className == "error")
			erreurListe += 1;
	}

	if (liste == 1)
		document.getElementById("customResume").innerHTML = "<ul><li>Enabled</li><li>"+liste+" custom value</li></ul>";
	else if (liste > 1)
		document.getElementById("customResume").innerHTML = "<ul><li>Enabled</li><li>"+liste+" custom values</li></ul>";
	else
	{
		document.getElementById("customResumeBlock").style.display = "none";
	}

	if (erreurListe > 0)
	{
		document.getElementById("customResumeIcon").className = "error";
		document.getElementById("customResume").innerHTML = "<ul><li>ERRORS</li><li>"+erreurListe+" false value(s)</li></ul>";
		document.getElementById("customResumeBlock").style.display = "block";
		return false;
	}
	else
	{
		document.getElementById("customResumeIcon").className = "";
		return elementsList.toJSONString();
	}
}





