var Cliente = {	

	acoes : '/cms/modulos/personalizado/clientes/Clientes.actions.php?',
	acoesComponete : '/cms/modulos/personalizado/clientes/componentes/acoes.php?',

	tipopessoa:function(tipo){
			$j('#dados').show();
			if(tipo=='J'){
				$j('#pessoa_fisica').slideUp('normal',function(){
					$j('#pessoa_juridica').slideDown();
					$j('#cpf_cliente').val("");
				});
			}else{
				$j('#pessoa_juridica').slideUp('normal',function(){
					$j('#pessoa_fisica').slideDown();	
					$j('#cnpj_cliente').val("");
				});
			}
	},
	
	editar : function(form) {	
		location='?cadastro&cpt=51&registrar';
	},
	
	validar : function(form) {	
		if(form.tipo_pessoa[0].checked == false && form.tipo_pessoa[1].checked == false){	
			$j.alert({type:'error', html:'Tipo de pessoa deve ser selecionado!'});
		}else if(form.num_cpf.value=='' && form.cnpj_empresa.value == ''){
			$j.alert({type:'error', html:'CPF ou CNPJ deve ser preenchido!'});
		}else if(form.senha.value != form.confirma_senha.value){
			$j.alert({type:'error', html:'Senha e Confirmaç&atilde;o de senha est&atilde;o incorretos'});	
		}else if($Form.validate(form) != false) {
			form.cliente.value == '' ? this.inserir(form) : this.salvar(form);
		}
		return false;
	},
	
	inserir : function(form) {
		$j.ajax({
			url:this.acoesComponete+'inserirParcial', type:'post', data:$j(form).serialize(),
			success:function(response) { //alert(response);
				if(response==""){
					$j.alert({type:'success', html:'Seu Cadastro foi realizado com sucesso', 'timeout':5000,
						out:function(){
							location.href = "index.php";
						}
					});
				} else {
					$j.alert({type:'error', html:response, timeout:5000});
				}
			}
		})
	},
	
	salvar : function(form) {		
		$j.ajax({
			url:this.acoesComponete+'salvar', type:'post', data:$j(form).serialize(),
			success:function(response) { //alert(response);
			if(response==""){
					$j.alert({type:'success', html:'Seu Cadastro foi alterado com sucesso', 'timeout':5000,
						out:function(){location.href = "index.php";}
					});
					
				} else {
					$j.alert({type:'error', html:'Seu Cadastro n&atilde;o pode ser alterado!', 'timeout':5000});
				}
				
			}
		})
	},
	
	recuperar_senha_cancelar:function(paranetro, form){
		if(paranetro == 'recuperar'){
			$j(form+' #senha, '+form+' #acessar, '+form+' #links_login, '+form+' #recuperar_a').hide();
			$j(form+' #senha').prev().hide();
			$j(form+' #senha').next().hide();
			$j(form+' #recuperar-b, '+form+' #cancelar, '+form+' #frase-recuperar').show();
		}else{
			$j(form+' #senha, '+form+' #acessar, '+form+' #links_login, '+form+' #recuperar_a').show();
			$j(form+' #senha').prev().show();
			$j(form+' #senha').next().show();
			$j(form+' #recuperar-b, '+form+' #cancelar, '+form+' #frase-recuperar').hide();
		}
	},
	
	recuperar_senha:function(form){

		if($j(form+' #cpf').val()==""){
			$j(form+' #cpf').attr('class','form-container invalid'); 
		}else{
			$j.ajax({
			url: this.acoesComponete+'recuperar_senha',	type: 'post', data: $j(form).serialize(),
			success: function(response){ //alert(response);
					if(response){
						$j.alert({type:'success', html:'Sua nova senha foi enviada por e-mail!', 'timeout':5000,
							out:function(){
								//location.href = form.url.value;
							}
						});
						
					} else {
						$j.alert({type:'error', html:'Cadastro n&atilde;o localizado!', 'timeout':5000});
					}
				}
			});	
		}
		
	},
	
	validar_parcial:function(form, sai, entra, sit_profissao){
		if($Form.validar(form)){
			$j("#"+sai).fadeOut('normal',function(){
				$j("#"+entra).fadeIn();
			});
			if(sit_profissao=='E'){
				$j("#content_profissional").slideDown("normal");
			}		
		}
		$j("#"+entra).show();
		return false;
	},
	
	altDisplay:function(entra,sai){
		$j("#"+sai).fadeOut("normal", function(){
												$j("#"+entra).fadeIn();		  
											});
	},
	
	logout:function(url){
		$j.ajax({
			type: "post", url:  this.acoesComponete+'logout',
			success: function(html){
				location=url;	
		   }
		});
	},
	
	visualizar:function(id){
		return void(open('componentes/visualizar_cliente.php?id='+id,'','width=650,height=650,scrollbars=1, resizable=1'));
	},

	login: function(form){
		if ($Form.validar(form)) {
			$j.ajax({
				url: this.acoesComponete+'logar', type: 'post', data: $j(form).serialize(),
				success: function(response){ //alert(response);
					if (response == 'logado') {
						location = '?downloads&cpt=51';
					} else {
						$j.alert({type:'error', html:'Seu E-mail e/ou Senha est&atilde;o incorretos!', 'timeout':5000});
					}
				}
			})
		}
		
		return false;
	},
	ver_tabela:function(tabela){
		tabelas = $j('.tabelas');
		for(i=0; i<tabelas.length; i++) {
			if($j('#'+tabelas[i].id).css("display")=="block"){
				$j('#'+tabelas[i].id).slideUp();
			}
		}
		$j('#'+tabela).slideDown();
	}
}
