var banknote_return = false;

function filter_categories()
{
	jQuery("#categories_list_items li").filter(function (index)
	{
		var cValue = jQuery("#filterCntiess").val().toLowerCase();
		cValue = jQuery.trim(cValue);
		var liValue = jQuery(this).text().toLowerCase();
		var ret = 1;
		if (liValue.indexOf(cValue) != -1)
		{
			ret = 0;
			jQuery(this).css("display", "");
		}
		else
		{
			jQuery(this).css("display", "none");
		}                
		return ret; 
	 });
}

function add_banknote()
{
	if (banknote_return == false)
	{
		$(".ajax_loader").show();
		$('#submit_form_button_1').attr('disabled', 'disabled');
		$('#submit_form_button_2').attr('disabled', 'disabled');
		$.post("/ajax/protected/check_add_banknote", {title_primary: $("#title_primary").val() ,year: $("#year").val(), image1: $("#image1").val(), image2: $("#image2").val()},
			function(data){
				$(".error-formelement").html("");
				$(".ajax_loader").hide();
				if (data != null && data.error)
				{
					$('#submit_form_button_1').removeAttr('disabled');
					$('#submit_form_button_2').removeAttr('disabled');
					for (error in data.error)
					{
						$("#error_"+error).show();
						$("#error_"+error).html(data.error[error]);
					}
				}
				else
				{
					banknote_return = true;
					$(".ajax_loader").show();
					$("#add_form").submit();
				}
		}, "json");
	}
	return banknote_return;
}


function edit_coin()
{
	if (banknote_return == false)
	{
		$(".ajax_loader").show();
		$('#submit_form_button_1').attr('disabled', 'disabled');
		$('#submit_form_button_2').attr('disabled', 'disabled');
		$.post("/ajax/protected/check_edit_banknote", {title_primary: $("#title_primary").val() ,year: $("#year").val()},
			function(data){
				$(".error-formelement").html("");
				$(".ajax_loader").hide();
				if (data != null && data.error)
				{
					$('#submit_form_button_1').removeAttr('disabled');
					$('#submit_form_button_2').removeAttr('disabled');
					for (error in data.error)
					{
						$("#error_"+error).show();
						$("#error_"+error).html(data.error[error]);
					}
				}
				else
				{
					banknote_return = true;
					$(".ajax_loader").show();
					$("#add_form").submit();
				}
		}, "json");
	}
	return banknote_return;
}

function remove_my_doubles_list(what, id)
{
	var reply = $("#i_doubled_it");
	$.post('/ajax/common/remove_i_exchange_it_list', {what : what, id : id}, function(data) {
		reply.html(data);
	});
	return false;
}
function add_my_doubles_list(what, id)
{
	var reply = $("#i_double_it");
	$.post('/ajax/common/add_i_exchange_it_list', {what : what, id : id, note : $("#i_swap_it_comment").val()}, function(data) {
		reply.html(data);
		$("#i_swap_it_comment_box").hide();
	});
	return false;
}

function remove_my_want_list(what, id)
{
	var reply = $("#i_wanted_it");
	$.post('/ajax/common/remove_i_want_it_list', {what : what, id : id}, function(data) {
		reply.html(data);
	});
	return false;
}
function add_my_want_list(what, id)
{
	var reply = $("#i_want_it");
	$.post('/ajax/common/add_i_want_it_list', {what : what, id : id, note : $("#i_want_it_comment").val()}, function(data) {
		reply.html(data);
		$("#i_want_it_comment_box").hide();
	});
	return false;
}
function remove_my_have_list(what, id)
{
	var reply = $("#i_had_it");
	$.post('/ajax/common/remove_i_have_it_list', {what : what, id : id}, function(data) {
		reply.html(data);
	});
	return false;
}

function add_my_have_list(what, id)
{
	$('#i_have_it_comment_box').hide();
	var reply = $("#i_have_it");
	$.post('/ajax/common/add_i_have_it_list', {what : what, id : id, note : $("#i_have_it_comment").val()}, function(data) {
		reply.html(data);
		$("#i_have_it_comment_box").hide();
	});
	return false;
}

function add_tag(what, id)
{
	var reply = $("#thank_you_tag");
	var new_tag = $("#suggest_tag").val();
	var button = $("#btn_add_tag");
	button.attr("disabled", true);
	$.post('/ajax/common/attach_tag', {what : what, tag : new_tag, id : id}, function(data) {
		reply.text(data);
		button.attr("disabled", false);
	});
}

var cache = {}; // Used on jQuery UI autocomplete
$(function () {

	$("#suggest_tag").autocomplete({
		source: function(request, response) {
			if (cache.term == request.term && cache.content) {
				response(cache.content);
			}
			if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
				var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
				response($.grep(cache.content, function(value) {
					return matcher.test(value.value)
				}));
			}
			$.ajax({
				url: "/ajax/autocomplete/tags",
				dataType: "json",
				data: request,
				success: function(data) {
					//cache.term = request.term;
					//cache.content = data;
					response(data);
				}
			});
		},
		minLength: 2,
		select: function(event, ui) {
			$("#suggest_tag").val(ui.item.value);
		}
	});
});

function add_comment()
{
	$('#submit_comment_form_button').attr('disabled', 'disabled');
	$.post("/ajax/common/comment", {
			name: $("#comment_name").val(),
			email: $("#comment_email").val(),
			msg: $("#comment_msg").val(),
			what: "0",
			id: $("#comment_coin_id").val()
		},
		function(data){
			$('#submit_comment_form_button').removeAttr('disabled');
			$(".error-formelement").html("");
			if (data != null && data.error)
			{
				for (error in data.error)
				{
					$("#error_"+error).show();
					$("#error_"+error).html(data.error[error]);
				}
			}
			else
			{
				$("#success_comment").html(data.success).show();
				$("#submit_comment_form_button").hide();
			}
	}, "json");
}


function edit_my_have_list(what, id)
{
	$.post('/ajax/common/remove_i_have_it_list', {what : what, id : id}, function(data) {
		$.post('/ajax/common/add_i_have_it_list', {what : what, id : id, note : $("#new_have_text").val()}, function(data) {
			$('#i_have_it_change').hide();
			$("#have_it_text").text($("#new_have_text").val());
			$('#my_have_btn').attr('disabled', false);
		});
	});
	return false;
}

function edit_my_want_list(what, id)
{
	$.post('/ajax/common/remove_i_want_it_list', {what : what, id : id}, function(data) {
		$.post('/ajax/common/add_i_want_it_list', {what : what, id : id, note : $("#new_want_text").val()}, function(data) {
			$('#i_want_it_change').hide();
			$("#want_it_text").text($("#new_want_text").val());
			$('#my_want_btn').attr('disabled', false);
		});
	});
	return false;
}

function edit_my_swap_list(what, id)
{
	$.post('/ajax/common/remove_i_exchange_it_list', {what : what, id : id}, function(data) {
		$.post('/ajax/common/add_i_exchange_it_list', {what : what, id : id, note : $("#new_swap_text").val()}, function(data) {
			$('#i_swap_it_change').hide();
			$("#swap_it_text").text($("#new_swap_text").val());
			$('#my_swap_btn').attr('disabled', false);
		});
	});
	return false;
}
