// JavaScript Document
var default_employee_salary = 45565;
var tax_credit = 1000;
var employers_ss_tax_rate = 0.062;
var total_savings;
var todays_date = new Date();
var employee_salary = default_employee_salary;
 var months_till_year_end = (12 - todays_date.getMonth());



$(function() {
	$('input.valid-number')
			.bind(
					'keypress',
					function(e) {
						return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) ? false
								: true;
					});
	$('select#month-hired').change(
								 function()
								 {
									 calcSavings();
	 							});
	$('input#employee-salary').focus(
									 function()
									 {
										 $(this).val(unFormatCurrency($(this).val()));
									 });
	$('input#employee-salary').blur(
									function()
									{
										$(this).val(formatCurrency($(this).val()));
										calcSavings();
									});
	
	$('input#employee-salary').keyup(
									 function()
									 {
										 calcSavings();
									 });
	$('input#apply-retention-bonus').click(
											function()
											{
												if($(this).is(':checked'))
												{
													$('div.form-ele').filter( function(index)
													{
														return $('input#new-hire-credit',this).length == 1;
													}).show();
												}
												else
												{
													$('div.form-ele').filter( function(index)
													{
														return $('input#new-hire-credit',this).length == 1;
													}).hide();
												}
												calcSavings();
											});
	setDefaults();

	function setDefaults() {
		$('input#employee-salary').val(formatCurrency(employee_salary));
		$('select#month-hired option').filter(function(index)
		{
			return $(this).val() == months_till_year_end; 
		}).attr('selected','selected');
		$('input#new-hire-credit').val(formatCurrency(tax_credit));
		calcSavings();
		
	}

	function calcSavings() {
		employee_salary = unFormatCurrency($('input#employee-salary').val());
		tax_credit = ($('input#apply-retention-bonus').is(':checked')) ? 1000: 0;
		var months_remaining = ($('select#month-hired option:selected').val() < 10) ? $('select#month-hired option:selected').val() : 9.5;
		var employer_ss_tax_savings = (employee_salary /12) * months_remaining * employers_ss_tax_rate;
		employer_ss_tax_savings  = (employer_ss_tax_savings  <=6621.60) ? employer_ss_tax_savings : 6621.60;
		employer_total_savings = employer_ss_tax_savings + tax_credit;
		$('input#ss-tax-savings').val(formatCurrency(employer_ss_tax_savings));
		$('input#total-savings').val(formatCurrency(employer_total_savings));
	}
});
