// JScript File


//Função assincrona do populaCidade.
function retorno_populaCidade(ret)
{
    var dt = ret.value;
    var cidade = document.getElementById('cidade');
    cidade.options.length = 0;
	cidade.options[cidade.options.length] = new Option("Selecione uma Cidade","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        cidade.options[cidade.options.length] = new Option(dt[i].Nome, dt[i].Codigo_Cidade);
    }
    document.getElementById('img_cidade').style.display = "none";
}
//Metodo responsável por popular as cidades de acordo com o estado selecionado.
function populaCidade(value)
{
    document.getElementById('img_cidade').style.display = "";
    cadastro.PopulaCidade(value,retorno_populaCidade);   
}





//Função assincrona do populaCidadeUser é a mesma de cima.
function retorno_populaCidadeUser(ret)
{
    var dt = ret.value;
    var cidadeUser = document.getElementById('cidade_user');
    cidadeUser.options.length = 0;
	cidadeUser.options[cidadeUser.options.length] = new Option("Selecione uma Cidade","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        cidadeUser.options[cidadeUser.options.length] = new Option(dt[i].Nome, dt[i].Codigo_Cidade);
    }
    document.getElementById('img_cidade_user').style.display = "none";
}
//Metodo responsável por popular as cidades de acordo com o estado selecionado.
function populaCidadeUser(value)
{
    document.getElementById('img_cidade_user').style.display = "";
    cadastro.PopulaCidadeUser(value,retorno_populaCidadeUser);   
}







//Função assincrona do populaCidade para Fazenda(São distintas).
function retorno_populaCidadeFaz(ret)
{
    var dt = ret.value;
    var cidadeFaz = document.getElementById('cidadeFaz');
    cidadeFaz.options.length = 0;
	cidadeFaz.options[cidadeFaz.options.length] = new Option("Selecione uma Cidade","0");            
    for(var i = 0; i < dt.length; i++)
    {                
        cidadeFaz.options[cidadeFaz.options.length] = new Option(dt[i].Nome, dt[i].Codigo_CidadeUsuario);
    }
    document.getElementById('img_cidadeFaz').style.display = "none";
}
//Metodo responsável por popular as cidades de acordo com o estado selecionado.
function populaCidadeFaz(value)
{
    document.getElementById('img_cidadeFaz').style.display = "";
    cadastro.PopulaCidadeFaz(value,retorno_populaCidadeFaz);   
}







//Realiza validação do Formulario
function ValidaFormCadastro()
{
    var sucesso = true;
    
    // Define os estilos padrões
    document.getElementById('form_nome').className = 'txtfield';
            
    document.getElementById('form_email').className = 'txtfield';
               
    document.getElementById('form_nascimento').className = 'txtfield';
               
    document.getElementById('form_senha').className = 'txtfield';
       
      
    
    //Verificando se os campos estão corretamente preenchidos.
    if(EmptyField(document.getElementById('form_nome')))
    {
        sucesso = false;
        document.getElementById('form_nome').className = 'txtfielderror';
        alert('Por favor informe seu nome.');
        document.getElementById('form_nome').focus();  
        return false;
    }
    if(EmptyField(document.getElementById('form_nascimento')))
    {
        sucesso = false;
        document.getElementById('form_nascimento').className = 'txtfielderror';
        alert('Por favor informe sua data de nascimento.');
        document.getElementById('form_nascimento').focus();  
        return false;
    }
    else
    {
        if(!valida_data(document.getElementById('form_nascimento')))
        {
            sucesso = false;
            document.getElementById('form_nascimento').className = 'txtfielderror';
            alert('Data em formato inválido.');
            document.getElementById('form_nascimento').focus();  
            return false;
        }
    }    
    if(EmptyField(document.getElementById('form_cpf')))
    {
        sucesso = false;
        document.getElementById('form_cpf').className = 'txtfielderror';
        alert('Por favor informe seu cpf.');
        document.getElementById('form_cpf').focus();  
        return false;
    }else{
        if(!valida_CPF(document.getElementById('form_cpf')))
        {
            sucesso = false;
            document.getElementById('form_cpf').className = 'txtfielderror';
            alert('CPF Inválido.');
            document.getElementById('form_cpf').focus();
            return false;
        }    
    }
    
    
    
    if(EmptyField(document.getElementById('form_rg')))
    {
        sucesso = false;
        document.getElementById('form_rg').className = 'txtfielderror';
        alert('Por favor informe seu rg.');
        document.getElementById('form_rg').focus();  
        return false;
    }
    
    
    if(EmptyField(document.getElementById('form_endereco_user')))
    {
        sucesso = false;
        document.getElementById('form_endereco_user').className = 'txtfielderror';
        alert('Por favor informe seu logradouro.');
        document.getElementById('form_endereco_user').focus();  
        return false;
    }
    if(EmptyField(document.getElementById('form_numero_user')))
    {
        sucesso = false;
        document.getElementById('form_numero_user').className = 'txtfielderror';
        alert('Por favor informe seu número.');
        document.getElementById('form_numero_user').focus();  
        return false;
    }
    if(EmptyField(document.getElementById('form_cep_user')))
    {
        sucesso = false;
        document.getElementById('form_cep_user').className = 'txtfielderror';
        alert('Por favor informe seu CEP.');
        document.getElementById('form_cep_user').focus();  
        return false;
    }
    
    if(EmptyField(document.getElementById('form_bairro_user')))
    {
        sucesso = false;
        document.getElementById('form_bairro_user').className = 'txtfielderror';
        alert('Por favor informe seu Bairro.');
        document.getElementById('form_bairro_user').focus();  
        return false;
    }
    
    if(document.getElementById('estado_user').value == '0')
    {
        sucesso = false;
        document.getElementById('estado_user').className = 'txtfielderror';
        alert('Por favor informe seu estado.');
        document.getElementById('estado_user').focus();  
        return false;
    }
    if(document.getElementById('cidade_user').value == '0')
    {
        sucesso = false;
        document.getElementById('cidade_user').className = 'txtfielderror';
        alert('Por favor selecione uma cidade.');
        document.getElementById('cidade_user').focus();  
        return false;
    }
    
    
    
    
    if(EmptyField(document.getElementById('form_residencial')))
    {
        sucesso = false;
        document.getElementById('form_residencial').className = 'txtfielderror';
        alert('Por favor informe seu telefone residencial.');
        document.getElementById('form_residencial').focus();  
        return false;
    }else{
        if(!valida_telefone(document.getElementById('form_residencial')))
        {
            sucesso = false;
            document.getElementById('form_residencial').className = 'txtfielderror';
            alert('Telefone Inválido.');
            document.getElementById('form_residencial').focus();
            return false;
        } 
    }
    
    if(EmptyField(document.getElementById('form_celular')))
    {
        sucesso = false;
        document.getElementById('form_celular').className = 'txtfielderror';
        alert('Por favor informe seu telefone celular.');
        document.getElementById('form_celular').focus();  
        return false;
    }else{
        if(!valida_telefone(document.getElementById('form_celular')))
        {
            sucesso = false;
            document.getElementById('form_celular').className = 'txtfielderror';
            alert('Telefone Celular Inválido.');
            document.getElementById('form_celular').focus();
            return false;
        } 
    } 
       
    
    if(EmptyField(document.getElementById('form_email')))
    {
        sucesso = false;
        document.getElementById('form_email').className = 'txtfielderror';
        alert('Por favor informe seu e-mail.');
        document.getElementById('form_email').focus();  
        return false;
    }
    else
    {
        if(!valida_email(document.getElementById('form_email')))
        {
            sucesso = false;
            document.getElementById('form_email').className = 'txtfielderror';
            alert('E-mail Inválido.');
            document.getElementById('form_email').focus();
            return false;
        }
    }
    /*Login = Email*/
    if(EmptyField(document.getElementById('form_email')))
    {
        sucesso = false;
        document.getElementById('form_email').className = 'txtfielderror';
        alert('Por favor informe o login de acesso.');
        document.getElementById('form_email').focus();
        return false;  
    }
    else
    {
        if(cadastro.LoginExistente(document.getElementById('form_email').value, document.getElementById('id_user').value).value)
        {
            sucesso = false;
            document.getElementById('form_email').className = 'txtfielderror';
            alert('Este login (E-mail) já existe. Por favor crie outro login (E-mail).');
            document.getElementById('form_email').focus();
            return false;  
        }
    }
    
    
    
    
    if(!EmptyField(document.getElementById('id_user')))//Está se alterando um cadastro.
    {   
        if(!EmptyField(document.getElementById('form_senha')) && !validaSenha(document.getElementById('form_senha')))
        {
            sucesso = false;
            document.getElementById('form_senha').className = 'txtfielderror';
            //document.getElementById('erroform_senha').style.display = '';
            //document.getElementById('erroform_senha').innerHTML = 'Para sua segurança a senha deve ter no mínimo 6 dígitos, deve conter letras e números (mínimo 3 números), não pode conter os seqüenciais 1234 ou 4321 e não é permitido colocar o ano como senha.  ';      
        }
    }
    else//Está se inserindo um cadastro
    {
        if(EmptyField(document.getElementById('form_senha')))
        {
            sucesso = false;
            document.getElementById('form_senha').className = 'txtfielderror';
            alert('Por favor informe sua senha de acesso.');
            document.getElementById('form_senha').focus();
            return false;
        }
        else
        {
            if(!validaSenha(document.getElementById('form_senha')))
            {
                sucesso = false;
                document.getElementById('form_senha').className = 'txtfielderror';
                alert('Para sua segurança a senha deve ter no mínimo 6 dígitos, deve conter letras e números (mínimo 3 números), não pode conter os seqüenciais 1234 ou 4321 e não é permitido colocar o ano como senha.');
                document.getElementById('form_senha').focus();
                return false;
            }
        }
    }
    if(!EmptyField(document.getElementById('id_user')))//Está se alterando um cadastro.
    {  
        if(!EmptyField(document.getElementById('form_senha')) && EmptyField(document.getElementById('form_senha2')))
        {
            sucesso = false;
            document.getElementById('form_senha2').className = 'txtfielderror';
            alert('Por favor confirme sua senha de acesso.');
            document.getElementById('form_senha2').focus();
            return false;
        }
        else
        {
            if(!EmptyField(document.getElementById('form_senha')) && !EmptyField(document.getElementById('form_senha2')) && document.getElementById('form_senha').value != document.getElementById('form_senha2').value)
            {
                sucesso = false;
                document.getElementById('form_senha2').className = 'txtfielderror';
                alert('A confirmação de senha não esta igual a senha digitada.Tente novamente.');
                document.getElementById('form_senha2').focus();
                return false;
            }
        }
    }
    else//Está se inserindo um cadastro
    {
        if(EmptyField(document.getElementById('form_senha2')))
        {
            sucesso = false;
            document.getElementById('form_senha2').className = 'txtfielderror';
            alert('Por favor confirme sua senha de acesso.');
            document.getElementById('form_senha2').focus();
            return false;
        }
        else
        {
            if(!EmptyField(document.getElementById('form_senha')) && !EmptyField(document.getElementById('form_senha2')) && document.getElementById('form_senha').value != document.getElementById('form_senha2').value)
            {
            
                sucesso = false;
                document.getElementById('form_senha2').className = 'txtfielderror';
                alert('A confirmação de senha não esta igual a senha digitada.Tente novamente.');
                document.getElementById('form_senha2').focus();
                return false;
            }
        }
    }
    
    var objeto = document.getElementById('approved');
    
    if(objeto != null){        
        
   if(document.getElementById('approved').checked == ''){
        document.getElementById('acordo').className = 'verifica_concordo';
        alert("Você precisa concordar com nosso termo de compromisso.");
        document.getElementById('approved').focus();
        return false;
    }
        
        
    
    //Caso tenha chegado até aqui, o cadastro do usuário é realizado.
    var nome_ = document.getElementById('form_nome').value;
    var email_ = document.getElementById('form_email').value;
    var cpf_ = document.getElementById('form_cpf').value;
    var rg_ = document.getElementById('form_rg').value;
    var estCivil_ = document.getElementById('form_estCivil').value;
    
    var logradouroUser_ = document.getElementById('form_endereco_user').value;
    var numeroUser_ = document.getElementById('form_numero_user').value;
    var cepUser_ = document.getElementById('form_cep_user').value;
    var bairroUser_ = document.getElementById('form_bairro_user').value;
    var cidadeUser_ = document.getElementById('cidade_user').value;
    var estadoUser_ = document.getElementById('estado_user').value;
    
    var telResidencial_ = document.getElementById('form_residencial').value;
    var telCelular_ = document.getElementById('form_celular').value;
    var senha_ = document.getElementById('form_senha').value;
    var nomeEmpresa_ = document.getElementById('form_nome_empresa').value;
    var cnpj_ = document.getElementById('form_cnpj').value;
    var inscEmpresa_ = document.getElementById('form_inscEmpresa').value;
    var telComercial_ = document.getElementById('form_comercial').value;
    var logradouro_ = document.getElementById('form_endereco').value;
    var numero_ = document.getElementById('form_numero').value;
    var cep_ = document.getElementById('form_cep').value;
    var telFax_ = document.getElementById('form_fax').value;
    var bairro_ = document.getElementById('form_bairro').value;
    var cidadeEmp_ = document.getElementById('cidade').value;
    var estadoEmp_ = document.getElementById('estado').value;
    var descricao_ = document.getElementById('form_descricao').value;
    var insFazenda_ = document.getElementById('form_inscFazenda').value;
    var cidadeFaz_ = document.getElementById('cidadeFaz').value;
    var estadoFaz_ = document.getElementById('estadoFaz').value;
    
    var producaoFaz_ = document.getElementById('form_producao').value;
    var animaisLactacao_ = document.getElementById('form_animais_lactacao').value;
    var animaisTotal_ = document.getElementById('form_animais_total').value;
        
    var id_ = document.getElementById('id_user').value;
    var dataNascimento_ = document.getElementById('form_nascimento').value;
    var senha_ = document.getElementById('form_senha').value;
    
    if(cadastro.InserirAlterarUsuarioCadastroSimples(nome_, dataNascimento_, cpf_, rg_, estCivil_, logradouroUser_, numeroUser_, cepUser_, bairroUser_, estadoUser_, cidadeUser_, telResidencial_, telCelular_, email_, senha_, nomeEmpresa_, cnpj_, inscEmpresa_, telComercial_, logradouro_, numero_, cep_, telFax_, bairro_, cidadeEmp_, estadoEmp_, descricao_, insFazenda_, cidadeFaz_, estadoFaz_, producaoFaz_, animaisLactacao_, animaisTotal_, id_ ).value)
    {
        if(id_ != "")
        {
            document.getElementById('sucesso_alt_cadastro').style.display = "";
        }
        else
        {        
            document.getElementById('sucesso_cadastro').style.display = "";
            document.getElementById('form_pessoal').style.display = "none";
            document.getElementById('form_empresa').style.display = "none";
            document.getElementById('form_fazenda').style.display = "none";
  
            
            window.scrollTo(0,0);	
		    var reg_color = '#345672';
		    var alt_color = '#DBDBDC';
		    Fat.fade_element('coluna_esquerda_login_welcome',null,null,reg_color,alt_color);
        }
        
        document.getElementById('form_pessoal').style.display = "none";
        document.getElementById('form_empresa').style.display = "none";
        document.getElementById('form_fazenda').style.display = "none";
    }
    else
    {
        document.getElementById('form_pessoal').style.display = "none";
        document.getElementById('form_empresa').style.display = "none";
        document.getElementById('form_fazenda').style.display = "none";
        
        //document.getElementById('erro_cadastro').style.display = "";
    }
}else{

    //Caso tenha chegado até aqui, o cadastro do usuário é realizado.
    var nome_ = document.getElementById('form_nome').value;
    var email_ = document.getElementById('form_email').value;
    var cpf_ = document.getElementById('form_cpf').value;
    var rg_ = document.getElementById('form_rg').value;
    var estCivil_ = document.getElementById('form_estCivil').value;
    
    var logradouroUser_ = document.getElementById('form_endereco_user').value;
    var numeroUser_ = document.getElementById('form_numero_user').value;
    var cepUser_ = document.getElementById('form_cep_user').value;
    var bairroUser_ = document.getElementById('form_bairro_user').value;
    var cidadeUser_ = document.getElementById('cidade_user').value;
    var estadoUser_ = document.getElementById('estado_user').value;
    
    var telResidencial_ = document.getElementById('form_residencial').value;
    var telCelular_ = document.getElementById('form_celular').value;
    var senha_ = document.getElementById('form_senha').value;
    var nomeEmpresa_ = document.getElementById('form_nome_empresa').value;
    var cnpj_ = document.getElementById('form_cnpj').value;
    var inscEmpresa_ = document.getElementById('form_inscEmpresa').value;
    var telComercial_ = document.getElementById('form_comercial').value;
    var logradouro_ = document.getElementById('form_endereco').value;
    var numero_ = document.getElementById('form_numero').value;
    var cep_ = document.getElementById('form_cep').value;
    var telFax_ = document.getElementById('form_fax').value;
    var bairro_ = document.getElementById('form_bairro').value;
    var cidadeEmp_ = document.getElementById('cidade').value;
    var estadoEmp_ = document.getElementById('estado').value;
    var descricao_ = document.getElementById('form_descricao').value;
    var insFazenda_ = document.getElementById('form_inscFazenda').value;
    var cidadeFaz_ = document.getElementById('cidadeFaz').value;
    var estadoFaz_ = document.getElementById('estadoFaz').value;
    
    var producaoFaz_ = document.getElementById('form_producao').value;
    var animaisLactacao_ = document.getElementById('form_animais_lactacao').value;
    var animaisTotal_ = document.getElementById('form_animais_total').value;
        
    var id_ = document.getElementById('id_user').value;
    var dataNascimento_ = document.getElementById('form_nascimento').value;
    var senha_ = document.getElementById('form_senha').value;
    
    if(cadastro.InserirAlterarUsuarioCadastroSimples(nome_, dataNascimento_, cpf_, rg_, estCivil_, logradouroUser_, numeroUser_, cepUser_, bairroUser_, estadoUser_, cidadeUser_, telResidencial_, telCelular_, email_, senha_, nomeEmpresa_, cnpj_, inscEmpresa_, telComercial_, logradouro_, numero_, cep_, telFax_, bairro_, cidadeEmp_, estadoEmp_, descricao_, insFazenda_, cidadeFaz_, estadoFaz_, producaoFaz_, animaisLactacao_, animaisTotal_, id_ ).value)
    {
        if(id_ != "")
        {
            document.getElementById('sucesso_alt_cadastro').style.display = "";
        }
        else
        {        
            document.getElementById('sucesso_cadastro').style.display = "";
            document.getElementById('form_pessoal').style.display = "none";
            document.getElementById('form_empresa').style.display = "none";
            document.getElementById('form_fazenda').style.display = "none";
  
            
            window.scrollTo(0,0);	
		    var reg_color = '#345672';
		    var alt_color = '#DBDBDC';
		    Fat.fade_element('coluna_esquerda_login_welcome',null,null,reg_color,alt_color);
        }
        
        document.getElementById('form_pessoal').style.display = "none";
        document.getElementById('form_empresa').style.display = "none";
        document.getElementById('form_fazenda').style.display = "none";
    }
    else
    {
        document.getElementById('form_pessoal').style.display = "none";
        document.getElementById('form_empresa').style.display = "none";
        document.getElementById('form_fazenda').style.display = "none";
        
        //document.getElementById('erro_cadastro').style.display = "";
    }

}


//Função para ao se dá um Enter cair sobre o evento de submit da pagina de contato...
function EnterCadastroSimples(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    
    if(charCode==13)
    {
         document.getElementById('bt_enviarCadastroSimples').onclick();		
         return false;
    }
    return true;
}
}