function mostraMensagem(mensagem)
{
	document.all.MSG.innerHTML = mensagem;
	window.location = "#topo";
}

function loginMensagem(mensagem)
{
	document.all.MSG.innerHTML = mensagem;
}

function validaFormulario()
{
	var falha = false;
	
	for (i=0; i<formulario.elements.length; i++) {
		if (formulario.elements[i].obrigatorio == 'true'){
			if (!validaCampo(formulario.elements[i])) {
				formulario.elements[i].style.background = "#FFCCCC";
				falha = true;
			}
			else {
				formulario.elements[i].style.background = "#FFFFFF";
			}
		}
	}
	
	if (falha) {
		mostraMensagem("Os campos em vermelho são Obrigatórios!");
		return false;
	}
	else {
		return true;
	}
	
}

function validaCampo(campo)
{
	if (campo.tipo == "" && campo.value == "") {
		return false;
	}
	if (campo.tipo == "nome" && campo.value == "") {
		return false;
	}
	if (campo.tipo == "numero" && campo.value == "") {
		return false;
	}
	if (campo.tipo == "moeda" && campo.value == "") {
		return false;
	}
	if (campo.tipo == "data") {
		if (campo.value.length == '10') {
			var dia = campo.value.substring(0, 2);
			var mes = campo.value.substring(3, 5);
			var ano = campo.value.substring(6, 10);
			if (dia.length < 2 || mes.length < 2 || ano.length < 4) {
				return false;
			}
			if (dia < 1 || dia > 31 || mes < 1 || mes > 12) {
				return false;
			}
			if ((mes == 2 || mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {
				return false;
			}
			if (mes == 2 && ((ano % 4 && dia > 28) || dia > 29)) {
				return false;
			}
		}
		else {
			return false;
		}
	}
	if (campo.tipo == "cpf") {
		var i; 
		var c = campo.value.substr(0,9); 
		var dv = campo.value.substr(9,2); 
		var d1 = 0; 
		for (i = 0; i < 9; i++) { 
			d1 += c.charAt(i)*(10-i); 
		} 
		if (d1 == 0) {
			return false; 
		} 
		d1 = 11 - (d1 % 11); 
		if (d1 > 9) {
			d1 = 0; 
		}
		if (dv.charAt(0) != d1) { 
			return false; 
		} 
		d1 *= 2; 
		for (i = 0; i < 9; i++) { 
			d1 += c.charAt(i)*(11-i); 
		} 
		d1 = 11 - (d1 % 11); 
		if (d1 > 9) {
			d1 = 0; 
		}
		if (dv.charAt(1) != d1) { 
			return false; 
		} 
	}
	if (campo.tipo == "cep" && campo.value.length != 8) {
		return false;
	}
	if (campo.tipo == "email") {
		mail = campo.value.match(/(\w+)@(.+)\.(\w+)$/);
		if (mail != null){
			if ((mail[3].length==2) || (mail[3].length==3))
				return true;
		}
		return false;
	}
	if (campo.tipo == "cnpj") {
		CNPJ = campo.value;
		CNPJ = LIMP(CNPJ);
		if(isNUMB(CNPJ) != 1) {
			return false;
		}
		else {
			if(CNPJ == 0) {
				return false;
			}
			else {
				g=CNPJ.length-2;
				if(RealTestaCNPJ(CNPJ,g) == 1) {
					g=CNPJ.length-1;
					if(RealTestaCNPJ(CNPJ,g) == 1) {	
						return true;
					}
					else {
						return false;
					}
				}
				else {
					return false;
				}
			}
		}
	}
	return true;
}

function geraMascara(campo, tecla)
{
	if (campo.tipo == "") {
		return true;
	}
	if (campo.tipo == "nome") {
		if (!(tecla > 47) || !(tecla < 58)) {
			return true;
		}
		else {
			return false;
		}
	}
	if (campo.tipo == "numero" || campo.tipo == "cpf" || campo.tipo == "cep") {
		if ((tecla > 47 && tecla < 58) || tecla == 13) {
			return true;
		}
		else {
			return false;
		}
	}
	if (campo.tipo == "data") {
		separador = '/';
		if (tecla > 47 && tecla < 58) {
			if (campo.value.length == 2 || campo.value.length == 5){
				campo.value = campo.value + separador;
			}
			return true;
		}
		else {
			return false;
		}
	}
	if (campo.tipo == "hora") {
		separador = ':';
		if (tecla > 47 && tecla < 58) {
			if (campo.value.length == 2){
				campo.value = campo.value + separador;
			}
			return true;
		}
		else {
			return false;
		}
	}
	if (campo.tipo == "moeda") {
		if (campo.value.length == 10) {
			return false;
		}
		separador = ',';
		if (tecla > 47 && tecla < 58) {
			if (campo.value.length >= 2) {
				campo.value = campo.value.replace(",", "");
				campo.value = campo.value.substr(0, campo.value.length-1) + separador + campo.value.substr(campo.value.length-1, campo.value.length);
			}
			return true;
		}
		else if(tecla == 13) {
			return true; 
		}
		else {
			return false;
		}
	}
}




//INÍCIO	>>	VALIDAÇÃO DE CNPJ
function RealTestaCNPJ(CNPJ,g)
{
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--) {
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8) {
			ind=2;
		}
		else {
			ind++;
		}
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1) {
		VerCNPJ=0;
	}
	else {
		VerCNPJ=11-VerCNPJ;
	}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))) {
		return(0);
	}
	else {
		return(1);
	}
}
function isNUMB(c)
{
	if((cx=c.indexOf(","))!=-1) {		
		c = c.substring(0,cx)+"."+c.substring(cx+1);
	}
	if((parseFloat(c) / c != 1)) {
		if(parseFloat(c) * c == 0) {
			return(1);
		}
		else {
			return(0);
		}
	}
	else {
		return(1);
	}
}
function LIMP(c)
{
	while((cx=c.indexOf("-"))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("/"))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(","))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("."))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("("))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(")"))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(" "))!=-1) {		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	return(c);
}
//FINAL	<<	VALIDAÇÃO DE CNPJ

