var brwsr = navigator.appName
if (brwsr=="Microsoft Internet Explorer") brwsr = "ie"
else brwsr = "ffx";

function mask(c_type, e, obj){
	switch(c_type){
		case 'tel':
			n_length = 14;
			break;
		case 'cep':
			n_length = 10;
			break;
		case 'cnpj':
			n_length = 19;
			break;
		case 'dinheiro':
			n_length = 10;
			break;
	}
	if(obj.value.length >= obj.maxLength) return obj.value;
	
	if(window.event) key = e.keyCode
	else if(e.which) key = e.which;

	if( ((key < 48 || key > 57) && key != 8 && (key < 96 || key > 105)) || ( obj.value.length == n_length && key != 8 ) )
		return obj.value;
	
	c_char = String.fromCharCode(key);
	c_char = retCharNumLock(c_char);

	cValue = (key==8) ? obj.value : obj.value + c_char;
	
	switch(c_type){
		case "tel":
			while(cValue.indexOf('(') > -1) cValue = cValue.replace('(', '');
			while(cValue.indexOf(')') > -1) cValue = cValue.replace(')', '');
			while(cValue.indexOf('-') > -1) cValue = cValue.replace('-', '');
			while(cValue.indexOf(' ') > -1) cValue = cValue.replace(' ', '');
			break;
		case "cep":
			while(cValue.indexOf('-') > -1) cValue = cValue.replace('-', '');
			while(cValue.indexOf(' ') > -1) cValue = cValue.replace(' ', '');
			break;
		case "cnpj":
			while(cValue.indexOf('.') > -1) cValue = cValue.replace('.', '');
			while(cValue.indexOf('/') > -1) cValue = cValue.replace('/', '');
			while(cValue.indexOf(' ') > -1) cValue = cValue.replace(' ', '');
			while(cValue.indexOf('-') > -1) cValue = cValue.replace('-', '');
			break;
		case "dinheiro":
			while(cValue.indexOf(',') > -1) cValue = cValue.replace(',', '');
			break;
	}

	if(brwsr == "ffx"){
		if(key==8)
			cValue = cValue.substr(0, cValue.length - 1);
	}

	switch(c_type){
		case "tel":
			if(cValue){
				if(cValue.length == 2)
					cValue = '(' + cValue.substr(0, 2) + ')';
				else if(cValue.length >= 3 && cValue.length <= 4)
					cValue = '(' + cValue.substr(0, 2) + ') ' + cValue.substr(2, 2);
				else if(cValue.length >= 5 & cValue.length <= 9)
					cValue = '(' + cValue.substr(0, 2) + ') ' + cValue.substr(2, 3) + '-' + cValue.substr(5, 4);
				else if(cValue.length == 10)
					cValue = '(' + cValue.substr(0, 2) + ') ' + cValue.substr(2, 4) + '-' + cValue.substr(6, 4);
				}
				if(brwsr == "ie"){
					if(key==8 && (cValue.length == 4 || cValue.length == 9))
						cValue = cValue.substr(0, cValue.length - 1);
			}
			break;
		case "cep":
			if(cValue){
				if(cValue.length == 5)
					cValue = cValue.substr(0, 5) + '-';
				else if(cValue.length > 5)
					cValue = cValue.substr(0, 5) + '-' + cValue.substr(5, 3);
			}
			if(brwsr == "ie"){
				if(key==8 && (cValue.length == 6 || cValue.length == 10))
					cValue = cValue.substr(0, cValue.length - 1);
			}
			break;
		case "cnpj":
			if(cValue){
				if(cValue.length == 2)
					cValue = cValue.substr(0, 5) + '.';
				else if(cValue.length > 2 && cValue.length < 5)
					cValue = cValue.substr(0, 2) + '.' + cValue.substr(2, 3);
				else if(cValue.length >= 5 && cValue.length < 8)
					cValue = cValue.substr(0, 2) + '.' + cValue.substr(2, 3) + '.' + cValue.substr(5, 3);
				else if(cValue.length >= 8 && cValue.length < 12)
					cValue = cValue.substr(0, 2) + '.' + cValue.substr(2, 3) + '.' + cValue.substr(5, 3) + '/' + cValue.substr(8, 4);
				else if(cValue.length >= 12)
					cValue = cValue.substr(0, 2) + '.' + cValue.substr(2, 3) + '.' + cValue.substr(5, 3) + '/' + cValue.substr(8, 4) + '-' + cValue.substr(12, 2);
			}
			if(brwsr == "ie"){
				if(key==8 && (cValue.length == 3 || cValue.length == 7 || cValue.length == 11 || cValue.length == 16 || cValue.length == 19))
					cValue = cValue.substr(0, cValue.length - 1);
			}
			break;
		case "dinheiro":
			if(cValue){
				if(cValue.length < 2)
					cValue = ',' + cValue;
				else if(cValue.length >= 2)
					cValue = cValue.substr(0, cValue.length - 2) + ',' + cValue.substr(cValue.length - 2, 2);
			}
			if(brwsr == "ie"){
				if(key==8 && (cValue.length == 2))
					cValue = cValue.substr(0, cValue.length - 1);
			}
			break;
	}

	return cValue;
}

function retCharNumLock(c_char){
	//convertedo os caracteres de numlock
	if(c_char=='`'){c_char="0";key=48}
	if(c_char=='a'){c_char="1";key=49}
	if(c_char=='b'){c_char="2";key=50}
	if(c_char=='c'){c_char="3";key=51}
	if(c_char=='d'){c_char="4";key=52}
	if(c_char=='e'){c_char="5";key=53}
	if(c_char=='f'){c_char="6";key=54}
	if(c_char=='g'){c_char="7";key=55}
	if(c_char=='h'){c_char="8";key=56}
	if(c_char=='i'){c_char="9";key=57}
	return c_char;
}

function formataNumero(objValor){
	cValor = objValor.value;
	if(!cValor) return false;
	while(cValor.indexOf('.') > -1) cValor = cValor.replace('.', ',');
	while(cValor.indexOf(',,') > -1) cValor = cValor.replace(',,', ',');
	while(cValor.indexOf(',') > -1) cValor = cValor.replace(',', '.');
	cValor = String(Number(cValor));
	if(String(cValor) == 'NaN'){
		alert('Valor inválido.');
		objValor.value = '';
		objValor.focus();
		return false;
	}
	while(cValor.indexOf('.') > -1) cValor = cValor.replace('.', ',');
	objValor.value = cValor;
}

function permiteTecla(e){
	if(window.event) key = e.keyCode
	else if(e.which) key = e.which;
	if(brwsr == "ns"){
		return true;
	}
	else{
		return (key == 9 || key == 16 || key == 35 || key == 36 || key == 37 || key == 39 || key == 46);
	}
}
