$(document).ready(function() {

	$('#program').change(function() {
		
		var program = document.getElementById("program");
		
		if
			(program.value == 'MA in Professional Counseling' ||
			program.value == 'Master of Arts Professional Counseling (48 hr)' ||
			program.value == 'Master of Arts Professional Counseling (60 hr)' ||
			program.value == 'Master of Arts Marriage and Family Therapy')
		{
			$('#not-available-states')
				.html('<strong>This program is not available</strong> in Arkansas, California, Illinois, Kansas, Mississippi, Missouri, New Hampshire, New Jersey, New York or Oregon.')
				.slideDown();
		}
		else
		{
			$('#not-available-states')
				.hide()
				.html('');
		}
	
	});

	// SUBMITTING THE FORM
	$("#contactform").submit(function() {

		// Front end validation before posting data
		if(validateForm(this))
		{

			if (!validateState())
			{
				return false;
			}
			
			// Hide form fields
			$('#step1,#step2,#step3').hide();
			$('#loading_image').show();

			$.ajax({
				type: 'POST',
				data: $('#contactform').serialize(),
				url: 'http://www.libertyonlinedegrees.com/post.php',
				success: function(msg) {
					$("#post_results").ajaxComplete(function(event, request, settings) {
						if (msg.search(/delinquencyfail/i) == -1) // Message Sent? Show the 'Thank You' message and hide the form
						{
							result = 'Your request has been sent. <br />Thank you!<br /><br /><h2 class="style-2">Are you ready to get started? If so, please go ahead and apply today by clicking the button below.</h2><p>We look forward to speaking with you soon!</p><br /><a href="https://selfservice.liberty.edu/livedata/bwskalog.p_disploginnew?sourceid=D80449"><img src="http://www.libertyonlinedegrees.com//images/new/button_apply_now.png" alt="Apply Now" title="Apply Now" /></a>';
							// Scroll to top of form
							$('#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.libertyonlinedegrees.com/index.php/site/thank_you/';
						}
						else
						{
							result = 'There was an error with your request. Sorry!';
							$('#loading_image')
								.hide();
							$('#post_results')
								.html(result)
								.fadeIn('fast');
						}
					});
				}

			});
			
		}

		return false;

	});
	
	function validateState() {
	
		var program = document.contactform.program;
		var state = document.contactform.state;
		
		if (
			(program.value == 'MA in Professional Counseling' ||
			program.value == 'Professional Counseling-48 hr (MA)' ||
			program.value == 'Professional Counseling-60 hr (MA)' ||
			program.value == 'Marriage and Family Therapy (MA)')
			&& 
			(state.value == 'AR' ||
			state.value == 'IL' ||
			state.value == 'KS' ||
			state.value == 'MO' ||
			state.value == 'MS' ||
			state.value == 'NH' ||
			state.value == 'OR')
			)
		{
			alert('You may not take your selected course online in your state.');
			return false;
		}
		else
		{
			return true;
		}
		
	}
	
	// Valid state when user selects one
	$('#state').change(function() {validateState();});

});
