
/**
* Para ser utilizado no evento onKeyPress
* ex: <input type="text" name="xyz" onkeypress="return maskField(this, '99999-999', event);">
* CEP    -> 99999-999
* CPF    -> 999.999.999-99
* CNPJ   -> 99.999.999/9999-99
* C/C    -> 999999-!
* Tel    -> (99) 9999-9999
* Hora   -> 99:99:99 ou 99:99
* N?mero -> R$ 99.999,99
*/
function maskField(campo, sMask, event) {

	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	if(document.all) { // Internet Explorer
		nTecla = event.keyCode;
	} else { // Nestcape
		nTecla = event.charCode;
	}
	
	if(nTecla == 39) return false;

	sValue = campo.value;
	fldLen = sValue.length;
	mskLen = sMask.length;

	if (nTecla >= 32) { // Caracteres acima de espa?o
		if (fldLen < mskLen) {
			if ((sMask.charAt(fldLen) != "!") && (sMask.charAt(fldLen) != "9")) {
				sValue = sValue + sMask.charAt(fldLen);
				campo.value = sValue;
				if ((nTecla < 48) || (nTecla > 57)) {
					return false;
				}
			} else {
				if (sMask.charAt(fldLen) == "9") {
					if ((nTecla < 48) || (nTecla > 57)) {
						return false;
					}
				}
			}
			return true;
		} else {
			return false;
		}
	} else { // Caracteres de controle
		return true; 
	}

}
	 
	/**
	 * Use o evento onKeyPress para executar esta fun??o
	 * ex.: <input type="text" name="xyz" onkeypress="return validateKeyNumber(this, 10, event);">
	 * 
	 * @param field					campo a ser mascarado
	 * @param maxCharBeforDecimal	maximo de caracteres antes da virgula, incluindo os pontos separadores de milhar
	 * @param event					evento do teclado capturado	
	 * 
	 */
	function validateKeyNumber(field, maxCharBeforDecimal, event) {
		var valor = field.value
		var key;

		if(document.all) { // Internet Explorer
			key = event.keyCode;
		} 
		else { // Nestcape
			key = event.charCode;
		}

		if (key == 44) {
			if (valor == '') {
				return false;
			} 
			else if (findVirgula(valor)) {
				return false;
			}
		} 
		else if (key < 48 || key > 57) {
			if (key > 0) {
				return false;
			}
		}

		if (valor.length == maxCharBeforDecimal && !findVirgula(valor)) {
			if(key != 44 && key != 0) {
				return false;
			}
		}

		if (valor.length == 1 && valor.substring(0, 1) == '0') {
			if(key != 44 && key != 0) {
				return false;
			}
		}

		if (findVirgula(valor)) {
			lastCharacter = valor.substring(valor.length - 1, valor.length);
			
			if (lastCharacter != ',') {
				index = valor.indexOf(',');
				decimal = valor.substring(index + 1, valor.length);
				
				if (decimal.length == 2 && key != 0) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Use o evento onKeyUp para executar esta fun??o
	 * ex.: <input type="text" name="xyz" onkeyup="return formatMoney(this, event);">
	 *
	 * @param field		campo a ser mascarado
	 * @param event		evento do teclado capturado	
	 *
	 */
	function formatMoney(field, event)	{
		var key;
		var valor = field.value;

		if(document.all) { // Internet Explorer
			key = event.keyCode;
		} 
		else { // Nestcape
			key = event.charCode;
		}

		if (valor.length >= 3 && valor.substring(0, 1) == '0') {
			if (valor.substring(1, 2) != ',') {
				field.value = valor.substring(1, valor.length)
			}
		}

		if (valor.length > 0 && valor.substring(0, 1) == ',') {
			field.value = valor.substring(1, valor.length)
		}

		if (key != 36 && key != 37 && key != 39 && key != 38 &&	key != 40 ) {
			formatNumber(field)
		}
		
		return true;
	}

	function findVirgula(pDado)	{
		if(pDado.indexOf(',') > -1) {
			return true
		}

		return false
	}

	function formatNumber(field)  {
		dec 	= ''
		arrNum 	= new Array()
		valor	= field.value

		if (findVirgula(valor)) {
			indVirg	= valor.indexOf(',')
			dec 	= valor.substring(indVirg, valor.length)
			valor   = valor.substring(0, indVirg)
		}

		valor = removePoint(valor)

		if (dec != '' && dec.indexOf('.') > -1) {
			dec = removePoint(dec)
		}

		div = valor / 1000

		if (div >= 1) {
			j = 0

			for(i = valor.length; i > 0; i = i - 3) {
				if (i - 3 > 0) {
					aux = valor.substring(i - 3, i)
					arrNum[j++] = '.' + aux
				} else {
					aux = valor.substring(0, i)
					arrNum[j++] = aux
				}
			}
			formattedValue = '';

			for(k = j - 1; k >= 0; k--) {
				formattedValue += arrNum[k]
			}

			field.value = formattedValue + dec
		} 
		else {
			field.value = valor + dec
		}
	}

	function removePoint(value)	{
		for(j = 0; j < value.length; j++) {
			if (value.indexOf('.') > 0) {
				j 	 	= value.indexOf('.')
				aux1 	= value.substring(0, j)
				aux2 	= value.substring(j + 1, value.length)
				value	= aux1 + aux2
			}
		}

		return value
	}
	
	/**
	 * Use esta fun??o no evento onblur
	 * ex.: <input type="text" name="xyz" onblur="return formatDecimal(this);">
	 *
	 * @param textField	 campo a ser mascarado
	 *
	 */
	function formatDecimal(textField) {
		value 	= textField.value;
		indVirg	= value.indexOf(',');
		isEmpty	= value == '';
		
		if (isEmpty) {
			return true;
		}
		if (indVirg == -1) {
			value 			= value + ',00';
			textField.value = value;
		}
		else if (indVirg == value.length - 1) {
			value 			= value + '00';
			textField.value = value;
		}
		else {
			decimal = value.substring(indVirg + 1, value.length);
			
			if (decimal.length < 2) {
				textField.value = value + '0';
			}
		}
	}
	
	/**
	 * Use esta fun??o no evento onblur
	 * ex.: <input type="text" name="xyz" onblur="return formatDecimal1(this);">
	 *
	 * @param textField	 campo a ser mascarado
	 *
	 */
	function formatDecimal1(textField) {
		value 	= textField.value;
		indVirg	= value.indexOf(',');
		isEmpty	= value == '';
		
		if (isEmpty) {
			return true;
		}
		if (indVirg == -1) {
			value 			= value + ',0';
			textField.value = value;
		}
		else if (indVirg == value.length - 1) {
			value 			= value + '0';
			textField.value = value;
		}
	}












