$(document).ready(function(){
	$("#user_previous_school_preset").change(function(){
		var school = $("#user_previous_school_preset").val();
		
		if (school != '') {
			$("#user_previous_school").val(school);
		}
	});
	
	$("label.remove").hide();
	if ($("a.remove").length > 1) $("a.remove").show();
	$("input[type=checkbox]:checked:hidden").attr("checked", false);
	
	if ($("#payment_form").length > 0) {
		$("#payment_form").submit(course_application.submit_payment);
		
		if ($("#order_card_type").val() != "maestro" && $("#order_card_type").val() != 'solo') {
			$("#maestro").hide();
		}
		$("#order_card_type").change(course_application.toggle_maestro_options);
	}
	
	// 3D Secure Form
	if ($("#secure_authentication").length > 0) {
		$("#secure_authentication").submit();
	}
	
	$("#payment_method_credit_card, #payment_method_cash, #payment_method_ila, #payment_method_employer").change( function(){
		course_application.set_payment_types();
	});
	$("#payment_method_credit_card, #payment_method_cash, #payment_method_ila, #payment_method_employer").click( function(){
		course_application.set_payment_types();
	});


	
	
});

namespace("course_application");

course_application = {
	max_adds: null,
	current_adds: 0,
	
  	add: function(container, type) {
		if (course_application.max_adds > 0 && course_application.current_adds >= course_application.max_adds) {
			return false;
		}
		
		var html = replace_ids(type);
		$(container).append(html);
		$(container + ">fieldset:last input[type=text]:first").focus();
		$("fieldset:first .first_show_only", container).show();
		$("fieldset .first_show_only:hidden", container).remove();
		$("a.remove").show();
		$("label.remove").hide();
		course_application.current_adds++;
		
        return false;
	},
	
	remove: function(_this) {
		var fs = $(_this).parents("fieldset")[0];
		$("input.delete_input", fs)[0].checked = true;
		$("ul, .remove", fs).hide();
		$("p.undo", fs).show();
		course_application.current_adds--;
		// if ($("fieldset.application fieldset ul:visible").length < 2)  $("a.remove").hide();
		return false;
	},
	
	undo: function(_this) {
		var fs = $(_this).parents("fieldset")[0];
		$("input.delete_input", fs)[0].checked = false;
		$(_this).parent().hide();
		$("ul, .remove", fs).show();
		$("a.remove").show();
		$("label.remove").hide();
		course_application.current_adds++;
		return false;
	},
	
	submit_payment: function() {
		$("#submit_order").css("visibility", "hidden");
		$("#processing_order").show();
	},
	
	toggle_maestro_options: function() {
		if (this.value == 'maestro' || this.value == 'solo') {
			$("#maestro").show();
		} else {
			$("#maestro").hide();
		}
	},
  	
	set_payment_types: function(){
		if($("#payment_method_credit_card:checked").length==0){
			$("#payment_type_credit_card").hide();
		} else {
			$("#payment_type_credit_card").show();
		}
	}
  
};