$(document).ready(function() {
	
	// Mask phone input
	$('#contact input[name=dayphone]')
		.mask('(999) 999-9999');
	
	// Validate signup form on keyup and submit
	$('#contact').validate({
		rules: {
			LocationID: "required",
			CurriculumID: "required",
			firstname: "required",
			LastName: "required",
			age: {
				required: true,
				digits: true,
				minlength: 2,
				maxlength: 3
			},
			dayphone: {
				required: true,
				phoneUS: true
			},
			email: {
				required: true,
				email: true
			},
			address: "required",
			city: "required",
			state: "required",
			zip: {
				required: true,
				minlength: 5,
				maxlength: 12
			},
			Gender: "required",
		},
		messages: {
			LocationID: 'Please select a campus.',
			CurriculumID: 'Please select a program.',
			firstname: 'Please enter your first name.',
			LastName: 'Please enter your last name.',
			age: 'Please enter your age.',
			dayphone: 'Please enter a phone number.',
			email: 'Please enter a valid email.',
			address: 'Please enter an address.',
			city: 'Please enter a city.',
			state: 'Please select a state.',
			zip: 'Please enter a Zip code.',
			Gender: 'Please select your gender.'
		},
		submitHandler: function() {
			
			// Hide form fields, show/hide loading image
			$('#step-1,#step-2')
				.hide();
			$('#form-loading')
				.show();
			
			$.ajax({
				// Posting the form data
				type: 'POST',
				data: $('#contact').serialize(),
				url: 'http://careercoursesathome.com/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(/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://careercoursesathome.com/thank-you.php';
						}
						else
						{
							result = 'There was an error with your request. Sorry!';
						}
						$('#form-loading')
								.hide();
						$('#post-results')
							.html(result)
							.fadeIn('fast');
						window.scrollTo(0,0);
					});
				}
				
			}); // CLOSING .ajax
			
		} // CLOSING submitHandler
		
	}) // CLOSING .validate
	
	// ACCREDITATION INFORMATION IN FOOTER
	$('#accreditation-tab').click(function() {
		$('#accreditation').slideToggle();
	});
	
	// NEXT BUTTON
	$('#continue').click(function() {
		$('#form-steps span').toggleClass('active');
		$('#step-1').hide();
		$('#step-2').fadeIn();
	});
	
	// Generate radio buttons
	$('#programs_listing').html('');
	$('#degreelvls input').click(function() {
		if($(this).val()=='High School Diplomas') {
			$('#CampaignID').val('4549');
			$('#FormID').val('3720');
			$('#locationid').val('35427');
			var pname='program_hidden_high_school'; 
			}
		else {
			$('#CampaignID').val('3061');
			$('#FormID').val('2782');
			$('#locationid').val('30913');
			var pname='programs_hidden'; 
			}
		$('#quote').hide();
		$('#programs_listing').html('');
		switch(this.value)
		{
			case 'Undergraduate Certificates':
				levels_index = 0;
				break;
			case 'High School Diplomas':
				levels_index = 1;
				break;
			case 'Bachelor':
				levels_index = 2;
				break;
			default:
				levels_index = 3;
				break;
		}
		$('#degreelvls_display').html($(this).val() + "<br /><small><a href='#'>Change my degree level</a></small>");
		$('#degreelvls_display a').click(function() {
			$('#degreelvls').slideDown();
			$('#degreelvls_display').slideUp();
			$('#programs_div').slideUp();
			return false;
		});
		$('#degreelvls').slideUp();
		$('#degreelvls_display').slideDown();
		$("#programs_div").show();
		$('#' + pname + ' option').each(function() {
			if (this.text != 'Please select' && levels[levels_index].test(this.text))
			{
				if (levels_index == 3)
				{
					if ($('input[name=degreelvl]:checked').val() == 'Associate' && isAssociate(this.text))
					{
						$('<label><input type="radio" name="CurriculumID" value="' + this.value + '"> <span>' + this.text + '</span></label><br />').appendTo('#programs_listing');
					}
					else if ($('input[name=degreelvl]:checked').val() == 'Career Diplomas' && !isAssociate(this.text))
					{
						$('<label><input type="radio" name="CurriculumID" value="' + this.value + '"> <span>' + this.text + '</span></label><br />').appendTo('#programs_listing');
					}
				}
				else
				{
					$('<label><input type="radio" name="CurriculumID" value="' + this.value + '"> <span>' + this.text + '</span></label><br />').appendTo('#programs_listing');
				}
			}
		});
		$('#programs_listing').slideDown();
		$('#program_display').slideUp();
		$('#programs_listing label').click(function() {
			$('#continue').show();
			var val = $(this).html(),
				showOptGroup = "<br /><small><a href='#'>Change my area of study</a></small>";
			$('#program_display').html(val + showOptGroup);
			$('#programs_listing').slideUp();
			$('#program_display').slideDown();
			// Scroll to top of page
			$('html,body,window')
				.animate(
					{scrollTop: 0 },
					{duration: 'slow', easing: 'swing'}
				);
			$('#program_display a').click(function() {
				$('#programs_listing').slideDown();
				$('#program_display').slideUp();
				return false;
			});
		});
	});
	
	if ($("#CurriculumID").val() != '')
	{
		if ($("#content h1").html().search(/high school/i) > -1) {
			$('#CampaignID').val('4549');
			$('#FormID').val('3720');
			$('#locationid').val('35427');
			var pname='program_hidden_high_school'; 
		}
		else {
			$('#CampaignID').val('3061');
			$('#FormID').val('2782');
			$('#locationid').val('30913');
			var pname='programs_hidden'; 
		}

	}

	// Hide form loading, show form
	$('#form-loading').hide();
	$('#contact').fadeIn();
		
});

var pName = "CurriculumID",
levels_index,
levels = new Array(  // 0 = Undergraduate Certificates, 1 = High School Diplomas, 2 = Bachelor, 3 = all others
	new RegExp(/ Cert/i),
	new RegExp(/High School/i),
	new RegExp(/Bachelor/i),
	new RegExp(/^((?! Cert|High School|Bachelor).)*$/i)
);

function array_match(val, arr)
{
	for (var i = 0, l = arr.length; i < l; i++)
		if (val.search(arr[i]) > -1)
			return true;
	return false;
}

function isAssociate(val)
{
	var arr = new Array(
		"Accounting",
		"Associate Degree-Hospitality Management",
		"Computer Information Systems",
		"Construction Technology",
		"Criminal Justice",
		"Early Childhood Education",
		"Engineering Technology",
		"Fashion Merchandising",
		"Finance",
		"Graphic Design",
		"Health Information Technology",
		"Hospitality Management",
		"Human Resources Management",
		"Industrial Electronics and Electrical Maintenance Technology",
		"Marketing",
		"Paralegal Studies",
		"PC Maintenance Technology",
		"Pet Groomer",
		"Retail Management",
		"Veterinary Technician",
		"Veterinary Technology"
	);
	for (var i = 0, l = arr.length; i < l; i++)
		if (val.search(arr[i]) > -1)
			return true;
	return false;
}
