// JavaScript Document

$(document).ready(function() {
	

/*
var my_options = $("#campusid option");

my_options.sort(function(a,b) {
    if (a.text > b.text) return 1;
    else if (a.text < b.text) return -1;
    else return 0
})

$("#campusid").empty().append( my_options );
*/

// sort campuses to remedy Chrome bug that sorts by value
function sortCampuses()
{
	var $dd = $('#campusid');
	// save the selected value
	var selectedVal = $dd.val();

	// get the options and loop through them
	var $options = $('option', $dd);
	var arrVals = [];
	$options.each(function(){
		// push each option value and text into an array
		arrVals.push({
			val: $(this).val(),
			text: $(this).text()
		});
	});

	// sort the array by the value (change val to text to sort by text instead)
	arrVals.sort(function(a, b){
		if (a.val != '' && a.val != '-1')
		{
			if(a.text > b.text)
				return 1;
			else if (a.text == b.text)
				return 0;
			else
				return -1;
		}
	});

	// loop through the sorted array and set the text/values to the options
	for (var i = 0, l = arrVals.length; i < l; i++)
		$($options[i]).val(arrVals[i].val).text(arrVals[i].text);

	// set the selected value back
	$dd.val(selectedVal);
}

	// Limit campuses by state
	$("#state").change(function() {
		InitDropDowns('campusid', 'program');
		var oCampus = document.getElementById('campusid');
		var iRecCnt;
			for(iRecCnt = oCampus.options.length-1;iRecCnt>=0;iRecCnt--) {
				//Debug
				//alert(parseInt(oCampus.options[iRecCnt].value));
				//alert(oCampus.options[iRecCnt].value);
				//alert(oCampus.options[iRecCnt].text);
				switch(parseInt(oCampus.options[iRecCnt].value)) {
					case 19071: oCampus.options[iRecCnt].text ='IN - Rockport';break;
					case 19070: oCampus.options[iRecCnt].text ='KY - Albany';break;
					case 19: oCampus.options[iRecCnt].text ='KY - Bellevue';break;
					case 22: oCampus.options[iRecCnt].text ='KY - Bowling Green';break;
					case 19073: oCampus.options[iRecCnt].text ='KY - Clinton';break;
					case 24: oCampus.options[iRecCnt].text ='KY - Hopkinsville';break;
					case 18669: oCampus.options[iRecCnt].text ='Online';break;
					case 18: oCampus.options[iRecCnt].text ='KY - Louisville Fern Valley';break;
					case 19064: oCampus.options[iRecCnt].text ='KY - Louisville Westport';break;
					case 19896: oCampus.options[iRecCnt].text ='KY - Madisonville';break;
					case 20: oCampus.options[iRecCnt].text ='KY - Owensboro';break;
					case 21: oCampus.options[iRecCnt].text ='KY - Paducah';break;
					case 27: oCampus.options[iRecCnt].text ='KY - Paducah Technical';break;
					case 19074: oCampus.options[iRecCnt].text ='KY - Russellville';break;
					case 19072: oCampus.options[iRecCnt].text ='KY - Scottsville';break;
					case 18956: oCampus.options[iRecCnt].text ='OH - Chillicothe';break;
					case 18955: oCampus.options[iRecCnt].text ='OH - Jackson';break;
					case 18954: oCampus.options[iRecCnt].text ='OH - Lancaster';break;
					case 18953: oCampus.options[iRecCnt].text ='OH - New Boston';break;
					<!--8.2011 modifiedRT# 63238-->
					case 23: oCampus.options[iRecCnt].text ='TN - Clarksville';break;
					case 25: oCampus.options[iRecCnt].text ='TN - Murfreesboro';break;
					case 26: oCampus.options[iRecCnt].text ='TN - Nashville';break;
					default:break;
				}
			}
			sortCampuses();
		if (
			$(this).val() != 'KY' &&
			$(this).val() != 'IN' &&
			$(this).val() != 'OH' &&
			$(this).val() != 'TN'
		)
		{
			$("#campusid option:contains('Bellevue')").remove();
			$("#campusid option:contains('Bowling Green')").remove();
			$("#campusid option:contains('Chillicothe')").remove();
			$("#campusid option:contains('Jackson')").remove();
			$("#campusid option:contains('Lancaster')").remove();
			$("#campusid option:contains('Louisville Westport')").remove();
			$("#campusid option:contains('Louisville Fern Valley')").remove();
			$("#campusid option:contains('New Boston')").remove();
			$("#campusid option:contains('Owensboro')").remove();
			$("#campusid option:contains('Paducah')").remove();
			$("#campusid option:contains('Madisonville')").remove();
			$("#campusid option:contains('Albany')").remove();
			$("#campusid option:contains('Clinton')").remove();
			$("#campusid option:contains('Russellville')").remove();
			$("#campusid option:contains('Scottsville')").remove();
			$("#campusid option:contains('Rockport')").remove();
		}
		$("#campusid").removeAttr("disabled");
	});

	document.getElementById('campusid').options[0].value = '';
	document.getElementById('program').options[0].value = '';

	// Add class to all required fields
	$('#contact :input').addClass('pageRequired');
	$('#contact select').addClass('pageRequired');
	
	// force format the phone number
	$('#contact input[name="dayphone"]').mask("(999) 999-9999");

	// MOVING BETWEEN FORM STEPS
	$('#contact .button').click(function() {
		
		var button = $(this);
		// Divs must use ID syntax of "step_1", "step_2", etc.
		var currentStep = $(button).parent().attr('id').substring(4);
		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: {
				email: {
					email: true
				},
				zip: {
					minlength: 5,
					maxlength: 12
				}
			},
			// Define error messages
			messages: {
				campusid: "Please select a location",
				program: "Please select a program.",
				gradYear: "Please select your high school graduation year.",
				firstname: "Please enter your first name.",
				lastname: "Please enter your last name.",
				dayphone: "Please enter a valid phone number.",
				best_time_to_contact: "Please select the best time to call.",
				email: "Please enter a valid email address.",
				address: "Please enter your address.",
				city: "Please enter a city.",
				state: "Please select a state.",
				zip: "Please enter a valid Zip code."
			}
		});
		
		// Hide all steps
		function hideAllSteps() {
			$('#' + stepDiv).hide();
			$('#' + nextDiv).hide();
		}
		
		// If next button was clicked
		if ($(button).hasClass('next'))
		{
			// Force validator to run
			if (v.form())
			{
				// Hide current step, show next step
				hideAllSteps();
				$('#' + nextDiv).fadeIn('fast');
			}
			return false;
		}
		// If submit button was clicked
		else if ($(button).attr('id') == 'submit')
		{
			if (v.form())
				return true;
			else
				return false;
		}
		// All that's left are the back buttons...
		else
		{
			// Hide current step, show previous step
			hideAllSteps();
			$('#' + prevDiv).fadeIn('fast');
			return false;
		}
		
	});

});

// 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);

//ACCORDION EFFECT - WRAP YOUR LIST WITH ACCORDION DESCRIPTIONS AND THE CONTENT YOU WANT TO SHOW/HIDE WITH ACCORDION CONTAINER
$(document).ready(function(){
	
$('.accordion_container').hide();

$('#accordion_descriptions h2').click(function(){
	if( $(this).next().is(':hidden') ) { 
		$('#accordion_descriptions h2').next().slideUp(); 
		$(this).next().slideDown(); 
	}
	else
	{
		$(this).next().slideUp();	
	}
	return false; 
	
});

});
