var total = 0;
var tax = 0;
var shipping = 0;
var tuition = 0;
var resource1 = 0;
var resource2 = 0;
var resource3 = 0;

function new_total() {
	total = (Number(shipping) + Number(tax) + Number(tuition) + Number(resource1) + Number(resource2) + Number(resource3));
	total = (Math.round(total * 100) / 100);
	$('#total').html('$' + total);
}

function new_tax() {
	var value = $('#course').val();
	value = value.split('|');
	$('#tuition').html('$' + value[1]);
	tuition = value[1];
	new_total();
	if (($('#state').val().toLowerCase() == 'co' || $('#state').val().toLowerCase() == 'colorado') && (value[0] == 'Mortgage Broker Distance Learning' || value[0] == 'Mortgage and Real Estate Distance Learning')) {
		tax = Math.round((total * .088)*100)/100;
	} else {
	 	tax = 0;
	}
	$('#tax').html('$' + tax);
	new_total();
}

function new_shipping() {
	var value = $('#course').val();
	value = value.split('|');

	if (value[0] == 'Mortgage Broker Distance Learning' || value[0] == 'Mortgage and Real Estate Distance Learning') {
		shipping = 9.95;
	} else {
	 	shipping = 0;
	}
	$('#shipping').html('$' + shipping);
	
	new_total();
}

$(document).ready(function(){
	
	$('#course').change(function(){
		new_tax();
		
		new_shipping();
	});

	$('#state').keyup(function(){
		new_tax();
	});
	
	$('#course_book').click(function(){
		if ($('#course_book').is(':checked') == true) {
			resource1 = 24.95;
		} else {
			resource1 = 0;
		}
		new_tax();
	});
	
	$('#note_book').click(function(){
		if ($('#note_book').is(':checked') == true) {
			resource2 = 79.95;
		} else {
			resource2 = 0;
		}
		new_tax();
	});
	
	$('#practice_questions').click(function(){
		if ($('#practice_questions').is(':checked') == true) {
			resource3 = 34.95;
		} else {
			resource3 = 0;
		}
		new_tax();
	});
	
	$('#submit_register').click(function(){
		
		$('#submit_register').hide();
		$('#submit_proccess').show();
		
		$('.required').each(function(el){
			if ($(this).val().length < 1) {
				$(this).addClass('required-on');
			} else {
				$(this).removeClass('required-on');
			}
		});
		
		if ($('.required-on').length){
			
			alert('Fields in red are required.');
			
		} else {
			var terms = 'no';
			var agreement = 'no';
			var opt = 'no';
			var course_book = 0;
			var note_book = 0;
			var practice_questions = 0;
			
			if ($('#terms').is(':checked') == true) {
				terms = '&terms=yes';
			}

			if ($('#agreement').is(':checked') == true) {
				agreement = '&agreement=yes';
			}

			if ($('#opt').is(':checked') == true) {
				opt = '&opt=yes';
			}

			if ($('#course_book').is(':checked') == true) {
				course_book = '&course_book=24.95';
			}

			if ($('#note_book').is(':checked') == true) {
				note_book = '&note_book=79.95';
			}

			if ($('#practice_questions').is(':checked') == true) {
				practice_questions = '&practice_questions=34.95';
			}

			if ($('#terms').is(':checked') == false) {
				alert('You must agree to the terms of service.');
				$('#submit_register').show();
				$('#submit_proccess').hide();
				return false;
			}
			
			if ($('#agreement').is(':checked') == false) {
				alert('You must agree to the registration agreement.');
				$('#submit_register').show();
				$('#submit_proccess').hide();
				return false;
			}
			
			if ($('#email').val() != $('#confirm_email').val()) {
				alert('Email fields must match.');
				$('#submit_register').show();
				$('#submit_proccess').hide();
				return false;
			}
			$.ajax({
				type: "POST",
				url: "register_submit.php",
				data: $('input[@type=text]').serialize()+'&course='+$('#course').val()+'&lead_source='+$('#lead_source').val() + terms + agreement + opt + course_book + note_book + practice_questions + "&total=" + total + "&tuition=" + tuition + "&tax=" + tax + "&shipping=" + shipping,
				success: function(msg){
					$('input[@type=text]').val('');
					alert(msg);
					$('#submit_register').show();
					$('#submit_proccess').hide();
				}
			});
		}
	});

});
