// JavaScript Document
var loginacess = 0;
var senhaacess = 0;

function limpalogin() {
	if(loginacess == 0) {
		document.getElementById("login").value = "";
		loginacess = 1;
	}
}

function limpasenha() {
	if(senhaacess == 0) {
		document.getElementById("senha").value = "";
		senhaacess = 1;
	}
}

function restituicampo(campo) {
	if(!document.getElementById(campo).value && campo == "login") {
		document.getElementById(campo).value = "Login"
		loginacess = 0;
	}
	if(!document.getElementById(campo).value && campo == "senha") {
		document.getElementById(campo).value = "Senha"
		//document.getElementById("senha").type = "text";
		senhaacess = 0;
	}
}
//Controles de mudança de cor de fundo de tabela
function mover(local){
	local.bgColor = "#C7D98F";
}
function mout(local){
	local.bgColor = "";
}
//Link para o destino do flash
function destLogo(){
	document.location.href = "default.asp";
}
//function de esconde e exibe
function ExibeEsconde(destino, fundoTotal){
	if(document.getElementById(destino).style.display == "block") {
		document.getElementById(destino).style.display = "none";
		CorFundo(fundoTotal, 0);
		document.location.href = "#";
	} else {
		document.getElementById(destino).style.display = "block";
		CorFundo(fundoTotal, 1);
		document.location.href = "#" + fundoTotal;
	}
}
function CorFundo(destino, oquefazer) {
	if(oquefazer == 1){
		document.getElementById(destino).style.border = "1px solid #006600";
		document.getElementById(destino).style.backgroundColor = "#FDFDFD";
	} else {
		document.getElementById(destino).style.border = "0px solid #FFFFFF";
		document.getElementById(destino).style.backgroundColor = "#FFFFFF";
	}
}
//Validação de email
function valida_email(campoform)
{
	email = campoform.value;
    if (email)
		{
		p=email.indexOf('@');
		pont=email.indexOf('.');
		if (p<1 || p==(email.length-1) || pont<1 || pont==(email.length-1))
			{
				alert ('O e-mail deve ser um endereço de e-mail válido.');
	    		campoform.value = '';
	    		campoform.focus();
				return false;
			}
		} else {
			alert ('O e-mail deve ser um endereço de e-mail válido.');
  		campoform.value = '';
	    campoform.focus();
		return false;
		}
}
//Validação de Telefone
function valida_tel(campo) {
   var valor='';
   var digito = false;
   a = campo.value;
   if (a) {
   num = a.length;
   for (f=0;f<num;f++)
     {
     if (parseInt(a.substr(f,1)) || a.substr(f,1)=='0') 
        if ((a.substr(f,1) != '0') || digito) valor = valor + '' + a.substr(f,1);
        if (parseInt(a.substr(f,1)) && a.substr(f,1) != '0') digito = true;
     };
   num = valor.length;
   if (num < 9 || num > 10)
      {
      alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
      campo.value = '';
      campo.focus();
      }
	  else
	  {
	  if (num == 9) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-7, 3) + '-' + valor.substr(num-4, 4);
	  if (num == 10) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-8, 4) + '-' + valor.substr(num-4, 4);
	  campo.value = valor;
	  }
   }
}
//Função para validar CNPJ
function valida_cnpj(campoform) {
	num_cpf = '';
	campo = campoform.value;

	if (!campo) return true;
	
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 14)
		{
		d1= 0;
		d4= 0;
		xx= 1;
		for (nCount = 0;nCount< num_cpf.length-2;nCount++)
			{
			if (xx < 5) fator= 6 - xx; else fator= 14 - xx;
			d1 = d1 + num_cpf.charAt(nCount) * fator;
			if (xx < 6) fator = 7 - xx; else fator = 15 - xx;
			d4 = d4 + num_cpf.charAt(nCount) * fator;
	    	xx = xx+1;
			}
		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;
        if (Check != num_cpf.substring(num_cpf.length-2,num_cpf.length))
			{
			alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else 
			{
			campoform.value= num_cpf.substring(0,2)
			+ '.' + num_cpf.substring(2,5) + '.' + num_cpf.substring(5,8) + '/' + num_cpf.substring(8,12)
			+ '-' + num_cpf.substring(12,14);
			return true;
			};
		}
	else
		{
		alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
		}
}
//Valida CPF
function valida_cpf(campoform) {
	num_cpf = '';
	campo = campoform.value;
	if (campo == "00000000000" || campo == "11111111111" || campo == "22222222222" || campo == "33333333333" || campo == "44444444444" || campo == "55555555555" || campo == "66666666666" || campo == "77777777777"  || campo == "88888888888"  || campo == "99999999999"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (campo == "000.000.000-00" || campo == "111.111.111-11" || campo == "222.222.222-22" || campo == "333.333.333-33" || campo == "444.444.444-44" || campo == "555.555.555-55" || campo == "666.666.666-66" || campo == "777.777.777-77"  || campo == "888.888.888-88"  || campo == "999.999.999-99"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (!campo) return true;
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 11)
		{
		soma = 0;
		for (i=0; i < 9; i ++) soma += parseInt(num_cpf.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)	resto = 0;
		soma = 0;
		for (i = 0; i < 10; i ++) soma += parseInt(num_cpf.charAt(i)) * (11 - i);
		resto2 = 11 - (soma % 11);
		if (resto2 == 10 || resto2 == 11) resto2 = 0;
		if ((resto != parseInt(num_cpf.charAt(9))) || (resto2 != parseInt(num_cpf.charAt(10))))
			{
			alert ('- CPF inválido. \nEntre com o CPF correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else
			{
			campoform.value= num_cpf.substring(0,3) + '.' + num_cpf.substring(3,6) + '.'
			+ num_cpf.substring(6,9) + '-' + num_cpf.substring(9,11);
			return true;
			}
		}
	else
		{
		alert ('- CPF inválido. \nEntre com o CPF correto.');
   		campoform.value = '';
   		campoform.focus();
		return false;
		}	
}
//Validação do cadastro de senha
function validaSenha(){
	if ((document.getElementById("senhaatual").value == document.getElementById("confSenha").value) && (document.getElementById("senhaatual").value.length == document.getElementById("confSenha").value.length)) {		
		return false;
	} else {
		return true;
		document.getElementById("senhaatual").value = "";
		document.getElementById("confSenha").value = "";
		document.getElementById("senha").focus();
	}
}
//Altera as variáveis
function mudaTipoUsu(tipo){
	if(tipo == 1)
	{
		document.getElementById("cadForm").style.display = "block";
		document.getElementById("cpf").disabled = false;
		document.getElementById("cpf").style.backgroundColor = "#FFFFFF";
		document.getElementById("cnpj").disabled = true;
		document.getElementById("cnpj").value = "";
		document.getElementById("cnpj").style.backgroundColor = "#CCCCCC";
		fisica = true;
		juridica = false;
	}
	if(tipo == 2)
	{
		document.getElementById("cadForm").style.display = "block";
		document.getElementById("cpf").disabled = true;
		document.getElementById("cnpj").disabled = false;
		document.getElementById("cnpj").style.backgroundColor = "#FFFFFF";
		document.getElementById("cpf").value = "";
		document.getElementById("cpf").style.backgroundColor = "#CCCCCC";
		fisica = false;		
		juridica = true;
	}
	if(tipo == 0)
	{
		document.getElementById("cadForm").style.display = "none";
		document.getElementById("cpf").disabled = true;
		document.getElementById("cnpj").disabled = true;
		document.getElementById("cpf").value = "";
		document.getElementById("cnpj").value = "";
		fisica = false;
		juridica = false;
	}
}
//Validação do formulario de cadastro de usuário e do cadastro do para acesso ao programa
function ValidaForm(formulario) {
	var erro = "";
	if(!juridica && !fisica) erro += "\n- Escolha entre Pessoa Física e Jurídica."
	else {
		if (!formulario.nomecompleto.value) erro = erro +"\n- O nome do contato deve ser informado.";
		if (!formulario.login.value) erro = erro +"\n- O Login deve ser informado.";
		if (!formulario.senhaatual.value) erro = erro +"\n- A senha do usuário deve ser informada.";
		if (validaSenha()) erro = erro +"\n- A senha digitada esta diferente em cada um dos campos. Redigite-a.";
		if(fisica) if (!formulario.cpf.value) erro = erro +"\n- O CPF deve ser informado.";
		if(juridica) {
			if (!formulario.cnpj.value) erro = erro +"\n- O CNPJ deve ser informado.";
			if (!formulario.empresa.value) erro = erro +"\n- O nome da empresa deve ser informado.";
		}
		if (!formulario.cidade.value) erro = erro +"\n- A cidade deve ser informada.";
		if (!formulario.uf.value) erro = erro +"\n- O UF deve ser informado.";
		if (!formulario.email.value) erro = erro +"\n- O email deve ser informado.";
	}
	if (erro) 
		{
		alert("Não foi possível prosseguir:\n" + erro);
		return false;
		}
		else return true;
}
