var savings_calculator = function() {
	
	return {
	
		init: function() {
			$('#savings-calculator').ajaxForm();
			$("#event_code").change(function() {
				savings_calculator.update_move_prefilled();
			});
			savings_calculator.update_move_prefilled();
			$("input.txt").blur(function() { savings_calculator.add_alt_value(this); });
			$("input.txt").focus(function() { savings_calculator.blank_alt_value(this); });
		},
		
		blank_alt_value: function(elem) {	
			if ($(elem).val() == $(elem).attr('alt')) {
				$(elem).val("");
			}
		},
		
		add_alt_value: function(elem) {
			if ($(elem).val() == "") {
				$(elem).val($(elem).attr('alt'));
			}
		},
		
		update_move_prefilled: function() {
			sc = savings_calculator;
			sc.event_arr = $('#event_code').val().split("|");
			if (sc.event_arr != "") {
				$('#nights').val(parseInt(sc.event_arr[2]));
				$('#meal_cost').val("included");
				$('#lodging_cost').val("included");
				$('#event_cost_per_student').val(sc.price_format(parseInt(sc.event_arr[0])));
			}
		},
		
		// requires the priceFormat jquery plugin and a hidden input with the id 
		price_format: function(num) {
		
			num = num.toString().replace(/\$|\,/g,'');
			if(isNaN(num)) num = "0";
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num*100+0.50000000001);
			cents = num%100;
			num = Math.floor(num/100).toString();
			if(cents<10) cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
				num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			}
			return (((sign)?'':'-') + '$' + num + '.' + cents);
	        
		},
		
		validate: function() {
			sc = savings_calculator;
			sc.error_arr = new Array();
			if ($('#event_code').val() === "") {
				sc.error_arr.push("Please select the event you plan to attend.");
			}
			if (isNaN(parseInt($('#previous_students').val())) == true) {
				sc.error_arr.push("Please enter a valid number of students you brought the last year you attended.");
			}
			if (isNaN(parseInt($('#total_students').val())) == true) {
				sc.error_arr.push("Please enter a valid number of students you will bring this year.");
			}
			
		},
		
		display_errors: function() {
			
			error  = '<div id="form-error">'+"\n";
			error += "\t<ul>\n";
			for (i=0; i < savings_calculator.error_arr.length; i++) {
				error += "\t\t<li>"+ savings_calculator.error_arr[i] +"</li>\n";
			}
			error += "\t</ul>\n";
			
			$("#savings-calculator").prepend(error);
			
		},
		
		set_vars: function() {
			
			sc = savings_calculator;
			sc.previous_students = parseInt($('#previous_students').val());
			sc.total_students    = parseInt($('#total_students').val());
			sc.event_arr         = $('#event_code').val().split("|");
			sc.early_bird_price  = parseInt(sc.event_arr[0]);
			sc.late_price        = parseInt(sc.event_arr[1]);
			sc.nights            = parseInt(sc.event_arr[2]);
			
			if ($('#attended_last_three_years_no').attr('checked') == true) {
				sc.new_students      = sc.total_students;
				sc.previous_students = 0;
			} else {
				sc.new_students      = sc.total_students - sc.previous_students;
			}
					
			// set MOVE event totals
			sc.total_cost       = ((sc.previous_students*sc.early_bird_price)+(sc.new_students*199));
			sc.cost_per_night   = (sc.total_cost/sc.nights);
			sc.cost_per_student = (sc.total_cost/sc.total_students);
			sc.summer_savings   = ((sc.total_students*sc.early_bird_price)-sc.total_cost);
			
			if (sc.summer_savings < 0) {
				sc.summer_savings = 0;
			}
			
			// figure other event totals
			sc.other_event_cost_per_student = parseInt($('#other_event_cost_per_student').val());
			sc.other_nights                 = parseInt($('#other_nights').val());
			sc.other_meal_cost              = parseInt($('#other_meal_cost').val());
			sc.other_lodging_cost           = parseInt($('#other_lodging_cost').val());
			sc.other_total_cost             = ((((sc.other_meal_cost+sc.other_lodging_cost)*sc.nights)+sc.other_event_cost_per_student)*sc.total_students);
			sc.other_cost_per_night         = (sc.other_total_cost/sc.other_nights);
			sc.other_cost_per_student       = (sc.other_total_cost/sc.total_students);
			sc.comparison_savings           = (sc.other_total_cost-sc.total_cost);
			
			// format fields
			var fields_arr = new Array('total_cost', 'cost_per_night', 'cost_per_student', 'other_total_cost', 'other_cost_per_night', 'other_cost_per_student');
			for (i = 0; i < fields_arr.length; i++) {
				$('#'+fields_arr[i]).val(sc.price_format(eval('sc.'+fields_arr[i])));
			}
			
			$('#event').val(sc.event_arr[3]);
			$('#summer_savings').val(sc.price_format(sc.summer_savings));
			$('#summer_savings_td').html($('#summer_savings').val());
			if (sc.other_event_cost_per_student == 0 && sc.other_nights == 0) {
				$('#comparison_savings_row').hide();
			} else {
				$('#comparison_savings_row').show();
				$('#comparison_savings').val(sc.price_format(sc.comparison_savings));
				$('#comparison_savings_td').html($('#comparison_savings').val());
			}
			
			// Submit the results to the database via ajax
			$('#savings-calculator').submit();
					
		},
		
		calculate: function() {
			
			if ($("#form-error").length !== 0) {
				$("#form-error").remove();
			}
			
			savings_calculator.validate();
			
			if (savings_calculator.error_arr.length !== 0) {
				savings_calculator.display_errors();
			} else {
				savings_calculator.set_vars();
				if ($("#totals").css('display') == 'none') {
					$("#calculate_btn").val("Re-calculate My Savings!");
					$("#totals").show("normal");	
				}
			}			
		}
		
	}
	
}();

var faqs = function() {
	return {
		
		toggle_answer: function(elem) {
			if ($(elem).hasClass('hid')) {
				$(elem).toggleClass('hid');
				$(elem).parent().next(".answer").css('display', 'block');
			} else {
				$(elem).toggleClass('hid');
				$(elem).parent().next(".answer").css('display', 'none');
			}
		}
		
	}
}();

$(document).ready(function() {
	savings_calculator.init();
	$("div.details .question a").each(function() {
		$(this).toggleClass('hid');
		$(this).click(function(){ faqs.toggle_answer(this); });
	});
});