	var filingStatus ; 
	var stdDeduct ;


function calc1040() {
	DefineVariable();
	CalcIncome();
	CalcDeductions();	
	CalcTax();
	CalcCreditPayment();
	
	$('input.sliderinput, input.roundValue').each(function() {  
        $(this).val(Math.round(Number($(this).val())));  
    });
	
}



function DefineVariable() {

	//  Determine Filing Status and Set Default Values for each, Standard Deduction, etc...

	

	
	filingStatus = $("input[name='filingStatus']:checked").val() ; 
		
	switch (filingStatus){
		case "Single": 
			stdDeduct = 5700	
		$("#FinalStatus").val("Single") ;
		break;
		case "MarriedJoint": 
			stdDeduct = 11400		
		$("#FinalStatus").val("Married Filing Jointly") ;
		break;
		case "MarriedSep": 
			stdDeduct = 5700
			$("#FinalStatus").val("Married Filing Seperately") ;
		break;
		case "HeadHouse": 
			stdDeduct = 8350	
			$("#FinalStatus").val("Head of Household") ;
		break;
		case "Widower": 
			stdDeduct = 11400
			$("#FinalStatus").val("Qualifying Widower") ;
		break;			
	default : stdDeduct = 0 ;
	
	
}
		
	
		
	$("#StdDeductionReview").val(stdDeduct) ; // standard deduction value based on filing status

	// End Filing Status

		
	//Total Exemptions
	var AllExempt = 0;
	if ($('#ClaimTaxpayer').is(':checked'))
		{ 
			AllExempt = AllExempt + 1
		}
	if ($('#ClaimSpouse').is(':checked'))
		{ 
			AllExempt = AllExempt + 1
		}	
	
	AllExempt = AllExempt + Number($("#ClaimOther").val()) ;
	
	$("#TotalExemptions").val(AllExempt)
	
	var exemptAmount = 3650 
	
	
	//calculate exemption total
	$("#testExempt").val(Number($("#TotalExemptions").val()) * exemptAmount)
	//End Total Exemptions
	
}


//total of income tab
function CalcIncome() { 
//value on review tab
		$("#IncomeReview").val(
			(
				Number($("#Wages").val()) + 
				Number($("#CapitalGains").val()) + 
				Number($("#OtherIncome").val())  + 
				Number($("#Income3").val())  + 
				Number($("#BusRealFarm").val())  + 
				Number($("#Income4").val()) 
			) 
		);
		//Value on right column		
		$("#TotalIncome").val($("#IncomeReview").val());

}
//end Income Calculation


//total of deduction tab
function CalcDeductions() { 

//value on review page
$("#TotalDeductionsReview").val(   
			(
				Number($("#Deductions").val()) + 
				Number($("#Deductions2").val()) + 
				Number($("#Deductions3").val()) + 
				Number($("#Deductions8").val()) + 
				Number($("#Deductions9").val()) + 
				Number($("#Deductions11").val()) + 
				Number($("#Deductions12").val()) + 
				Number($("#Deductions13").val()) + 
				Number($("#Deductions14").val()) + 
				Number($("#Deductions15").val()) + 
				Number($("#Deductions16").val()) + 
				Number($("#Deductions17").val())
				
				/* these deductions are figured in towards itemized deduction total
				Number($("#Deductions4").val()) +  //Taxes you Paid
				Number($("#Deductions5").val()) +   //Interest you Paid
				Number($("#Deductions6").val()) +   //Gifts to Charity
				Number($("#Deductions7").val()) +  //Other Miscellaneous Deductions
				Number($("#Deductions10").val()) +  //Medical and Dental
				Number($("#Deductions18").val())  // Job Expenses and Miscellaneous Work Deductions
				*/
			) 
);

//value in right column of calc
$("#TotalDeductions").val(Number($("#TotalDeductionsReview").val())) ;

$("#AGIReview").val(
		$("#IncomeReview").val() - 
		Number($("#TotalDeductionsReview").val())
	);


// calc itemized deduction total

//medical-dental total

medLine1 = Number($("#Deductions10").val()) ; // Medical and Dental Expenses
medLine2 = Number($("#AGIReview").val()) ;
medLine3 = medLine2 * .075 ;
medLine4 = medLine1 - medLine3 ;

if(medLine4 < 0) {
MedTotal = 0;	
}else{
	MedTotal = medLine4;
}


//Job Expense calculation
jobLine1 = Number($("#Deductions18").val());
jobLine2 = Number($("#AGIReview").val()) ;
jobLine3 = jobLine2 * .02 ;
jobLine4 = jobLine1 - jobLine3 ;

if(jobLine4 < 0) {
JobTotal = 0;	
}else{
	JobTotal = jobLine4;
}


$("#ItmDeductionReview").val(
	Number($("#REDeductions4").val()) +  //Real Estate Taxes you Paid
	Number($("#Deductions4").val()) +  //Other Taxes you Paid
	Number($("#Deductions5").val()) +   //Interest you Paid
	Number($("#Deductions6").val()) +   //Gifts to Charity
	Number($("#Deductions7").val()) +  //Other Miscellaneous Deductions	 
    MedTotal + //Medical and Dental Total
	JobTotal  // Job Expenses and Miscellaneous Work Deductions				 
) ;


if(filingStatus == "MarriedJoint") {


if($("#REDeductions4").val() < 1000 ) {
$("#StdDeductionReview").val(Number($("#StdDeductionReview").val()) + Number($("#REDeductions4").val()));
}else{
$("#StdDeductionReview").val(Number($("#StdDeductionReview").val()) + 1000);
}



}else{

if($("#REDeductions4").val() < 500 ) {
$("#StdDeductionReview").val(Number($("#StdDeductionReview").val()) + Number($("#REDeductions4").val()));
}else{
$("#StdDeductionReview").val(Number($("#StdDeductionReview").val()) + 500);
}


}


if(Number($("#ItmDeductionReview").val()) > Number($("#StdDeductionReview").val())) {
	DeductToUse = Number($("#ItmDeductionReview").val()) ;
}else{
DeductToUse = Number($("#StdDeductionReview").val()) ;	
}



}
//end Deductions Calculation





//Calculate Tax Owed by TaxPayer
function CalcTax() {

$("#TaxableReview").val(
			$("#AGIReview").val() - 
			DeductToUse - 
			Number($("#testExempt").val())
		);
		
$("#TotalTaxableIncome").val($("#TaxableReview").val());		

	switch (filingStatus){
		case "Single": 
		Single(Number($("#TaxableReview").val()));
		
		break;
		case "MarriedJoint": 
		MarriedJoint(Number($("#TaxableReview").val()));
		
		break;
		case "MarriedSep": 
		Separate(Number($("#TaxableReview").val()));
	
		
		break;
		case "HeadHouse": 
		Head(Number($("#TaxableReview").val()));
		
		break;
		case "Widower": 
		MarriedJoint(Number($("#TaxableReview").val()));
		
		break;			
	default : stdDeduct = 0 ;
	
	}
}	
//end Tax Owed Calculation	
	
//Calculate Credits and Payments	
function CalcCreditPayment() {
	
$("#TotalOtherTaxes").val(
	(
		Number($("#OtherTaxes1").val()) + 
		Number($("#OtherTaxes3").val()) + 
		Number($("#OtherTaxes4").val())
	) 
);
	
	$("#TotalCredits").val(
			(
				Number($("#Credits0").val()) +
				Number($("#Credits1").val()) + 
				Number($("#Credits2").val()) + 
				Number($("#Credits3").val()) + 
				Number($("#Credits4").val()) + 
				Number($("#Credits5").val()) 
			) 
);
	
	
	
//Making Work Pay  and Governement Retiree Tax Credit
if((filingStatus == "MarriedJoint") && ($("#Wages").val()>12903))
    {
        schedM1a = 0;
        schedM1b = 0;
        schedM2 = 0;
        schedM3 = 0;
        schedM4 = 800;
    }else if($("#Wages").val()>6451) {
        schedM1a = 0;
        schedM1b = 0;
        schedM2 = 0;
        schedM3 = 0;
        schedM4 = 400;
    }else{
        schedM1a = 0;
        schedM1b = 0;
        schedM2 = 0;
        schedM3 = 0;
        schedM4 = 0;
    }


MakingWorkPayTotal4 = schedM4 ;
MakingWorkPayTotal5= Number($("#AGIReview").val());

if(filingStatus == "MarriedJoint"){
    MakingWorkPayTotal6 = 150000;  
}else{
    MakingWorkPayTotal6 = 75000;
}


if(Number(MakingWorkPayTotal5) > Number(MakingWorkPayTotal6)) {

    MakingWorkPayTotal7 = Number(MakingWorkPayTotal5) - Number(MakingWorkPayTotal6);
    
    MakingWorkPayTotal8 = Number(MakingWorkPayTotal7 * .02);
    
    if(Number(MakingWorkPayTotal4) - Number(MakingWorkPayTotal8) <= 0){
        MakingWorkPayTotal9 = 0 ;
    }else{
        MakingWorkPayTotal9 = (Number(MakingWorkPayTotal8) - Number(MakingWorkPayTotal4));    
    }
    
}else{
    MakingWorkPayTotal7 = 0 ;
    MakingWorkPayTotal8 = 0 ;
    
    MakingWorkPayTotal9 = MakingWorkPayTotal4 ;
}

$("#MakingWorkPayTotal12").val(
    Number($("#MakingWorkPayTotal10").val()) +
    Number($("#MakingWorkPayTotal11").val())    
    );


if((Number(MakingWorkPayTotal9) - Number($("#MakingWorkPayTotal12").val())) <= 0) {
$("#MakingWorkPayTotal13").val(Number(0));
}else{
$("#MakingWorkPayTotal13").val(
Number(MakingWorkPayTotal9) - 
Number($("#MakingWorkPayTotal12").val()));
}



$("#MakingWorkPayTotal14").val(
    Number($("#MakingWorkPayTotal11").val()) +
    Number($("#MakingWorkPayTotal13").val())    
    );

makingworkpaytotaltocarryover = Number($("#MakingWorkPayTotal14").val());



//Making Work Pay  and Governement Retiree Tax Credit


	
	if( Number($("#PaymentsFTHB").val()) > 80000 ) {
		     FTHB_Value_line1 = 8000 ;
		   }else{
		     FTHB_Value_line1 = Number($("#PaymentsFTHB").val()) * .1 ;
		   }
		   
		   $("#line1").val(FTHB_Value_line1) ;
		   
		   FTHB_Value_line2 = Number($("#testAGI").val()) ;
		  
		   $("#line2").val(FTHB_Value_line2) ;
		   
		   FTHB_Status = $("input[name='filingStatus']:checked").val()
		    $("#line3status").val(FTHB_Status) ;
			
		   if(FTHB_Status == "MarriedJoint") {
			  //alert(FTHB_Status);
			   if((Number(FTHB_Value_line2) > 150000)) {
				   FTHB_Value_line3 = Number(FTHB_Value_line2) - 150000;
				   $("#line3").val(FTHB_Value_line3) ;
				   FTHB_SUB_FUNCTIONa();
			   }else{
				   FTHB_Value = Number(FTHB_Value_line1) ;
			   }
		   }else{
			   if((Number(FTHB_Value_line2) > 75000)) {
				   FTHB_Value_line3 = Number(FTHB_Value_line2) - 75000;
				   $("#line3").val(FTHB_Value_line3) ;
				   FTHB_SUB_FUNCTIONa();
			   }else{
				   FTHB_Value = Number(FTHB_Value_line1) ;
			   }
		   }
		   
	function FTHB_SUB_FUNCTIONa() {
		   FTHB_Value_line4Work = Number(FTHB_Value_line3)/20000 ;
		   
		   FTHB_Value_line4 = Math.round(Number(FTHB_Value_line4Work)*1000)/1000
		   $("#line4").val(FTHB_Value_line4) ;
		   if(Number(FTHB_Value_line4) > 1)
		   {
			FTHB_Value_line4 = 1.000 ;   
		   }
		   
		   FTHB_Value_line5 = Number(FTHB_Value_line1) * Number(FTHB_Value_line4) ;
		    $("#line5").val(FTHB_Value_line5) ;
		   FTHB_Value_line6 = Number(FTHB_Value_line1) - Number(FTHB_Value_line5) ; 	
		   $("#line6").val(FTHB_Value_line6) ;
		   FTHB_Value = Number(FTHB_Value_line6) ;		
		}
		
		
		
			$("#TotalPayments").val(
				(
				Number($("#Payments1").val()) + 
				Number($("#Payments2").val()) + 
				Number($("#Payments3").val()) + 
				Number($("#Payments4").val()) + 
				Number($("#Payments5").val()) + 
				FTHB_Value +
				makingworkpaytotaltocarryover
				)
);
		
	function roundVal(val){
	var dec = 0;
	var result = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
	}

	
	
	var finalValue = roundVal((Number($("#TaxOwed").val()) + Number($("#TotalOtherTaxes").val())) - Number($("#TotalPayments").val()) - Number($("#TotalCredits").val()));
	
	if (finalValue > 0) { //amt due
		$("#testFinalValueConstant").val( finalValue )
		$("#constantRefund").css("display","none");
		$("#constantAmountDue").css("display","block");
	}else{ // refund
		$("#testFinalValueConstant").val( Math.abs(finalValue) ) 		
		$("#constantRefund").css("display","block");
		$("#constantAmountDue").css("display","none");
	}
}
//End of Credit and Payments



































// tax tables taken from http://www.irs.gov/pub/irs-pdf/i1040tt.pdf



function Single(val) {
			
			if(val<8350)
			{ 
			   Calc44(0, 0.1, val, 0);
			 } else if (val<33950) 
			 {
				 base = 835;
				 rate = 0.15;
				 surplus = 8350;				 
				 Calc44(base, rate, val, surplus);
			 } else if (val<82250)
			 {
				 base = 4675;
				 rate = 0.25;
				 surplus = 33950;				 
				 Calc44(base, rate, val, surplus);
				 
			 } else if (val<171550)
			 {
				 base = 16750;
				 rate = 0.28;
				 surplus = 82250;				 
				 Calc44(base, rate, val, surplus);
				 
			 }else if (val<372950)
			 {
				 base = 41754;
				 rate = 0.33;
				 surplus = 171550;				 
				 Calc44(base, rate, val, surplus);
				 
			 }else 
			 {
				 base = 108216;
				 rate = 0.35;
				 surplus = 372950;				 
				 Calc44(base, rate, val, surplus);
				 
			 }
			 
		}
	// end of single	

//joint and widow begins
	 function MarriedJoint(val)	 {
		 
		if(val<16700)
			{  Calc44(0, 0.1, val, 0);
			 } else if (val<67900) 
			 {
				base = 1670;
				 rate = 0.15;
				 surplus = 16700;				 
				 Calc44(base, rate, val, surplus); 
				 
			 } else if (val<137050)
			 {
				 base = 9350;
				 rate = 0.25;
				 surplus = 67900;				 
				 Calc44(base, rate, val, surplus);
			 } else if (val<208850)
			 {
				 base = 26637.5;
				 rate = 0.28;
				 surplus = 137050;				 
				 Calc44(base, rate, val, surplus);
			 }else if (val<372950)
			 {
				 base = 46741;
				 rate = 0.33;
				 surplus = 208850;				 
				 Calc44(base, rate, val, surplus);
			 }else 
			 {
				 base = 100897.5;
				 rate = 0.35;
				 surplus = 372950;				 
				 Calc44(base, rate, val, surplus);
			 } 
	 }
	//end of joint/widow	
function Separate(val) // for married filing seperately
	 {
		if(val<8350)
			{  Calc44(0, 0.1, val, 0);
			 } else if (val<33950) 
			 {
				  base = 835;
				 rate = 0.15;
				 surplus = 8350;				 
				 Calc44(base, rate, val, surplus);
				 
			 } else if (val<68525)
			 {
				 base = 4675;
				 rate = 0.25;
				 surplus = 33950;				 
				 Calc44(base, rate, val, surplus);
			 } else if (val<104425)
			 {
				 base = 13318.75;
				 rate = 0.28;
				 surplus = 68525;				 
				 Calc44(base, rate, val, surplus);
				 
			 }else if (val<186475)
			 {
				 base = 23370.75;
				 rate = 0.33;
				 surplus = 104425;				 
				 Calc44(base, rate, val, surplus);
			 }else 
			 {
				 base = 50447.25;
				 rate = 0.35;
				 surplus = 186475;				 
				 Calc44(base, rate, val, surplus);
				 
			 } 
	 }
	//end of separate	
	
function Head(val) {
		if(val<11950)
			{  Calc44(0, 0.1, val, 0);
			 } else if (val<45500) 
			 {
				 base = 1195;
				 rate = 0.15;
				 surplus = 11950;				 
				 Calc44(base, rate, val, surplus);
			 } else if (val<117450)
			 {
				 base = 6227.50;
				 rate = 0.25;
				 surplus = 45500;				 
				 Calc44(base, rate, val, surplus);
				 
			 } else if (val<190200)
			 {
				 base = 24215;
				 rate = 0.28;
				 surplus = 117450;				 
				 Calc44(base, rate, val, surplus);
				 
			 }else if (val<372950)
			 {
				base = 44585;
				 rate = 0.33;
				 surplus = 190200;				 
				 Calc44(base, rate, val, surplus);
			 }else 
			 {
				 base = 104892.5;
				 rate = 0.35;
				 surplus = 372950;				 
				 Calc44(base, rate, val, surplus);
				 
			 } 
	 }
	//end of head
	
	
	
function Calc44(base, rate, val, surplus) {
	$("#TaxOwed").val(Number( base + rate*(Number(val)-surplus)))
	$("#RateReview").val(Math.round(rate*100) + "%")
}



