function remove_film_from_cart(idx)
{
  var form = $('#removefilmform');
  $('#removefilmcodefield', form).attr('value', $('#filmcode-'+idx).attr('value'));
  form.submit();
  return false;
}

function online_form_remove_row(e, msg)
{
	if (confirm(msg))
	{
		var p = $(e).parent().parent();
		
		if (p.next().length > 0 || p.prev().length > 0)
		{
			if (jQuery.browser.msie)
			{
				$('#'+p.attr('id')+'-error').remove();
				p.remove();
			}
			else
			{
				$('input', $(p)).attr('disabled', true);
				$('#'+p.attr('id')+'-error').fadeOut("normal", function() { $(this).remove(); } );
				p.fadeOut("fast", function() { $(this).remove(); } );
			}
		}
		else
			$('input', $(p)).attr('value', '');
	}
	return false;
}

;(function($)
{
	$.fn.extend({

		initShoppingCartForm: function(settings) 
		{
			$(this).each(function() {
				$.data(this, "shoppingcart", settings);
			}).submit(onSubmit);
		}
	});

	function onSubmit(event)
	{
		var errors = {
			invalid_value : false,
			empty_purchase : false,
			row_max_error : false,
			total_max_error : false
		};

		var check = true;

		$('#no-screening').each(function()
		{
			check = false;
		});
		
		if (!check)
		{
			return true;
		}

		var sum = 0;
		var data = $(this).data('shoppingcart');
		$('#cart-ticketing tbody tr').each(function(idx) {

			var rowsum = 0;
			var filmcode = $('#filmcode-'+idx).attr('value');

			var skipItemMax = (filmcode.charAt(filmcode.length - 1).toUpperCase() == 'R');

			$('input.ticketqty', this).each(function(){
				
				var num_s = $(this).attr('value');
				var num = parseInt(num_s,10);

				if (num_s != num)
				{
					errors.invalid_value = true;
				}
				else
				{
					rowsum += num;
				}
			});

			if (rowsum == 0)
			{
				errors.empty_purchase = true;
			}
			else
			if (!skipItemMax && rowsum > data.row_max)
			{
				errors.row_max_error = true;
			}

			sum += rowsum;
		});

		if (sum > data.total_max)
		{
			errors.total_max_error = true;
		}

		var s = '';
		
		if (errors.invalid_value)
			s += data.msg_invalid_value + "\n";

		if (errors.empty_purchase)
			s += data.msg_empty_purchase + "\n";

		if (errors.row_max_error)
			s += data.msg_row_max_error + "\n";

		if (errors.total_max_error)
			s += data.msg_total_max_error + "\n";

		if (s)
		{
			alert(s);
			return false;
		}
		
		return true;
	}

})(jQuery);

