function DFchangeField(f) {
	if(f.value.length==f.maxLength) {
  	for(var i=0;i<f.form.length;i++) {
    	if(f.form[i]==f) {f.form[i+1].focus();f.form[i+1].select();break}
		}
	}
}
function DFchangeField2(f) {
	if(f.value.length==f.maxLength) {
  	for(var i=0;i<f.form.length;i++) {
		if(f.form[i]==f) {f.form[i+1].focus();break}
		}
	}
}

function DFchangeField3(f, tam) {
	if(f.value.length > tam) {f.value = f.value.substr(0, tam);}
}

function FU_ValidaCPF(cpf) {
	if(cpf.length!=11||
		cpf=="00000000000"||cpf=="11111111111"||cpf=="22222222222"||cpf=="33333333333"||cpf=="44444444444"||
		cpf=="55555555555"||cpf=="66666666666"||cpf=="77777777777"||cpf=="88888888888"||cpf=="99999999999") return false
	soma=0;
	for(var i=0;i<9;i++) soma+=parseInt(cpf.charAt(i))*(10-i);
	resto=11-(soma%11);
	if(resto==10||resto==11) resto=0;
	if(resto!=parseInt(cpf.charAt(9))) return false;
	soma=0;
	for(var i=0;i<10;i++) soma+=parseInt(cpf.charAt(i))*(11-i);
	resto=11-(soma%11);
	if(resto==10||resto==11) resto=0;
	if(resto!=parseInt(cpf.charAt(10))) return false;
	return true;
}

function FU_ValidaCEP(cep1, cep2) {
	var msg;
	var msgCep1;
	var msgCep2;
	msg = "";
	msgCep1 = "";
	msgCep2 = "";
	//valida cep1 se é numero
	for(i=0 ; i < 5 ; i++) {
		if(cep1.charCodeAt(i) <48 || cep1.charCodeAt(i)>57) {
			alert(msg);
			return false;
		}
	}
	//valida cep2
	for(i=0 ; i < 3 ; i++) {
		if(cep2.charCodeAt(i) <48 || cep2.charCodeAt(i)>57) {
			alert(msg);
			return false;
		}
	}
	//verifica se cep1 tem exatamente 5 digitos
	if(cep1.length<5) {
		alert(msgCep1);
		return false;
	}
	//verifica se cep2 tem exatamente 3 digitos
	if(cep2.length<3) {
		alert(msgCep2);
		return false;
	}
	return true;
}

function verificaData(dia, mes, ano) {
	var msg;
	msg = "A data está num formato inválido. Digite novamente.";
	if(isNaN(dia) || isNaN(mes) || isNaN(ano)) 
	{
		alert(msg);
		return false;
	}
	if(mes==2 && (dia==30 || dia==31)) 
	{
		alert(msg);
		return false;
	}
	if(mes<1 || mes>12) 
	{
		alert(msg);
		return false;
	}
	if(dia<1 || dia>31) 
	{
		alert(msg);
		return false;
	}
	var d;
	d = new Date();
	if((ano < 1900) || (ano > d.getFullYear())) {
		alert(msg);
		return false;
	}
	//verificar as combinacoes de dia e mes
	if(dia==31 && (mes==4 || mes==6 || mes==9 || mes==11)) {
		alert(msg);
		return false;
	}
	return true;
}
	
function ValidarEmail(strEmail) {
	msgEMailNulo = "Preencha corretamente o campo E-mail.";
	msgEMailIncorreto = "Preencha corretamente o campo E-mail.";
	if(strEmail == "") {
		alert(msgEMailNulo);
		return false;
	}
	if(strEmail.indexOf("@")==-1) {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.indexOf("@.")!=-1) {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.indexOf(".@")!=-1) {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.indexOf(".")==-1) {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.indexOf("..")!=-1) {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.charAt(strEmail.length - 1) == ".") {
		alert(msgEMailIncorreto);
		return false;
	}
	if(strEmail.charAt(0) == ".") {
		alert(msgEMailIncorreto);
		return false;
	}	
	var Erro = 0;
	for(i=0 ; i < strEmail.length ; i++) {
		if(strEmail.charCodeAt(i) != 95 && ((strEmail.charCodeAt(i) < 45) || (strEmail.charCodeAt(i) == 47) || ((strEmail.charCodeAt(i) > 57) && (strEmail.charCodeAt(i) < 64)) || ((strEmail.charCodeAt(i) > 90) && (strEmail.charCodeAt(i) < 97)) || (strEmail.charCodeAt(i) > 122))) {
			Erro = 1;
			i = strEmail.length;
		}
	}
	if(Erro == 1) {
		var msg;
		msg = msgEMailIncorreto;
		alert(msg);
		return false;
	}
	return true;
}

function verificaBranco(strCampo) {
	if(trim(strCampo) == "") {
		alert("Preencha todos os campos obrigatorios");
		return false;
	}
	return true;
}

function verificaTamanho(strCampo, strTamanho) {
	if(trim(strCampo).length < strTamanho) {
		alert("Preencha o campo corretamente.");
		return false;
	}
	return true;
}

function verificaSelect(strSelect) {
	if(strSelect == 0) {
		alert("Selecione uma das opcoes abaixo.");
		return false;
	}
	return true;
}

function verificaNumero(strNumero){
	for (i=0 ; i < strNumero.length ; i++){
		if ( (strNumero.charCodeAt(i) <48 || strNumero.charCodeAt(i)>57) && strNumero.charCodeAt(i) != 45){	
			alert("Preencha o campo corretamente.");
			return false;
		}
	}
	return true;
}

function msgverificaNumero(strNumero, msg){
	for (i=0 ; i < strNumero.length ; i++){
		if ( (strNumero.charCodeAt(i) <48 || strNumero.charCodeAt(i)>57) && strNumero.charCodeAt(i) != 45){	
			alert(msg);
			return false;
		}
	}
	return true;
}

function msgverificaSelect(strSelect, msg) {
	if(strSelect == 0) {
		alert(msg);
		return false;
	}
	return true;
}

function msgverificaTamanho(strCampo, strTamanho, msg) {
	if(trim(strCampo).length < strTamanho) {
		alert(msg);
		return false;
	}
	return true;
}

function msgverificaRadio(strCampo, msg) {
	var j = true;
	for(var i = 0; i<strCampo.length;i++)
	{
		if(strCampo[i].checked) j=false;
	}
	if(j) {
		alert(msg);
		return false;
	}
	return true;
}

function msgverificaBranco(strCampo,msg) {
	if(trim(strCampo) == "") {
		alert(msg);
		return false;
	}
	return true;
}

function emailValido(strEmail) {
	if(strEmail == "") { return false; }
	if(strEmail.indexOf("@")==-1) { return false; }
	if(strEmail.indexOf("@.")!=-1) { return false; }
	if(strEmail.indexOf(".@")!=-1) { return false; }
	if(strEmail.indexOf(".")==-1) { return false; }
	if(strEmail.indexOf("..")!=-1) { return false; }
	if(strEmail.charAt(strEmail.length - 1) == ".") { return false; }
	if(strEmail.charAt(0) == ".") { return false; }
	var Erro = 0;
	for(i=0 ; i < strEmail.length ; i++) {
		if(strEmail.charCodeAt(i) != 95 && ((strEmail.charCodeAt(i) < 45) || (strEmail.charCodeAt(i) == 47) || ((strEmail.charCodeAt(i) > 57) && (strEmail.charCodeAt(i) < 64)) || ((strEmail.charCodeAt(i) > 90) && (strEmail.charCodeAt(i) < 97)) || (strEmail.charCodeAt(i) > 122))) {
			Erro = 1;
			i = strEmail.length;
		}
	}
	if(Erro == 1) { return false; }
	return true;
}

function numeroValido(strNumero) {
	for (i=0 ; i < strNumero.length ; i++) {
		if ( (strNumero.charCodeAt(i) <48 || strNumero.charCodeAt(i)>57) && strNumero.charCodeAt(i) != 45) {
			return false;
		}
	}
	return true;
}

function trim(s) {
	i = 0;
	j = s.length-1;
	while(i < j && s.charAt(i) == " ") i++;
	while(j > i && s.charAt(j) == " ") j--;
	return s.substring(i, j+1);
}


//////////////////////////////////////////
// novas

var msgErro = "";
var campoErro = null;

function limpaErro() {
	msgErro = "";
	campoErro = null;
}

function marcaErro(campo, msg) {
	if(msgErro == "") {
		msgErro = msg;
		campoErro = campo;
	}
}

function mostraErro() {
	if(msgErro != "") {
		alert(msgErro);
		campoErro.focus();
		return true;
	} else {
		return false;
	}
}

function trim(s) {
	i = 0;
	j = s.length-1;
	while(i < j && s.charAt(i) == " ") i++;
	while(j > i && s.charAt(j) == " ") j--;
	return s.substring(i, j+1);
}

function tstSelectPreenchido(campo) {
	if(msgErro != "") { return false; }
	return campo.selectedIndex != 0;
}

function tstRadioPreenchido(campo) {
	if(msgErro != "") { return false; }
	algum = false;
	for(i = 0; i < campo.length; i++) {
		algum = algum || campo[i].checked;
	}
	return algum;
}

function tstCampoPreenchido(strCampo) {
	if(msgErro != "") { return false; }
	return trim(strCampo).length > 0;
}

function tstTamanhoExato(strCampo, intTamanho) {
	if(msgErro != "") { return false; }
	return trim(strCampo).length == intTamanho;
}

function tstTamanhoMinimo(strCampo, intTamanho) {
	if(msgErro != "") { return false; }
	return trim(strCampo).length >= intTamanho;
}

function tstTamanhoMaximo(strCampo, intTamanho) {
	if(msgErro != "") { return false; }
	return trim(strCampo).length <= intTamanho;
}

function tstTamanhoValido(strCampo, intTamanhoMin, intTamanhoMax) {
	if(msgErro != "") { return false; }
	t = trim(strCampo).length;
	return (t >= intTamanhoMin) && (t <= intTamanhoMax);
}

function tstNumeroValido(strCampo) {
	if(msgErro != "") { return false; }
	strCampo = trim(strCampo)
	t = strCampo.length;
	numeroValido = t>0;
	i = 0;
	while((i<t) && numeroValido) {
		c = strCampo.charAt(i);
		numeroValido = (c>="0" && c<="9") || c==".";
		i++;
	}
	numeroValido = numeroValido;
	return numeroValido;
}

function tstTelefoneValido(ddd, tel1, tel2) {
	if(msgErro != "") { return false; }
	return tstTamanhoExato(ddd, 2) && tstNumeroValido(ddd) && tstTamanhoValido(tel1, 3, 4) && tstNumeroValido(tel1) && tstTamanhoExato(tel2, 4) && tstNumeroValido(tel2);
}

function tstTelefoneOpcionalValido(ddd, tel1, tel2) {
	if(msgErro != "") { return false; }
	return !(tstCampoPreenchido(ddd) || tstCampoPreenchido(tel1) || tstCampoPreenchido(tel2)) || tstTelefoneValido(ddd, tel1, tel2);
}

function tstCepValido(cep1, cep2) {
	if(msgErro != "") { return false; }
	return tstTamanhoExato(cep1, 5) && tstNumeroValido(cep1) && tstTamanhoExato(cep2, 3) && tstNumeroValido(cep2);
}

function tstDataValida(strCampo) {
	if(msgErro != "") { return false; }
	strCampo = trim(strCampo)
	t = strCampo.length;
	dataValida = t>6;
	if(dataValida) {
		p1 = strCampo.indexOf("/");
		p2 = strCampo.indexOf("/", p1+1);
		dia = strCampo.substring(0, p1);
		mes = strCampo.substring(p1+1, p2);
		ano = strCampo.substring(p2+1, t);
		dataValida = !isNaN(dia) && !isNaN(mes) && !isNaN(ano)
			&& (mes>=1 && mes<=12)
			&& (dia>=1 && dia<=31)
			&& (mes!=2 || (dia!=30 && dia!=31))
			&& ((mes!=4 && mes!=6 && mes!=9 && mes!=11) || dia!=31);
	}
	return dataValida;
}

function tstEmailValido(strCampo) {
	if(msgErro != "") { return false; }
	strCampo = trim(strCampo)
	t = strCampo.length;
	emailValido = t>0
		&& strCampo.indexOf("@")!=-1
		&& strCampo.indexOf("@.")==-1
		&& strCampo.indexOf(".@")==-1
		&& strCampo.indexOf(".")!=-1
		&& strCampo.indexOf("..")==-1
		&& strCampo.charAt(t-1)!="."
		&& strCampo.charAt(0)!=".";
	i = 0;
	while((i<t) && emailValido) {
		c = strCampo.charCodeAt(i);
		emailValido = c==95 || (c>=45 && c<=122 && c!=47 && (c<=57 || c>=64) && (c<=90 || c>=97));
		i++;
	}
	return emailValido;
}

function tstLoginValido(strCampo) {
	if(msgErro != "") { return false; }
	
	strCampo = strCampo;
	t = strCampo.length;
	loginValido = (t>=4) && (t<=16);
	i = 0;
	while((i<t) && loginValido) {
		c = strCampo.charCodeAt(i);
		loginValido = ((c>=48) && (c<=57)) || ((c>=97) && (c<=122));
		i++;
	}
	return loginValido;
}

function tstSenhaValida(strCampo) {
	return tstLoginValido(strCampo);
}

