$(document).ready(function(){
	// calculate total cost and display
	var tickets = $('#PaymentTickets');
	tickets.after('<div id="total"></div>');
	$('#PaymentTickets').change(function() {
		var total = 'Total cost = $'+($(this).val() * 60)+'.00';
		$('#total').html(total);
	});
	$("form#order").validate({
		rules: {
    		"data[Payment][tickets]": {
      			required: true,
    		},
			"data[Payment][email]": {
		      	required: true,
		      	email: true
		    }
  		},
		messages: {
        	"data[Payment][tickets]": {
            	required: "Please choose the number of tickets you require"
        	}
		},
   		submitHandler: function(form) {
   			$(form).ajaxSubmit({ 
				beforeSubmit:  hideButton,
				success: showResponse
			});	
   		}
	});
	
	function hideButton(formData, jqForm, options) {
		//$("div.submit input").diasbled = 1;	
		$(".submit").replaceWith('<img src="/img/load.gif" alt="Loading" id="loading" width="32" height="32" />');
		return true;
	};
	
	function showResponse(responseText, statusText, xhr, $form)  { 
		$("form#order").after('<div id="results"/>');
		$("form#order").remove();
		$('#results').html(responseText);
		// if there is an error in the form, don't hide it
		if(!$('form#order').length) {
			$('#results').hide();
		}
		$("#tickets").append('<img src="/img/load.gif" alt="Loading" id="loading" width="32" height="32" />');
		//auto-submit the payment form
		$("#mypaymentform").submit();
	};
	
	// print receipt - open in a new window
	$("a.new-window").after(' <em>(opens in a new window)</em>').attr('target', '_blank');
	$("a.pop-up").attr('target', '_blank');
	
	// display link to print, and add onclick to show print dialog 
	$('#receipt').after('<p id="print"><a href="#" id="printlink">Print this page</a></p>');
	$('#printlink').click(function() {
		window.print();		
		return false;
	});	


});
