$().ready(function() {
	update_fees();
	tabbed_form();
	payment_bits();
	
	$('body').append('<div id="dialog"></div>');
	$('.location').click(function() {
			var url = $(this).attr('href') + '&silent=1';
			var options = { 
				modal: true,
				height: 500,
        width: 500,
        autoOpen: false,
        title: "Choose A Start Date" 
			}
			$("#dialog").load(url);
			$("#dialog").dialog(options);
			$("#dialog").dialog('open');
			return false;
	});
	// Update Payment amount
	$('input[name=book]').change(update_fees);
	$('input[name=book7]').change(update_fees);
	$('input[name=supplement]').change(update_fees);

	// Mask input
	$("input.phone").setMask({mask:'(999) 999-9999'});
	$("input#cc_num").setMask({mask:'9999 9999 9999 9999'});
	$("input#cc_num_print").setMask({mask:'9999 9999 9999 9999'});
});

function update_fees() {
	$('body').data('course-fee', $('#course-fee').text());
	$('#book_text').remove();
	$('#book7_text').remove();
	
	$('#supplement_text').remove();
	$('body').data('supplement', '<tr id="supplement_text"><td>Supplemental Books </td><td><strong>$55.00</strong></td></tr>');
	$('body').data('supplement_amount', 55);
	$('#total_fees').before($('body').data('supplement'));
		
	if($('input[name=book]:checked').val() == 'yes') {
		$('body').data('book', '<tr id="book_text"><td>7th Edition Commercial Code Book </td><td><strong>$60.00</strong></td></tr>');
		$('body').data('book_amount', 60);
		$('#total_fees').before($('body').data('book'));
	} else {
		$('body').removeData('book');
		$('body').data('book_amount', 0);
		$('#book_text').remove();;
	}
	if($('input[name=book7]:checked').val() == 'yes') {
		$('body').data('book7', '<tr id="book7_text"><td>7th Edition Residential Code Book </td><td><strong>$45.00</strong></td></tr>');
		$('body').data('book7_amount', 45);
		$('#total_fees').before($('body').data('book7'));
	} else {
		$('body').removeData('book7');
		$('body').data('book7_amount', 0);
		$('#book7_text').remove();
	}
	/*
	// now required...
	if($('input[name=supplement]:checked').val() == 'yes') {
		$('body').data('supplement', '<tr id="supplement_text"><td>Supplemental Books </td><td><strong>$55.00</strong></td></tr>');
		$('body').data('supplement_amount', 55);
		$('#total_fees').before($('body').data('supplement'));
	} else {
		$('body').removeData('supplement');
		$('body').data('supplement_amount', 0);
		$('#supplement_text').remove();
	}
	*/
	$('#total_fees span').text(parseInt($('body').data('course-fee')) + $('body').data('book_amount') + $('body').data('book7_amount') + $('body').data('supplement_amount'));
	$('#amount').val($('#total_fees span').text());
	$('.course_fees span').text($('#total_fees span').text());
}

function tabbed_form() {
	var $tabs = $("#tabs").tabs({disabled: [1,2,3,4,5,6]});
	$("#tabs legend").css('display', 'none');
	$("#tabs button").click(function() {
		// Hack to get around IE LabelText vs value bug on buttons 
		// http://msdn.microsoft.com/en-us/library/ms535211%28VS.85%29.aspx
		// http://stackoverflow.com/questions/487056/retrieve-button-value-with-jquery
		var label = $(this).text(); 
		$(this).text('');
		var buttonValue = $(this).val();
		$(this).text(label);
		$tab = parseInt(buttonValue);
		// End Hack
		if(validate($tab)) {
			$tabs.tabs('enable', $tab);
			$tabs.tabs('select', $tab);
		}
		return false;
	});
}

function validate($tab) {
	$('form#signup').validate();
	switch ($tab) {
		case 1: 
			return true;
		break;
		case 2:
			var required = {
				required: true,
				messages: {
					required: 'required'
				}
			};
			$('input[name=book]').rules("add", required);
			$('input[name=book7]').rules("add", required);
			$('input[name=supplement]').rules("add", required);
			$('input[name=yrsexp]').rules("add", required);
			if(! $("form#signup").valid()) {
				return false;
			}
			return true;
		break;
		case 3:
			return true;
		break;
		case 4:
			var required = {
				required: true,
				messages: {
					required: ''
				}
			};
			var required_with_note = {
				required: true,
				messages: {
					required: 'required'
				}
			};
			var email_options = {
				required: function(element) {
					return isOnline();
				},
				messages: {
					required: ''
				}
			};
			var cc_options = {
				required: function(element) {
					return isCC();
				},
				messages: {
					required: ''
				}
			};
			var cc_options_with_note = {
				required: function(element) {
					return isCC();
				},
				messages: {
					required: 'required'
				}
			};
			
			$('input[name=last_name]').rules("add", required);
			$('input[name=first_name]').rules("add", required);
			$('input[name=home_phone]').rules("add", required);
			$('input[name=home_address]').rules("add", required);
			$('input[name=home_city]').rules("add", required);
			$('input[name=home_state]').rules("add", required);
			$('input[name=home_zip]').rules("add", required);
			$('input[name=readguarantee]').rules("add", required_with_note);
			
			$('input[name=email]').rules("add", email_options);
			$('input[name=cc_num]').rules("add", cc_options);
			$('#month').rules("add", cc_options_with_note);
			$('#year').rules("add", cc_options_with_note);

			if(! $("form#signup").valid()) {
				return false;
			}
			return true;
		break;
		case 5:
			return true;
		break;
		case 6:
			return true;
		break;
	}
}

function payment_bits() {
	$('.payment-details').hide();
	var payment_type = $('input.payment_method').val();
	$('.' + payment_type).show();
	$('input.payment_method').click(function() {
		$('.payment-details').hide();
		var payment_type = $(this).val();
		$('.' + payment_type).show();
	});
}

function isCC() {
	if(($('input.payment_method:checked').val() == 'credit_card_online')
		|| ($('input.payment_method:checked').val() == 'credit_card_print')
	) {
		return true;
	}
	return false;
}

function isOnline() {
	if($('input.payment_method:checked').val() == 'credit_card_online') {
		return true;
	}
	return false;
}