var mtValidChars;
var mtSpecialChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
var mtClipboardChars = /[cvxz]/i;
var mtValidString;

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

function mtIsKey13(objEvent)
{
	var iKeyCode;  
	if (isIE)
	{
		iKeyCode = objEvent.keyCode;
	}
	else
	{
		iKeyCode = objEvent.which;
	}
	
	if (iKeyCode == 13)
	{
		return true;
	}
	return false;
}

function mtKeyPress(objEvent,mtType)
{
	var iKeyCode, strKey, objInput;  
	if (isIE)
	{
		iKeyCode = objEvent.keyCode;
		objInput = objEvent.srcElement;
	}
	else
	{
		iKeyCode = objEvent.which;
		objInput = objEvent.target;
	}
               
    if (mtType == "int")
	{
		mtValidChars = /[0-9]/;
	}
    else if (mtType == "dec")
	{
		mtValidChars = /[0123456789.]/;
	}

	strKey = String.fromCharCode(iKeyCode);
   
	if (isValid(objInput.value,mtType))
	{
		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,mtType)
{
    if (mtType == "int")
	{
		mtValidString = /^[0-9]*$/;
	}
    else if (mtType == "dec")
	{
		mtValidString = /^[0-9]*\.?[0-9]*$/;
	}

	return mtValidString.test(str) || str.length == 0;
}

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

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

	if (isIE)
	{
		objInput = objEvent.srcElement; 
	}
	else
	{
		objInput = objEvent.target;
    }
   
	if (!isValid(strPasteData,mtType))
	{
		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;
			if (fieldType != null)
			{
				fieldType = fieldType.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;
				}
			}
		}
	}
}

