$(document).ready(function() {

	// set first campus value to be null for validation
	$("#campusid").prepend('<option selected=selected value="">Please select a campus</option>');
	$("#campusid option[value='-1']").remove();
	
	// set first program value to be null after location change
	$("#campusid").change(function() {
		$("#program").prepend('<option selected=selected value="">Please select a program</option>');
		$("#program option[value='-1']").remove();
	});
	
	// Expand program dropdown for IE
	$("#program").mousedown(function() {
		$(this).css('width','370px');
	})
	$("#program").blur(function() {
		$(this).css('width','270px');
	})
	$("#program").change(function() {
		$(this).css('width','270px');
	})

	// 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: {
				phonenumber: {
					phoneUS: true
				},
				eveningphonenumber: {
					phoneUS: true
				},
				email: {
					email: true
				},
				zipcode: {
					minlength: 5,
					maxlength: 12
				}
			},
			// Define error messages
			messages: {
				campusid: "Please select a campus.",
				program: "Please select a program.",
				education: "Please select your highest level of education.",
				expectedstart: "Please select when you would like to start.",
				firstname: "Please enter your first name.",
				lastname: "Please enter your last name.",
				phonenumber: "Please enter a valid phone number.",
				eveningphonenumber: "Please enter a valid phone number.",
				address: "Please enter your address.",
				city: "Please enter a city.",
				state: "Please select a state.",
				zipcode: "Please enter a valid Zip code.",
				email: "Please enter a valid email address."
			}
		});
		
		// 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
				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")
		{
			// Display selected program
			var campusSelect = $('#campusid option:selected').text();
			var progSelect = $('#program option:selected').text();
			$("#step_1_results")
				.show()
				.html('<p><strong>I am interested in:</strong><br /> ' + progSelect + '</p>');
		}
		
		return true;
	}

	// Code to account for different field names on the two different campaigns
	$('#contact').submit(function() {
		$('#NameFirst').val($('#firstname').val());
		$('#NameLast').val($('#lastname').val());
		$('#phoneday').val($('#phonenumber').val());
		$('#phonenight').val($('#eveningphonenumber').val());
		$('#address1').val($('#address').val());
		$('#zip').val($('#zipcode').val());
		$('#edlevel').val($('#education').val());
		$('#startdate').val($('#expectedstart').val());
	});
	
	// Accordion effect
	$('#accordian h2').click(function(){
		
		var clicked = $(this);
		if ($(clicked).next().is(':hidden')) { 
			$('#accordian h2')
				.next()
				.slideUp(); 
			$(clicked)
				.next()
				.slideDown();
		}
		else
		{
			$(clicked)
				.next()
				.slideUp();	
		}
		return false; 

	});
	
});
