NewsLetterUsuario = {

	cancelNewsLetter : function()
	{
		var parametros = 'email='+$('#frmCancelarNL #mail').val()+'&senha='+$('#frmCancelarNL #password').val();
		$('#newsContent').html(DC.loadingText);
		$.ajax({
			url:'admAssinante.php?acao=cancelNewsLetter',
			type:'POST',
			data:parametros,
			success:function(retorno)
			{
				$('#newsContent').html(retorno);
			}
		});
	},
	
	formConfigNL : function(usuarioId)
	{
		$('#newsContent').html(DC.loadingText);
		var parametros = 'usuarioId='+usuarioId;
		$.ajax({
			url:'admAssinante.php?acao=formConfigNewsLetter',
			type:'POST',
			data:parametros,
			success:function(retorno)
			{
				$('#newsContent').html(retorno);
				NewsLetterUsuario.showDescription('#descricaoTipos',$('#tipos').attr('selecionada'));
				NewsLetterUsuario.showDescription('#descricaoStatus',$('#divStatus').attr('selecionada'));
			}
		});
	},
	
	showDescription : function(seletor, id)
	{
		$(seletor+' p.descricao').each(function(){
			$(this).hide();
		});
		$(seletor+' p#'+id).show();
		if(seletor == '#descricaoStatus')
		{
			if(id == 3)
			{
				$('#frmNewsLetter .personalizada').show();
			}
			else
			{
				$('#frmNewsLetter .personalizada').hide();
			}
		}
	},
	
	saveNewsLetter : function(usuarioId)
	{
		var parametros = NewsLetterUsuario.serialize(usuarioId);
		$('#frmNewsLetter').hide();
		$('#newsContent').append(DC.loadingText);
		$.ajax({
			url:'admAssinante.php?acao=saveNewsLetter',
			type:'POST',
			data:parametros,
			success:function(retorno)
			{
				if(retorno == true)
				{
					alert('Suas configurações foram salvas com sucesso!');
					DC.load('home');
				}
				else
				{
					alert('Erro ao tentar salvar as configurações:\n'+retorno);
					$('#loadingText').remove();
					$('#frmNewsLetter').show();
				}
			}
		});
	},
	
	serialize : function(id)
	{
		//var campanha = $('#frmNewsLetter #campanha').get(0).checked ? 1 : 0;
		var status = $('#divStatus input[@type=radio][@checked]').val();
		var tipoNewsLetter = $('#divTipos input[@type=radio][@checked]').val();
		var editorias = '';
		if(status == 3)
		{
			$('#frmNewsLetter #selectEditoriasSelecionadas option').each(function(){
				editorias += '&editorias[]='+this.value;
			});
		}
		parametros = 'usuarioId='+id+/*'&campanha='+campanha+*/'&status='+status+'&tipoNewsLetter='+tipoNewsLetter+editorias;
		return parametros;
	},
	
	addEditorias : function()
	{
		$('#selectEditoriasExistentes option[@selected]').each(function(){
			if(!$('#selectEditoriasSelecionadas option[@value='+this.value+']').get(0))
			{
				$('#selectEditoriasSelecionadas').append(NewsLetterUsuario.createOption(this.value,this.text));
			}
		});
	},
	
	removeEditorias : function()
	{
		$('#selectEditoriasSelecionadas option[@selected]').remove();
	},
	
	createOption : function(id, text)
	{
		var option = '<option value="'+id+'">'+text+'</option>';
		return option;
	}
};
