﻿Type.registerNamespace("ITkey.Web.UI");

ITkey.Web.UI.ItkInputControl = function(element) 
{
	ITkey.Web.UI.ItkInputControl.initializeBase(this, [element]);
	
    this._autoPostBack = false;
    this._enabled = true;
    this._showButton = false;
    this._invalidStyleDuration = 100;
    this._emptyMessage = "";
    this._selectionOnFocus = ITkey.Web.UI.SelectionOnFocus.None;
    this._postBackEventReferenceScript = "";
    this._styles = null; 
    
    this._isEnterPressed = false;
    this._isDroped = false;
    
    
    this._onTextBoxKeyUpDelegate = null;
    this._onTextBoxKeyPressDelegate = null;
    this._onTextBoxBlurDelegate = null;
    this._onTextBoxFocusDelegate = null;
    this._onTextBoxMouseOutDelegate = null;
    this._onTextBoxMouseOverDelegate = null;
    this._onTextBoxKeyDownDelegate = null;
    this._onTextBoxMouseWheelDelegate = null;
    this._onTextBoxDragDropDelegate = null;
    
    //fix a problem with Safari and selectiononfocus
    if ($ITkey.isSafari)
        this._onTextBoxMouseUpDelegate = null;
    
    
    this._focused = false;
}

ITkey.Web.UI.ItkInputControl.prototype = 
{
    initialize : function() {
        ITkey.Web.UI.ItkInputControl.callBaseMethod(this, 'initialize');        
        this._clientID = this.get_id();        
        this._wrapperElementID = this.get_id() + "_wrapper";        
        this._textBoxElement = $get(this.get_id() + "_text");
        this._originalTextBoxCssText = this._textBoxElement.style.cssText; 
        if(this._originalTextBoxCssText.indexOf(";") != this._originalTextBoxCssText.length - 1)
        {
            this._originalTextBoxCssText += ";" ;
        }
        
        
        this._updatePercentageHeight();
        this._originalMaxLength = this._textBoxElement.maxLength;
        if (this._originalMaxLength == -1) //not IE browser default value when not set
            this._originalMaxLength = 2147483647;
            
        this._initializeHiddenElement(this.get_id());
        this._initializeValidationField(this.get_id());
        this._selectionEnd = 0;
        this._selectionStart = 0;
        
        //_isInFocus fix blur event fires twice for input elements in Firefox 3
        this._isInFocus = true;

        // state flags
        this._hovered = false;
        this._invalid = false;
        
        this._attachEventHandlers();
        //this.updateDisplayValue();
        this.updateCssClass();       
        this._initializeButtons();
        
        this._initialValue = this.get_value();
        
        //obsolete fields
        //this.InitialValue = this._initialValue;
        
        this.raise_load(Sys.EventArgs.Empty);  
        
        if(this._focused)
        {
            this._updateStateOnFocus();
        }
    },
    
    dispose : function() 
    {
        ITkey.Web.UI.ItkInputControl.callBaseMethod(this, 'dispose');
  
        if (this.Button)
        {
            if (this._onButtonClickDelegate)
            {
                $removeHandler(this.Button, "click", this._onButtonClickDelegate);                                              
                this._onButtonClickDelegate = null;
            }
        }
        if (this._onTextBoxKeyDownDelegate)
        {
            $removeHandler(this._textBoxElement, "keydown", this._onTextBoxKeyDownDelegate);
            this._onTextBoxKeyDownDelegate = null;
        }
        if (this._onTextBoxKeyPressDelegate)
        {
            $removeHandler(this._textBoxElement, "keypress", this._onTextBoxKeyPressDelegate);
            this._onTextBoxKeyPressDelegate = null;
        }
        if (this._onTextBoxKeyUpDelegate)
        {
            $removeHandler(this._textBoxElement, "keyup", this._onTextBoxKeyUpDelegate);    
            this._onTextBoxKeyUpDelegate = null;
        }
        if (this._onTextBoxBlurDelegate)
        {
            $removeHandler(this._textBoxElement, "blur", this._onTextBoxBlurDelegate);
            this._onTextBoxBlurDelegate = null;
        }
        if (this._onTextBoxFocusDelegate)
        {
            $removeHandler(this._textBoxElement, "focus", this._onTextBoxFocusDelegate);
            this._onTextBoxFocusDelegate = null;
        }
        
        if (this._onTextBoxMouseOutDelegate)
        {
            $removeHandler(this._textBoxElement, "mouseout", this._onTextBoxMouseOutDelegate);   
            this._onTextBoxMouseOutDelegate = null;
        }
        if (this._onTextBoxMouseOverDelegate)
        {
            $removeHandler(this._textBoxElement, "mouseover", this._onTextBoxMouseOverDelegate);
            this._onTextBoxMouseOverDelegate = null;
        }
        //fix a problem with Safari and selectiononfocus        
        if ($ITkey.isSafari 
            && this._onTextBoxMouseUpDelegate)
        {
            $removeHandler(this._textBoxElement, "mouseup", this._onTextBoxMouseUpDelegate);
            this._onTextBoxMouseUpDelegate = null;
        }        
        
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer)
		{
		    //Gecko, Opera?
		    if (this._onTextBoxMouseWheelDelegate)
		    {
		        $removeHandler(this._textBoxElement, "DOMMouseScroll", this._onTextBoxMouseWheelDelegate);
		        this._onTextBoxMouseWheelDelegate = null;
		    }
		    if (this._onTextBoxDragDropDelegate)
		    {
		        $removeHandler(this._textBoxElement, "dragdrop", this._onTextBoxDragDropDelegate);
		        this._onTextBoxDragDropDelegate = null;
		    }
		}
		else
		{
			//IE
			if (this._onTextBoxMouseWheelDelegate)
			{
		        $removeHandler(this._textBoxElement, "mousewheel", this._onTextBoxMouseWheelDelegate);
		        this._onTextBoxMouseWheelDelegate = null;
		    }
		    if (this._onTextBoxDragDropDelegate)
		    {
		        $removeHandler(this._textBoxElement, "drop", this._onTextBoxDragDropDelegate);
		        this._onTextBoxDragDropDelegate = null;
		    }
		}
		
		if(this._textBoxElement)
		    this._textBoxElement._events = null;
    },
    
    // PUBLIC API START
    
    clear : function ()
    {
        this.set_value("");
    },
    
    disable : function()
    {
        this.set_enabled(false);
        this._textBoxElement.disabled = "disabled";
        this.updateCssClass();
        this.raise_disable(Sys.EventArgs.Empty);
    },
        
    enable : function ()
    {
        this.set_enabled(true);
        this._textBoxElement.disabled = "";
        this.updateCssClass();
        this.raise_enable(Sys.EventArgs.Empty);
    },
    
    focus : function ()
    {
        this._textBoxElement.focus();
    },

    blur : function ()
    {
        this._textBoxElement.blur();
    },
    
    isEmpty : function ()
    {
        return this._hiddenElement.value == "";
    }, 

    isNegative : function ()
	{
		return false;
	},
	
	isReadOnly : function ()
	{	    
	    return this._textBoxElement.readOnly || !this._enabled;
	},
	
	isMultiLine: function()
    {
        return this._textBoxElement.tagName.toUpperCase() == "TEXTAREA";
    },    
    
    updateDisplayValue : function()
    {
        if (this._focused)
        {
            this.set_textBoxValue(this.get_editValue());
        }
        else
        {
            if (this.isEmpty() && this.get_emptyMessage())
            {
                this._textBoxElement.maxLength = 2147483647; //the default value (when not set)               
                this._isEmptyMessage = true;
                this.set_textBoxValue(this.get_emptyMessage());
                this._textBoxElement.maxLength = this._originalMaxLength;         
            }
            else
            {
                this._isEmptyMessage = false;
                this.set_textBoxValue(this.get_displayValue());
            }
        }
    },
    
    __isEmptyMessage  : function ()
    {
        return this.isEmpty() && this.get_emptyMessage();
    },    
    
    updateCssClass  : function ()
    {      
        if (this._enabled && (!this.__isEmptyMessage()) && (!this.isNegative()))
        {
             this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["EnabledStyle"][0]);
             this._textBoxElement.className = this.get_styles()["EnabledStyle"][1];            
        }     
        if (this._enabled && (!this.__isEmptyMessage()) && this.isNegative())
        {
             this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["NegativeStyle"][0]);
             this._textBoxElement.className = this.get_styles()["NegativeStyle"][1];
        }          
        if (this._enabled && this.__isEmptyMessage())
        {
             this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["EmptyMessageStyle"][0]);
             this._textBoxElement.className = this.get_styles()["EmptyMessageStyle"][1];
        }   
        if (this._hovered)
        {
            this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["HoveredStyle"][0]);
            this._textBoxElement.className = this.get_styles()["HoveredStyle"][1];
        }
        if (this._focused)
        {
            this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["FocusedStyle"][0]);
            this._textBoxElement.className = this.get_styles()["FocusedStyle"][1];   
        }
        if (this._invalid)
        {
            this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["InvalidStyle"][0]);
            this._textBoxElement.className = this.get_styles()["InvalidStyle"][1];
        }   
        if (this._textBoxElement.readOnly)
        {
            this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["ReadOnlyStyle"][0]);
            this._textBoxElement.className = this.get_styles()["ReadOnlyStyle"][1];        
        }
        if (!this._enabled)
        {   
            this._textBoxElement.style.cssText = this._originalTextBoxCssText + this.updateCssText(this.get_styles()["DisabledStyle"][0]);
            this._textBoxElement.className = this.get_styles()["DisabledStyle"][1];        
        }
    },
    
    updateCssText : function(styleCssText)
    {
        var settings = styleCssText.split(';');
        var i;
        var result = ""; 
        for (i=0; i < settings.length; i++)
        {
            var setting = settings[i].split(':');
            if(setting.length == 2)
            {   
                var name = "" + setting[0].toLowerCase();
                if(name != "width" && name != "height")
                {
                    result += settings[i] + ";";
                }  
            }
        }
        return result;
    },
    
    selectText : function (start, end)
    {
        this._selectionStart = start;
        this._selectionEnd = end;
        this._applySelection();
    },

    selectAllText : function ()
    {
        if (this._textBoxElement.value.length > 0)
        {
             this.selectText(0, this._textBoxElement.value.length);
             return true;
        }
        return false;
    },    
    
    // obsolete: use the value property
	GetValue : function()
	{
	    return this.get_value();
	},
	
    // obsolete: use the value property
    SetValue : function (newValue)
    {
        this.set_value(newValue);
    },
	
	// obsolete: use the displayValue property
	GetDisplayValue : function ()
    {
        return this.get_displayValue();
    },
    
    // obsolete: use the editValue property
    GetEditValue : function ()
    {
        return this.get_editValue();
    },
    
    // obsolete: use the caretPosition property
    SetCaretPosition : function(position)
    {
        this.set_caretPosition(position);
    },
    
    // obsolete: use the wrapperElement property
    GetWrapperElement : function()
    {
        return this.get_wrapperElement();
    },
    
    // obsolete: use the textBoxValue property    
    GetTextBoxValue : function ()
    {
        return this.get_textBoxValue();
    },
    
    // obsolete: use the textBoxValue property
    SetTextBoxValue : function (value)
    {
        this.set_textBoxValue(value);
    },
    
    get_value : function ()
    {
        return this._hiddenElement.value;
    },
    
    set_value : function(newValue)
    {
        var eventArgs = new ITkey.Web.UI.InputValueChangingEventArgs(newValue, this._initialValue);
        this.raise_valueChanging(eventArgs);
        
        if (eventArgs.get_cancel() == true)
        {
            this._SetValue(this._initialValue);
            return false;
        }
            
        if (eventArgs.get_newValue())
            newValue = eventArgs.get_newValue();
            
        var validValue = this._setHiddenValue(newValue);
        
        if (validValue == false)
            newValue = "";
            
        this._triggerDOMChangeEvent(this._getValidationField());
        this.raise_valueChanged(newValue, this._initialValue);
        
        if (typeof(validValue) == "undefined" || validValue == true)
        {
            this.set_textBoxValue(this.get_editValue());
            this.updateDisplayValue();
            this.updateCssClass();
        }
    },    

    get_displayValue : function()
    {
        return this._hiddenElement.value;
    },

    get_editValue: function()
    {
        return this._hiddenElement.value;
    },

    set_caretPosition : function (position)
    {   
        this._selectionStart = position;
        this._selectionEnd = position;
        this._applySelection();
    },
    
    get_caretPosition : function ()
    {
        this._calculateSelection();
        if (this._selectionStart != this._selectionEnd)
            return new Array(this._selectionStart, this._selectionEnd)
        else 
            return this._selectionStart;      
    },
    
    raisePostBackEvent : function()
    {
        eval(this._postBackEventReferenceScript);
    },
    
    get_wrapperElement : function()
    {
        return $get(this._wrapperElementID);
    },
    
    get_textBoxValue : function()
    {
        return this._textBoxElement.value; 
    },
    
    set_textBoxValue : function(value)
    {
        if (this._textBoxElement.value != value)
        {
            this._textBoxElement.value = value;
        }
    },
    
    get_autoPostBack : function() 
    {
        return this._autoPostBack;
    },
    set_autoPostBack : function(value) 
    {
        if (this._autoPostBack !== value) 
        {
            this._autoPostBack = value;
            this.raisePropertyChanged('autoPostBack');
        }
    },
    
    get_emptyMessage : function() 
    {
        return this._emptyMessage;
    },
    set_emptyMessage : function(value) 
    {
        if (this._emptyMessage !== value) 
        {
            this._emptyMessage = value;
            this._isEmptyMessage = (value != "");
            this.raisePropertyChanged('emptyMessage');
        }
    },
    
    get_selectionOnFocus : function() 
    {
        return this._selectionOnFocus;
    },
    set_selectionOnFocus : function(value) 
    {
        if (this._selectionOnFocus !== value) 
        {
            this._selectionOnFocus = value;
            this.raisePropertyChanged('selectionOnFocus');
        }
    },
    
    get_showButton : function() 
    {
        return this._showButton;
    },
    set_showButton : function(value) 
    {
        if (this._showButton !== value) 
        {
            this._showButton = value;
            this.raisePropertyChanged('showButton');
        }
    },
    
    get_invalidStyleDuration : function() 
    {
        return this._invalidStyleDuration;
    },
    set_invalidStyleDuration : function(value) 
    {
        if (this._invalidStyleDuration !== value) 
        {
            this._invalidStyleDuration = value;
            this.raisePropertyChanged('invalidStyleDuration');
        }
    },    
    
    get_enabled : function() 
    {
        return this._enabled;
    },
    set_enabled : function(value) 
    {
        if (this._enabled !== value) 
        {
            this._enabled = value;
            this.raisePropertyChanged('enabled');
        }
    },
    
    get_styles : function() 
    {
        return this._styles;
    },
    set_styles : function(value) 
    {
        if (this._styles !== value) 
        {
            this._styles = value;
            this.raisePropertyChanged('styles');
        }
    },
    
    // PUBLIC API END
    
    _updatePercentageHeight : function ()
    {        
        var wrapper = $get(this._wrapperElementID);
        if(wrapper.style.height.indexOf("%")> -1) 
        {       
            if(wrapper.offsetHeight != 0)
            {
                this._textBoxElement.style.height = wrapper.offsetHeight + "px";
                this._originalTextBoxCssText += "height:" + this._textBoxElement.style.height + ";";
            }
            else
            {
                var obj = this;
                window.setTimeout(
	            function()
	            {
	                obj._textBoxElement.style.height = wrapper.offsetHeight + "px";
                    obj._originalTextBoxCssText += "height:" + obj._textBoxElement.style.height + ";";                        
	            }, 
	            0);
            }
        }
    },
    
    _initializeHiddenElement : function(id)
    {
        this._hiddenElement = $get(id);
    },
    
    _initializeValidationField : function(id)
    {
    },
    
    _initializeButtons : function()
    {
        this._onButtonClickDelegate = Function.createDelegate(this, this._onButtonClickHandler);
        this.Button = null;
		var domElement = $get(this._wrapperElementID);
        var anchors = domElement.getElementsByTagName("a");
        
        for (i = 0; i < anchors.length; i++)
        {
			if (anchors[i].parentNode.className.indexOf("riBtn") != (-1))
            {
                this.Button = anchors[i];
                $addHandler(this.Button, "click", this._onButtonClickDelegate);        
            }            
        }   
    },

    _attachEventHandlers : function ()
    {   
        this._onTextBoxKeyUpDelegate = Function.createDelegate(this, this._onTextBoxKeyUpHandler);    
        this._onTextBoxKeyPressDelegate = Function.createDelegate(this, this._onTextBoxKeyPressHandler);
        this._onTextBoxBlurDelegate = Function.createDelegate(this, this._onTextBoxBlurHandler);
        this._onTextBoxFocusDelegate = Function.createDelegate(this, this._onTextBoxFocusHandler);
        this._onTextBoxKeyDownDelegate = Function.createDelegate(this, this._onTextBoxKeyDownHandler);
    
        $addHandler(this._textBoxElement, "keydown", this._onTextBoxKeyDownDelegate);
        $addHandler(this._textBoxElement, "keypress", this._onTextBoxKeyPressDelegate);
        $addHandler(this._textBoxElement, "keyup", this._onTextBoxKeyUpDelegate);    
        $addHandler(this._textBoxElement, "blur", this._onTextBoxBlurDelegate);
        $addHandler(this._textBoxElement, "focus", this._onTextBoxFocusDelegate);

        this._attachMouseEventHandlers();
    },
    
    _attachMouseEventHandlers : function()
    {
        //fix a problem with Safari and selectiononfocus
        if ($ITkey.isSafari)
        {
            this._onTextBoxMouseUpDelegate = Function.createDelegate(this, this._onTextBoxMouseUpHandler);        
            $addHandler(this._textBoxElement, "mouseup", this._onTextBoxMouseUpDelegate);        
        }
        
        this._onTextBoxMouseOutDelegate = Function.createDelegate(this, this._onTextBoxMouseOutHandler);
        this._onTextBoxMouseOverDelegate = Function.createDelegate(this, this._onTextBoxMouseOverHandler);
        this._onTextBoxMouseWheelDelegate = Function.createDelegate(this, this._onTextBoxMouseWheelHandler);
        this._onTextBoxDragDropDelegate = Function.createDelegate(this, this._onTextBoxDragDropHandler);        
        
        $addHandler(this._textBoxElement, "mouseout", this._onTextBoxMouseOutDelegate);   
        $addHandler(this._textBoxElement, "mouseover", this._onTextBoxMouseOverDelegate);
        
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer)
		{
		    //Gecko, Opera?
		    $addHandler(this._textBoxElement, "DOMMouseScroll", this._onTextBoxMouseWheelDelegate);
		    $addHandler(this._textBoxElement, "dragdrop", this._onTextBoxDragDropDelegate);
		}
		else
		{
			//IE
		    $addHandler(this._textBoxElement, "mousewheel", this._onTextBoxMouseWheelDelegate);
		    $addHandler(this._textBoxElement, "drop", this._onTextBoxDragDropDelegate);
		}        
    },
    
    //
    //Event Handlers
    //
    //fix a problem with Safari and selectiononfocus    
    _onTextBoxMouseUpHandler : function (e)
    {
        this._updateSelectionOnFocus();   
        e.preventDefault();
        e.stopPropagation();
    },
    
    _onTextBoxKeyPressHandler : function (e)
    {        
        var eventArgs = new ITkey.Web.UI.InputKeyPressEventArgs(e, e.charCode, String.fromCharCode(e.charCode));
        this.raise_keyPress(eventArgs);
        
        if (eventArgs.get_cancel())
        {
            e.stopPropagation();
		    e.preventDefault();        
			return false;        
        }           
       
        if ((e.charCode == 13) 
			&& !this.isMultiLine())
        {           
           this._updateHiddenValueOnKeyPress(e); 
           if (this.get_autoPostBack())
           {
               this._isEnterPressed = true;
               this.raisePostBackEvent();
               
               if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
               {
                    e.stopPropagation();
		            e.preventDefault();     
               }         
           }
           return true;
        }        
    },
    
    _onTextBoxKeyUpHandler : function (e)
    {
        this._updateHiddenValueOnKeyPress(e);  
    },     
    
    _onTextBoxBlurHandler : function (e)
    {
        if (!this._isInFocus)
        {
		    e.preventDefault();
            e.stopPropagation();            
            return false;
        }
            
        this._isInFocus = false;        
                    
        this._focused = false;
        
        var textBoxValue = this.get_textBoxValue(); 
        if (this._initialValue != textBoxValue)
        {
            this.set_value(textBoxValue);
        }
        else
        {
            this.updateDisplayValue();
            this.updateCssClass();
        }
        
        this.raise_blur(Sys.EventArgs.Empty); 
    },
    
    _onTextBoxFocusHandler : function (e)
    {
        this._updateStateOnFocus();
    },
    
    _updateStateOnFocus : function()
    {
        if (this._isDroped)
        {
            this._updateHiddenValue();
            this._isDroped = false;
        }
        this._isInFocus = true;      
        
        this._focused = true;
        this.updateDisplayValue();
        this.updateCssClass();
        if (!$ITkey.isSafari)
            this._updateSelectionOnFocus();
        this.raise_focus(Sys.EventArgs.Empty); 
    },
    
    _onTextBoxMouseOutHandler : function (e)
    {
        this._hovered = false;
        this.updateCssClass();
        this.raise_mouseOut(Sys.EventArgs.Empty); 
    },    
    
    _onTextBoxMouseOverHandler : function (e)
    {
        this._hovered = true;
        this.updateCssClass(); 
        this.raise_mouseOver(Sys.EventArgs.Empty); 
    },    
    
	_onTextBoxKeyDownHandler : function(e)
	{
	},    
	
    _onTextBoxMouseWheelHandler : function(e)
	{
	    var delta;
	    if (this._focused)
	    {            
	    
            if (e.rawEvent.wheelDelta) 
            { 
                /* IE/Opera. */
                delta = e.rawEvent.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.*/
                if (window.opera)
                    delta = -delta;
            } 
            else if (e.detail) 
            {
                    /** Mozilla case. In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3.*/
                    delta = - e.rawEvent.detail/3;
            }
            else if (e.rawEvent && e.rawEvent.detail)
            {
                    /** Firefox 3.0 case.**/
                    delta = - e.rawEvent.detail/3;
            }
            
            
            if (delta > 0)
            {
                this._handleWheel(false);
            }
            else
            {
                this._handleWheel(true);
            }
            
            return true;
        }
        return false;
	},
    
    _onButtonClickHandler : function (e)
    {
        var eventArgs = new ITkey.Web.UI.InputButtonClickEventArgs(ITkey.Web.UI.InputButtonType.Button);
        this.raise_buttonClick(eventArgs); 
    },    
    
    _onTextBoxDragDropHandler : function (e)
    {    
          this._isDroped = true;
    },
       
    _getValidationField : function()
    {
        return this._hiddenElement;
    },
	
    _calculateSelection : function ()
    {
        if ((Sys.Browser.agent == Sys.Browser.Opera) || !document.selection)
        {
            this._selectionEnd = this._textBoxElement.selectionEnd;
            this._selectionStart = this._textBoxElement.selectionStart;
            return;
        }
        
        var s1 = document.selection.createRange();
        if (s1.parentElement() != this._textBoxElement) return;
        var s = s1.duplicate();

        s.move('character', -this._textBoxElement.value.length);
        
        s.setEndPoint('EndToStart', s1);
        
        var sel1 = s.text.length;
        var sel2 = s.text.length +  s1.text.length;
        this._selectionEnd = Math.max(sel1, sel2);
        this._selectionStart = Math.min(sel1, sel2);
    },
    
    _SetValue : function (newValue)
    {
        var validValue = this._setHiddenValue(newValue);
			
        if (typeof(validValue) == "undefined" || validValue == true)
        {
            this.set_textBoxValue(this.get_editValue());
        }
    },    
        
    _triggerDOMChangeEvent : function(element) 
    {
        if (element.fireEvent && document.createEventObject) 
        {
            var eventObject = document.createEventObject();
            
                element.fireEvent("onchange", eventObject);
            
        } 
        else if (element.dispatchEvent)
        {
            var canBubble = true;
            var eventObject = document.createEvent("HTMLEvents");
            eventObject.initEvent("change", canBubble, true);
            element.dispatchEvent(eventObject);
        }
    },   
    
    _updateSelectionOnFocus : function()
    {
        if (!this.get_textBoxValue())
            this.set_caretPosition(0);
        
        switch (this.get_selectionOnFocus())
        {
            case ITkey.Web.UI.SelectionOnFocus.None:
                break;
            case ITkey.Web.UI.SelectionOnFocus.CaretToBeginning:

                this.set_caretPosition(0);
                break;
            case ITkey.Web.UI.SelectionOnFocus.CaretToEnd:
                if (this._textBoxElement.value.length > 0)
                {
					if ($ITkey.isIE && this.isMultiLine())
					{
						// fix the caret's end position when new lines are present
						var newLines10 = 0;
						var newLines13 = 0;
						for (var j = 0; j < this._textBoxElement.value.length; j++)
						{
							if (this._textBoxElement.value.charCodeAt(j) == 10)
							{
								newLines10++;
							}
							else if (this._textBoxElement.value.charCodeAt(j) == 13)
							{
								newLines13++;
							}
						}
						
						this.set_caretPosition(this._textBoxElement.value.length - Math.max(newLines10, newLines13));
					}
					else
					{
						this.set_caretPosition(this._textBoxElement.value.length);
					}
                }
                break;
            case ITkey.Web.UI.SelectionOnFocus.SelectAll:
                this.selectAllText();
                break;            
            default : 
				this.set_caretPosition(0); //CaretToBeginning
				break;
        }
    },	   
    
    _applySelection : function ()
    {
            if ((Sys.Browser.agent == Sys.Browser.Opera) || !document.selection)
            {
                this._textBoxElement.selectionStart = this._selectionStart;
                this._textBoxElement.selectionEnd = this._selectionEnd;
                return;
            }

            this._textBoxElement.select();
            sel = document.selection.createRange();
            sel.collapse();
            sel.moveStart('character', this._selectionStart);
            sel.collapse();
            sel.moveEnd('character', this._selectionEnd - this._selectionStart);
            sel.select();        
    },     
    
    _clearHiddenValue : function()
    {
        this._hiddenElement.value = "";
    },   
    
    _handleWheel : function(isNegativeWheel)
	{
	},

    _setHiddenValue : function(value)
    {
        if (this._hiddenElement.value != value.toString())
        {
            this._hiddenElement.value = value;
        }
        
        this._setValidationField(value);
        
        return true;
    },
    
    _setValidationField : function(value)
    {
    },     
    
    _updateHiddenValueOnKeyPress : function()
    {
		this._updateHiddenValue();
    },
    
    _updateHiddenValue : function()
    {
        return this._setHiddenValue(this._textBoxElement.value);
    }, 
    
    _escapeNewLineChars : function(text, replaceWith)
    { 
        text = escape(text);
        var i;
        for(i = 0; i < text.length; i++)
        { 
            if(text.indexOf("%0D%0A") > -1)
            { 
                text = text.replace("%0D%0A", replaceWith);
            }
            else if (text.indexOf("%0A") > -1)
            { 
                text = text.replace("%0A", replaceWith);
            }
            else if (text.indexOf("%0D") > -1)
            { 
                text = text.replace("%0D", replaceWith);
            }       
        }
        return unescape(text);
    },
    
    _isNormalChar : function(e)
	{
        if (($ITkey.isFirefox&& e.rawEvent.keyCode) 
            || 
            ($ITkey.isOpera && e.rawEvent.which == 0)
            ||
            ($ITkey.isSafari && (e.charCode < Sys.UI.Key.space || e.charCode > 60000)))
        {
            return false;
        }	
        return true;
	},
    
    //register events    
    add_blur: function(handler) 
    {
        this.get_events().addHandler('blur', handler);
    },
    remove_blur: function(handler) 
    {
        this.get_events().removeHandler('blur', handler);
    },
	raise_blur : function(args)
	{
		this.raiseEvent("blur", args);
	}, 
	
    add_mouseOut : function(handler) 
    {
        this.get_events().addHandler('mouseOut', handler);
    },
    remove_mouseOut : function(handler) 
    {
        this.get_events().removeHandler('mouseOut', handler);
    },
	raise_mouseOut : function(args)
	{
		this.raiseEvent("mouseOut", args);
	},   
	
    add_valueChanged : function(handler) 
    {
        this.get_events().addHandler('valueChanged', handler);
    },
    remove_valueChanged : function(handler) 
    {
        this.get_events().removeHandler('valueChanged', handler);
    },
	raise_valueChanged : function(newValue, oldValue)
	{
        if (newValue.toString() == oldValue.toString())
            return false;
        this._initialValue = this.get_value();
        var eventArgs = new ITkey.Web.UI.InputValueChangedEventArgs(newValue, oldValue);
        this.raiseEvent("valueChanged", eventArgs);
        
		var shouldPostBack = !eventArgs.get_cancel();		    
		
        if (this.get_autoPostBack() && shouldPostBack && !this._isEnterPressed)
            this.raisePostBackEvent();
	},
	
    add_error : function(handler) 
    {
        this.get_events().addHandler('error', handler);
    },
    remove_error : function(handler) 
    {
        this.get_events().removeHandler('error', handler);
    },
    
    raise_error : function(args)
    {
        if (this.InEventRaise)
        {
            return;
        }

        this.InEventRaise = true;
        this.raiseEvent("error", args);
        if (!args.get_cancel())
        {
            this._invalid = true;
            this._errorHandlingCanceled = false;
            
            this.updateCssClass();
        	
            var instance = this;
            var restore = function()
            {
	            instance._invalid = false;
	            instance.updateCssClass();
            }
            setTimeout(restore, this.get_invalidStyleDuration());	        
        }
        else
        {
            this._errorHandlingCanceled = true;
        }
        
        this.InEventRaise = false;	
    },
	
    add_load : function(handler) 
    {
        this.get_events().addHandler('load', handler);
    },
    remove_load : function(handler) 
    {
        this.get_events().removeHandler('load', handler);
    },
	raise_load : function(args)
	{
		this.raiseEvent("load", args);
	},
	
    add_mouseOver : function(handler) 
    {
        this.get_events().addHandler('mouseOver', handler);
    },
    remove_mouseOver : function(handler) 
    {
        this.get_events().removeHandler('mouseOver', handler);
    },
	raise_mouseOver : function(args)
	{
		this.raiseEvent("mouseOver", args);
	},
	
    add_focus : function(handler) 
    {
        this.get_events().addHandler('focus', handler);
    },
    remove_focus : function(handler) 
    {
        this.get_events().removeHandler('focus', handler);
    },
	raise_focus : function(args)
	{
		this.raiseEvent("focus", args);
	},
	
    add_disable : function(handler) 
    {
        this.get_events().addHandler('disable', handler);
    },
    remove_disable : function(handler) 
    {
        this.get_events().removeHandler('disable', handler);
    },
	raise_disable : function(args)
	{
		this.raiseEvent("disable", args);
	},
	
    add_enable : function(handler) 
    {
        this.get_events().addHandler('enable', handler);
    },
    remove_enable : function(handler) 
    {
        this.get_events().removeHandler('enable', handler);
    },
	raise_enable : function(args)
	{
		this.raiseEvent("enable", args);
	},	
	
    add_keyPress : function(handler) 
    {
        this.get_events().addHandler('keyPress', handler);
    },
    remove_keyPress : function(handler) 
    {
        this.get_events().removeHandler('keyPress', handler);
    },
	raise_keyPress : function(args)
	{
		this.raiseEvent("keyPress", args);
	},
	
    add_enumerationChanged : function(handler) 
    {
        this.get_events().addHandler('enumerationChanged', handler);
    },
    remove_enumerationChanged : function(handler) 
    {
        this.get_events().removeHandler('enumerationChanged', handler);
    },
	raise_enumerationChanged : function(args)
	{
		this.raiseEvent("enumerationChanged", args);
	},
	
    add_moveUp : function(handler) 
    {
        this.get_events().addHandler('moveUp', handler);
    },
    remove_moveUp : function(handler) 
    {
        this.get_events().removeHandler('moveUp', handler);
    },
	raise_moveUp : function(args)
	{
		this.raiseEvent("moveUp", args);
	},
	
    add_moveDown : function(handler) 
    {
        this.get_events().addHandler('moveDown', handler);
    },
    remove_moveDown : function(handler) 
    {
        this.get_events().removeHandler('moveDown', handler);
    },
	raise_moveDown : function(args)
	{
		this.raiseEvent("moveDown", args);
	},
	
    add_buttonClick : function(handler) 
    {
        this.get_events().addHandler('buttonClick', handler);
    },
    remove_buttonClick : function(handler) 
    {
        this.get_events().removeHandler('buttonClick', handler);
    },
	raise_buttonClick : function(args)
	{
		this.raiseEvent("buttonClick", args);
	},
	
    add_valueChanging : function(handler) 
    {
        this.get_events().addHandler('valueChanging', handler);
    },
    remove_valueChanging : function(handler) 
    {
        this.get_events().removeHandler('valueChanging', handler);
    },
	raise_valueChanging : function(args)
	{
		this.raiseEvent("valueChanging", args);
	}								
}

//$ITkey.makeCompatible(ITkey.Web.UI.ItkInputControl);
ITkey.Web.UI.ItkInputControl.registerClass('ITkey.Web.UI.ItkInputControl', ITkey.Web.UI.ItkWebControl);


// fix ValidatorSetFocus: Work Item #6120
if (typeof(ValidatorSetFocus) == "function")
{
    ValidatorSetFocus = function (val, event) 
    {
        var ctrl;
        if (typeof(val.controlhookup) == "string") 
        {
            var eventCtrl;
            if ((typeof(event) != "undefined") && (event != null)) 
            {
                if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null)) 
                {
                    eventCtrl = event.srcElement;
                }
                else 
                {
                    eventCtrl = event.target;
                }
            }
            if ((typeof(eventCtrl) != "undefined") && (eventCtrl != null) &&
                (typeof(eventCtrl.id) == "string") &&
                (eventCtrl.id == val.controlhookup)) 
            {
                ctrl = eventCtrl;
            }
        }
        if ((typeof(ctrl) == "undefined") || (ctrl == null)) 
        {
            ctrl = document.getElementById(val.controltovalidate);
        }
        var isItkInputControl = false;
        if ((ctrl.style)
            && (typeof(ctrl.style.visibility) != "undefined") 
            && (ctrl.style.visibility == "hidden")
            && (typeof(ctrl.style.width) != "undefined")
            && (document.getElementById(ctrl.id + "_text"))
            && (ctrl.tagName.toLowerCase() == "input"))
        {
            isItkInputControl = true;     
        }       
        if ((typeof(ctrl) != "undefined") && (ctrl != null) &&  
            (ctrl.tagName.toLowerCase() != "table" || (typeof(event) == "undefined") || (event == null)) &&
            ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
            (typeof(ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
            (typeof(ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
            (IsInVisibleContainer(ctrl) || isItkInputControl)) 
        {
            if (ctrl.tagName.toLowerCase() == "table" &&
                (typeof(__nonMSDOMBrowser) == "undefined" || __nonMSDOMBrowser)) 
            {
                var inputElements = ctrl.getElementsByTagName("input");
                var lastInputElement = inputElements[inputElements.length -1];
                if (lastInputElement != null) 
                {
                    ctrl = lastInputElement;
                }
            }

            if (typeof(ctrl.focus) != "undefined" && ctrl.focus != null) 
            {
                if (isItkInputControl)
                    document.getElementById(ctrl.id + "_text").focus();
                else
                    ctrl.focus();
                    
                Page_InvalidControlToBeFocused = ctrl;
            }
        }
    } 
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();