// JavaScript Document

$().ready(function() {
	// FORM VALIDATION
	// set first campus value to be null for validation
	$("#campusid option[value='-1']").val("").html("Please select a campus");

	// Reset default program message
	$("#campusid").change(function() {
		$("#program option[value='-1']").val("");
	});

	// force format the phone number
	$("#phonenumber").mask("(999) 999-9999",{completed:function(){this.removeClass('unfilled');}});

	// Set phone number field
	$('#submit').click(function() {
		var number = $('#phonenumber').val();
		$('#phone_number').val(number);
	});

	// Post to online campaign if online campus is selected
	$('#campusid').change(function() {
		
		if($('#campaignid').val() != '9836' && $('#campaignid').val() != '10052') // Don't switch if the Pandora campaign
		{
			if ($(this).val() == '14669') // Switch to PSM online campaign
			{
				$('#form input[name=campaignid]').val('9119');
			}
			else // Regular PSM campaign
			{
				$('#form input[name=campaignid]').val('3805');
			}
		}
	
	});

	
	// Check input value against inline label
	jQuery.validator.addMethod("checkLabel", function( value, element ) {
		return this.optional(element) || value != element.title;
	}, "Please enter a value.");
	
	// Validate signup form on keyup and submit
	$("#contact").validate({
		rules: {
			// Insert fields from the form
			campusid: "required",
			program: "required",
			first_name: {
				required: true,
				checkLabel: true
			},
			last_name: {
				required: true,
				checkLabel: true
			},
			phonenumber: {
				required: true,
				phoneUS: true,
				checkLabel: true
			},
			email: {
				required: true,
				email: true,
				checkLabel: true
			},
			address: {
				required: true,
				checkLabel: true
			},
			city: {
				required: true,
				checkLabel: true
			},
			state: "required",
			gradYear: "required",
			zip: {
				required: true,
				minlength: 5,
				maxlength: 12,
				checkLabel: true
			}
		},
		messages: {
			// Place custom error messages
			campusid: "Please select a campus.",
			program: "Please select a program.",
			first_name: "Please enter your first name.",
			last_name: "Please enter your last name.",
			phonenumber: "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.",
			gradYear: "Please select a graduation year."
		},
		errorPlacement: function(error, element) { 
			if ($(".madlib").length > 0)
				$("#errors").append(error);
			else
				error.insertAfter(element);
		},
		submitHandler: function(form) {
			form.submit();
			$('input#submit').hide();
			$('#loading_image').show();
		}
	});
	
	// SHOW STEP 2
	$('#program').change(function() {
		$('#form_header').css('background-position','bottom left');
		$('#testimonial').fadeOut('fast');
		$('#form_logo').fadeOut('fast');
		animatedcollapse.show('step2');
	});
	$('#c1_link').click(function() {
		animatedcollapse.show('c1');
		return false;
	});
	$('#c2_link').click(function() {
		animatedcollapse.show('c2');
		return false;
	});
	$('#c3_link').click(function() {
		animatedcollapse.show('c3');
		return false;
	});
	$('#c4_link').click(function() {
		animatedcollapse.show('c4');
		return false;
	});
	$('#c5_link').click(function() {
		animatedcollapse.show('c5');
		return false;
	});
	
	// Inline labels on text 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');
	});
});

// 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);

// COLLAPSE LOCATION DESCRIPTIONS
 if ($('#location_descriptions'))
 {
  var divs = $('#location_descriptions div');
  var divsLength = divs.length;
  
  for (i = 0; i < divsLength; i++) 
  {
   animatedcollapse.addDiv(divs[i].id, 'fade=1,height=auto,group=location')
   animatedcollapse.init()
  }
  
 }
