$().ready(function() {
				   
	//Applcaition Form Validation
	
	//custom phone validator
	$.validator.addMethod("phone", function(phone_number, element) {
		var minLen = 10;
		var num = phone_number.replace(/[^0-9]/g,"");
		
		//format the number if possible
		if (num.length >= 10) {
			element.value = ((num.length > 10) ? num.substr(0,num.length-10) +  " " : "") + "(" + num.substr(num.length-10,3) + ") " + num.substr(num.length-7,3) + "-" + num.substr(num.length-4,4);
		}
		
		return this.optional(element) || num.length >= minLen;
	}, "Please enter a valid phone number");

	$("#grad-apply").validate({
		errorPlacement: function(error, element) {
				error.appendTo(element.parent());
		},
		rules: {
			program_application: "required",
			bs_degree: "required",
			bu_student_before: "required",
			dau_courses: "required",
			bu_student: {
				required: "#bu_student_before_y:checked"
			},
			title: "required",
			first_name: "required",
			last_name: "required",
			employer: "required",
			other_employer: {
				required: "#employer[value=other]"
			},
			country: "required",
			dayphone: "required",
			email: "required",
			semester: "required"
		},
		messages: {
			program_application: "Please select a program.",
			bs_degree: "Please select an answer.",
			bu_student_before: "Please select an answer.",
			bu_student: "You have indicated that you are a current or former BU student. Please select at least one applicable item from the list above.",
			dau_courses: "Please select an answer.",
			title: "Please choose a title.",
			first_name: "Please tell us your first name.",
			last_name: "Please tell us your last name.",
			employer: "Please indicate your employer.",
			other_employer: "You have selected \"Other\" above. Please indicated your employer here.",
			country: "Please select your country.",
			dayphone: "Please enter a valid phone number.",
			email: "Please enter a valid e-mail address.",
			semester: "Please indicate in which semester you intend to start."
		}
	});
	
	$("[name=bu_student]").click(function() {
		var selected = false;
		
		if ($(this).attr("checked")) {
			$("#bu_student_before_y").attr("checked",true);	
		} else {
			$("[name=bu_student]").each(function() {
				selected = selected || $(this).attr("checked");
			});
			if (!selected) {
				$("#bu_student_before_n").attr("checked",true);
			}
		}
	});
	
	$("#bu_student_before_n").click(function() {
		if ($(this).attr("checked")) {
			$("[name=bu_student]").attr("checked",false);	
		}
	});
	
	$("#grad-apply").submit(function(e) {
		var to = $("[name=to]");
		var rt = $("[name=return]");
									 
		var employer = $("#employer").val();
		var program = $("#program_application").val();
		
		//defaults
		to.val("mspm@bu.edu");
		rt.val("http://www.bu.edu/online/prospective_students/apply/online_metropolitan_thankyouEM.html");

		//set appropriate recipient
		if ((employer == 'other') && ($("[name=bu_student_before]:checked").val() != "yes") && (($("[name=dau_courses]:checked").val() != "yes"))) { 
			to.val("mspm");
			rt.val("http://www.bu.edu/online/prospective_students/apply/online_met_insurance_ty.html");
		} else if (((employer != 'other') || $($("[name=ncma]:checked").length > 0))) {
			to.val("metcorp@bu.edu");
			rt.val("http://www.bu.edu/online/prospective_students/apply/pdfs/management_app.pdf");																
		}
	});
});
