var mtValidChars = /[0-9]/;
var mtSpecialChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
var mtClipboardChars = /[cvxz]/i;
var mtValidString = /^[0-9]*$/;

var isIE = navigator.userAgent.toLowerCase().indexOf('msie');
var isNS6 = navigator.userAgent.toLowerCase().indexOf('netscape6');
var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox');

function mtKeyPress(objEvent)
{
	var iKeyCode, strKey, objInput;  
	if (isIE)
	{
		iKeyCode = objEvent.keyCode;
		objInput = objEvent.srcElement;
	}
	else
	{
		iKeyCode = objEvent.which;
		objInput = objEvent.target;
	}
               
	strKey = String.fromCharCode(iKeyCode);
   
	if (isValid(objInput.value))
	{
		objInput.lastValue = objInput.value;
   
		if (!mtValidChars.test(strKey) && !mtSpecialChars.test(strKey) && !checkClipboardCode(objEvent, strKey))
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

   
function checkClipboardCode(objEvent, strKey)
{
	if (isNS6)
	{
		return objEvent.ctrlKey && mtClipboardChars.test(strKey);
	}
	else
	{
		return false;
	}
}
   
function isValid(str)
{
	return mtValidString.test(str) || str.length == 0;
}

function mtChange(objEvent) {
	var objInput;
            
	if (isIE)
	{
		objInput = objEvent.srcElement; 
	}
	else
	{
		objInput = objEvent.target;
    }
             
	if (!isValid(objInput.value))
	{
		objInput.value = objInput.lastValue || "";
		objInput.focus();
		objInput.select(); 
	}
	else
	{
		objInput.lastValue = objInput.value;
	}
}

function mtPaste(objEvent)
{
	var strPasteData = window.clipboardData.getData("Text");

	if (isIE)
	{
		objInput = objEvent.srcElement; 
	}
	else
	{
		objInput = objEvent.target;
    }
   
	if (!isValid(strPasteData))
	{
		objInput.focus();
		return false;
	}
}

function focusOnFirstEditionField()
{
	if (document.main_form != null)
	{
		var form = document.main_form;
		for (i = 0; i < form.length; i++)
		{
			field = form.elements[i];
			fieldType = field.type.toLowerCase();
			if ((field.disabled != true) && (field.name != "login") && (field.name != "password") && ((fieldType == "text") || (fieldType == "textarea") || (fieldType == "file") || (fieldType == "radio") || (fieldType == "checkbox") || (fieldType == "select-one") || (fieldType == "select-multiple")))
			{
				field.focus();
				break;
			}
		}
	}
}

