function totalHandler() {
	if ($("#payment-form .required.so_payment").valid()){
		var total = 0;
		$("#payment-form .required.so_payment").each(function(){
			total += parseFloat(this.value); 
		}) 
		$('#payment-form #ssl_amount, #payment-form #ssl_total').attr('value', total+'');
	}
}

$(document).ready(function(){
	$("#payment-form").validate();	
	
	$("#payment-form .required.so_payment").keyup(totalHandler);
	
	$('#payment-form .add-another').toggle(
		function(){
			var next_iteration = this.id.split('-').pop();
			$(this)
				.css('background', 'url(/assets/templates/fis/images/minus_button.gif) no-repeat scroll 0 0 transparent')
				.text('Remove');
			$('#payment-form #field-group' + next_iteration + ' input:text')
				.addClass('required')
				.attr('value', '');
			$('#payment-form #field-group' + next_iteration).slideDown();
			$('#payment-form #add-another-' + (parseInt(next_iteration) - 1)).slideUp();
			$("#payment-form .required.so_payment").keyup(totalHandler);
		},
		function(){
			var next_iteration = this.id.split('-').pop();
			$(this)
				.css('background', 'url(/assets/templates/fis/images/add_button.gif) no-repeat scroll 0 0 transparent')
				.text('Add Another');
			$('#payment-form #field-group' + next_iteration + ' input:text, #payment-form #field-group' + next_iteration + ' textarea')
				.removeClass('required')
				.attr('value', '');
			$('#payment-form #field-group' + next_iteration).slideUp();
			$('#payment-form #add-another-' + (parseInt(next_iteration) - 1)).slideDown();
			totalHandler();
		}
	) 

})