/**
$(): utilizado em todas as funções que devem ser referenciadas a jQuery
document: expressão que indica o documento HTML
ready(): associado a leitura do documento enquanto está sendo carregado
*/
$(document).ready(function(){

    // Crio uma variável chamada $forms que pega o valor da tag form
    $forms = $('#form_registro');
	 $('#loading').hide();

    // hide(): esconde a div cadastro enquanto carrega o ready()
    //$('#fields').hide();

    /**
     bind(): é manipulador de  evento exemplo submit, click e/ou double click
     a: é a tag <a href>
    */

    $('a').bind('click', function(){
        switch(this.id){
            case 'c':
                $('#loading').show(); // show(): mostra div que está oculta (hide()).
                return false;
                break;
        }
    })
    
    $forms.submit(function(){
    	nome = $('#nome').val();
    	email = $('#email').val();
    	doc = $('#doc').val();
    	tel = $('#tel').val();
    	usuario = $('#txtusuario').val();
    	senha = $('#txtsenha').val();
    	senhax = $('#senhax').val();
    	$.ajax({
    		url: this.action
    		,type: 'POST'
    		,data:{nome:nome,email:email,doc:doc,tel:tel,usuario:usuario,senha:senha,senhax:senhax}
    		,beforeSend:function(){
    			$('#loading').html("<ul class=\"system_messages\"><li class=\"yellow\"><span class=\"ico\"></span><strong class=\"system_title\">Aguarde! </strong>Conferindo Dados...</li></ul>");
    		}
    		,success:function(txt){
    			$('#loading').html(txt);
    			self.reset();
    			$button.attr('disabled',false);
    		}	
            ,error: function(txt){
                $('#loading').html(txt);
            } 		
    		
    	}) ;
    	return false;
    });

    
    
    /**
     * Executa um ajax para válidar o CPF na base de dados via WebService
     * @author Pilha Digital
     * @since 1 de julho de 2010
     * @return bool O Ajax Retorna um bool true se estiver cadastrado ou um false caso não exista o CPF 
     * 
     * 
     * */
    $('#doc').blur(function(){
    	$.ajax({
    		url: 'valida_cpf_webservice.php'
    		,type:'POST'
    		,data:{cpf:$('#doc').val()}
    		,beforeSend:function(){
    			$('#loading').show();
				$('#loading').html("<ul class=\"system_messages\"><li class=\"yellow\"><span class=\"ico\"></span><strong class=\"system_title\">Aguarde! </strong>Conferindo Dados...</li></ul>");	
				
    		}
    		,success:function(ret){
    			if(ret == 1){
    				$('#hidenCPFValido').val(1);
    				$('#loading').html("<ul class=\"system_messages\"><li class=\"green\"><span class=\"ico\"></span><strong class=\"system_title\">CPF V&aacute;lido</strong></li></ul>");
    			}else{  				
        			$('#loading').html("<ul class=\"system_messages\"><li class=\"red\"><span class=\"ico\"></span><strong class=\"system_title\">CPF n&atilde;o encontrado</strong</li></ul>");
        			return false;    	
    				$('#hidenCPFValido').val(0);
    			}
    			
    		}
    		
    	});
    	
    });
});

