

$(document).ready(function() {
	var checked;
	var weekly;
	var bi;
	var semi; 
	var monthly;
	var toto;
	var irregular;
	var reissued;
	var paperCost;
	var ach;
	var netSavings;
	var employees;
	var paycheckCost;
	var weekSum, biSum, semiSum, monthlySum,employeeTotal;
	
	$("#paperCost").blur(function () {
		try {
		
			if (isNaN($(this).val())) {
				$("#mainError").html("There was an error in your entry. Please correct the highlighted fields below.");
				window.location.hash = 'mistake';
				$("#paperCost").addClass("red");
				$("#paperCost").focus();
				
			}
			else {
				$(this).val(Number($(this).val()).toFixed(2));
				$("#paperCost").removeClass("red");
				$("#mainError").html("&nbsp;");
			}
		} catch (e) {
			//
		}
	});	

  
	$("#submitt").click(function adder(){
		if(formErrors())
		{
		$("#mainError").html("There was an error in your entry. Please correct the highlighted fields below.");
		window.location.hash = 'mistake';
		}
		else
		{

		
		checked = Number(document.payroll.checked.value);
		weekly = Number(document.payroll.weekly.value/100);
		bi = Number(document.payroll.bi.value/100);
		semi = Number(document.payroll.semi.value/100);
		monthly = Number(document.payroll.monthly.value/100);
		employees = (Number(weekly) + Number(bi) + Number(semi) + Number(monthly));
		paperCost = Number(document.payroll.paperCost.value);
		irregular = Number(document.payroll.irregular.value);
		reissued = Number(document.payroll.reissued.value);
		ach = .35;
		netSavings = (paperCost - ach);


//this sets the values based on clients formula
		weekSum = parseInt(weekly * checked * 52 * paperCost); 
		biSum = parseInt(bi * checked * 2.15 * 12 * paperCost);
		semiSum = parseInt(semi * checked * 24 * paperCost);
		monthlySum = parseInt(monthly * checked * 12 * paperCost);
		irregSum = parseInt(irregular * 12 * paperCost);
	
//more client formula
		paycheckCost = parseInt(parseInt(weekSum) + parseInt(biSum) + parseInt(semiSum) + parseInt(monthlySum) +  parseInt(irregSum));
		$("#paycheckCost").html(paycheckCost.toFixed(2));
		reissuedSum = parseInt(reissued * 12 * 9);
		$("#reissueCost").html(reissuedSum.toFixed(2));

		totoCost = parseInt(parseInt(paycheckCost) + parseInt(reissuedSum));
		$("#totalCost").html(totoCost.toFixed(2));

		weekSav = parseInt(weekly * checked * 52 * netSavings); 
		biSav = parseInt(bi * checked * 2.15 * 12 * netSavings);
		semiSav = parseInt(semi * checked * 24 * netSavings);
		monthlySav = parseInt(monthly * checked * 12 * netSavings);
		irregSav = parseInt(irregular * 12 * netSavings);
		estSav = parseInt(parseInt(weekSav) + parseInt(biSav) + parseInt(semiSav) + parseInt(monthlySav) +  parseInt(irregSav) + parseInt(reissuedSum));

//write out the total
		$("#estSavings").html(estSav.toFixed(2));
//get rid of the error message
		$("#mainError").html("&nbsp");
//format the results
		$("#paycheckCost").formatCurrency({useHtml:true, symbol: ''});
		$("#reissueCost").formatCurrency({useHtml:true, symbol: ''});
		$("#totalCost").formatCurrency({useHtml:true, symbol: ''});
		$("#estSavings").formatCurrency({useHtml:true, symbol: ''});
		

	    window.location.hash = 'success';
		
		
}

//if the fields below are not filled in properly an error is thrown
		function formErrors(){
		var formErrors = false;
			if($("#checked").val() == "" || $("#checked").val() == "0")
				{
					$("#checkedText").addClass("red");	
formErrors = true;
				} 

		
			if($("#paperCost").val() == "")
				{
					$("#costText").addClass("red");	
formErrors = true;
				} 

			if($("#toto").html() != "100")
				{
					$("#frequencyText").addClass("red");	
formErrors = true;
				} 
			return formErrors;
		}
	
	});


//removes red highlighting after an error
		$("#checked").focus(function(){

		$("#checkedText").removeClass("red");	
			
});	

		$("#weekly").focus(function(){

		$("#frequencyText").removeClass("red");	
			
});

		$("#bi").focus(function(){

		$("#frequencyText").removeClass("red");	
			
});
		
		$("#semi").focus(function(){

		$("#frequencyText").removeClass("red");	
			
});
		
		$("#monthly").focus(function(){

		$("#frequencyText").removeClass("red");	
			
});

		$("#paperCost").focus(function(){

		$("#costText").removeClass("red");	
			

});


//adds up the total field as user clicks through the frequency of pay fields
		$("input").change(function() {
		
			totWeekly = Number(document.payroll.weekly.value);
			totBi = Number(document.payroll.bi.value);
			totSemi = Number(document.payroll.semi.value);
			totMonthly = Number(document.payroll.monthly.value);
			$("#toto").html(Number(totWeekly) + Number(totBi) + Number(totSemi) + Number(totMonthly) );
		
			
		});

//clears the fields
		$("#kleer").click(function() {
		$('input').val("");
		$(".result").html("");
		$("#toto").html("&nbsp;");
		$("#paperCost").val("2.00");
		$("#checkedText").removeClass("red");
		$("#frequencyText").removeClass("red");	
		$("#costText").removeClass("red");	
		
		$("#mainError").html("&nbsp");

		});



});