
// --------------------------------------------JavaScript TextBox class-------------------------

function TextBox(htmlTextBox)
{			
	//TextBox.prototype = new HTMLObject(htmlTextBox);
	this.inheritFrom = HTMLObject;
	this.inheritFrom(htmlTextBox);
			
	this.AddEventListener('focus',this.Style_GotFocus);
	this.AddEventListener('blur',this.Style_LostFocus);
	this.AddEventListener('change',this.TextChange);
}

TextBox.prototype._keyControlType;

TextBox.prototype.validateNumeric = function( strValue ) {
/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}


TextBox.prototype.validateInteger = function( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid integer number.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
**************************************************/
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}


	TextBox.prototype.SetKeyControlType = function(type)
	{			
		this._keyControlType=type;				

		//adicionar event listener a caixa de texto para controlar o seu input
		this.AddEventListener('keypress',this.rejeitaTecla);
								
		switch(this._keyControlType)
		{
			case "numeric":
				if(!this.validateNumeric(this._obj.value))
					this.SetValue('0');
			break;
			case "number":
				if(!this.validateInteger(this._obj.value))
					this.SetValue('0');		
			break;
			default:
			break;
				
		}
	
	}

	TextBox.prototype.GetKeyControlType = function()
	{			
		return this._keyControlType;			
	}
			
	

	TextBox.prototype.TextChange = function (oEvent)
	{		
		var obj=window.event?oEvent.srcElement:this;
		switch(obj._obj.GetKeyControlType())
		{
			case "numeric":
			case "number":
			if(obj.value.length==0) obj.value='0';
			
			if(obj._obj.GetKeyControlType()=="numeric" && !obj._obj.validateNumeric(obj.value))
				obj.value='0';
			else if(obj._obj.GetKeyControlType()=="number" && !obj._obj.validateInteger(obj.value))
				obj.value='0';

			break;
			default:
			break;
		}
	}
			
	TextBox.prototype.rejeitaTecla = function(oEvent)
	{    								 		   				
	
		
 		var locked=false;				
		var newInputValue='';
		
				
		//filtrando o evento
    		var oEvent = oEvent ? oEvent : window.event;
		var obj=window.event?oEvent.srcElement:this;
		
		
		var selection = new Selection(obj);
		
		var s = selection.getCaret();
		var selectionStart=s.start;		
				
			
    		var tecla = (oEvent.keyCode) ? oEvent.keyCode : oEvent.which;
    		    			    		
    		if(oEvent.type=="keydown" && navigator.appName.indexOf('Internet Explorer')<0 )
    		{
		        //se for keydown e não for o IE, vazarei pois o keypress já foi executado			        
        		return false;
    		}
						

    		if(typeof(oEvent.keyCode)=='number')
    		{
    			
    			if(
    				tecla==8
    				||
    				tecla==9
    				||
    				tecla==37
    				||
    				tecla==38
    				||
    				tecla==39
    				||
    				tecla==40    				
    							
    			)return false;
    				   
			
			newInputValue = obj.value.substring(0,selectionStart) + String.fromCharCode(tecla)			
			+ obj.value.substring(selectionStart,obj.value.length);			
								

			switch(obj._obj.GetKeyControlType())
			{
				case "numeric":		
					if(!obj._obj.validateNumeric(newInputValue))
						locked=true;
				break;
				case "number":
					if(!obj._obj.validateInteger(newInputValue))
						locked=true;
				break;
				case "alpha":
				default:
					locked=false;
				break;						
			}						    					
			if(locked)
			{
				
				if (typeof(oEvent.preventDefault)=='function')
       				{
		           		oEvent.preventDefault();            	
       				}
		       		else 
       					return false;       										
			}
       			
    			
    		}    			
 		return true;
	}

	


	
	
	//SELECTION------------------------------------------------------------------------------------------------------------------------------
	Selection = function(input){
    this.isTA = (this.input = input).nodeName.toLowerCase() == "textarea";
};
with({o: Selection.prototype}){
    o.setCaret = function(start, end){
        var o = this.input;
        if(Selection.isStandard)
            o.setSelectionRange(start, end);
        else if(Selection.isSupported){
            var t = this.input.createTextRange();
            end -= start + o.value.slice(start + 1, end).split("\n").length - 1;
            start -= o.value.slice(0, start).split("\n").length - 1;
            t.move("character", start), t.moveEnd("character", end), t.select();
        }
    };
    o.getCaret = function(){
        var o = this.input;
        if(Selection.isStandard)
            return {start: o.selectionStart, end: o.selectionEnd};
        else if(Selection.isSupported){
            var s = (this.input.focus(), document.selection.createRange()), r, start, end, value;
            if(s.parentElement() != o)
                return {start: 0, end: 0};
            if(this.isTA ? (r = s.duplicate()).moveToElementText(o) : r = o.createTextRange(), !this.isTA)
                return r.setEndPoint("EndToStart", s), {start: r.text.length, end: r.text.length + s.text.length};
            for(var $ = "[###]"; (value = o.value).indexOf($) + 1; $ += $);
            r.setEndPoint("StartToEnd", s), r.text = $ + r.text, end = o.value.indexOf($);
            s.text = $, start = o.value.indexOf($);
            if(document.execCommand && document.queryCommandSupported("Undo"))
                for(r in {0:0, 0:0})
                    document.execCommand("Undo");
            return o.value = value, this.setCaret(start, end), {start: start, end: end};
        }
        return {start: 0, end: 0};
    };
    o.getText = function(){
        var o = this.getCaret();
        return this.input.value.slice(o.start, o.end);
    };
    o.setText = function(text){
        var o = this.getCaret(), i = this.input, s = i.value;
        i.value = s.slice(0, o.start) + text + s.slice(o.end);
        this.setCaret(o.start += text.length, o.start);
    };
    new function(){
        var d = document, o = d.createElement("input"), s = Selection;
        s.isStandard = "selectionStart" in o;
        s.isSupported = s.isStandard || (o = d.selection) && !!o.createRange();
    };
}	
	
//SELECTION-END-----------------------------------------------------------------------------------------------------------------------------

		






// --------------------------------------------JavaScript TextBox class End-------------------------
