// JavaScript Document

$.validator.setDefaults({
	submitHandler: function(form) {
		form.submit();
	}
});

$().ready(function() {

	// set first campus value to be null for validation
	$("#campusid option[value='-1']").val("");
	
	// set first program value to be null after location change
	$("#campusid").change(function() {
		$("#program option[value='-1']").val("");
	});

	// Check input value against inline label
	jQuery.validator.addMethod("checkLabel", function( value, element ) {
		return this.optional(element) || value != element.title;
	}, "Please enter a value.");
	
	// force format the phone number
	$("#dayphone").mask("(999) 999-9999");
	
	// validate signup form on keyup and submit
	$("#contact").validate({
		rules: {
			campusid: "required",
			program: "required",
			firstname: {
				required: true,
				checkLabel: true
			},
			lastname: {
				required: true,
				checkLabel: true
			},
			dayphone: {
				required: true,
				checkLabel: true
			},
			email: {
				required: true,
				email: true,
				checkLabel: true
			},
			address: {
				required: true,
				checkLabel: true
			},
			city: {
				required: true,
				checkLabel: true
			},
			state: "required",
			zip: {
				required: true,
				minlength: 5,
				maxlength: 12,
				checkLabel: true
			},
			ed_level: "required",
			gradyear: "required"
		},
		messages: {
			campusid: "Please select a campus.",
			program: "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.",
			address: "Please enter your address.",
			city: "Please enter a city.",
			state: "Please choose a state.",
			zip: "Please enter a Zip code.",
			ed_level: "Please enter your education level.",
			gradyear: "Please enter your graduation year."
		}
	});
	
	// Inline labels for inputs
	$(".madlib input[type=text]").attr('value', function(){return this.title}).addClass('unfilled');
	$(".madlib input[type=text]").focus(function() {
		if (this.value == this.title)
			$(this).val('').removeClass('unfilled');
	});
	$(".madlib input[type=text]").blur(function() {
		if (this.value == '')
			$(this).attr('value', function(){return this.title}).addClass('unfilled');
	});
});

function checkFields()
{
	var e = '';
	$('#contact input[type=text]').each(function() {
		if (this.value == this.title)
			e += '- Please tell us your ' + this.title + '\n';
	});
	if (e.length)
	{
		alert('Please fix the following:\n\n' + e);
		return false;
	}
	else
		return true;
}

// Focus Attribute for IE
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);

// Collapse program descriptions
if ($('.descriptions'))
{
	var divs = $('.descriptions div.blurb');
	var divsLength = divs.length;
	
	for (i = 0; i < divsLength; i++) 
	{
		animatedcollapse.addDiv(divs[i].id, 'fade=1,height=auto,group=programs')
		animatedcollapse.init()
	}
}

animatedcollapse.addDiv('certificate', 'fade=1,height=auto')
animatedcollapse.init()
animatedcollapse.addDiv('associate', 'fade=1,height=auto')
animatedcollapse.init()
animatedcollapse.addDiv('bachelor', 'fade=1,height=auto')
animatedcollapse.init()
animatedcollapse.addDiv('master', 'fade=1,height=auto')
animatedcollapse.init()
animatedcollapse.addDiv('doctoral', 'fade=1,height=auto')
animatedcollapse.init()

function fireEvent(elementID,e)
{
	var element = document.getElementById(elementID);
	if (document.createEvent)  // all browsers except IE before version 9
	{
		// dispatch for modern browsers
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(e, true, true); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
	else
	{
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on' + e,evt)
	}
}

function setPrograms(p)
{
	var programs = p.split("|");
		
	// remove options not in the array
	$("#program option").each(function() {
		if ($(this).val().length > 0 && jQuery.inArray($(this).val(), programs) == -1)
			$(this).remove();
	});

	// remove empty optgroups
	$("#program optgroup:empty").remove();
}

function limitCampuses()
{
	var val = $("#campusid option:selected").val();
	$("#campusid option").each(function() {
		if ($(this).val() != val)
			$(this).remove();
	});
}

// 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);
}

