// JavaScript Document
function decode(str) {
     return unescape(str.replace(/\+/g, " "));
}
function produto_exibir(CD_PRODUTO){
	window.location = "pagina_produtos.php?acao=produto&CD_PRODUTO="+CD_PRODUTO;
}

function enter(e) 
{
	if(window.event && window.event.keyCode == 13 || e.which == 13) {  
		buscar();
		return false; 
	}	
}

function menuHover(element, id){
	$(element).removeClass('bg_menu'); 
	$(element).addClass('bg_menu_hover'); 
	$("#"+id).show();
}

function menuHoverCompleta(element, id){
	$(element).removeClass('bg_menu'); 
	$(element).addClass('bg_menu_hover_completa'); 
	$("#"+id).show();
}

function menuOut(element, id){
	$(element).removeClass('bg_menu_hover'); 
	$(element).addClass('bg_menu'); 	
	$("#"+id).hide();
}

function menuOutCompleta(element, id){
	$(element).removeClass('bg_menu_hover_completa'); 
	$(element).addClass('bg_menu'); 	
	$("#"+id).hide();
}

function enter(e) 
{
	if(window.event && window.event.keyCode == 13 || e.which == 13) {  
		buscar();
		return false; 
	}	
}

function valida_form(form, regras, callback)
{
	$('#'+form+' :submit, :image').click(function()
	{
		$('#div_erro').html('');
		$("#div_erro_cadastro").html('');
	});
	
	$("#"+form).validate({
		rules: regras, 
		errorPlacement: function(label, element)
		{  
			window.location = '#ini';
			mostra_erro(label);
		},
		wrapper: "li",
		focusInvalid: false,
		onfocusout: false,
		onkeyup: false,
		submitHandler: function(){
			if(callback != '' && callback != 'undefined' && callback != undefined){
				if(eval(callback+"()")){
					this[form].submit();
					return true;
				}
			} else {
				this[form].submit();
				return true;
			}
		}
	});
}

function mostra_erro(lbl){
	$("#div_erro_cadastro").append(lbl);
	$("#div_erro_cadastro").dialog({
		minWidth: 400,
		modal: true,
		buttons: {
			Ok: function(){
				$(this).dialog('destroy');
			}
		}
	});
}

function mostrar_mensagem(lbl){
	$("#div_mensagem").append(lbl);
	$("#div_mensagem").dialog({
		minWidth: 400,
		modal: true,
		buttons: {
			Ok: function(){
				$(this).dialog('destroy');
			}
		}
	});
}

function addNewsletter()
{
	var email = $("#newsletter").val();
	if(email != ''){
		$.ajax({
			url: "index.php",
			type: 'post',
			cache: false,
			data: {acao: 'addNewsletter', email: email},
			dataType: 'json',
			success: function(retorno)
			{
				$("#retorno_news").html(retorno.msg);
			}
		});
	}	
}

function busca_ideal_home()
{
	var cd_produto = $("#produto_ideal_home").val();
	if(cd_produto != ''){
		window.location = 'index.php?acao=ajuda_produto&CD_PRODUTO='+cd_produto;
	}
}

function muda_ideal(direcao)
{
	if(direcao == 'ant'){
		if(indice_ideal <= 0){
			indice_ideal = ideais.length-1;
		} else {
			indice_ideal--;
		}
	}
	if(direcao == 'prox'){
		if(indice_ideal >= ideais.length-1){
			indice_ideal = 0;
		} else {
			indice_ideal++;
		}
	}
	$("#link_ideal").attr('href', 'index.php?acao=ajuda_produto&CD_PRODUTO='+ideais[indice_ideal][0]);
	$("#achar_ideal").attr('href', 'index.php?acao=ajuda_produto&CD_PRODUTO='+ideais[indice_ideal][0]);
	$("#img_ideal").attr('src', 'pagina_produtos.php?acao=produto_exibe_foto&tipo=destaque&CD_PRODUTO='+ideais[indice_ideal][0]+'&CD_FOTO_PRODUTO='+ideais[indice_ideal][1]);
	$("#lbl_categoria_ideal").html(ideais[indice_ideal][2]);
	$("#lbl_subcategoria_ideal").html(ideais[indice_ideal][3]);
}

