Yke.UI.InputText = function () 
{
	this._controlName = "InputText";
	
	this._formatter = null;
	this._tbx = null;
	
	// options
	this._maxLength = -1;
	this._size = 20;
	this._mode = 'singleline';
	this._rows = 1;
	
	this._pvaluelenght = 0;

	
	this._allowNumerics = true; /*  0 ... 9 */
	this._allowAlpha = true; /* a ... z */
	this._allowDelete = true; /*suppr et backspace */
	this._allowArrows = true; /* < > /\ \/ begin et end */
	this._allowDecimalPoint = true; /* . */
	this._allowSpecChars = true; /* le reste */
	this._allowAccents = true;
	
	this._evtClick = new Yke.UI.EventManager();
	this._evtKeyDown = new Yke.UI.EventManager();
	this._evtKeyUp= new Yke.UI.EventManager();
	
	this._evtOnChange = new Yke.UI.EventManager();
	
	this._valueIsValid = true;
	
	this._validatorMinLength = 0;
	this._validatorRegex =  null; //COMMENTAIRE//  /\d{4}-\d{2}-\d{2}.\d{2}:\d{2}:\d{2}/;
	
};
ykeCopyPrototype(Yke.UI.InputText, Yke.UI.ValueControl);

/* ========= properties ============*/
Yke.UI.InputText.prototype.getAllowNumerics = function () { return this._allowNumerics; }
Yke.UI.InputText.prototype.setAllowNumerics = function (value) { this._allowNumerics = value; }

Yke.UI.InputText.prototype.getAllowAlpha = function () { return this._allowAlpha; }
Yke.UI.InputText.prototype.setAllowAlpha = function (value) { this._allowAlpha = value; }

Yke.UI.InputText.prototype.getAllowDelete = function () { return this._allowDelete; }
Yke.UI.InputText.prototype.setAllowDelete= function (value) { this._allowDelete = value; }

Yke.UI.InputText.prototype.getAllowArrows = function () { return this._allowArrows; }
Yke.UI.InputText.prototype.setAllowArrows = function (value) { this._allowArrows = value; }

Yke.UI.InputText.prototype.getAllowDecimalPoint = function () { return this._allowDecimalPoint; }
Yke.UI.InputText.prototype.setAllowDecimalPoint= function (value) { this._allowDecimalPoint = value; }

Yke.UI.InputText.prototype.getAllowSpecChars = function () { return this._allowSpecChars; }
Yke.UI.InputText.prototype.setAllowSpecChars= function (value) { this._allowSpecChars = value; }

Yke.UI.InputText.prototype.getAllowAccents = function () { return this._allowAccents; }
Yke.UI.InputText.prototype.setAllowAccents= function (value) { this._allowAccents = value; }

Yke.UI.InputText.prototype.getValidatorMinLength = function () { return this._validatorMinLength; }
Yke.UI.InputText.prototype.setValidatorMinLength= function (value) { this._validatorMinLength = value; try{ this.validateValue(); } catch(e) {}  }

Yke.UI.InputText.prototype.getValidatorRegex = function () { return this._validatorRegex; }
Yke.UI.InputText.prototype.setValidatorRegex= function (value) { this._validatorRegex = value; try{ this.validateValue(); } catch(e) {}  }

Yke.UI.InputText.prototype.getIsValidValue = function () { try{ this.validateValue(); } catch(e) {} return this._valueIsValid; }

/* ============ events ============ */
/* + int */ Yke.UI.InputText.prototype.listenOnClick = function (f)
{
	return this._evtClick.add(f);
}
/* + int */ Yke.UI.InputText.prototype.listenOnKeyDown= function (f)
{
	return this._evtKeyDown.add(f);
}
/* + int */ Yke.UI.InputText.prototype.listenOnKeyUp= function (f)
{
	return this._evtKeyUp.add(f);
}
/* + int */ Yke.UI.InputText.prototype.listenOnChange= function (f)
{	
	return this._evtOnChange.add(f);
}


/* ============ methods ===========*/
/* + void */ Yke.UI.InputText.prototype.insertText = function (str, position)
{
	/* position est facultatif */
	var st;
	if(position != null && position > -1) st = position;
	else st = this.getSelection().start;
	var left = this._tbx.value.substr(0, st);
	var right = this._tbx.value.substr(st, this._tbx.value.length);
	
	this._tbx.value = left + str + right;
	st += str.length;
	
	this.setSelection(st,0);
}

/* + int */ Yke.UI.InputText.prototype.getSelection = function ()
{
	return Yke.Inputs.getCaretPosition(this._tbx);
}

/* + void */ Yke.UI.InputText.prototype.setSelection = function (start, len)
{
	Yke.Inputs.setCaretPosition(this._tbx, start, len);
}


/* # void */ Yke.UI.InputText.prototype.validateValue = function ()
{
	this._valueIsValid = true;
	
	if(this._tbx.value.length < this._validatorMinLength)
	{
		this._valueIsValid = false;
	}
	
	if(this._validatorRegex != null)
	{
		this._valueIsValid = this._valueIsValid && this._validatorRegex.test(this._tbx.value);
	}
	
	// regex
/*	if(this._valueIsValid)
	{
		this._tbx.style.background = '';
	}
	else
	{
		this._tbx.style.background = '#fab464';
	}*/
}

/* =========== rendering ================*/
/* + void */ Yke.UI.InputText.prototype.makeDomControl = function ()
{
	this._domControl = $YCE('div');
	
	if(this._mode.toLowerCase() == 'multiline')
	{
		this._tbx = $YCE('textarea');
	}
	else
	{
		this._tbx = $YCE('input');
		
		if(this._mode.toLowerCase() == 'password') this._tbx.type='password';
		else this._tbx.type='text';
	}
	
	if(this._highlight==true)
		this._tbx.style.backgroundColor = "#fab464";	
	
	if(this._readOnly)
	{
		//this._tbx.style.fontWeight = "bold";	
		this._tbx.disabled = 'disabled';		
	} 
	
	if(this._size>0) this._tbx.size=this._size;
	if(this._maxLength>0) this._tbx.maxLength=this._maxLength;
	

	
	this.listenOnKeyDown(this.lstnKeyDown);
	this.listenOnKeyDown(this.lstnKeyDown2);
	this.listenOnKeyUp(this.lstnKeyUp);
	this.listenOnKeyUp(this.lstnKeyUp2);
	
	this._tbx.InputText = this;
	this._tbx.onclick = function(e)
	{
		return this.InputText._evtClick.fire(this.InputText, e);
	}
	this._tbx.onkeydown = function (e)
	{
		return this.InputText._evtKeyDown.fire(this.InputText, e);
	}
	this._tbx.onkeyup = function (e)
	{
		this.InputText._evtOnChange.fire(this.InputText, e);
		return this.InputText._evtKeyUp.fire(this.InputText, e);
		
	}

	
	$YAC(this._domControl, this._tbx);
}
/* + void */ Yke.UI.InputText.prototype.updateControlValue = function()
{
	this._tbx.value = this._value;
	this.validateValue();
}
/* + void */ Yke.UI.InputText.prototype.getValueFromControl = function()
{
	this._value = this._tbx.value;
}


/* ============ internal events ============= */
/* # bool */ Yke.UI.InputText.prototype.lstnKeyDown = function (sender, e)
{
		
		var keyCode = null;
		if(e != null) keyCode = e.keyCode;
		else 
		try
		{
			if(event != null) keyCode = event.keyCode;
		}
		catch(e)
		{
		}
		
		var allowevt = sender._allowSpecChars;
		

	
		
		if(keyCode >= 65 && keyCode <= 90)
		{
			allowevt = sender._allowAlpha;
		}
		if((keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105))
		{
			allowevt = sender._allowNumerics;
		}	
		if(keyCode == 46 || keyCode == 8)
		{
			allowevt = sender._allowDelete;
		}
		if((keyCode >= 37 && keyCode <= 40) || keyCode == 35 || keyCode == 36)
		{
			allowevt = sender._allowArrows;
		}
		if(keyCode == 110) 
		{
			allowevt = sender._allowDecimalPoint;
		}
		
		if(keyCode == 9 || keyCode == 16)
		{
			allowevt = true;
		}
		
		$YDEBUG('a','a',keyCode,1);
		
		return allowevt;
}

/* # bool */ Yke.UI.InputText.prototype.lstnKeyDown2 = function (sender, e)
{

}

/* # bool */ Yke.UI.InputText.prototype.lstnKeyUp = function (sender, e)
{
	
	var keyCode = null;
	var check = true;
	if(e != null) keyCode = e.keyCode;
	else 
	try
	{
		if(event != null) keyCode = event.keyCode;
	}
	catch(e)
	{
	}
	if(keyCode <= 46 || keyCode == 91 || keyCode == 92 || (keyCode >= 112 && keyCode <= 123) || keyCode == 144 || keyCode == 145)
	{
		check = false;
	}
	
	if(check)
	{
		var val = sender._tbx.value;
		var selStart = (sender.getSelection().start-1);
		var chr = val.charAt(selStart);
		var undo = false;
		var rpl = null;
		
		if(sender._allowNumerics == false)
		{
			if(chr != '0' || chr != '1' || chr != '2' || chr != '3' || chr != '4' || chr != '5' || chr != '6' || chr != '7' || chr != '8' || chr != '9')
			{
					undo = true;
					rpl = '';
			}
		}
		
		if(sender._allowNumerics == true && sender._allowAlpha == false && sender._allowSpecChars == false)
		{
			if( (parseInt(chr)).toString() != chr & keyCode != 110 && chr != Yke.Culture.getCurrentCulture().number.decimalSymbol )
			{
				undo = true;
				rpl = '';		
			}
		}
		else if(sender._allowAccents == false)
		{
			if(chr == 'à' || chr == 'â' || chr == 'ä' || chr == 'ã' ) { undo = true; rpl = 'a' };
			if(chr == 'é' || chr == 'è' || chr == 'ë' || chr == 'ê') { undo = true; rpl = 'e' };
			if(chr == 'ì' || chr == 'î' || chr == 'ï') { undo = true; rpl = 'i' };
			if(chr == 'ò' || chr == 'ô' || chr == 'ö' || chr == 'õ') { undo = true; rpl = 'o' };
			if(chr == 'ù' || chr == 'û' || chr == 'ü') { undo = true; rpl = 'u' };
			if(chr == 'ÿ' ) { undo = true; rpl = 'y' };
			if(chr == 'ñ' ) { undo = true; rpl = 'n' };
			if(chr == 'ç' ) { undo = true; rpl = 'c' };
		}
		
		if((sender._pvaluelenght != val.length) || sender.getSelection().length > 0)
		{
			if(undo)
			{
				var left = val.substr(0, selStart);
				var right = val.substr(selStart + 1, val.length -1);
				
				sender._tbx.value = left + rpl + right;
				if(rpl != '' && rpl != null) selStart++;
				sender.setSelection(selStart , 0);
			}
		
		}
		sender._pvaluelenght = sender._tbx.value.length;
	}
	
	
	sender.validateValue();
}


/* # bool */ Yke.UI.InputText.prototype.lstnKeyUp2 = function (sender, e)
{

}

Yke.UI.InputText.prototype.focus = function()
{
	this._tbx.focus();
}




