﻿$(document).ready(function()
{
	$("[id^=txt_busca]")
	.blur(function(){
		if ($(this).val() == "")
		{
			$(this).val(this.defaultValue)
		}
	})
	.focus(function(){
		if ($(this).val() == this.defaultValue)
		{
			$(this).val("");
		}
	})

	$("[id^=id_c_t_]").change(function(){
		var busca_text = $(this).parents("form").find("[id^=txt_busca]").val();
		if (busca_text != 'Digite o que procura' && busca_text != '') $(this).parents("form").submit();
	});
	
	$("[id^=id_c_t_]").jdrop();
	
	/* Login */
	$("#topo_login_login").bind('click focus', function() {
		if ($(this).val() == 'cnpj' || $(this).val() == 'cnpj')
			$(this).mask("99.999.999/9999-99");
	});
	$("#topo_login_login").bind('blur', function() {
		if ($(this).val().substr(-1) == '_' || $(this).val() == '') {
			$(this).unbind(".mask");
			$(this).val('cnpj');
		}
	});
	
	/* Clica botão logar */
	$("#topo_btn_ef_login").click(function(){
		topoMenuSlide($("#topo_login_btn"), true);
	});
	
	/* Menu */
	$(".topo_menu_btn").hover(function(){
		if ($(this).find('a').hasClass('topo_fechar_btn') == false) {
			var obj = $(this).find('.topo_menu_btn_info');
			$(obj).css("left", ($(this).position().left-$(obj).width()+($(this).width()+10))+'px');
			$(obj).find('.ind').css('left', ($(obj).width()-17)+'px');
			$(obj).show();
		}
	}, function(){$(this).find('.topo_menu_btn_info').hide();});
	$("#topo_pedidos_btn, #topo_atendimento_btn, #topo_login_btn").click(function(e){
		topoMenuSlide(this);
		e.preventDefault();
	});
	
	// Login
	var canLogin = true;
	$("#topo_login_form").submit(function(e){
		if (canLogin) {
			var cpf = $("#topo_login_login");			 
			var senha = $("#topo_login_senha");
			
			if (validaCNPJ(cpf.val()) == false) {
				alert(VldCadAlertCNPJ_Inv);
				cpf.focus();
				return false;
			}
			
			if (senha.val().length == 0) {
				alert(VldPwdALERTcamposenha);
				senha.focus();
				return false;
			}
			
			senha.attr("disabled", true);
			cpf.attr("disabled", true);
			canLogin = false;
			$.ajax({
				url : "e-commerce/actions/cliente.php",
				dataType:"xml",
				type: "POST",
				data: 'act=4&senha='+senha.val()+"&cpf="+cpf.val()+'&actval='+$("#area_act").val(), 
				success:function(xml) {
					if ($(xml).find('erro').text() == 1){
						alert(AlertErroAutenticacaoCnpj);
						cpf.focus();
						senha.attr("disabled", false);
						cpf.attr("disabled", false);
						canLogin = true;
					} else if ($(xml).find('erro').text() == 2) {
						location.href = "cliente_alterar_dados.php?df=1";
					} else if ($(xml).find('erro').text() == 0) {		
						window.location.reload();
					} else if ($(xml).find('erro').text() == 3) {
						alert("Conta bloqueada. Para mais informações entre em contato com a loja.");
						senha.attr("disabled", false);
						cpf.attr("disabled", false);
						canLogin = true;
					} else if ($(xml).find('erro').text() == 4) {
						alert("Conta ainda não liberada. Para mais informações entre em contato com a loja.");
						senha.attr("disabled", false);
						cpf.attr("disabled", false);
						canLogin = true;
					} else {
						senha.attr("disabled", false);
						cpf.attr("disabled", false);
						canLogin = true;
					}
				}, 
				error: function () {
					senha.attr("disabled", false);
					cpf.attr("disabled", false);	
					canLogin = true;
				}
			});
		}
		e.preventDefault();
	});// Fim do bloco btn_autenticar_user
	$("#topo_login_sair").click(function(){
		$.ajax({
			url 		: "e-commerce/actions/cliente.php",
			dataType	: "xml",
			type		: "POST",
			data		: 'act=14', 
			success:function(xml) 
			{
				location.href = "index.php";
			}
		});
	});
	
	$("#btn_esqueci_senha_topo").click(topo_esqueceu_senha);
});

function topoMenuSlide (thisobj, extAct) {
	var obj = $(thisobj).parents('li:first').prevAll('.topo_menu_item:first');
	var show = $(obj).is(':visible');
	
	if (extAct == true) show = false;
	
	$('.topo_menu_btn_info').hide();
	
	$(".topo_menu_item").each(function(){
		$(this).children(':first').css('min-width', $(this).width()+'px');
		if ($(thisobj).attr('id') != $(this).next().find('a').attr("id") || extAct != true)
			$(this).animate( { width: "hide", paddingLeft: "hide", paddingRight: "hide", marginLeft: "hide", marginRight: "hide" });
	});
	
	$(".topo_menu_btn a").removeClass('topo_fechar_btn');
	$(".topo_menu_item").unbind('mousemove');
	if (!show) {
		var link_onj = thisobj;
		$(obj).animate({ width: "show", paddingLeft: "show", paddingRight: "show", marginLeft: "show", marginRight: "show" }, null, function(){
			if ($(thisobj).attr('id') == "topo_login_btn") $("#topo_login_login").focus();
			$(obj).bind('mousemove', function(){
				$(link_onj).addClass('topo_fechar_btn');
				$(obj).unbind('mousemove');
			});
		});
	}
	
	if ($(thisobj).attr("id") == "topo_login_btn") {
		$(thisobj).parents('li:first').slideUp();
	} else {
		$(".topo_menu_btn").slideDown();
	}	
}

function topo_esqueceu_senha (e) {
	var _login = $("#topo_login_login");
	
	if (confirm("Tem certeza que deseja REDEFINIR A SUA SENHA? Uma nova senha será enviada à você por e-mail.")) {
		if (_login.val() != '') {
			$.ajax({
				url 		: "e-commerce/actions/cliente.php",
				dataType	: "xml",
				type		: "POST",
				data		: 'act=15&cpf='+_login.val(), 
				success:function(xml) {
					if ($(xml).find('erro').text() == 0) {
						alert("Requisição de nova senha enviada para seu email.")
					} else if ($(xml).find('erro').text() == 1) {
						alert("CNPJ está inválido");
						_login.focus();
					} else {
						alert("Erro enviar e-mail");
					}
				},
				error:function(xml) {
					alert("Erro:"+xml);
				}
			});
		} else {
			alert("Informe seu CNPJ");
			_login.focus();
		}
	}
	
	e.preventDefault();
}
