//var dateMask = new FieldMask('##/##/####');

function validaCnpj(CNPJ) {
 erro = new String;
 if (CNPJ.length < 18){ 
  return false;
 }
 arrCNPJ = CNPJ.split("."); CNPJ = arrCNPJ.join("");
 arrCNPJ = CNPJ.split("/"); CNPJ = arrCNPJ.join("");
 arrCNPJ = CNPJ.split("-"); CNPJ = arrCNPJ.join("");
 var nonNumbers = /\D/;
 if (nonNumbers.test(CNPJ)){ 
  return false;
 }
 var a = [];
 var b = new Number;
 var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
 for (i=0; i<12; i++){
  a[i] = CNPJ.charAt(i);
  b += a[i] * c[i+1];
 }
 if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
 b = 0;
 for (y=0; y<13; y++) {
  b += (a[y] * c[y]); 
 }
 if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
 if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
  return false;
 }
 return true;
}

function validaCpf(CPF) {
    erro = new String;
    if (CPF.length < 14){ 
        return false;
    }
	var CPFsempt = CPF;
	CPFsempt = CPFsempt.replace(".","");
	CPFsempt = CPFsempt.replace(".","");
	CPFsempt = CPFsempt.replace(".","");
	CPFsempt = CPFsempt.replace(".","");
	CPFsempt = CPFsempt.replace("-","");	
	if ((CPFsempt=='00000000000')||
		(CPFsempt=='11111111111')||
		(CPFsempt=='22222222222')||
		(CPFsempt=='33333333333')||
		(CPFsempt=='44444444444')||
		(CPFsempt=='55555555555')||
		(CPFsempt=='66666666666')||
		(CPFsempt=='77777777777')||
		(CPFsempt=='88888888888')||
		(CPFsempt=='99999999999'))
	{
		return false;
	}
	
    arrCPF = CPF.split("."); CPF = arrCPF.join("");
    arrCPF = CPF.split("-"); CPF = arrCPF.join("");
    var nonNumbers = /\D/;
    if (nonNumbers.test(CPF)){ 
        return false;
    }
    var a = [];
    var b = new Number;
    var c = [11,10,9,8,7,6,5,4,3,2];
    for (i=0; i<9; i++){
        a[i] = CPF.charAt(i);
        b += a[i] * c[i+1];
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11 - x }
    b = 0;
    for (y=0; y<10; y++) {
        b += (a[y] * c[y]); 
    }
    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
    if ((CPF.charAt(9) != a[9]) || (CPF.charAt(10) != a[10])){
        return false;
    }
    return true;
}




function sc_formatNumber(valor, casas) {
	var multiplicador = 0;
	var aux = 0;
	var resultado = '';
	var virgula = false;
	var posicao = 0; //casas após a vírgula

	if (valor == 0) {
		resultado = '0,';
		for (ind=1;ind<=casas;ind++)
			resultado += '0';
		return resultado;
	}

	multiplicador = Math.pow(10, casas);
	aux = Math.round( parseFloat(valor) * multiplicador );
	aux = parseFloat(aux / multiplicador);
	aux = String(aux).replace('.',','); //transforma em string e troca ponto por vírgula
	for (m=0;m<aux.length;m++) {
		if ( posicao == casas )
			break;
		resultado += aux.charAt(m);
		if ( virgula )
			posicao++;
		if ( aux.charAt(m) == ',' )
			virgula = true
	}

	if ( posicao == 0 ) resultado += ',';
	for (m=posicao;m<casas;m++)
		resultado += '0';

	tamanho = resultado.length - (casas + 1); //tamanho do rsultado - tudo após a vírgula
	posicao = 0;
	for (ind=tamanho-1;ind>=0;ind--) { //string começa na posição 0
		if ( posicao == 3 ) {
			resultado = resultado.substr( 0, ind+1 ) + '.' + resultado.substr( ind + 1, resultado.length );
			posicao = 0;
		}
		posicao++;
	}

	return resultado;
}
function SC_numero(e){
            var key = '';
            var len = len2 = 0;
            var strCheck = '0123456789';
            var codigo = (window.addEventListener) ? e.which : e.keyCode;
            var strCodsPossiveis = '0,13,33,34,35,36,44,46,17,8,37,39,96,97,98,99,100,101,102,103,104,105';
            var arrayCodPossiveis = strCodsPossiveis.split(',');
            for (i=0;i<arrayCodPossiveis.length;i++)
            { 
                if (codigo == parseInt(arrayCodPossiveis[i])) 
                return true;
            }
        key = String.fromCharCode(codigo);
        if (strCheck.indexOf(key) == -1) return false;
}

function Limpar(valor, validos) {
	var result = "";
	var aux;
	
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}


function FormataMoeda(campo,e,decimal) {
	var tecla = e.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	var formatado;

	if (tecla != 8)
	{
		tam = vr.length + 1 ;
	}

	if (tecla == 8){
		tam = tam - 1;
	}

	if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
	{

		if (tam <= dec)
		{
			formatado = vr;
		}

		if ((tam > dec) && (tam <= 5))
		{
			formatado = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ((tam >= 6) && (tam <= 8))
		{
			formatado = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
		}
		if ((tam >= 9) && (tam <= 11))
		{
			formatado = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
		}
		if ((tam >= 12) && (tam <= 14))
		{
			formatado = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
		}
		if ((tam >= 15) && (tam <= 17))
		{
			formatado = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam );
		}
		
		if (tam < 18)
		{
			campo.value = formatado;
		}
		else
		{
			campo.value = formatado.substr( 0, 17 );
		}
		
	} 

}

	
function SC_letras(e) {
	var key = '';
	var len = len2 = 0;
	var strCheck = ' qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPASDFGHJKLMNBVCXZ';
	var codigo = (window.Event) ? e.which : e.keyCode;
	if (codigo == 13) return true;
		key = String.fromCharCode(codigo);
	if (strCheck.indexOf(key) == -1) return false;
}

function SC_numero_negativo(e) {
	var key = '';
	var len = len2 = 0;
	var strCheck = '0123456789,-';
	var codigo = (window.Event) ? e.which : e.keyCode;
	if (codigo == 13) return true;
		key = String.fromCharCode(codigo);
	if (strCheck.indexOf(key) == -1) return false;
}

function SC_numero_preco(e) {
	var key = '';
	var len = len2 = 0;
	var strCheck = '0123456789,';
	var codigo = (window.Event) ? e.which : e.keyCode;
	if (codigo == 13) return true;
		key = String.fromCharCode(codigo);
	if (strCheck.indexOf(key) == -1) return false;
}

function SC_numero_hifen(e) {
	var key = '';
	var len = len2 = 0;
	var strCheck = '0123456789-';
	var codigo = (window.Event) ? e.which : e.keyCode;
	if (codigo == 13) return true;
		key = String.fromCharCode(codigo);
	if (strCheck.indexOf(key) == -1) return false;
}

function SC_numero_ponto(e) {
	var key = '';
	var len = len2 = 0;
	var strCheck = '0123456789.';
	var codigo = (window.Event) ? e.which : e.keyCode;
	if (codigo == 13) return true;
		key = String.fromCharCode(codigo);
	if (strCheck.indexOf(key) == -1) return false;
}

function mFormat(objTxt, strMsk) {
    var chrKey = "#"; var strTmp = ""; var strDef = "";
    for (i = 0; i < objTxt.value.length; i++)
        if (strMsk.indexOf(objTxt.value.substr(i, 1)) == -1) strTmp += objTxt.value.substr(i, 1);
    var intCnt = 0;
    faz_mascara:
    for (i = 0; i < strMsk.length; i++) {
        if ((strMsk.substr(i, 1) != chrKey) && (intCnt < strTmp.length)) strDef += strMsk.substr(i, 1);
        else if (intCnt < strTmp.length) {
            strDef += strTmp.substr(intCnt, 1);
            intCnt ++;
        } else break faz_mascara;
    }
    objTxt.value = strDef;
}
function Formatar(src, mask) {
 if (src.value.length > mask.length) {
  src.value = src.value.substring(0,mask.length);
  return false;
 }
 var i = src.value.length;
 var saida = "#";
 var texto = mask.substring(i)
 if (texto.substring(0,1) != saida) 
  src.value += texto.substring(0,1);
}

function sc_CnpjFormat(Campo,teclapres) {
 var tecla = teclapres.keyCode;
 vr = Campo.value;
 tam = vr.length+1;
 	if (tecla!=8){
//		alert(vr.substr(0,2));
		if ((vr.substr(2,1)!=".")&&(tam>3)){
			vr = vr.substr(0,2) + "." + vr.substr(2,tam);
		}
		if ((vr.substr(6,1)!=".")&&(tam>7)){
			vr = vr.substr(0,6) + "." + vr.substr(6,tam);
		}
		if ((vr.substr(10,1)!="/")&&(tam>11)){
			vr = vr.substr(0,10) + "/" + vr.substr(10,tam);
		}
		if ((vr.substr(15,1)!="-")&&(tam>16)){
			vr = vr.substr(0,15) + "-" + vr.substr(15,tam);
		}
	}
	if (tam>19){
		vr = vr.substr(0,18)
	}
	Campo.value = vr;
}
function sc_CpfFormat(Campo,teclapres) {
 var tecla = teclapres.keyCode;
 vr = Campo.value;
 tam = vr.length+1;
 	if (tecla!=8){
//		alert(vr.substr(0,2));
		if ((vr.substr(3,1)!=".")&&(tam>4)){
			vr = vr.substr(0,3) + "." + vr.substr(3,tam);
		}
		if ((vr.substr(7,1)!=".")&&(tam>8)){
			vr = vr.substr(0,7) + "." + vr.substr(7,tam);
		}
		if ((vr.substr(11,1)!="-")&&(tam>12)){
			vr = vr.substr(0,11) + "-" + vr.substr(11,tam);
		}

	}
	if (tam>15){
		vr = vr.substr(0,14)
	}
	Campo.value = vr;
}


function sc_CepFormat(Campo){
 if (Campo.value.length == 5)
	 Campo.value = Campo.value + '-';
} 

function FormataTelefone(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form1[Campo].value;
	tam = vr.length+1;
	if (tecla!=8){
		if ((vr.substr(0,1)!="(")&&(tam>1)){
			vr = "(" + document.form1[Campo].value; 
		}
		if ((vr.substr(1,1)!="0")&&(tam>2)){
			vr = vr.substr(0,1) + "0" + vr.substr(1,tam);
		}
		if ((vr.substr(4,1)!=")")&&(tam>5)){
			vr = vr.substr(0,4) + ")" + vr.substr(4,tam);
		}
		if ((vr.substr(9,1)!="-")&&(tam>10)){
			vr = vr.substr(0,9) + "-" + vr.substr(9,tam);
		}
	}
	document.form1[Campo].value = vr;
}

function FormataCNPJ(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form1[Campo].value;
	tam = vr.length+1;
	if (tecla!=8){
		if ((vr.substr(2,1)!=".")&&(tam>3)){
		vr = vr.substr(0,2) + "." + vr.substr(2,tam);
		}
		if ((vr.substr(6,1)!=".")&&(tam>7)){
			vr = vr.substr(0,6) + "." + vr.substr(6,tam);
		}
		if ((vr.substr(10,1)!="/")&&(tam>11)){
			vr = vr.substr(0,10) + "/" + vr.substr(10,tam);
		}
		if ((vr.substr(15,1)!="-")&&(tam>16)){
			vr = vr.substr(0,15) + "-" + vr.substr(15,tam);
		}
	}
	if (tam>19){
		vr = vr.substr(0,18)
	}
	document.form1[Campo].value = vr;
}

function FormataCEP(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form1[Campo].value;
	tam = vr.length+1;
	if (tecla!=8){
		if ((vr.substr(5,1)!="-")&&(tam>5)){
			vr = vr.substr(0,5) + "-" + vr.substr(5,tam);
		}
	}
	if (tam>9){
		vr = vr.substr(0,9)
	}
	document.form1[Campo].value = vr;
}

function FormataCPF(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form1[Campo].value;
	tam = vr.length+1;
	if (tecla!=8){
		if ((vr.substr(3,1)!=".")&&(tam>4)){
			vr = vr.substr(0,3) + "." + vr.substr(3,tam);
		}
		if ((vr.substr(7,1)!=".")&&(tam>8)){
			vr = vr.substr(0,7) + "." + vr.substr(7,tam);
		}
		if ((vr.substr(11,1)!="-")&&(tam>12)){
			vr = vr.substr(0,11) + "-" + vr.substr(11,tam);
		}		 
	}
	if (tam>15){
		vr = vr.substr(0,14)
	}
	document.form1[Campo].value = vr;
}

function valida_data(string) {
	bissesto = ''
	data = string
	if (data.length != 10) {
		return '#- A data deve ser no formato DD/MM/AAAA'
	}
	else {
		if (data.substring(0,4).indexOf('/') == 2) {
			if (data.substring(3,8).indexOf('/') == 2) {
				dia = data.substring(0,2)
				mes = data.substring(3,5)
				ano = data.substring(6,10)
					if (mes < 0 || mes > 12) {
						return '#- Mês inválido';
					}
					if (ano < 1890 || ano > 2100) {
						return '#- Ano inválido';
					}
					if (mes == 01 || mes == 03 || mes == 05 || mes == 07 || mes == 08 || mes == 10 || mes == 12) {
						n_dias = 31
					}
					else if (mes == 04 || mes == 06 || mes == 09 || mes == 11) {
						n_dias = 30
					}
					else if (mes == 02) { if (ano % 4 == 0) {
						n_dias = 29 ; bissesto = 'SIM'
					}
					else {
					n_dias = 28
					}
				}
				if (dia < 0 || dia > n_dias) {
					return '#- Dia inválido';
				}
				if (bissesto == '') bissesto = 'NÃO'
				return 'true';
			}
		}
	}
}

function FormataData(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form1[Campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
 
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.form1[Campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form1[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}

function validaData(str_data) {
	if (str_data.length != 10) return false;
	var arr_data = str_data.split("/"); if (arr_data.length != 3) return false;
	var _dia = new Number(arr_data[0]); var _mes = new Number(arr_data[1]); _mes--; var _ano = new Number(arr_data[2]);
	var _dat = new Date(_ano, _mes, _dia);
	if ((_dat.getDate() != _dia) || (_dat.getMonth() != _mes) || (_dat.getFullYear() != _ano)) return false;
	return true;
}

function mFormat(objTxt, strMsk) {
    var chrKey = "#"; var strTmp = ""; var strDef = "";
    for (i = 0; i < objTxt.value.length; i++)
        if (strMsk.indexOf(objTxt.value.substr(i, 1)) == -1) strTmp += objTxt.value.substr(i, 1);
    var intCnt = 0;
    faz_mascara:
    for (i = 0; i < strMsk.length; i++) {
        if ((strMsk.substr(i, 1) != chrKey) && (intCnt < strTmp.length)) strDef += strMsk.substr(i, 1);
        else if (intCnt < strTmp.length) {
            strDef += strTmp.substr(intCnt, 1);
            intCnt ++;
        } else break faz_mascara;
    }
    objTxt.value = strDef;
}

function formata_rs(objTxt) {
	str_mascara = "";
	str_valor = String(objTxt.value);
	arr_mskElm = Array('R$', ' ', '.', ',');
	str_numeros = "1234567890";
	for (i = 0; i < str_valor.length; i++)
		if (str_numeros.indexOf(str_valor.substr(i, 1)) == -1) {
			str_valor = str_valor.replace(str_valor.substr(i, 1), "");
			i--;
		}
	if (Number(str_valor) < 10) {
		str_mascara = '#,##';
		str_valor = '00' + String(Number(str_valor));
	} else if (Number(str_valor) < 100) {
		str_mascara = '#,##';
		str_valor = '0' + String(Number(str_valor));
	} else {
		if (str_valor.substr(0, 1) == "0") str_valor = str_valor.substr(1, str_valor.length);
		for (i = 2; i < str_valor.length; i++) {
			if ((((i - 2) % 3) == 0) && ((i - 2) > 0)) str_mascara = "." + str_mascara;
			str_mascara = '#' + str_mascara;
		}
		str_mascara += ',##';
	}
	objTxt.value = str_valor;
	objTxt.style.textAlign = 'right';
	mFormat(objTxt, str_mascara);
}
function banco(x){
	if (x=="13"){
		document.form1.des_digito.maxLength = 2;
		document.form1.des_digito.value = "";
	}
	else{
		document.form1.des_digito.maxLength = 1;
		document.form1.des_digito.value = "";
	}
	if (x=="6"){
		document.form1.des_agencia.maxLength = 4;
		document.form1.des_agencia.value = "";
		spanMensagem.innerHTML = "  NÃO É NECESSÁRIO DIGITAR O DÍGITO !!!";
	}
	else{
		document.form1.des_agencia.maxLength = 14;
		document.form1.des_agencia.value = "";
		spanMensagem.innerHTML = "";
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function f_UCase(objField) {objField.value = objField.value.toUpperCase()}
function f_LCase(objField) {objField.value = objField.value.toLowerCase()}
