//- txtFormat: formata um campo do formulário de acordo com a máscara informada
//		formato que o dado será apresentado, usando o algarismo "9" para definir números e o símbolo "!" para qualquer caracter...
//		<input type="textbox" name="xxx" onkeypress="return txtFormat(document.nomeForm, 'nomeCampo', '99999-999', event);"> 
//		qualquer tipo de mascara que use os caracteres "/" "-" ":" "." " "

function txtFormat(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else { // Nestcape / Firefox
		nTecla = evtKeyPress.which;
	}

	sValue = objForm[strField].value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;

	// para adicionar um novo caracter para mascara basta adicionar
	// na var sValue(acima) e bolMask(abaixo)

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	if(nTecla == 8) { //backspace
		mskLen--;
	}
	
	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++;
		} else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	objForm[strField].value = sCod;

	if (nTecla != 8 && nTecla != 0) { // backspace e tab
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); // números de 0 a 9
		} else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}

function retiraMascara(objForm, strField) {
	var sValue;
	sValue = objForm[strField].value;

	// Limpa todos os caracteres de formatação que estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );

	objForm[strField].value = sValue;	
}

function adicionaMascaraCep(objForm, strField) {
	var sValue;
	sValue = objForm[strField].value;
	if(sValue.toString().length == 8) {
	    objForm[strField].value = 
	    sValue.toString().substring(0,2) + "." +
	    sValue.toString().substring(2,5) + "-" +
	    sValue.toString().substring(5,8);
	}
}

function adicionaMascaraCnpj(objForm, strField) {
	var sValue;
	sValue = objForm[strField].value;
	if(sValue.toString().length == 14) {
	    objForm[strField].value = 
	    sValue.toString().substring(0,2) + "." +
	    sValue.toString().substring(2,5) + "." +
	    sValue.toString().substring(5,8) + "/" +
	    sValue.toString().substring(8,12) + "-" +
	    sValue.toString().substring(12,14);
	}
}
