$(document).ready(function() {

	// Change default values for campus and program for validation
	document.contact.CurriculumID.options[0].value = '';

	// Expand program dropdown for IE
	$("#CurriculumID").mousedown(function() {
		$(this).css('width','400px');
	})
	$("#CurriculumID").blur(function() {
		$(this).css('width','290px');
	})
	$("#CurriculumID").change(function() {
		$(this).css('width','290px');
	})

	//ADD REQUIRED FEILD CLASS
	$('#step_1 input,#step_1 select,#step_2 input,#step_2 select,#step_3 input,#step_3 select')
		.not('#comments,#submit,#start_date')
		.addClass('pageRequired');
		


	// MOVING BETWEEN FORM STEPS
	$('#contact .form_button').click(function() {
		
		var button = $(this);
		// Divs must use ID syntax of "step_1", "step_2", etc.
		var currentStep = $(button).parent().attr('id').substring(5);
		var stepDiv = 'step_' + currentStep;
		var nextDiv = 'step_' + (parseInt(currentStep) + 1);
		var prevDiv = 'step_' + (parseInt(currentStep) - 1);
		
		// Add method to validate one step at a time
		$.validator.addMethod("pageRequired", function(value, element) {
			var $element = $(element)
			function match(index) {
				return currentStep == index && $(element).parents("#step_" + index).length;
			}
			if (match(currentStep)) {
				return !this.optional(element);
			}
			return "dependency-mismatch";
		}, $.validator.messages.required)
		
		// Set validator options
		var v = $("#contact").validate({
			errorClass: "error",
			onkeyup: false,
			onblur: false,
			submitHandler: function(form) {
				form.submit();
				$('input#submit').hide();
				$('#loading_image').show();
			},
			// Define special validation rules
			rules: {
				dayphone: {
					phoneUS: true
				},
				eveningphonenumber: {
					phoneUS: true
				},
				email: {
					email: true
				},
				zip: {
					minlength: 5,
					maxlength: 12
				}
			},
			// Define error messages
			messages: {
				CurriculumID: "Please select a program.",
				program: "Please select a program.",
				education_level_code: "Please select your highest level of education.",
				age_requirement: "Please select your age.",
				firstname: "Please enter your first name.",
				lastname: "Please enter your last name.",
				dayphone: "Please enter a valid phone number.",
				evephone: "Please enter a valid phone number.",
				call_time: "Please select best time to call.",
				address1: "Please enter your address.",
				city: "Please enter a city.",
				state: "Please select a state.",
				zip: "Please enter a valid Zip code.",
				email: "Please enter a valid email address."
			},
			
			submitHandler: function() {
		
			// Hide form fields, show/hide loading image
			$('#step_1,#step_2,#step_3')
				.hide();
			$('#form_content')
				.addClass('submitted');
			$('#loading_image')
				.show();
							
			$.ajax({
				// 'this' refers to the current submitted form
				type: 'POST',
				data: $('#contact').serialize(),
				url: 'post.php',
				success: function(msg) {
					$("#post_results").ajaxComplete(function(event, request, settings) {
						if (msg.search(/delinquencyFail/i) == -1) // Message Sent? Show the 'Thank You' message and hide the form
						{
							result = 'Your request has been sent. <br />Thank you!';
							$('#loading_image')
								.hide();
							$('#post_results')
								.html(result)
								.fadeIn(1200);
							// Fire conversion tracking with iframe
							var iframe = document.createElement('iframe');
							iframe.style.width = '0px';
							iframe.style.height = '0px';
							document.body.appendChild(iframe);
							iframe.src = 'http://www.waldenonlinedegrees.com/thank_you.html';
						}
						else
						{
							result = 'There was an error with your request. Sorry!';
							$('#loading_image')
								.hide();
							$('#post_results')
								.html(result)
								.fadeIn('fast');
						}
					});
				}
				
			}); // CLOSING .ajax
			
		} // CLOSING submitHandler
		});
		
		// Hide all steps
		function hideAllSteps() {
			$('#form_steps li').removeClass('active');
			$('#' + prevDiv).hide();
			$('#' + stepDiv).hide();
			$('#' + nextDiv).hide();
		}
		
		// If next button was clicked
		if ($(button).hasClass('next_button'))
		{
			// Force validator to run
			if (v.form())
			{
				// Hide current step, show results and next step
				if (processStep(currentStep))
				{
					hideAllSteps();
					$('#form_steps li').eq(parseInt(currentStep)).addClass('active');
					$('#' + nextDiv).fadeIn('fast');
				}
			}
			return false;
		}
		// If submit button was clicked
		else if ($(button).hasClass('submit_button'))
		{
			if (v.form())
				return true;
			else
				return false;
		}
		// All that's left are the back buttons...
		else
		{
			// Hide current step, show previous step
			hideAllSteps();
			// Hide results if going back to step 1
			if (prevDiv == 'step_1')
			{
				$("#step_1_results").hide();
			}
			$('#form_steps li').eq(parseInt(currentStep) - 2).addClass('active');
			$('#' + prevDiv).fadeIn('fast');
			return false;
		}
		
		// VALIDATE STEPS
		function processStep(step) {
			
			if (step == "1" && validateStep1())
			{
				return true;
				
			}
			else if (step == "2" && validateSecondStep())
				
				return true;
				
			else 
			
				return false;
		}
		
	
	});
	
});

function validateStep1() {
	var program = document.getElementById("CurriculumID");
	var education_level = document.getElementById("education_level_code");
	
	var form1 = document.getElementById("step_1");
	var step2 = document.getElementById("step_2");
	var form2 = document.getElementById("step_3");
	
	var seg = document.getElementById("segmentation_question");
	var opt = document.getElementById("CurriculumID").options[document.getElementById("CurriculumID").selectedIndex];
	var edu = document.getElementById("education_level_code").options[document.getElementById("education_level_code").selectedIndex];
	
	if (opt.parentNode.nodeName == "OPTGROUP") // Is contained in an optgroup
	{
		//To inquire for a Master's-level degree, must have a Bachelor's degree or higher
		if ((opt.parentNode.label.search(/Master/i) >= 0 || opt.parentNode.label.search(/M.S./i) >= 0) && (opt.parentNode.label.search(/M.S. degree program in Nursing/i) < 0 && opt.parentNode.label.search(/RN Track/i) < 0) && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		//To inquire for TPP and Sp.Ed. Endorsement, must have a Bachelor's degree or higher
		else if ((opt.parentNode.label.search(/Special Education/i) >= 0 || opt.parentNode.label.search(/Teacher Preparation/i) >= 0) && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		//To inquire for a Post-Doctoral Certificate, must have a PhD degree or higher
		else if (opt.parentNode.label.search(/Post-Doctoral/i) >= 0 && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC" || edu.value == "BACH" || edu.value == "MAST"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		//To inquire for a Post-Masters Certificate, must have a Masters degree or higher
		else if (opt.parentNode.label.search(/Post-Masters/i) >= 0 && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC" || edu.value == "BACH"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		//To inquire for a EdD or PhD program, must have a Master's degree or higher
		else if ((opt.parentNode.label.search(/Ph.D/i) >= 0 || opt.parentNode.label.search(/Ed.D/i) >= 0) && (opt.parentNode.label.search(/Ph.D. in Psychology/i) < 0) && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC" || edu.value == "BACH"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		// validate exceptions
		//M.S. degree in Nursing (RN Track) allows entrance with Nursing Diploma, Some College or an AA/AS degree (or higher)
		else if (opt.parentNode.label.search(/M.S. degree program in Nursing/i) >= 0 && opt.parentNode.label.search(/RN Track/i) >= 0 && (edu.value == "HS"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		//The Ph.D. in Psychology allows entrance with a B.S. in Psychology. Allow Bachelors or higher to inquire
		else if (opt.parentNode.label.search(/Ph.D. in Psychology/i) >= 0 && (edu.value == "HS" || edu.value == "NURS" || edu.value == "SOMECOL30" || edu.value == "SOMECOL60" || edu.value == "SOMECOL61" || edu.value == "ASSOC"))
		{
			alert("We're sorry, but your level of education does not qualify you for this program.");
			return false;
		}
		else if (education_level.selectedIndex == 0)
		{
			alert("Please select an education level.");
			return false;
		}
		//Program and Education Level validation passed
		else
		{
			//Show step2 - aka select_boxes
			form1.style.display = "none";
			step2.style.display = "block";
			var age = document.getElementById("age_question");
			var nursing = document.getElementById("nursing_question");
			var teaching = document.getElementById("teaching_question");
			var engineering = document.getElementById("engineering_questions");

			//Only show Bachelors questions if any of the Bachelor programs are selected
			if(opt.parentNode.label.search(/Bachelor/i) >= 0 || opt.parentNode.label.search(/B.S./i) >= 0)
			{
				age.style.display = "block";
			}
			//Only show RN question if the nursing programs are selected.
			if(opt.parentNode.label.search(/Nursing/i) >= 0)
			{
				nursing.style.display = "block";
			}
			//Only show teaching cert question if MS Education or EdD are selected
			if(opt.parentNode.label.search(/Ed.D/i) >= 0 || (opt.parentNode.label.search(/Education/i) >= 0 && (opt.parentNode.label.search(/Master/i) >= 0 || opt.parentNode.label.search(/M.S./i) >= 0)))
			{
				teaching.style.display = "block";
			}
			//Only show qualifying question if Engineering is selected
			if(opt.parentNode.label.search(/Engineer/i) >= 0)
			{
				engineering.style.display = "block";
			}
			return true;
		}
	}
	else if (seg.selectedIndex == 0)
	{
		alert("Please select whether you prefer earning your degree online.");
		return false;
	}
	else if (CurriculumID.selectedIndex == 0)
	{
		alert("Please select a program of interest.");
		return false;
	}
	else if (education_level.selectedIndex == 0)
	{
		alert("Please select an education level.");
		return false;
	}
	else
	{
		//Show all questions since the program is not in a category - this should not happen
		age.style.display = "block";
		nursing.style.display = "block";
		teaching.style.display = "block";
		engineering.style.display = "block";
		form1.style.display = "none";
		step2.style.display = "block";
	}
}

function validateSecondStep() {
	//Get questions
	var age = document.getElementById("age_question");
	var nursing = document.getElementById("nursing_question");
	var teaching = document.getElementById("teaching_question");
	var engineering = document.getElementById("engineering_questions");
	
	//Which questions were asked
	var age_asked = (age.style.display == "block") ? true:false;
	var nursing_asked = (nursing.style.display == "block") ? true:false;
	var teaching_asked = (teaching.style.display == "block") ? true:false;
	var engineering_asked = (engineering.style.display == "block") ? true:false;

	//Answers
	var age_requirement = document.getElementById("age_requirement");
	var rn_license = document.getElementById("rn_license");
	var teaching_certificate = document.getElementById("teaching_certificate");
	var required_courses = document.getElementById("required_courses");
	var undergrad_degree = document.getElementById("undergrad_degree");
	
	var form1 = document.getElementById("step_1");
	var form2 = document.getElementById("step_2");
	var form3 = document.getElementById("step_3");
	var step2 = document.getElementById("select_boxes");
	
	var passed = true;
	var disqualified = false;

	if (age_asked)
	{
		if (age_requirement.selectedIndex != 2) 
		{
			alert("You must be at least 21 years of age.");
			passed = false;
		}

	}
	if (nursing_asked)
	{
		if (rn_license.selectedIndex == 0) 
		{
			alert("You must have a current nursing liscense to apply for this program.");
			passed = false;
		}

	}
	if (teaching_asked)
	{
		if (teaching_certificate.selectedIndex == 0) 
		{
			alert("You must hold a current teaching certficate to apply for this program.");
			passed = false;
		}

	}
	if (engineering_asked)
	{
		if (required_courses.selectedIndex == 0 && undergrad_degree.selectedIndex == 0) 
		{
			alert("Please select if you have an undergrad degree in Electrical/Computer Engineering or Computer Science.");
			alert("Please select if you successfully completed one year of college-level calculus and a college course in differential equations and linear algebra.");
			passed = false;
		}

	}
	if (passed && !disqualified)
	{
		form1.style.display = "none";
		form2.style.display = "none";
		form3.style.display = "block";
	}

	return passed;
}

function brandons_fillPrograms(LocationElementID, ProgramElementID)
{
    __CurriculumObject = _LocationID;
                __locationObj = document.getElementById(LocationElementID);
                
                FillPrograms(__locationObj.value, ProgramElementID)
}

