$(document).ready(function(){
	$('.store_amount').keyup(function() {
		var val = parseInt($(this).val())
		if (!val) val = 0
		if (val < 0) val = 0
		$(this).val(val)
	})
	$('.store_buy').click(function() {
		var id = $(this).attr('id')
		id = id.replace('buy_', '')
		var amount = parseInt($('#catalogue_item_'+id+' .amount').val())

		if (!amount) amount = 1
		if (amount < 1) amount = 1

		$.post('/ajx.basket.php', {act: 'add', type: 'item', id: id, amount: amount}, function(data) {
			var callback = (data['status'] == 'ok') ? 'Added' : 'Error adding'
			$('#head_basket').html(data['basket_head'])
			location = '?basket'
		}, "json")
	})
	$('.store_callback').click(function() {
		$(this).html('')
	})
})


function change_amount(id, z, cart) {
	var amount = parseInt($('#amount_'+id).val())
	if (z == 1) {
		amount = (amount > 0) ? amount - 1 : 0
	}
	if (z == 2) {
		amount = amount + 1
	}
	if (cart) {
		$.post('/ajx.basket.php', {act: 'change_amount', id: id, amount: amount}, function(data) {
			$('#basket_ajx').html(data)
		})
		setTimeout('refresh_basket()', 300)
	} else {
		$('#amount_'+id).val(amount)
	}
}


function refresh_basket() {
	$.post('/ajx.basket.php', {act: 'get'}, function(data) {
		$('#head_basket').html(data['basket_head'])
	}, "json")
}


function delete_basket_item(id) {
	if (confirm('Delete selected item from cart?')) {
		$.post('/ajx.basket.php', {act: 'delete', id:id}, function(data) {
			if (data) $('#basket_ajx').html(data)
		})
		refresh_basket()
	}
}

function change_attachment(id) {
	var action = ($('#attachment_'+id).attr('checked')) ? 'add' : 'delete'
	$.post('/ajx.basket.php', {act: action, type: 'attachment', id: id}, function(data) {
		if (data) $('#basket_ajx').html(data)
		refresh_basket()
	})
}

$(function(){
	$('#delivery_date').datetime()
})


function check_ta_message(je) {
	if (je.val() == 'Print your message here') {
		je.val('')
	}
}
