/***
* Descrição.: formata um campo do formulário de acordo com a máscara informada...
* Parâmetros: 
* - objForm (o Objeto Form)
* - strField (string contendo o nome do textbox)
* - sMask (mascara que define o  formato que o dado será apresentado:
* "9" para definir números 
* "x" para definir somente letras maiusculas e minusculas SEM espaço
* "!" para qualquer caracter
*
* Uso...: onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);"
*
* Caracteres aceitos para mascara : -,;:./() espaço
***/

function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	nTecla = (evtKeyPress.which) ? evtKeyPress.which : evtKeyPress.keyCode;
	
	sValue = objForm[strField].value;		
	
	if (nTecla == 8 || nTecla == 9 || nTecla == 17) {
		return true;
	}
	
	//expressao = /[\.\/\-\(\)\,\;\: ]/gi;
	//sValue = sValue.toString().replace(expressao, '');
	//sValue = sValue.replace(/\D/g,"");
	
	//if (sValue.length==1 && sValue==0){sValue="";}
	
	fldLen = sValue.length;
	mskLen = sMask.length;
	
	i = 0;
	nCount = 0;
	sCod = "";
	//mskLen = fldLen;

	while (i <= fldLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (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(i);
			//nCount++;
		}

		i++;
	}
	
	sCod = sCod.substr(0,mskLen);

	objForm[strField].value = sCod;
	
	//exceções (del, home, end, left, right)
	if (nTecla == 46 || nTecla == 36 || nTecla == 35 || nTecla == 37 || nTecla == 39){
		return true;
	}
	
	// backspace, enter
	if (nTecla != 8 && nTecla != 13) {  
		if (sMask.charAt(i-1) == "9") {// apenas números...
			if (objForm[strField].value.length < sMask.length){
				return ((nTecla > 47) && (nTecla < 58)) || ((nTecla > 95) && (nTecla < 106)); 
			}
			else {
				return false;
			}
		}
		else { // números de 0 a 9
			if (sMask.charAt(i-1) == "x") { // apenas letras... Sem espaco
				if (objForm[strField].value.length < sMask.length) {
					return ((nTecla > 64) && (nTecla < 123)); 
				}
				else {
					return false;
				}
			} // maiusculas e minusculas de A a z sem acentos
			else { // qualquer caracter...
				
				if (objForm[strField].value.length < sMask.length) {
					return objForm[strField].value.length < sMask.length;
				}
				else {
					return false;
				}
				
			} 
		} 
	}
	else {
		if (objForm[strField].value.length < sMask.length) {
			return true;
		}
		else {
			return false;
		}
	}
}

function only_number(evt) {
	var charCode = (window.event) ? evt.keyCode : (evt.which) ? evt.which : null;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    		return false;
    return true;
}

function formatText(e) {
	var key;
	if(window.event) {key = e.keyCode;} else if(e.which) {key = e.which;}	
	if (key==8 || (key != 60 && key != 62)) {return true;} else {return false;}
}

function clearHtmlTags(value) {	
	value = value.replace(/</g,"").replace(/>/g,"");
	return value;	
}

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
   
    if (objTextBox.value.length >= objTextBox.maxLength) return false;
    
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function MoedaInteiro(campo){  
	v = campo.value;
	v = v.replace(/\D/g,"");	

	if (v.length==1 && v==0){v="";}

	if (v.length >=3) {
		
		if (v.length = 9) {
			v = v.replace(/(\d{1})(\d{9})$/,"$1.$2");
		}
		if (v.length = 6) {
			v = v.replace(/(\d{1})(\d{6})$/,"$1.$2");
		}
		if (v.length = 3) {
			v = v.replace(/(\d{1})(\d{3})$/,"$1.$2");
		}
	}
	campo.value = v;
}

function InscricaoEstadual(campo) { 
	campo.value = campo.value.match(/^[0-9\-\.\/]{0,20}/);
}

function MascaraData(evento, objeto){
	var keypress=(window.event)?event.keyCode:evento.which;
	campo = eval(objeto);
	caracteres = '0123456789'; separacao1 = '/'; separacao2 = ''; separacao3 = '';
	conjunto1 = 2; conjunto2 = 5; conjunto3 = 10; conjunto4 = 13; conjunto5 = 16;
	
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (10)) {
		if (campo.value.length == conjunto1 )
			campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto2)
			campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto3)
			campo.value = campo.value + separacao2;
		else if (campo.value.length == conjunto4)
			campo.value = campo.value + separacao3;
		else if (campo.value.length == conjunto5)
			campo.value = campo.value + separacao3;
			
		return true;
	}
	else if (keypress == 8 || keypress == 0) {
		return true;
	}
	else
		return false;
}

function MascaraTelefone(o){
    v_obj = o;
    setTimeout("ExecMascara()", 1);
}

function ExecMascara(){
    v_obj.value = MakePhoneMask(v_obj.value.substr(0,14));
}

function MakePhoneMask(v){
    v=v.replace(/\D/g,"");                 /*Remove tudo o que não é dígito*/
    v=v.replace(/^(\d\d)(\d)/g,"($1) $2"); /*Coloca parênteses em volta dos dois primeiros dígitos*/
    v=v.replace(/(\d{4})(\d)/,"$1-$2");    /*Coloca hífen entre o quarto e o quinto dígitos*/
    return v;
}

function mask(o, f) {
	v_obj = o;
	v_fun = f;
	setTimeout("execmask()", 1);
}

function execmask() {
	v_obj.value = v_fun(v_obj.value);
}

function cpf(v) {
	v = v.replace(/\D/g, "");  //Remove tudo o que não é dígito
	v = v.replace(/(\d{3})(\d)/, "$1.$2");  //Coloca um ponto entre o terceiro e o quarto dígitos
	v = v.replace(/(\d{3})(\d)/, "$1.$2"); //Coloca um ponto entre o terceiro e o quarto dígitos
	//de novo (para o segundo bloco de números)
	v = v.replace(/(\d{3})(\d{1,2})$/, "$1-$2");  //Coloca um hífen entre o terceiro e o quarto dígitos
	if (v.length > 14) {
		v = v.substring(0, 14);
	}

	//	if (v.length == 14) {
	//		if (!ValidateCPF(v)) {
	//			v.value = '';
	//			jAlert("\nCPF inválido!", "", function() { v.focus(); });
	//			return false;
	//		}
	//	}
	return v;
}

function cep(v) {
	v = v.replace(/\D/g, "");  //Remove tudo o que não é dígito
	v = v.replace(/^(\d{5})(\d)/, "$1-$2");  //Esse é tão fácil que não merece explicações
	if (v.length > 9) {
		v = v.substring(0, 9);
	}
	return v;
}

function cnpj(v) {
	v = v.replace(/\D/g, "");  //Remove tudo o que não é dígito
	v = v.replace(/^(\d{2})(\d)/, "$1.$2");  //Coloca ponto entre o segundo e o terceiro dígitos
	v = v.replace(/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3");  //Coloca ponto entre o quinto e o sexto dígitos
	v = v.replace(/\.(\d{3})(\d)/, ".$1/$2");  //Coloca uma barra entre o oitavo e o nono dígitos
	v = v.replace(/(\d{4})(\d)/, "$1-$2");  //Coloca um hífen depois do bloco de quatro dígitos
	if (v.length > 18) {
		v = v.substring(0, 18);
	}
	return v;
} 

