///
/// Event codes
///
var mcntTextBoxRealTimeKeyDownId = 1;
var mcntTextBoxRealTimeKeyPressId = 2;
var mcntTextBoxRealTimeKeyUpId = 4;


/// <method name="TextBox_Execute">
/// <summary>
/// Executes a textbox command(used by the server control)
/// </summary>
/// <param name="strGuid">The component id.</param>
/// <param name="strCommand">The command to be executed.</param>
/// <param name="strParamA">The first command parameter.</param>
/// <param name="strParamB">The second command parameter.</param>
function TextBox_Execute(strGuid,strCommand,strParamA,strParamB)
{
    // Get textarea/input element
    var objElement = Web_GetTargetElementByDataId(strGuid);
    if (objElement)
    {
        switch (strCommand)
        {
            case "Select":                
                var intStart = parseInt(strParamA);
                var intLength = parseInt(strParamB);
                Web_SetSelection(objElement,intStart,intLength);
                Data_SetTextSelectionData(strGuid,intStart,intLength);
                break;
            case "ScrollToCaret":
                Web_ScrollToCaret(objElement);
                break;
        }
    }
}
/// </method>

/// <method name="TextBox_OnSelect">
/// <summary>
///
/// </summary>
/// <param name="strGuid">TextBox guid.</param>
/// <param name="objInput">The input control value</param>
function TextBox_OnSelect(strGuid,objInput)
{
    Events_TextSelectionChanged(strGuid ,objInput);
}
/// </method>

/// <method name="TextBox_Blur">
/// <summary>
///
/// </summary>
/// <param name="strGuid">TextBox guid.</param>
/// <param name="objInput">The input control value</param>
/// <param name="objWindow">The text box window</param>
/// <param name="blnIsEnter">The event was generated by enter key</param>
function TextBox_Blur(strGuid,objInput,objWindow)
{
    // Get the input node 
    var objNode = Data_GetNode(strGuid);
    
    // Raise TextChange event
    Common_TextChange(strGuid,objInput.value,objWindow);
}
/// </method>

function TextBox_IsDeleteKey(intKeyCode)
{
    return (intKeyCode==mcntDeleteKey || intKeyCode==mcntBackspaceKey);
}

function TextBox_IsMultiline(strGuid)
{
    var blnIsMultiline = false;
    
    if(strGuid && strGuid!="")
    {
        // Get text box node
	    var objNode = Data_GetNode(strGuid);
	    if(objNode)
	    {
	        blnIsMultiline = Xml_IsAttribute(objNode,"Attr.Multiline","1");
	    }
    }
    
    return blnIsMultiline;
}



/// <method name="TextBox_KeyPress">
/// <summary>
/// TextBox_KeyPress - Handles onkeypress event for all types of keys
/// </summary>
function TextBox_KeyPress(strGuid,strCharacterValidationMask,strCharacterValidationExpression,objWindow,objInput,objEvent)
{
    // Get the pressed key
    var intKeyCode = Web_GetEventKeyCode(objEvent)*1;

    // Check if enter was pressed.
    if (intKeyCode==mcntEnterKey && !TextBox_IsMultiline(strGuid))
    {
        Common_TextChange(strGuid,objInput.value,objWindow,true);
    }
    
    // Check if a character was pressed and that the control key is not held.
    else if(Web_IsCharacterPressed(objEvent) && !Web_IsControl(objEvent))
    {
        // Validate character.
        if(!TextBox_IsValidCharacter(intKeyCode,strCharacterValidationMask,strCharacterValidationExpression,objWindow,objInput,objEvent))
        {
	        // Cancel last key press.
            Web_EventCancelBubble(objEvent);
        }
    }

    // check if key events sync is needed
    Common_TextBoxSyncKeyEvent(strGuid, objWindow, objEvent, objInput, "TextBoxRealTimeKeyPress");
}
/// </method>


/// <method name="TextBox_KeyPressMultiLine">
/// <summary>
/// Handles the Key Multi line
/// </summary>
function TextBox_KeyPressMultiLine(strGuid,objTextArea,objEvent,objWindow)
{
    
    objWindow = objWindow || window;
    // Check if the pressed key is a character and the ctrl button is not pressed
    if ((Web_IsCharacterPressed(objEvent) && !Web_IsControl(objEvent)) || objEvent.keyCode == mcntEnterKey)
    {
        // Try getting selection range.
        var objSelection = Web_GetSelectionRange(objTextArea);

        // If there is no selected text
        if (!objSelection || objSelection.Start == objSelection.End)
        {
            // Gets max length attribute.
            var intMaxLength = parseInt(Data_GetAttribute(strGuid, "Attr.MaxLength", "0"), 10);
            if (intMaxLength > 0)
            {
                //If we reached the max length Stop
                if (objTextArea.value.length >= intMaxLength)
                {
                    // Cancel default behavior.
                    Web_EventCancelBubble(objEvent, true, false);
                }
            }
        }
    }

    // check if key events sync is needed
    Common_TextBoxSyncKeyEvent(strGuid, objWindow, objEvent, objTextArea, "TextBoxRealTimeKeyPress");
}
/// </method>


/// <method name="TextBox_KeyDown">
/// <summary>
/// TextBox_KeyDown - Handle onkeydown event for all types of keys
/// </summary>
function TextBox_KeyDown(strGuid,objWindow,objEvent,objElement)
{
    // Get the pressed key
	var intKeyCode = Web_GetEventKeyCode(objEvent);
    
	// Check event code
	if (intKeyCode == mcntTabKey)
	{
	    // Get data node
	    var objNode = Data_GetNode(strGuid);
	    if (objNode)
	    {
	        if (Xml_IsAttribute(objNode, "Attr.AcceptsTab", "1"))
	        {
	            // Gets max length attribute.
	            var intMaxLength = parseInt(XML_GetAttribute(objNode, "Attr.MaxLength", "-1"), 10);

	            var blnSetTab = false;

	            //If we reached the max length Stop or that there is no limit
	            if (intMaxLength == -1 || (objElement.value.length < intMaxLength))
	            {
	                blnSetTab = true;
	            }
	            else //check if some text is selected so the tab will replace it
	            {
	                var objRange = Web_GetSelectionRange(objElement);
	                if (objRange.End - objRange.Start > 0)
	                {
	                    blnSetTab = true;
	                }
	            }

	            if (blnSetTab)
	            {
	                //set the character "\t" for tab character
	                Web_SetSelectedTextValue(objEvent, objWindow, "\t");
	            }

	            // Cancel bubble
	            Web_EventCancelBubble(objEvent);
	        }
	    }
	}

    Common_TextBoxSyncKeyEvent(strGuid, objWindow, objEvent, objElement, "TextBoxRealTimeKeyDown");
}
/// </method>

/// <method name="TextBox_KeyUp">
/// <summary>
/// TextBox_KeyUp - Handle onkeyup event for all types of keys
/// </summary>
function TextBox_KeyUp(strGuid, objWindow, objEvent, objInput)
{
    Common_TextBoxRaiseKeyEvent(strGuid, objWindow, objEvent, objInput, "TextBoxRealTimeKeyUp");
}
/// </method>

/// <method name="TextBox_OnPasteAsync">
/// <summary>
/// 
/// </summary>
function TextBox_OnPasteAsync(strID, objTextElement, blnIsTextArea)
{
    if (objTextElement)
    {
        if (blnIsTextArea)
        {
            // Textarea does not have a max-length attribute so we need to simulate it
            var intMaxLength = parseInt(Data_GetAttribute(strID, "Attr.MaxLength", "0"), 10);

            if (intMaxLength > 0)
            {
                // Get text area value.
                var strTextAreaValue = objTextElement.value;

                // Check if text area value exceeded maximal length. 
                if (strTextAreaValue.length > intMaxLength)
                {
                    objTextElement.value = strTextAreaValue.substr(0, intMaxLength);
                }
            }
        }
    }
}
/// </method>

/// <method name="TextBox_OnPaste">
/// <summary>
/// 
/// </summary>
function TextBox_OnPaste(objTextElement, strID, blnIsTextArea)
{
    Aux_InvokeCallbackDelegateWithDelay(new Aux_CallbackDelegate(TextBox_OnPasteAsync, strID, objTextElement, blnIsTextArea), 1);
}
/// </method>

// For compatibility
var TextBox_EnforcePaste = TextBox_OnPasteAsync;

/// <method name="TextBox_IsValidCharacter">
/// <summary>
/// Checks if a character is valid.
/// </summary>
function TextBox_IsValidCharacter(intKeyCode,strCharacterValidationMask,strCharacterValidationExpression,objWindow,objInput,objEvent)
{
    // Define a casing indicator.
    var blnValidCharacter = true;
    
    // Get char value as string.
    var strValue  = String.fromCharCode(intKeyCode);
              
    // Check mask.
    if (strCharacterValidationMask!="")
    { 		          
        // Build a RegExp object as for the textbox's mask.
        var objRegExp = new RegExp("[" +strCharacterValidationMask+ "]","gm");
		
	    // Check mask matching.
        blnValidCharacter = objRegExp.test(strValue);
    }
    
    if(blnValidCharacter && strCharacterValidationExpression!="")
    {
       blnValidCharacter = Web_ValidateValue(strCharacterValidationExpression,strValue,objInput,objWindow,objEvent);
    }
    
    return blnValidCharacter;
}
/// </method>

/// <method name="TextBox_ClearValue">
/// <summary>
/// Clear the receievd element's inner text property.
/// </summary>
function TextBox_ClearValue(objElement)
{
    if(objElement)
    {
        Web_SetInnerText(objElement,"");
    }
}
/// </method>
