$(document).ready(function()
{
	$("#contact").click(function()
	{
		var fullname = $("#fullname").val();
		var email = $("#email").val();
		var subject = $("#subject").val();
		var reason = $("#reason").val();

		$("#contact").attr("disabled", true);
		$("#contact").attr('value', 'sending request...');

		$.ajax({ type: "POST", dataType: "json", url: "/contact.html", data: "name=" + fullname + "&email=" + email + "&subject=" + subject + "&reason=" + reason,
		success: function(data)
			{
				if (data["type"] == 'notif')
				{
					$("#contactform").hide();
					$("#notification").slideDown();
				}
				else
				{
					$("span[id^='n_']").html('');
					$.each(data["msg"], function(i, n)
					{
						$("#n_" + i).html(n);
					});

					$("#contact").attr("disabled", false);
					$("#contact").attr('value','Submit');
				}
			}
		});
	});

	$("a[id^='buy_']").click(function()
	{
		var id_prod = $(this).attr("id").split("_")[1];

		$.ajax(
		{
			type: "POST",
			url: "/cart/",
			data: "id_product=" + id_prod + "&action=add_to_cart",
			async: false,
			success: function(msg)
			{
				alert('Product added to cart!');
			}
		});

		return false;
	});

	/* --- Shopping cart code start --- */
	$("#purchase").click(function()
	{
		var total = $("#total").text();

		$("#total_amount").val(total);
	});

	$("a[id^='del_']").click(function()
	{
		var id_prod = $(this).attr("id").split("_")[1];
		var price = $(this).attr("id").split("_")[2];
		var total = $("#total").text();

		newval = parseFloat(total) - parseFloat(price);
		$.ajax(
		{
			type: "POST",
			url: "/cart/",
			data: "id_product=" + id_prod + "&action=remove_from_cart",
			async: false,
			success: function(msg)
			{
				if (newval > 0)
				{
					$("#prod_id_" + id_prod).attr('disabled', true);
					$("#prod_price_" + id_prod).attr('disabled', true);
					$("#prod_qty_" + id_prod).attr('disabled', true);
					$("#line_" + id_prod).hide();
					$("#total").html(newval.toFixed(2));
				}
				else
				{
					document.location.href = '/cart/';
					return true;
				}
			}
		});

		return false;
	});
	/* --- Shopping cart code end --- */
});
