// JavaScript Document

// Submit the form on successful validation
$.validator.setDefaults({
	submitHandler: function(form) {
   		form.submit();
	}
});

$().ready(function() {
	
	// Scroll the header and form
	$(window).scroll(function () {
		if ($(window).scrollTop() >= 1 && $('#step2').is(':hidden'))
		{
			$('#form')
				.css('position','fixed')
				.css('top','0');
			$('#form')
				.css('top','20px')
		}
		else
		{
			$("#form")
				.css('position','static')
				.css('top','0');
			$('#form')
				.css('top','123px')
		}
	});

	// Change default values for campus and program for validation
	document.contact.program.options[0].value = '';
	
	// Expand program dropdown for IE
	$("#program").mousedown(function() {
		$(this).css('width','460px');
	})
	$("#program").blur(function() {
		$(this).css('width','274px');
	})
	$("#program").change(function() {
		$(this).css('width','274px');
	})
	
	// Collapse form when Military field selected
	$('#notbeingused').change(function() {
		
		if ($('#step1 input[name=Military]:checked').val() != null)
		{
			$('#testimonials').hide();
			$('#step2').slideDown('fast');
		}
	});
	
	$('#step1 input[name=Military]').click(function() {
		$('#form').css('position','static');
		$('#testimonials').hide();
		$('#quote_box').hide();
		$('#step2').slideDown('fast');
		// Scroll to top of form
		$('html,body,window')
			.animate({ scrollTop: $('#form').offset().top }, { duration: 'fast', easing: 'swing'});
	});
	
	// Validate signup form on keyup and submit
	$("#contact").validate({
		rules: {
			// Insert fields from the form
			program: "required",
			FirstName: "required",
			LastName: "required",
			PrimaryPhone: {
				required: true,
				phoneUS: true
			},
			Email: {
				required: true,
				email: true
			},
			Address1: "required",
			City: "required",
			State: "required",
			Zip: {
				required: true,
				minlength: 5,
				maxlength: 12
			},
			gradyear: "required",
			TimeToContactID: "required",
			DayToContactID: "required",
			EducationID: "required",
			StartTermID: "required"
		},
		messages: {
			// Place custom error messages
			program: "Please select a program.",
			FirstName: "Please enter your first name.",
			LastName: "Please enter your last name.",
			PrimaryPhone: "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.",
			gradyear: "Please select your High School graduation year.",
			TimeToContactID: "Please choose the best time to contact.",
			DayToContactID: "Please choose the best day to contact.",
			EducationID: "Please choose highest level of education.",
			StartTermID: "Please choose a start term."
		},
		submitHandler: function() {
		
			// Hide form fields, show/hide loading image
			$('#step1,#step2')
				.hide();
			$('#contact')
				.addClass('submitted');
			$('#loading_image')
				.show();
			
			$.ajax({
				// 'this' refers to the current submitted form
				type: 'POST',
				data: $('#contact').serialize(),
				url: 'post.php',
				success: function(msg) {
					$("#post_results").ajaxComplete(function(event, request, settings) {
						// Message Sent? Show the 'Thank You' message and hide the form
						if (msg.search(/delinquencyfail/i) == -1) 
						{
							result = 'Your request has been sent. <br />Thank you!';
							// Scroll to top of form
							$('html,body,window')
								.animate({ scrollTop: $('#banner').offset().top }, { duration: 'fast', easing: 'swing'});
							$('#loading_image')
								.hide();
							$('#post_results')
								.html(result)
								.fadeIn(1200);
							// Fire conversion tracking with iframe
							var iframe = document.createElement('iframe');
							iframe.style.width = '0px';
							iframe.style.height = '0px';
							document.body.appendChild(iframe);
							iframe.src = 'http://www.post.edu/onlinelearning/thank_you.html';
						}
						else
						{
							result = 'There was an error with your request. Sorry!';
							$('#loading_image')
								.hide();
							$('#post_results')
								.html(result)
								.fadeIn('fast');
						}
					});
				}
				
			}); // CLOSING .ajax
			
		} // CLOSING submitHandler
	});
	
	// Accordion effect on program descriptions
	$('#program_descriptions h2').click(function(){
		
		var clicked = $(this);
		if ($(clicked).next().is(':hidden')) { 
			$('#program_descriptions h2')
				.next()
				.slideUp()
				.prev('h2')
				.removeClass('active'); 
			$(clicked)
				.next()
				.slideDown()
				.prev('h2')
				.addClass('active');
			// Select option on form, set it as html in results div
			// but only if user is on Step 1
			if (!$('#step2').is(':visible'))
			{
				$('#program')
					.val($(clicked).text());
			}
		}
		else
		{
			$(clicked)
				.next()
				.slideUp()
				.prev('h2')
				.removeClass('active');	
		}
		return false; 

	});
	
});

// IE focus fix
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);

