$(document).ready(function() {
	
	// Set first option in program dropdown to null for validation
	document.contact.program.options[0].value = '';
	
	// Force format phone number
	$('#contact input[name=mainphone]')
		.mask('(999) 999-9999');
	$('#contact input[name=alternatephone]')
		.mask('(999) 999-9999');
	
	// Check for Education Level
	jQuery.validator.addMethod('checkEducationLevel', function( value, element ) {
		return this.optional(element) || (value != 'Some College' && value != 'Associates Degree or over 60 credits');
	}, "You must have a Bachelor's degree or higher to request information.");
	
	// Validate signup form on keyup and submit
	$('#contact').validate({
		rules: {
			program: 'required',
			firstname: 'required',
			lastname: 'required',
			mainphone: {
				required: true,
				phoneUS: true
			},
			BestTime: 'required',
			email: {
				required: true,
				email: true
			},
			address: 'required',
			city: 'required',
			state: 'required',
			citizen: 'required',
			zipcode: {
				required: true,
				minlength: 5,
				maxlength: 12
			},
			education: {
				required: true,
				checkEducationLevel: true
			},
			projected_start: 'required'
		},
		messages: {
			program: 'Please select a program.',
			education: 'You must have a Bachelor\'s degree or higher to request information.',
			firstname: 'Please enter your first name.',
			lastname: 'Please enter your last name.',
			mainphone: 'Please enter a phone number.',
			BestTime: 'Please choose a time you would like to be contacted.',
			email: 'Please enter a valid email.',
			address: 'Please enter your address.',
			city: 'Please enter a city.',
			state: 'Please choose a state.',
			zipcode: 'Please enter a Zip code.',
		},
		submitHandler: function(form) {
			
			// Hide form fields, show/hide loading image
			$('#contact')
				.hide();
			$('#loading')
				.show();
			
			$.ajax({
				// Posting the form data
				type: 'POST',
				data: $('#contact').serialize(),
				url: 'post.php',
				success: function(msg) {
					$('#post-results').ajaxComplete(function(event, request, settings) {
						// Form data processed --- Show the 'Thank You' message and hide loading image
						if (msg.search(/edufail/i) != -1)
						{
							result = 'You must have a Bachelors degree<br />or higher to qualify';
						}
						else if (msg.search(/statefail/i) != -1)
						{
							result = 'We are currently not accepting requests from your state. Sorry!';
						}
						else if (msg.search(/delinquencyfail/i) == -1) 
						{
							result = 'Your request has been sent. <br />Thank you!';
							// 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.concordiaonline.net/thank-you.php';
						}
						else
						{
							result = 'There was an error with your request. Sorry!';
						}
						$('#loading')
							.hide();
						$('#post-results')
							.html(result)
							.fadeIn('fast');
					});
				}
				
			}); // CLOSING .ajax
			
		} // CLOSING submitHandler
		
	}) // CLOSING .validate
	
	//Step 2 of form slides down
	$('#step2_Btn').click(function() {
	
		var isValid = 
			$('#contact select[name=program]').valid() &&
			$('#contact select[name=education]').valid() &&
			$('#contact select[name=projected_start]').valid() &&
			$('#contact select[name=citizen]').valid();
		
		if (isValid)
		{
			$('#step1').slideUp();
			$('#step2').slideDown();
		}
			
		return false;
	
	});
	
});
