﻿/*
 * $Id: CustomTextBox.js 12 2008-11-11 16:18:11Z marina $
 *
 * (c) 2007-2008 Microdivision
 */

var enterKeyCode = 13;

// Processes clicking the button specified by the buttonID on pressing Enter.
function processEnter(buttonID, evt)
{
	var currentEvent = false;
	if (typeof(evt) != 'undefined')
		currentEvent = evt;
	if (!currentEvent && window.event)
		currentEvent = window.event;

	if (currentEvent && (currentEvent.which || currentEvent.keyCode))
	{
		if (currentEvent.which == enterKeyCode || currentEvent.keyCode == enterKeyCode)
		{
			var button = document.getElementById(buttonID);
			
			if (typeof(button) == "undefined" || button == null)
			{
				var errorMessage = "CustomTextBox.js";
				errorMessage += "\nprocessEnter()";
				errorMessage += "\nError: The buttonID is not set correctly!";
				alert(errorMessage);
			}
			else if (typeof(button.click) != 'undefined')
			{
				button.focus();
				button.click();
			}
			else
			{
				if (button.tagName.toUpperCase() == 'A')
				{
					if (button.onclick != '')
					{
						eval('var func = ' + button.onclick);
						var result = func.call();
						if (!result)
							return;
					}
				
					if (button.href != '')
						eval(button.href.replace('javascript:', ''));
				}
			}
		}
	}
}

// Simple helper to return the "exMaxLength" attribute for
// the specified field. Using "getAttribute" won't work
// with Firefox.
function getMaxLength(targetField)
{
	return targetField.exMaxLength;
}

// Limits the text input in the specified field.
function limitInput(targetField, sourceEvent)
{
	var isPermittedKeystroke;
	var enteredKeystroke;
	var maximumFieldLength;
	var currentFieldLength;
	var inputAllowed = true;
	var selectionLength = parseInt(getSelectionLength(targetField));

	if (getMaxLength(targetField) != null)
	{
		// Get the current and maximum field length.
		currentFieldLength = parseInt(targetField.value.length);
		maximumFieldLength = parseInt(getMaxLength(targetField));

		// Allow non-printing, arrow and delete keys
		enteredKeystroke = window.event ? sourceEvent.keyCode : sourceEvent.which;
		isPermittedKeystroke = ((enteredKeystroke < 32) || 
			(enteredKeystroke >= 33 && enteredKeystroke <= 40) ||
			(enteredKeystroke == 46));

		// Decide whether the keystroke is allowed to proceed.
		if (!isPermittedKeystroke)
		{
			if ((currentFieldLength - selectionLength) >= maximumFieldLength)
				inputAllowed = false;
		}

		// Force a trim of the textarea contents if necessary.
		if (currentFieldLength > maximumFieldLength)
			targetField.value = targetField.value.substring(0, maximumFieldLength);
	}
 
	sourceEvent.returnValue = inputAllowed;
	return inputAllowed;
}

// Limits the text input in the specified field.
function limitPaste(targetField, sourceEvent)
{
	var clipboardText;
	var resultantLength;
	var maximumFieldLength;
	var currentFieldLength;
	var pasteAllowed = true;
	var selectionLength = getSelectionLength(targetField);

	if (getMaxLength(targetField) != null)
	{
		// Get the current and maximum field length.
		currentFieldLength = parseInt(targetField.value.length);
		maximumFieldLength = parseInt(getMaxLength(targetField));
		
		clipboardText = window.clipboardData.getData("Text");
		resultantLength = currentFieldLength + clipboardText.length - selectionLength;

		if (resultantLength > maximumFieldLength)
			pasteAllowed = false;
	}

	sourceEvent.returnValue = pasteAllowed;
	return pasteAllowed;
}

// Returns the number of selected characters in the specified element.
function getSelectionLength(targetField)
{
	if (targetField.selectionStart == undefined)
		return document.selection.createRange().text.length;
	else
		return targetField.selectionEnd - targetField.selectionStart;
}