(function($){$(document).ready(function(){
	showFieldsBasedOnCategory();
	
	$('select#tx_hmrpayment_pi1_data_category').change(showFieldsBasedOnCategory);
	
	
	// Enable backbutton
	if($('.paymentform-backbutton').length>0) {
		var label = $('.paymentform-backbutton').html();
		
		$('.paymentform-backbutton').html('<input type="submit" value="'+label+'" id="paymentform-backbuttonsubmit" />');
		
		$('input#paymentform-backbuttonsubmit').click(function(){
			
			// Create hidden restart field
			$('.hidden-fields').append('<input type="hidden" name="tx_hmrpayment_pi1[restart]" id="formrestart" value="1" />');
			
			$('input#md5key').remove();
			$('form#paymentform-form').attr('method','post');
		
			// Change action
			$('#paymentform-form').attr('action',$('#origaction').val());

		});
	}
	
	// Submit to card payment
	$('form#paymentform-form').submit(function(){
	
		// if submit and on confirmation form and and not back
		if($('#confirmationform').val()==1 && $('#formrestart').val()!=1) {
		
			// Remove all fields not to be kept (marked with class keepcreditcard)
			$('form#paymentform-form input:not(.keeponsubmit)').remove();
			
		}
	});
	
	
	// Enable terms checkbox
	$('#termandconditionscheck').click(function(){
		if($('#submit-form').attr('disabled')) {
			$('#submit-form').removeAttr('disabled');
		} else {
			$('#submit-form').attr('disabled','disabled');
		}
	});
	
});

function showFieldsBasedOnCategory() {
	var category = parseInt($('#tx_hmrpayment_pi1_data_category').val());
	
	if(category==1) {
		$('.payment-consultationdate').show();
		$('.payment-invoiceno').hide();
		$('.payment-otherremark').hide();
	}
	else if(category==2 || category==3) {
		$('.payment-invoiceno').show();
		$('.payment-otherremark').hide();
		$('.payment-consultationdate').hide();
	} else if(category==4) {
		$('.payment-invoiceno').hide();
		$('.payment-otherremark').show();
		$('.payment-consultationdate').hide();
	} else {
		$('.payment-invoiceno').hide();
		$('.payment-otherremark').hide();
		$('.payment-consultationdate').hide();
	}
}}(jQuery));
