// JavaScript Document
$(document).ready(function() {
	
	//Show the appropriate student infor boxes depending on the grade that they are applying for.
	$("#applyForGrade").change(function() {
		var grade  = $(this).val();
		
		if(grade <= 4) {
			$(".studentInfo").hide();
			$("#lsInfo").slideDown("fast").fadeIn("fast");
		} else {
			$(".studentInfo").hide();
			$("#msUsInfo").slideDown("fast").fadeIn("fast");
		}
		
	});
	
	//When they have entered the parent 1 name, populate the select lists in the marital section
	$("#parent1Name").blur(function() {
		var name1 = $(this).val();
		var name2 = $("#parent2Name").val();
		

		$("#primaryCustody").removeOption(/./);
		$("#primaryCustody").addOption(name1, name1, true);
		
		if(name2.length > 0)
			$("#primaryCustody").addOption(name2 ,name2, false);
		
		$("#educationDecision").removeOption(/./);
			$("#educationDecision").addOption(name1, name1, true);
			
		if(name2.length > 0)
			$("#educationDecision").addOption(name2, name2, false);
		
		name1 = name1.toUpperCase();
		$("#sigDisplay").text(name1);
	});
	
	//When they have entered the parent 1 name, populate the select lists in the marital section
	$("#parent2Name").blur(function() {
		var name2 = $(this).val();
		var name1 = $("#parent1Name").val();
		

		$("#primaryCustody").removeOption(/./);
		$("#primaryCustody").addOption(name1, name1, true);
		
		if(name2.length > 0)
			$("#primaryCustody").addOption(name2 ,name2, false);
		
		$("#educationDecision").removeOption(/./);
			$("#educationDecision").addOption(name1, name1, true);
			
		if(name2.length > 0)
			$("#educationDecision").addOption(name2, name2, false);
	});
	
	//If toggle the custody section if they change the marrigage dropdown
	$("#maritalStatus").change(function() {
		var mStatus = $(this).val();
		
		if(mStatus == "Married" || mStatus == "Single") {
			$(".additionalInfo").fadeOut().slideUp("fast");
		} else {
			$(".additionalInfo").fadeIn().slideDown("fast");
		}
	});
	
	//Show the suspension explanation box if they switch the dropdown to yes
	$("#suspended").change(function() {
		var mStatus = $(this).val();
		if(mStatus == "No") {
			$(".additionalInfoSuspended").fadeOut().slideUp("fast");
		} else {
			$(".additionalInfoSuspended").fadeIn().slideDown("fast");
		}
	});
	
	//Show the financial aid information paragraph if they select the fa checkbox
	$("#financialAid").change(function() {
		var mStatus = $(this).is(":checked");
		
		if(!mStatus) {
			$("#financialAidInfo").fadeOut().slideUp("fast");
		} else {
			$("#financialAidInfo").fadeIn().slideDown("fast");
		}
	});
	
	//Reset the select menus as people manipulate the priority dropdowns
	$(".factor").change(function() {
		var selectValue = $(this).val();
		
		if(selectValue > 0) {
			$(".factor[value = " + selectValue + "]").not(this).selectOptions("-");
		}
	});
	
	//Check for specific form logic that is not handled by CF
	$("#Submit").click(function() {
		var mStatus = $("#maritalStatus").val();
		var edDes = $("#educationDecision :selected").text();
		var suspended = $("#suspended").val();
		var explanation = $("#suspensionExplanation").val();
		var sig = $("#electronicSignature").val();
		var sigCheck = $("#sigDisplay").text();
		var grade = $("#applyForGrade").val();
		var priorityCount = $(".factor[value != '-']").size();
		returnVal = true;
		
		if(grade <= 4) {
			if($("#activitiesHobbiesLS").val().length  == 0) {
				alert("You must fill in the information about your young child's activities, hobbies, and interests.");
				returnVal = false;
			}
		} else {
			if(returnVal && $("#activitiesHobbies").val().length  == 0) {
				alert("You must fill in the information about your child's activities, hobbies, and interests.");
				returnVal = false;
			}
			if(returnVal && $("#studentInfo").val().length  == 0) {
				alert("You must fill in the information about what MPA should know about the applicant.");
				returnVal = false;
			}
			if(returnVal && $("#whyMPA").val().length  == 0) {
				alert("Please tell us why you are applying to MPA.");
				returnVal = false;
			}
		}
		
		if(mStatus != "Married" && mStatus != "Single") {s
			if(edDes.length == 0) {
				alert("You need to choose at least one parent who can make education decisions for this student.");
				returnVal = false;
			}
		}
		
		if(suspended != "No") {
			if(explanation.length == 0) {
				alert("You have indicated that this student has been suspended, please explain the reason for the suspension.");
				returnVal = false;
			}	
		}
		
		if(returnVal && priorityCount != 3) {
			alert("Please choose three priorities that are important to you in applying to Mounds Park Academy.");
			returnVal = false;
		}
		
		if(returnVal && sig != sigCheck) {
			$("#sigDisplay").addClass("error");
			alert("Your electronic signature must match: " + sigCheck + " (the name displayed in red).");
			returnVal = false;
		}
		
		return returnVal;
	});
	
	$("#suspended").change();
	$("#financialAid").change();
	$("#maritalStatus").change();
	$("#applyForGrade").change();
	$("#parent1Name").blur();
	//alert("jQuery ran.");
});
