$().ready(function() {
				   
	//Applcaition Form Validation
	/*$.validator.setDefaults({
		submitHandler: function() { alert("submitted!"); }
	});*/
	
	//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",
			member_fbi_naa: "required",
			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.",
			dau_courses: "Please select an answer.",
			member_fbi_naa: "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."
		}
	});
	
	
	//Destination Routing
	$("#grad-apply").submit(function(e) {
		var to = $("[name=to]");
		var df = $("[name=datafile]");
									 
		var employer = $("#employer").val();
		var program = $("#program_application").val();
		
		//defaults
		to.val("metcorp@bu.edu");
		df.val("graduate_application_download.txt");
		
		//mapping programs to emails/datafiles, make edits here
		//no external addresses
		var emailMap = {
			"MSCIS": "P.Giles2@cisonline.bu.edu",
			"Health Communication": "Q.Givens2@healthcommunication.bu.edu",
			"MS Business Continuity": "onlinemsbcsrm",
			"MS Banking Financial Services": "onlinemsbfsm",
			"MSPM": "onlineprojmgmt",
			"Marketing Management": "onlineintmktg",
			"Insurance Management": "onlinemsimdgr",
			"MstrCJ": "onlinecjdegree",
			"PM Certificate": "onlineprojmgmt",
			"Human Resources": "onlinemshrm"
		};
		var fileMap = {
			"MSCIS": "mscis_graduate_application.txt",
			"MstrCJ": "mcj_graduate_application.txt",
			"Health Communication": "mshc_graduate_application.txt"
		};
		
		
		//set appropriate datafile
		var f = fileMap[program];
		if (f) {
			df.val(f);
		}
			
		
		//set appropriate recipient
		if ((employer == 'other') && ($("[name=bu_student_before]:checked").val() != "yes") && (($("[name=dau_courses]:checked").val() != "yes") && ($("[name=member_fbi_naa]:checked").val() != "yes"))) { 
			var dest = emailMap[program];
			if (dest) {
				to.val(dest);
			} else {
				to.val("mspm");
			}
		}
	});
});
