/********************************************************
* 	CLASSE DE CHAMP DE SAISIE : Texte	     	*
* 							*
* Permet la saisie de texte sur une ligne dans un champ	*
* input.						*
*********************************************************/

var input = null;

//Constructeur de l'objet
function Texte()
{
	this.id = -1;
	this.valeur = "";
	this.nomChamp = "";
	this.parent = null;
	this.texteErreur = "";
	
	this.btn = null;
}

//Fonction de remplacement du texte de parent par le champ
Texte.prototype.remplacerTexte = function (parent, sauvegarde)
{
	if(!parent || !sauvegarde)
	{
		return false;
	}
	else
	{
		this.parent = parent;
	}

	input = document.createElement("input");
	
	input.value = this.valeur;
	input.style.width = getTextWidth(this.valeur) + 10 + "px";
	
	//Assignation des événements qui déclencheront la sauvegarde de la valeur
	
	//Sortie de l'input
	input.onblur = function ()
	{
		sauvegarde();
	};

	//Appui sur la touche Entrée
	input.onkeydown = function keyDown(event)
	{
		if((window.event && (getKeyCode(window.event) == 13)) || (getKeyCode(event) == 13))
		{
			sauvegarde.call();
		}		
	};
	
	parent.replaceChild(input, parent.firstChild);
	
	
	
	
	//création d'un bouton de sauvegarde
	this.btn = document.createElement('input');
	this.btn.type="button";
	this.btn.value = '\uFEFF\u0053\u0061\u0075\u0076\u0065\u0067\u0061\u0072\u0064\u0065\u0072';
	this.btn.className = "btn_edition";
	this.btn.onClick = function (){
				sauvegarde.call();
	};
	
	parent.appendChild(this.btn);
		
	
	
	
}

//Fonction permettant de récupérer la valeur du champ
Texte.prototype.getValeur = function ()
{
	return input.value;
}

//Fonction d'activation du champ
Texte.prototype.activerChamp = function ()
{
	input.focus();
	input.select();
}

//Fonction de sortie du mode d'édition
Texte.prototype.terminerEdition = function ()
{
	this.parent.replaceChild(document.createTextNode(input.value), this.parent.firstChild);
	
	this.parent.removeChild(this.btn);
	
	delete this.btn;	
	
	delete input;
}

//Fonction déterminant si la valeur passée au script PHP doit être formatée par celui-ci ou pas (avec mysql_real_escape_string($str);)
//Ici oui, car il s'agit de texte
Texte.prototype.echaperValeur = function ()
{
	return "true";
}

//Erreur si champ vide
Texte.prototype.erreur = function ()
{
	if(this.getValeur() == "")
	{
		this.texteErreur = '\uFEFF\u0041\u0075\u0063\u0075\u006E\u0065\u0020\u0073\u0061\u0069\u0073\u0069\u0065\u0020\u0065\u0066\u0066\u0065\u0063\u0074\u0075\u00E9\u0065\u0020\u0021';
		return true;
	}
	else
		return false;
}