var erros;

function add_error2(msg, campo) {
	alert(msg)
	campo.focus()
}

function add_error(msg) {
	erros = erros + "\n" + "- " + msg
}

function tem_erros() {
	return (erros.length != 0)
}

function mostra_erros() {
	alert("Erros no formulário: \n" + erros);
}

function vazio(texto) {
	var formato_correto = "([ ])*[a-zA-Z0-9\\-\\.]+([ ])*";
	var correto = new RegExp(formato_correto);
	var vazio
	vazio = false
	if (texto.length == 0) {
		vazio = true
	}
	return ((!correto.test(texto)) || vazio)
}

function email_valido(email) {
	var formato_errado = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
	var formato_certo = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
	var errado = new RegExp(formato_errado);
	var certo = new RegExp(formato_certo);
	return (!errado.test(email) && certo.test(email))
}

function n_parcelas(data_compra, data_viagem) {
	var dia1 = data_compra.substring(0,2);
	var mes1 = data_compra.substring(3,5);
	var ano1 = data_compra.substring(6,10);
	var dia2 = data_viagem.substring(0,2);
	var mes2 = data_viagem.substring(3,5);
	var ano2 = data_viagem.substring(6,10);
	var data1 = new Date(ano1, mes1 - 1, dia1);
	var data2 = new Date(ano2, mes2 - 1, dia2);
	var meses = data2.getMonth() - data1.getMonth()
	meses = meses + 3
	if (meses >= 12) {
		return 12
	}
	else return meses
}

function data_br_valida(data) {
	var formato = "[0-3][0-9]/[0-1][0-9]/[0-9][0-9][0-9][0-9]";
	var reg = new RegExp(formato);
	var formato_ok = reg.test(data);
	var dia = data.substring(0,2);
	var mes = data.substring(3,5);
	var ano = data.substring(6,10);
	var nova_data = new Date(ano, mes - 1, dia);
	var dia_ok = (nova_data.getDate() == dia);
	var mes_ok = (nova_data.getMonth() + 1 == mes);
	return (formato_ok && dia_ok && mes_ok)
}

function data_br_valida_mes_ano(data) {
	var formato = "[0-1][0-9]/[0-9][0-9][0-9][0-9]";
	var reg = new RegExp(formato);
	var formato_ok = reg.test(data);
	var mes = data.substring(0,2);
	var ano = data.substring(3,7);
	var nova_data = new Date(ano, mes - 1);
	var mes_ok = (nova_data.getMonth() + 1 == mes);
	return (formato_ok && mes_ok)
}

function inteiro_valido(numero) {
	var validos = "0123456789";
	if (numero.length == 0)
		 return (false);
	for (j = 0; j < numero.length; j++)  {
		if (validos.indexOf(numero.charAt(j)) == -1)
			return (false)
	}
	return (true)
}

function menor_4000(texto) {
	if (texto.length > 4000)
		return false
	else
		return true
}

function menor_96(texto) {
	if (texto.length > 95)
		return false
	else
		return true
}

function menor_255(texto) {
	if (texto.length >= 255)
		return false
	else
		return true
}

function menor_400(texto) {
	if (texto.length >= 400)
		return false
	else
		return true
}


function extrai_numeros(texto) {
	var novo = ""
	for (var i = 0; i < texto.length; i++) {
		caracter = texto.charAt(i)
		if (inteiro_valido(caracter))
			novo = novo + caracter
	}
	return (novo)
}

function verificacao_cnpj(cgc) {
	var n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14
	var d1,d2
	var digitado, calculado
	cgc = extrai_numeros(cgc)
	if(cgc.length < 14) return false
	n1 = cgc.substring(0,1)
	n2 = cgc.substring(1,2)
	n3 = cgc.substring(2,3)
	n4 = cgc.substring(3,4)
	n5 = cgc.substring(4,5)
	n6 = cgc.substring(5,6)
	n7 = cgc.substring(6,7)
	n8 = cgc.substring(7,8)
	n9 = cgc.substring(8,9)
	n10 = cgc.substring(9,10)
	n11 = cgc.substring(10,11)
	n12 = cgc.substring(11,12)
	n13 = cgc.substring(12,13)
	n14 = cgc.substring(13,14)
	d1 = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5
	d1= 11-(d1 % 11)
	if (d1 >= 10) d1=0
	d2 = d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6
	d2 = 11-(d2 % 11)
	if (d2 >= 10) d2=0
	calculado = d1 + d2
	digitado = n13*1 + n14*1
	if (calculado == digitado)
		return true
	else
		return false 
	//return (calculado == digitado)
	
}

function cpf_valido(cpf) {
	cpf = extrai_numeros(cpf)
	if (cpf.length < 1) return (false)
	var d1,d4,xx,nCount,resto,digito1,digito2,check;
	d1 = 0;	d4 = 0;	xx = 1;
	for (nCount = 0; nCount < (cpf.length - 1); nCount++) {
		d1 = d1 + (11 - xx) * cpf.substring(nCount, nCount + 1)
		d4 = d4 + (12 - xx) * cpf.substring(nCount, nCount + 1)
		xx++
	}
	resto = (d1 % 11)
	if (resto < 2)
		digito1 = 0
	else
		digito1 = 11 - resto
	d4 = d4 + 2 * digito1
	resto = (d4 % 11)
	if (resto < 2)
		digito2 = 0 
	else
		digito2 = 11 - resto
	check = digito1 + digito2
	return (check == cpf.substring(cpf, cpf.length - 1, cpf.length + 1))
}

function decimal_valido(numero) {
	var validos = "0123456789.";
	if (numero.length == 0)
		 return (false);
	for (i = 0; i < numero.length; i++)  {
		if (validos.indexOf(numero.charAt(i)) == -1)
			return (false)
	}
	if (numero.indexOf(".") == -1)
		return (false);
		
	if (numero.indexOf(".") != numero.lastIndexOf("."))
		return (false);
		
	return (true)
}

function hora_valida(hora) {
	var formato = "[0-2][0-9]:[0-5][0-9]";
	var reg = new RegExp(formato);
	var formato_ok = reg.test(hora);
	var h = hora.substring(0,2);
	var m = hora.substring(3,5);
	var nova_hora = new Date(2000, 1, 1, h, m);
	var h_ok = (nova_hora.getHours() == h);
	var m_ok = (nova_hora.getMinutes() == m);
	return (formato_ok && h_ok && m_ok)
}

function DataMaior(parametro1,parametro2){
		
	dia1 = parametro1.slice(0,2)
	mes1 = parametro1.slice(3,5)
	ano1 = parametro1.slice(6,10)
		
	dia2 = parametro2.slice(0,2)
	mes2 = parametro2.slice(3,5)
	ano2 = parametro2.slice(6,10)
		
	data1 = new Date(ano1,mes1,dia1)
	data2 = new Date(ano2,mes2,dia2)
		
	if (data2 <= data1)
		return false
	else
		return true
}

function DataMaiorIgual(parametro1,parametro2){
		
	dia1 = parametro1.slice(0,2)
	mes1 = parametro1.slice(3,5)
	ano1 = parametro1.slice(6,10)
		
	dia2 = parametro2.slice(0,2)
	mes2 = parametro2.slice(3,5)
	ano2 = parametro2.slice(6,10)
		
	data1 = new Date(ano1,mes1 -1,dia1)
	data2 = new Date(ano2,mes2 -1,dia2)
	if (data2 > data1){
		return false
	}
	else {
		return true
	}
}


function Passado(parametro1){
		
	dia = parametro1.slice(0,2)
	mes = parametro1.slice(3,5)
	ano = parametro1.slice(6,10)
		
	data1 = new Date(ano,mes,dia)
	data2 = new Date()	
	if (data1 < data2)
		return true
	else
		return false
}


function DataMaiorCompleta(parametro1,parametro2){
		
	dia1 = parametro1.slice(0,2)
	mes1 = parametro1.slice(3,5)
	ano1 = parametro1.slice(6,10)
	hora1 = parametro1.slice(10,12)
	minuto1 = parametro1.slice(13,15)
		
	dia2 = parametro2.slice(0,2)
	mes2 = parametro2.slice(3,5)
	ano2 = parametro2.slice(6,10)
	hora2 = parametro2.slice(10,12)
	minuto2 = parametro2.slice(13,15)

	data1 = new Date(ano1,mes1,dia1,hora1,minuto1)
	data2 = new Date(ano2,mes2,dia2,hora2,minuto2)
		
	if (data2 <= data1)
		return false
	else
		return true
}
	
