// JavaScript Document

// Submit the form on successful validation
$.validator.setDefaults({
	submitHandler: function(form) {
   		form.submit();
		$('input#submit').hide();
		$('#loading_image').show();
	}
});

$().ready(function() {

// State Filter
$("#state").change(function () {
    // Get the text from the option of the selected state
	var selectedState = $("#state option:selected").text();
	// Array of states to filter through
	var states = ["Alabama", "Arkansas", "Florida", "Iowa", "Kentucky", "Louisiana", "Massachusetts", "Minnesota", "Nevada", "New Hampshire", "North Carolina", "Oregon", "Tennessee", "Wisconsin", "Maryland", "New Mexico", "Ohio"];
    // Check if selected state is in the array
	var inAry = (jQuery.inArray(selectedState, states));
	// If state is in array...
	if( inAry>=0) {
		$("#LocationID").hide();
		if ($(".no-programs").is(':visible'))
		{
		} 
		else 
		{
			$("label[for='LocationID']").after('<span class="no-programs">Sorry, there are currently no programs offered in this area.</span>');
		}
	} 
	else
	{
		$("#LocationID").show();
		$('.no-programs').remove();
	}

});

	

	
	// FORM VALIDATION
	// set first campus value to be null for validation
	$("#LocationID").prepend('<option selected=selected value="">Please select a campus</option>');
	$("#LocationID option[value='-1']").remove();
	
	// Remove Online Campus from dropdown
	// $("#LocationID option[value='2317']").remove();
	
	// set first program value to be null after location change
	$("#LocationID").change(function() {
		$("#CurriculumID").prepend('<option selected=selected value="">Please select a program</option>');
		$("#CurriculumID option[value='-1']").remove();
	});
	
	// force format the phone number
	$("#dayphone").mask("(999) 999-9999");
	
	// Validate signup form on keyup and submit
	$("#contact").validate({
		rules: {
			// Insert fields from the form
			LocationID: "required",
			CurriculumID: "required",
			firstName: "required",
			lastName: "required",
			dayphone: "required",
			email: {
				required: true,
				email: true
			},
			address1: "required",
			city: "required",
			state: "required",
			zip: {
				required: true,
				minlength: 5,
				maxlength: 12
			},
			edulevel: "required",
			gradyear: "required"
		},
		messages: {
			// Place custom error messages
			LocationID: "Please select a campus.",
			CurriculumID: "Please select a program.",
			firstName: "Please enter your first name.",
			lastName: "Please enter your last name.",
			dayphone: "Please enter a phone number.",
			email: "Please enter a valid email address.",
			address1: "Please enter your address.",
			city: "Please enter a city.",
			state: "Please choose a state.",
			zip: "Please enter a Zip code.",
			edulevel: "Please select your education level.",
			gradyear: "Please select a grad year."
		}
	});
	
	// SHOW STEP 2
	$('#program').change(function() {
		animatedcollapse.show('step2');
	});
	
});

// ANIMATED COLLAPSE FOR FORM
animatedcollapse.addDiv('step2', 'fade=1,height=auto')
animatedcollapse.init()

// FOCUS FIX FOR INTERNET EXPLORER
sfFocus = function() {
	var sfEls = document.getElementsByTagName("INPUT");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfFocus);

// PROGRAM DESCRIPTIONS

animatedcollapse.addDiv('p1', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p2', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p3', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p4', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p5', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p6', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p7', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p8', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p9', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p10', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p11', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p12', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p13', 'fade=1,height=auto,group=program')
animatedcollapse.init()

animatedcollapse.addDiv('p14', 'fade=1,height=auto,group=program')
animatedcollapse.init()

// COLLAPSE PROGRAM DESCRIPTIONS
if (document.getElementById("program_descriptions"))
{
	var divs = document.getElementById("program_descriptions").getElementsByTagName("div");
	var divsLength = divs.length;
	
	for (i = 0; i < divsLength; i++) 
	{
		animatedcollapse.addDiv(divs[i].id, 'fade=1,height=auto,group=programs')
		animatedcollapse.init()
	}
}
