// Filing Status

var YEAR  	                      = 2007;
var SINGLE_FILER	  		      = 0;
var MARRIED_JOINT_FILER	  	      = 1;
var MARRIED_SEPARATE_FILER	 	  = 2;
var HEAD_HOUSE			          = 3;
var QUAL_WIDOW			          = 4;
var MAX_STATUS		       	      = 5;
var NUM_TAXRATES			      = 6;
var CURRENT_YEAR	              = 0;
var PP_BIWEEKLY 		      	  = 0;
var Tax_DedMedicalPct	      	  = 0.075;
var ED_CREDIT_THRESHOLD_MFJ       = 114000; 
var ED_CREDIT_THRESHOLD_NON_MFJ   = 57000;
var AMT_EXEMP                     = 6300;
var AMTI_MFS                      = 29000; // unchanged in 2007?
var AMTI_LIMIT_MFS                = 191000; // unchanged in 2007?
var RefCTC_Threshold              = 11750;
var RefCTC_Threshold_Wages        = 97500;	// same as FICA max?
var	CHILD_TAX_CREDIT              = 1000;
var	ExtraAmtMFJ                   = 1050;
var	ExtraAmtSingle                = 1300; 
var	MinDepStd                     = 850;  // unchanged in 2007
var	PersonalExemption             = 3400;
var ExcessInvInc                  = 2900;
var EIC_MAX_INCOME 		= 39783;

// ***************************************************************************
//
// Class: TaxRateYearConst
//
// ***************************************************************************

function TaxRateYearConst( ) 
{

		this.NumBrackets = NUM_TAXRATES;

		this.TaxRate = new Array( 10.00, 15.00, 25.00, 28.00, 33.00, 35.00 );

		this.IncomeLimit = new Array(
		0, 7825.00, 	31850.00,  77100.00, 160850.00, 349700.00,
		0, 15650.00,	63700.00, 128500.00, 195850.00, 349700.00,
		0, 7825.00, 	31850.00,  64250.00,  97925.00, 174850.00,
		0, 11200.00,	42650.00,  110100.00, 178350.00, 349700.00,
		0, 15650.00,	63700.00, 128500.00, 195850.00, 349700.00)	


		//standard deductions
		this.Deduction = new Array(
			5350.00,
			10700.00,
			5350.00,
			7850.00,
			10700.00 );


		this.AMTExemption = new Array(
    						44350.00,
    						66250.00,
    						33125.00,
    						44350.00,
    						66250.00 );   





		// obsolete
		this.BracketWH = new Array(
            2650, 9700, 30800, 68500, 148700, 321200,
            8000, 22300, 64750, 118050, 185550, 326100 );
        
		// obsolete
        this.WHAmt = new Array(
            0, 705, 3870, 13295, 35751, 92676,
            0, 1430, 7797.5, 21122.50, 40022.50, 86404 );

		// obsolete
		this.TaxRateOld = new Array( 10.00, 15.00, 27.00, 30.00, 35.00, 38.60 );

		// obsolete
		this.IncomeLimitOld = new Array(
			0, 6000.00, 	28400.00,  68800.00, 143500.00, 311950.00,
			0, 12000.00,	47450.00, 114650.00, 174700.00, 311950.00,
			0, 6000.00, 	23725.00,  57325.00,  87350.00, 155975.00,
			0, 10000.00,	38050.00,  98250.00, 159100.00, 311950.00,
			0, 12000.00,	47450.00, 114650.00, 174700.00, 311950.00 );

		// obsolete
		this.AMTExemptionOld = new Array(
			 35750.00,
			 49000.00,
			 24500.00,
			 35750.00,
			 49000.00 );

		// obsolete
		this.DeductionOld = new Array(
			4750.00,
			7950.00,
			3975.00,
			7000.00,
			7950.00 );



} // TaxRateYearConst

// ***************************************************************************
//
// Class: TaxRates
//
// ***************************************************************************
function TaxRates()
{
		//
		// Public Methods
		//
			this.SetTaxRatesForYear	= SetTaxRatesForYear;

		//
		// Public Members
		//
			this.Tax_NumTaxRates;
			this.Tax_TaxRates		= new Array( NUM_TAXRATES );	 // taxRate
			this.Tax_DeductionStandard = new Array( MAX_STATUS );	 // deduction
			this.Tax_DeductionStandardOld = new Array( MAX_STATUS );

			this.IDPOThreshold		= new Array( MAX_STATUS );

			this.iIDPOThreshold		= new Array(
				156400.00,	// SINGLE
				156400.00,	// MARRIED_JOINT
				 78200.00,	// MARRIED_SEPARATE_FILER
				156400.00,	// HEAD_HOUSE
				156400.00 );	// QUAL_WIDOW


			this.PEPOThreshold		= new Array( MAX_STATUS );

			this.iPEPOThreshold		= new Array(
				156400.00,	// SINGLE
				234600.00,	// MARRIED_JOINT
				117300.00,	// MARRIED_SEPARATE_FILER
				195500.00,	// HEAD_HOUSE
				234600.00); // QUAL_WIDOW


			this.AMTExemptionPO		= new Array( MAX_STATUS );
			this.iAMTExemptionPO	= new Array(
				112500.00,	// SINGLE
				150000.00,	// MARRIED_JOINT
				 75000.00,	// MARRIED_SEPARATE_FILER
				112500.00,	// HEAD_HOUSE
				150000.00); // QUAL_WIDOW


			this.rThisYear			= new TaxRateYearConst( 2004 ); 

} // TaxRates


function SetTaxRatesForYear( TaxYear)
{

		var i, j;
		var rPtr;

		if( TaxYear == CURRENT_YEAR )
		{
			rPtr = this.rThisYear;
		}

		this.Tax_NumTaxRates  = rPtr.NumBrackets;


		for( i=0;i<MAX_STATUS;i++ )
		{
			var taxAmount;

			this.Tax_DeductionStandard[i] = rPtr.Deduction[i]; 

			this.Tax_DeductionStandardOld[i] = rPtr.DeductionOld[i];

			if( TaxYear == CURRENT_YEAR )
			{
			  this.IDPOThreshold[i] = this.iIDPOThreshold[i];
			}


			this.AMTExemptionPO[i] = this.iAMTExemptionPO[i];

		}

} // SetTaxRatesForYear






// ***************************************************************************
//  function: Calc_SelfAndSpouse (class: FormBasic)
//
//	1, if "You" are not a dependent, +
//	1, if married filing joint and the spouse is not a dependent
//
// ***************************************************************************
function Calc_SelfAndSpouse( FilingStatus, Deduct_DedOther, Deduct_SPDedOther )
{
		var NumCount = 0;

		if( Deduct_DedOther == 0  ) {
			NumCount = 1;
		}

		if( FilingStatus == MARRIED_JOINT_FILER  && Deduct_SPDedOther == 0 ) {
			NumCount += 1;
		}

		this.Exempt_SelfAndSpouse = NumCount;
		return( this.Exempt_SelfAndSpouse );

} // Calc_SelfAndSpouse


// ***************************************************************************
// function: Calc_TotChildDependents (class: FormBasic)
//
//                        TotChildDependents Calculation
// ***************************************************************************
function Calc_TotChildDependents()
{
		var TotDepChild = 0;
		TotDepChild += FormatDollarAmont(this.Exempt_Dependents);
		TotDepChild += FormatDollarAmont(this.Exempt_Under17);
		TotDepChild += FormatDollarAmont(this.Exempt_Under14to16);
		TotDepChild += FormatDollarAmont(this.Exempt_Student19to24);
		TotDepChild += FormatDollarAmont(this.Exempt_DisabledChild);
		TotDepChild += FormatDollarAmont(this.Other_Dependents);

		return ( TotDepChild );

} //Calc_TotChildDependents


// ***************************************************************************
// function: Calc_TotExemptions (class: FormBasic)
//
//                            Personal Exemption  Calculation
// ***************************************************************************

function Calc_TotExemptions( FilingStatus, Deduct_DedOther, Deduct_SPDedOther, AGI )
{
            var DisPerExemptions;
            var status = Estimator.FilingStatus;
            var dropRemalue = 0;
            var diff = 0;
            var PEPOThreshold = this.TaxRates.iPEPOThreshold[status];

            this.Exempt_TotExemptions = FormatDollarAmont(this.Calc_SelfAndSpouse( FilingStatus, Deduct_DedOther, Deduct_SPDedOther ));
            this.Exempt_TotExemptions += FormatDollarAmont(this.Exempt_Dependents);
            this.Exempt_TotExemptions += FormatDollarAmont(this.Exempt_Under17);
            this.Exempt_TotExemptions += FormatDollarAmont(this.Exempt_Under14to16);
            this.Exempt_TotExemptions += FormatDollarAmont(this.Exempt_Student19to24);
            this.Exempt_TotExemptions += FormatDollarAmont(this.Exempt_DisabledChild);
            this.Exempt_TotExemptions += FormatDollarAmont(this.Other_Dependents);
            this.Exempt_TotExemptions *= PersonalExemption;


            if ( AGI > PEPOThreshold )
            {

                   if ( status == MARRIED_SEPARATE_FILER )
                   {
                         if (AGI - PEPOThreshold > 61250 ) { // unchanged for 2007
                             this.Exempt_TotExemptions *= 1/3;
                         }
                         else {
                             // Drop Remainder Calculations
                             diff =  (( AGI - PEPOThreshold ) / 1250 );
                             dropRemalue =  Math.round( diff );

                             if ( diff > dropRemalue ) {
                               dropRemalue += 1;
                             }
                             DisPerExemptions = 0.02 *  ( FormatDollarAmont(dropRemalue) ) * this.Exempt_TotExemptions;
                             DisPerExemptions *= 2/3; 
                             this.Exempt_TotExemptions -= DisPerExemptions;
                         }
                   }else
                   {
                         if (AGI - PEPOThreshold > 122500  ) {
                            this.Exempt_TotExemptions *=1/3;
                         }
                         else {
                            // Drop Remainder Calculations
                             diff =  (( AGI - PEPOThreshold) / 2500 );
                             dropRemalue =  Math.round( diff );

                             if ( diff > dropRemalue ) {
                               dropRemalue += 1;
                             }
                             DisPerExemptions = 0.02 *  ( FormatDollarAmont(dropRemalue) ) * this.Exempt_TotExemptions;
                             DisPerExemptions *= 2/3; 
                             this.Exempt_TotExemptions -= DisPerExemptions;
                         }
                   }

            }
            //this.Exempt_TotExemptions +=  FormatDollarAmont(500 * Math.min(4, FormatDollarAmont(Estimator.FBasic.Katrina_Dependents)));
                return ( Math.max( this.Exempt_TotExemptions, 0.0 ));

}// Calc_TotExemptions




// ***************************************************************************
// function: Calc_AllowMed (class: FormBasic)
//
//           Allowable Medical Expenses Calculation
// ***************************************************************************
function Calc_AllowMed( AGI )
{
		var amt = 0;

		amt =  this.Deduct_Med - round( Tax_DedMedicalPct * AGI );
		this.Deduct_AllowMed = Math.max( 0.0, amt );

		return ( this.Deduct_AllowMed );
}// Calc_AllowMed



// ***************************************************************************
// function: Calc_StudentLoanInterest (class: FormBasic)
//
//            Calculation Returns value for Student Loan Interst Deduction
// ***************************************************************************

function Calc_StudentLoanInterest()
{
		var status = Estimator.FilingStatus;
		var IntDisAllowed = 0;
		var POThreshold = 0;

		var AGI = Estimator.Calc_StudentLoanIntAGI();
		var Interest = Estimator.FBasic.StudentLoanInt;
		var MaxExp = Math.min( 2500, Interest )

		if ( status  == MARRIED_SEPARATE_FILER )
		{
		  return (0);
		}

		if ( status == MARRIED_JOINT_FILER )
		{
		  POThreshold = 110000;
		}
		else {
		  POThreshold = 55000;
		}

		if ( AGI > POThreshold &&  status != MARRIED_JOINT_FILER )
		{
		  IntDisAllowed = MaxExp * Math.min(1.0, (( AGI - POThreshold )/ 15000 ));
		}
		else
		if ( AGI > POThreshold &&  status == MARRIED_JOINT_FILER )
		{
		  IntDisAllowed = MaxExp * Math.min(1.0, (( AGI - POThreshold )/ 30000 ));
		}

		return ( MaxExp - IntDisAllowed );

 }  // Calc_StudentLoanInterest




// ***************************************************************************
//      function: Calc_StdDeduction (class: FormBasic)
//
//      Function Returns Calculated value of standard Deduction
// ***************************************************************************
function Calc_StdDeduction() 
{
		var status   = Estimator.FilingStatus;
		var IsADependent = Estimator.FBasic.Deduct_DedOther;
		var IsSPDependent =  Estimator.FBasic.Deduct_SPDedOther;

		var self     = this.Deduct_SelfBlind;
		var spouse   = this.Deduct_SpouseBlind;

		var deduction;

		var NumExtras	= 0;
		var amtToAdd = 0;
		var ExtraAmt = 0;
		var TPIncome = Estimator.FBasic.TPIncLoss;
		var SelfWages = Estimator.SelfWages;
		var SpouseWages = Estimator.SpouseWages
		var Income = 0;

		var SelfWages = Estimator.SelfWages;
		var SpouseWages = Estimator.SpouseWages

		var SEIncomeLoss = Estimator.FBasic.TPIncLoss;
		var SPIncomeLoss = Estimator.FBasic.SPIncLoss;
		var TPSETax = CalcTPSETax();



		if ( Estimator.TPAge > 64 ){
			NumExtras += 1;
		}

		if ( self == 1){
		   NumExtras += 1;
		}

		if ( status == MARRIED_JOINT_FILER )
		{
			 if ( Estimator.SPAge > 64 ){
				NumExtras += 1;
			  }
			 if ( spouse == 1 ){
				NumExtras += 1;
			  }
		}


		if (( status == MARRIED_JOINT_FILER ) || ( status == QUAL_WIDOW ) || ( status == MARRIED_SEPARATE_FILER ))
		{
		   ExtraAmt = ExtraAmtMFJ ;
		}else
		{
		   ExtraAmt = ExtraAmtSingle;
		}


		amtToAdd = ExtraAmt * NumExtras;
		stdDed = this.TaxRates.Tax_DeductionStandard[status];

		Income = (FormatDollarAmont(SelfWages) + FormatDollarAmont(SpouseWages) + FormatDollarAmont(SEIncomeLoss) + FormatDollarAmont(SPIncomeLoss)  - (0.5 * CalcTPSETax()) - (0.5 * CalcSPSETax() ));
		if ( IsADependent == 1  ||  IsSPDependent == 1 )
		{
		  stdDed = amtToAdd + Math.min(Math.max( MinDepStd, Income + 300 ), stdDed );
		}
		else
		{
		  stdDed += FormatDollarAmont(amtToAdd);
		}

	    return ( stdDed );

} // Calc_StdDeduction


function Calc_CapitalGainTax(AGI)
{
 var OrdinaryInc = 0;
 var TaxableInc = 0;
 var NetCapitalGain = 0;
 var DiffTaxIncAndNGC = 0;

 var status = Estimator.FilingStatus;

 var theTaxRateYearConst;
 var theTaxRateYearConst = Estimator.TaxRates.rThisYear;

 var MinTIOr3dBP = 0;
 var PctTax10 = 0;
 var PctTax20 = 0;

 var OrdTax1;
 var OrdTax2 = 0;
 var TentCGTax = 0;

 TaxableInc = this.Calc_TaxableInc(AGI);

 NCapGain = FormatDollarAmont(this.NetCapitalGain()) + FormatDollarAmont(Estimator.FBasic.QualifyingDividend);

 var GainGTOneYear = FormatDollarAmont(Estimator.FBasic.HeldGTOneYr) + FormatDollarAmont(Estimator.FBasic.QualifyingDividend);
 var NetCapitalGain = Math.min(GainGTOneYear, NCapGain);


 DiffTaxIncAndNGC = TaxableInc  - NetCapitalGain;


 OrdinaryInc = Math.max(0, DiffTaxIncAndNGC);
 OrdTax1 = Calc_OrdinaryTaxWithOrdIncome(OrdinaryInc);


 var TaxBracket = (theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + 2])

var LowestRateGain = Math.max(0, FormatDollarAmont(Estimator.FBasic.QualifyingDividend) + FormatDollarAmont( Estimator.FBasic.HeldGTOneYr));
 var AmtToTaxAt5Pct = 0;
 var PctTax5 = 0;
 var AmtToTaxAt15Pct = 0;
 var PctTax15 = 0;

 MinTIOr3dBP = Math.min(TaxableInc, TaxBracket);

 if (MinTIOr3dBP >  OrdinaryInc) {
     AmtToTaxAt5Pct =  Math.min(LowestRateGain, MinTIOr3dBP - OrdinaryInc);
     PctTax5 = 0.05 * AmtToTaxAt5Pct;
     PctTax10 = 0.10 * (MinTIOr3dBP - OrdinaryInc - AmtToTaxAt5Pct);
  }
  else {
    PctTax10 = 0;
  }


 if (NetCapitalGain == (MinTIOr3dBP - OrdinaryInc)) {
    PctTax20 = 0;
  }
  else {
        var CapGainNotToTax = (MinTIOr3dBP - OrdinaryInc);
                if ( CapGainNotToTax < 0 )
                {
                  CapGainNotToTax = 0;
                }
                var CapGainLeftToTax = Math.max(0,  Math.min(TaxableInc, NetCapitalGain) - CapGainNotToTax);
                AmtToTaxAt15Pct = Math.min(LowestRateGain, CapGainLeftToTax);
                PctTax15 = 0.15 * AmtToTaxAt15Pct;
        PctTax20 = 0.20 *  (CapGainLeftToTax - AmtToTaxAt15Pct);
  }

 TentCGTax =  FormatDollarAmont(OrdTax1) + FormatDollarAmont(PctTax10) + FormatDollarAmont(PctTax20) + FormatDollarAmont(PctTax5) + FormatDollarAmont(PctTax15);


 OrdTax2 = Calc_OrdinaryTax(Estimator.AGI)



 return (Math.min(TentCGTax, OrdTax2));


}





// ***************************************************************************
// function: Calc_OrdinaryTaxWithOrdIncome (class: FormEstimator)
//  Calculation For Regular Tax
// ***************************************************************************

function Calc_OrdinaryTaxWithOrdIncome(OrdinaryInc)
{
        var status = Estimator.FilingStatus;

    var Tax = 0;
    var i = 0;
    var MinInc = 0;
    var TaxableInc = 0;

        var DiffTaxIncAndNGC = 0;
        var     NetCapitalGain = 0;

    TaxableInc = OrdinaryInc;

    var theTaxRateYearConst;
        var theTaxRateYearConst = Estimator.TaxRates.rThisYear;


                for ( i=1 ; i<6; i++ )

                {
                  if ( FormatDollarAmont(TaxableInc) > theTaxRateYearConst.IncomeLimit[((status*NUM_TAXRATES)) + ((i-1)) ] )
                  {
                     MinInc = Math.min(TaxableInc, theTaxRateYearConst.IncomeLimit[((status*NUM_TAXRATES)) + (i)]) - theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + (i-1) ];
                     Tax = Tax + Math.round(theTaxRateYearConst.TaxRate[i-1] * MinInc);

                  }
                }

                if ( TaxableInc > theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + 5] )
                {
                        Tax = Tax + Math.round(theTaxRateYearConst.TaxRate[5] * (TaxableInc - theTaxRateYearConst.IncomeLimit[(status * NUM_TAXRATES) + 5] )); }

                        Tax = Math.round(1.0 * Tax / 100);
                        return ( Tax );

} // Calc_OrdinaryTaxWithOrdIncome


// ***************************************************************************
// function: Calc_OrdinaryTax (class: FormEstimator)
//  Calculation For Regular Tax
// ***************************************************************************

function Calc_OrdinaryTax(AGI)
{
        var status = Estimator.FilingStatus;

    var Tax = 0;
    var i = 0;
    var MinInc = 0;
    var TaxableInc = 0;
        var OrdinaryInc = 0;

        var DiffTaxIncAndNGC = 0;
        var     NetCapitalGain = 0;

    TaxableInc = this.Calc_TaxableInc(AGI);

	//alert ("taxable Income  " + TaxableInc);

        NetCapitalGain = FormatDollarAmont(this.NetCapitalGain()) + FormatDollarAmont(Estimator.FBasic.QualifyingDividend); // BH 6/6 add qualifying divs
    DiffTaxIncAndNGC = TaxableInc  - NetCapitalGain;
    OrdinaryInc = Math.max(0, DiffTaxIncAndNGC);

    var theTaxRateYearConst;
        var theTaxRateYearConst = Estimator.TaxRates.rThisYear;

                for ( i=1 ; i<6; i++ )

                {
                  if ( TaxableInc > theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + (i-1) ] )
                  {
                     MinInc = Math.min(TaxableInc, theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + i]) - theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + (i-1) ];
                     Tax = Tax + Math.round(theTaxRateYearConst.TaxRate[i-1] * MinInc);
                  }
                }

                if ( TaxableInc > theTaxRateYearConst.IncomeLimit[(status*NUM_TAXRATES) + 5] )
                {
                        Tax = Tax + Math.round(theTaxRateYearConst.TaxRate[5] * (TaxableInc - theTaxRateYearConst.IncomeLimit[(status * NUM_TAXRATES) + 5] ));
                }

                        Tax = Math.round(1.0 * Tax / 100);
                        return ( Tax );
} // Calc_OrdinaryTax



// ***************************************************************************
// function: Calc_RegularTax(class: FormEstimator)
//  Calculation For RegularTax
// ***************************************************************************


function Calc_RegularTax(AGI)
{
  var NCapGain = 0;
	 NCapGain = FormatDollarAmont(this.NetCapitalGain()) + FormatDollarAmont(Estimator.FBasic.QualifyingDividend);

  var GainGTOneYear = FormatDollarAmont(Estimator.FBasic.HeldGTOneYr) + FormatDollarAmont(Estimator.FBasic.QualifyingDividend);     
  var NCG = Math.min(GainGTOneYear, NCapGain);

  var RegTax = 0;
  var TaxableInc = 0;

  if ( NCG  > 0 ){

        RegTax = this.Calc_CapitalGainTax(AGI);

  } else
  {
        RegTax = Estimator.Calc_OrdinaryTax(AGI);
  }

  return ( RegTax );

}





// ***************************************************************************
// function: Calc_ChildTaxCredit (class: FormEstimator)
//   Calculation For Child Tax Credit
// ***************************************************************************

function Calc_ChildTaxCredit()
{
        var result;
        var TopLimit, tL1, tL2, tL4, tL5, tL6, tL7;
        var ChildRate;
        var JointLim = 110000;
        var SepLim = 55000;
        var ElseLim = 75000;
        var AGIToUse = Estimator.AGI;
        var TotCldUnder17 = 0;

        ChildRate = CHILD_TAX_CREDIT;  

        TotCldUnder17 = FormatDollarAmont(Estimator.FBasic.Exempt_Dependents) + FormatDollarAmont(Estimator.FBasic.Exempt_Under14to16);

        if ( TotCldUnder17 > 0 )
        {

                tL1 = ChildRate * TotCldUnder17;

                if (Estimator.FilingStatus == MARRIED_JOINT_FILER)
                        TopLimit = JointLim
                else
                if (Estimator.FilingStatus == MARRIED_SEPARATE_FILER)
                        TopLimit = SepLim
                else
                        TopLimit = ElseLim;

                tL4 = Math.max(0, AGIToUse - TopLimit);
                if (tL4 > 0)
                {

          if ( Math.round(tL4/1000) != (tL4/1000) )
                  {
                        tL4 = tL4 + 1000;
          }
                        tL4 = parseInt( tL4/1000 );
            tL5 = tL4 * 1000;
                        tL6 = tL5 * .05;

                }
                else
                        tL6 = 0;


                tL7 = (tL1 - tL6);

                if (tL7 <= 0)
                        result = 0
                else
                        result = Math.max(0,tL7);

		result = Math.max(0, result); 

                return result
        }else
        {
         return 0;
        }

}



// ***************************************************************************

// function: Calc_EIC (class: FormEstimator)

//   Calculation For Earned Income Credit

// ***************************************************************************



function Calc_EIC(AGI)

{



var TotDep = 0;

var NumberEICKids = 0;

var NetGain;

var Status = Estimator.FilingStatus;

var earnedIncCrdt;

var earnedIncCrdtAGI;

var earnedIncCrdtWages;

var TotWagesNSelfEmly;



var TPSETax = CalcTPSETax();

var SPSETax = CalcSPSETax();

var IsADependent = Estimator.FBasic.Deduct_DedOther;

var IsSPDependent =  Estimator.FBasic.Deduct_SPDedOther;



TotDep = FormatDollarAmont( Estimator.FBasic.Exempt_Dependents);

TotDep += FormatDollarAmont( Estimator.FBasic.Exempt_Under17);

TotDep += FormatDollarAmont( Estimator.FBasic.Exempt_DisabledChild);

TotDep += FormatDollarAmont( Estimator.FBasic.Exempt_Under14to16);

TotDep += FormatDollarAmont( Estimator.FBasic.Exempt_Student19to24);

TotDep += FormatDollarAmont( Estimator.FBasic.Other_Dependents);



NumberEICKids = TotDep -  Estimator.FBasic.Other_Dependents;

NetGain = NetCapitalGain();



earnedIncCrdtAGI = EarnedIncomeCredit(AGI, NumberEICKids);



TotWagesNSelfEmly =  FormatDollarAmont(Estimator.SelfWages ) +  FormatDollarAmont( Estimator.SpouseWages ) +  FormatDollarAmont(  Estimator.FBasic.TPIncLoss ) +  FormatDollarAmont( Estimator.FBasic.SPIncLoss - (0.5 * ( FormatDollarAmont(TPSETax ) +  FormatDollarAmont( SPSETax))));



earnedIncCrdtWages = EarnedIncomeCredit(TotWagesNSelfEmly, NumberEICKids);



if ((Status == MARRIED_SEPARATE_FILER ) || ((FormatDollarAmont(NetGain)  + FormatDollarAmont(Estimator.FBasic.QualifyingDividend))> ExcessInvInc) || (AGI > EIC_MAX_INCOME) || (IsADependent == 1)  ||  (IsSPDependent == 1) || (NumberEICKids == 0 && ((Estimator.TPAge < 25 || Estimator.TPAge > 64) && (Estimator.SPAge < 25 || Estimator.SPAge > 64))))

    {

      earnedIncCrdt = 0;

    }

else

{

 earnedIncCrdt = Math.min( earnedIncCrdtAGI, earnedIncCrdtWages );
}




 this.TaxCalc_TotalCredits = earnedIncCrdt;
	

 return (FormatDollarAmont(this.TaxCalc_TotalCredits));



//return earnedIncCrdtAGI;



}



// New EIC numbers









function EarnedIncomeCredit(Income, NumKids)

{



    var status = Estimator.FilingStatus;

    var  kMFJAdjustment = 0;

    if ( status == MARRIED_JOINT_FILER  )

	{

		kMFJAdjustment = 2000;
// goes to 2000 for 2005 - 2007
	}

	

    var CreditPct = new Array(3);

    var PhaseOutPct = new Array(3);

    var EICAmt = new Array(3);

    var PhaseOutAmt = new Array(3);



    var GrossEIC = 0;

    var PhaseOutAmtFinal = 0.0;



    CreditPct[0] = 7.65;

    CreditPct[1] = 34.0;

    CreditPct[2] = 40.0;



    PhaseOutPct[0] = 7.65;

    PhaseOutPct[1] = 15.98;

    PhaseOutPct[2] = 21.06;
    
    EICAmt[0] = 5590;

    EICAmt[1] = 8390;

    EICAmt[2] = 11790;



    PhaseOutAmt[0] = 7000 +  FormatDollarAmont( kMFJAdjustment);

    PhaseOutAmt[1] = 15390 +  FormatDollarAmont( kMFJAdjustment);

    PhaseOutAmt[2] = 15390 +  FormatDollarAmont( kMFJAdjustment);
    
    if (Income > 0)

    {

         Income = ( 50.0 * (Income / 50) + 25);

    }


    if (NumKids > 2)

    {

      NumKids = 2;

    }


    GrossEIC = CreditPct[NumKids] * (Math.min(Income, EICAmt[NumKids]));
    PhaseOutAmtFinal = PhaseOutPct[NumKids] * (Math.max(0, (Income - PhaseOutAmt[NumKids]))); 

    return (FormatDollarAmont(Math.max(0, (GrossEIC - PhaseOutAmtFinal) / 100.0)));      


}



// ***************************************************************************
// function: min
// ***************************************************************************
function min(a,b)
{

        if(a < b) {
                return(a);
        }
        else {
                return(b);
        }

}

// ***************************************************************************
// function: Math.max
// ***************************************************************************
function max(a,b)

{
        if(a > b)
                {
                return(a);
        }
                else
                {
                return(b);
        }

}


// ***************************************************************************
// function: round
// ***************************************************************************
function round(a)
{

        return (Math.round(a*100)/100);
}


// ***************************************************************************
// function: round100
// ***************************************************************************
function round100(a)
{
        return (Math.round(a));
}


function NetCapitalGain()
{
 var NGC;
 NGC =  FormatDollarAmont(Estimator.FBasic.HeldOneYrOrLess) +  FormatDollarAmont( Estimator.FBasic.HeldGTOneYr);

 if ( NGC < 0 )
 {
   if ( Estimator.FilingStatus == MARRIED_SEPARATE_FILER )
   {
     NGC = Math.max(NGC, -1500)
   }
   else
   {
    NGC = Math.max(NGC, -3000)
   }

 }

return NGC;

}


function CalcTPSETax()
{
        var TPSEBase;
        if ( (.9235 * Estimator.FBasic.TPIncLoss) < 400 )
        {
                this.TPSETax = 0;
        }
        else
        {
                TPSEBase = ( 0.9235 * Math.max(0, Estimator.FBasic.TPIncLoss ));
                this.TPSETax  = FormatDollarAmont( 0.029  * TPSEBase )  +  FormatDollarAmont( 0.124 * Math.min( TPSEBase, Math.max(0, (RefCTC_Threshold_Wages - Estimator.SelfWages))));
        }

       return this.TPSETax;

}

function CalcSPSETax()
{
        var SPSETax;
        var SPSEBase;
        if ( (.9235 * Estimator.FBasic.SPIncLoss) < 400 )
        {
                SPSETax = 0;
        }
        else
        {
                SPSEBase = ( 0.9235 * Math.max(0, Estimator.FBasic.SPIncLoss ));
                SPSETax  = FormatDollarAmont( 0.029  * SPSEBase )  +  FormatDollarAmont( 0.124 * Math.min( SPSEBase, Math.max(0, (RefCTC_Threshold_Wages - Estimator.SpouseWages))));
        }

       return SPSETax;
}


function OnChangeCalculatedField(theField)
{
                alert("This field will be Calculated automatically.");
}

function FormatDollarAmont( theVar )
{
        var Stringal = new String;
        Stringal = round100( theVar );
        return Stringal;
}

function FormatInteger( theVar )
{
                var Stringal = new String;

                Stringal = theVar;

                return Stringal;
}

function EvaluateDollar( theField )
{

                var TemStr      = new String;
                var Retal = 0.0;
                var bCheckDigit = false;

                for ( var i=0 ; i<theField.value.length ; i++ )
                {
                        if ( '0' <= theField.value.charAt(i) &&
                                 theField.value.charAt(i) <= '9' )
                                {
                                                TemStr += theField.value.charAt(i);
                                                bCheckDigit = true;
                                }
                                else
                                if ( theField.value.charAt(i) == '.' ||
                                         theField.value.charAt(i) == '-' )
                                {
                                                TemStr += theField.value.charAt(i);
                                }
                }

                if ( bCheckDigit == true )
                {
                                Retal = parseFloat( TemStr );
                                Retal = Math.round(Retal);
                }
                if (Retal > 999999)
                                {
                                Retal = 999999;
                                }
                                if (Retal  < 0)
                                {
                                Retal = 0;
                                }

                return Retal;
}



function EvaluateIntal( theField )
{
                var TemStr      = new String;
                var Retal = 0;
                var bCheckDigit = false;

                for ( var i=0 ; i<theField.value.length ; i++ )
                {
                        if ( '0' <= theField.value.charAt(i) &&
                                 theField.value.charAt(i) <= '9' )
                                {
                                                TemStr += theField.value.charAt(i);
                                                bCheckDigit = true;
                                }
                                else
                                if ( theField.value.charAt(i) == '-' )
                                {
                                                TemStr += theField.value.charAt(i);
                                }
                }

                if ( bCheckDigit == true )
                {
                                Retal = parseInt( TemStr,10 );
                }
                return Retal;
}

function validateField(theField)
{
                                if (theField.value > 999999)
                                {
                                theField.value = 999999;
                                }
                                if (theField.value  < 0)
                                {
                                theField.value = 0;
                                }
}



// ***************************************************************************
// function: Calc_AllowedItemDeduction (class: FormBasic)
//
//            Calculation Returns value for Allowed Itemized Deductions
//
// ***************************************************************************
function Calc_AllowedItemDeduction( FilingStatus, AGI )
{
                var C,D;
                var status = FilingStatus;

                var itemDeduction;
                this.Deduct_ItemReduction = 0.0;

                itemDeduction = this.Calc_TotItemized( FilingStatus, AGI );

                if ( AGI > this.TaxRates.IDPOThreshold[status] )
                {
                        C  =  0.03 * ( AGI - this.TaxRates.IDPOThreshold[status] );
                        D  =  0.80 * ( itemDeduction - this.Calc_AllowMed(AGI) - Estimator.FBasic.Deduct_CashCharity );

                        this.Deduct_ItemReduction = Math.min(C, D);
						// BH 12/22/06 phaseout is being phased out beginning in 2006
			this.Deduct_ItemReduction *= 2/3;
                        this.Deduct_ItemReduction = itemDeduction -  this.Deduct_ItemReduction;
                }
                else
                {
                    this.Deduct_ItemReduction = itemDeduction;
                }


                return ( this.Deduct_ItemReduction );


} // Calc_AllowedItemDeduction


// ***************************************************************************
// function: Calc_DisAllowedItemDeduction (class: FormBasic)
//
//            Calculation Returns value for DisAllowed Itemized Deductions
//
// ***************************************************************************
function Calc_DisAllowedItemDeduction( FilingStatus, AGI )
{
        var C,D;
        var status = FilingStatus;

        var itemDeduction;
        this.Deduct_ItemReduction = 0.0;

        itemDeduction = this.Calc_TotItemized( FilingStatus, AGI );
        if ( AGI > this.TaxRates.IDPOThreshold[status] )
        {
                C  =  0.03 * ( AGI - this.TaxRates.IDPOThreshold[status] );
                D  =  0.80 * ( itemDeduction - this.Calc_AllowMed(AGI) - Estimator.FBasic.Deduct_CashCharity );

                   this.Deduct_ItemReduction = Math.min( C, D ); 
			this.Deduct_ItemReduction *= 2/3;
        }


        return ( this.Deduct_ItemReduction );


} // Calc_DisAllowedItemDeduction




// ***************************************************************************
// function: Calc_GrStdOrItmReduction (class: FormBasic)
//
// BH: actually this returns the greater of standard or itemized
// ***************************************************************************
function Calc_GrStdOrItmReduction( FilingStatus, AGI)
{
                var C,D;
                var status = FilingStatus;
                var stdDed = Estimator.FBasic.Calc_StdDeduction();

                var itemDeduction;
                this.Deduct_ItemReduction = 0.0;

                itemDeduction = this.Calc_TotItemized( FilingStatus, AGI );

                if ( AGI > this.TaxRates.IDPOThreshold[status] )
                {
                        C  =  0.03 * ( AGI - this.TaxRates.IDPOThreshold[status] );
                        D  =  0.80 * ( itemDeduction - this.Calc_AllowMed(AGI) - Estimator.FBasic.Deduct_CashCharity );

                this.Deduct_ItemReduction = Math.min( C, D );
		this.Deduct_ItemReduction *= 2/3;
                this.Deduct_ItemReduction = itemDeduction -  this.Deduct_ItemReduction;
                }
                else
                {
                  this.Deduct_ItemReduction = itemDeduction;
                }

                this.Deduct_ItemReduction = Math.max( this.Deduct_ItemReduction, stdDed );

                return ( this.Deduct_ItemReduction );

} // Calc_GrStdOrItmReduction

// ***************************************************************************
//
//      Function CalcChildCareCredit
//      Calculation for Cahild Care Credit
//
// ***************************************************************************

function CalcChildCareCredit( AGI)
{

 EligibleChildCareExpenses = 0;
 ChildCareCredit = 0;
 childCareMultiplyer = 0;
 var ChildCareExpenses = Estimator.TaxCalc_DepCare;
 var MaxChildCareExpenses = 0;

 var SelfWages = Estimator.SelfWages;
 var SpouseWages = Estimator.SpouseWages

 var SEIncomeLoss = Estimator.FBasic.TPIncLoss;
 var SPIncomeLoss = Estimator.FBasic.SPIncLoss;

 var mAGI = Estimator.AGI;

 var TotUnder13 =  Estimator.FBasic.Exempt_Dependents;
 var TotDisabled = Estimator.FBasic.Exempt_DisabledChild;

if( Estimator.FilingStatus == MARRIED_SEPARATE_FILER )
           {
	return 0;
       }


                if ( mAGI <= 15000)  {
                   childCareMultiplyer = 0.35;
                  }
                else
                 if ( mAGI > 15000 && mAGI <= 17000 ){
                   childCareMultiplyer = 0.34;
                  }
                else
                 if ( mAGI > 17000 && mAGI <= 19000 ) {
                   childCareMultiplyer = 0.33;
                 }
                else
                if ( mAGI > 19000 &&  mAGI <= 21000 ){
                  childCareMultiplyer = 0.32;
                }
                else
                if ( mAGI > 21000 && mAGI <= 23000 ){
                  childCareMultiplyer = 0.31;
                }
                else
                if ( mAGI > 23000 && mAGI <= 25000 ){
                  childCareMultiplyer = 0.30;
                }
                else
                if ( mAGI > 25000 && mAGI <= 27000 ){
                  childCareMultiplyer = 0.29;
                }
                else
                if ( mAGI > 27000 && mAGI <= 29000 ){
                  childCareMultiplyer = 0.28;
                }
                else
                if ( mAGI > 29000 && mAGI <= 31000 ){
                  childCareMultiplyer = 0.27;
                }
                else
                if ( mAGI > 31000 && mAGI <= 33000 ){
                  childCareMultiplyer = 0.26;
                }

                else
                if ( mAGI > 33000 && mAGI <= 35000 ){
                  childCareMultiplyer = 0.25;
                }

                else
                if ( mAGI > 35000 && mAGI <= 37000 ){
                  childCareMultiplyer = 0.24;
                }

                else
                if ( mAGI > 37000 && mAGI <= 39000 ){
                  childCareMultiplyer = 0.23;
                }

                else
                if ( mAGI > 39000 && mAGI <= 41000 ){
                  childCareMultiplyer = 0.22;
                }

                else
                if ( mAGI > 41000 && mAGI <= 43000 ){
                  childCareMultiplyer = 0.21;
                }

        else
                if ( mAGI >= 43000){
                  childCareMultiplyer = 0.20;
                }


        MaxChildCareExpenses = Math.min( 6000, ( 3000 * [ FormatDollarAmont( TotUnder13) +  FormatDollarAmont( TotDisabled)] ));
   EligibleChildCareExpenses = Math.min( MaxChildCareExpenses, ChildCareExpenses );
   EligibleChildCareExpenses = Math.min( EligibleChildCareExpenses, ( FormatDollarAmont( SelfWages) +  FormatDollarAmont( SEIncomeLoss) - 0.5 * CalcTPSETax() ));



       if( Estimator.FilingStatus == MARRIED_JOINT_FILER )
           {
                   EligibleChildCareExpenses = Math.min( EligibleChildCareExpenses, (  FormatDollarAmont( SpouseWages) +  FormatDollarAmont( SPIncomeLoss) - 0.5 * CalcSPSETax() ));
       }


 childCareCredit = ( childCareMultiplyer * EligibleChildCareExpenses );


 var RegTax = this.Calc_RegularTax( Estimator.AGI, YEAR ); //Changed 12/13

 var AMT = Estimator.Calc_AMT( Estimator.AGI);

childCareCredit = Math.min(childCareCredit, Math.max(RegTax, AMT));

 return (Math.max( 0, FormatDollarAmont(childCareCredit)));


}


//**********************************************************************************
//   Function Calc_TaxAfterCredit
//
//       Calulation For Tax After Credit
//**********************************************************************************

function Calc_TaxAfterCredit()
{
         var childCareCredit;
         var NonRefChildCareCredit = 0;
         var TaxAfterCredit = 0;
         var AGI = Estimator.AGI;
         var Year = YEAR; 

         RegTax = this.Calc_RegularTax( AGI, Year );

         var AMTTax = Estimator.Calc_AMT( AGI, Year );
         var AMT = Math.max( 0.0, (AMTTax - RegTax ));
         var OtherCredits = Estimator.FBasic.Deduct_OtherCredits;
         var EducationCredit;
         var SaversCredit = Estimator.FBasic.Deduct_SaversCredit;    

         var ChldTaxCrdt = this.Calc_ChildTaxCredit()
         childCareCredit = CalcChildCareCredit( AGI, Year );

         EducationCredit =  Estimator.Calc_EducationCredit();
         NonRefChildCareCredit = Math.min( ChldTaxCrdt, Math.max( RegTax, AMTTax ) - (  FormatDollarAmont( EducationCredit) +  FormatDollarAmont( childCareCredit) ));

      //Sandy added 08/15/05 savers credit to fix bug 32351
         // BH 2/6/06 we need to change the next line
         //TaxAfterCredit = Math.max( RegTax, AMT ) - childCareCredit - NonRefChildCareCredit - EducationCredit - SaversCredit - OtherCredits;

         TaxAfterCredit = Math.max( RegTax, AMTTax) - childCareCredit - NonRefChildCareCredit - EducationCredit - SaversCredit - OtherCredits;


         return ( Math.max( 0.0, TaxAfterCredit ));

}




//**********************************************************************************
//   Function Calc_TotalTax
//
//       Calulation For Total Tax
//**********************************************************************************

function Calc_TotalTax()
{
        var TaxAfterCredit = Calc_TaxAfterCredit();
        var TPSETax = CalcTPSETax();
        var SPSETax = CalcSPSETax();
        var OtherTaxes  = Estimator.FBasic.Deduct_RETax;

        this.TotalTax = (  FormatDollarAmont( TaxAfterCredit) +  FormatDollarAmont( TPSETax) +  FormatDollarAmont( OtherTaxes) );

         if( Estimator.FilingStatus == MARRIED_JOINT_FILER )
         {
                this.TotalTax =  FormatDollarAmont( TaxAfterCredit) +  FormatDollarAmont( TPSETax) +  FormatDollarAmont( SPSETax) +  FormatDollarAmont( OtherTaxes);
         }
         return ( this.TotalTax );
  }

// ***************************************************************************
// function: Calc_StudentLoanIntAGI (class: FormEstimator)
//   Calculation For Student Loan Interset AGIncome
// ***************************************************************************
function Calc_StudentLoanIntAGI()
{
          var NetCapGain = NetCapitalGain();
          var TPSETax = CalcTPSETax();
          var SPSETax = CalcSPSETax();
	  var SSBen = SocSecTaxableBenefit();
         var StudentLoanAGI;

           StudentLoanAGI  =   FormatDollarAmont( Estimator.SelfWages) +  FormatDollarAmont( Estimator.FBasic.QualifyingDividend) +  FormatDollarAmont( NetCapGain) +  FormatDollarAmont( Estimator.FBasic.TPIncLoss) + FormatDollarAmont(SSBen);
           StudentLoanAGI +=  FormatDollarAmont( Estimator.FBasic.OtherIncLoss - Estimator.FBasic.DeductibleIRA - Estimator.FBasic.OtherAdjToInc - (0.5 * TPSETax ));

          if( Estimator.FilingStatus == MARRIED_JOINT_FILER )
          {
            StudentLoanAGI +=   FormatDollarAmont( Estimator.SpouseWages) +  FormatDollarAmont( Estimator.FBasic.SPIncLoss);
            StudentLoanAGI -= (0.5 *  SPSETax);
          }

           return( StudentLoanAGI );
}


// ***************************************************************************
// function: Calc_AGI (class: FormEstimator)
//   Calculation For Adjusted Gross Income
// ***************************************************************************
function Calc_AGI()
{
          var NetCapGain = NetCapitalGain();
          var TPSETax = CalcTPSETax();
          var SPSETax = CalcSPSETax();

          var StudentLoanInt = Estimator.FBasic.Calc_StudentLoanInterest();
	  var SSBen = SocSecTaxableBenefit();

          this.AGI  =   FormatDollarAmont( Estimator.SelfWages) +  FormatDollarAmont( Estimator.FBasic.QualifyingDividend) +  FormatDollarAmont( NetCapGain) +  FormatDollarAmont( Estimator.FBasic.TPIncLoss) +  FormatDollarAmont(SSBen);
          this.AGI +=  FormatDollarAmont( Estimator.FBasic.OtherIncLoss - Estimator.FBasic.DeductibleIRA - StudentLoanInt - Estimator.FBasic.OtherAdjToInc - (0.5 * TPSETax ));

          if( Estimator.FilingStatus == MARRIED_JOINT_FILER )
          {
            this.AGI +=   FormatDollarAmont( Estimator.SpouseWages) + FormatDollarAmont(Estimator.FBasic.SPIncLoss);
            this.AGI -= (0.5 *  SPSETax);
          }

          return(this.AGI);
}


// ***************************************************************************
// function: Calc_TaxableInc (class: FormEstimator)
//   Calculation For Taxable Income
//
// ***************************************************************************
function Calc_TaxableInc(AGI)
{

                var count  = 0;

                var status = Estimator.FilingStatus;

                var GtItmOrStd = Estimator.FBasic.Calc_GrStdOrItmReduction(Estimator.FilingStatus, AGI)

                var Deduct_DedOther = Estimator.FBasic.Deduct_DedOther;
                var Deduct_SPDedOther = Estimator.FBasic.Deduct_SPDedOther;


                count = Estimator.FBasic.Calc_TotExemptions(status, Deduct_DedOther, Deduct_SPDedOther,AGI);


                //this.TaxableInc = this.Calc_AGI() - GtItmOrStd - count ;

		//alert("AGI in tax in func  " + AGI);
		//alert ("grof...  " + GtItmOrStd);
		//alert ("count    "+ count);

		this.TaxableInc = AGI - GtItmOrStd - count ;

                if ( this.TaxableInc < 0.0 )
                {
                                this.TaxableInc = 0.0;
                }

                return (this.TaxableInc);


}


//**********************************************************************
//function: AMTExemption
//Calculation of AMT Exemption amount
//**********************************************************************
function CalcDeduct_AMTExemp(AMTI)
{
        var status = Estimator.FilingStatus;
        var theTaxRateYearConst;
        theTaxRateYearConst = Estimator.TaxRates.rThisYear;
        var CalcMax =  ( 0.25 * Math.max(0, [AMTI - Estimator.TaxRates.AMTExemptionPO[status]] ));
        var AMTExmp = Math.round(theTaxRateYearConst.AMTExemption[status] - CalcMax);
        if (Estimator.TPAge < 18)
         {
                AMTExmp = Math.min(AMTExmp, (AMT_EXEMP +  FormatDollarAmont( Estimator.SelfWages) +  FormatDollarAmont( Estimator.FBasic.TPIncLoss) - (0.5 * CalcTPSETax())));
        }

        var AMTEXMP = Math.round (AMTExmp);

	AMTEXMP = (Math.max(0, AMTEXMP));

	AMTEXMP += FormatDollarAmont(500 * Math.min(4, FormatDollarAmont(Estimator.FBasic.Katrina_Dependents)));

                return (AMTEXMP);
}



function RefundableChildTaxCredit(AGI, Year)
{
 var RefdChildTaxCredit = 0 ;
 var SEIncomeLoss = Estimator.FBasic.TPIncLoss;
 var SPIncomeLoss = Estimator.FBasic.SPIncLoss;
 var TentativeCreditFor3OrMore = 0;
 var RegTax = Estimator.Calc_RegularTax(AGI, Year);
 var TotWages =  FormatDollarAmont( Estimator.SelfWages) + FormatDollarAmont(Estimator.SpouseWages) +  FormatDollarAmont( SEIncomeLoss) +  FormatDollarAmont(SPIncomeLoss) - 0.5 * CalcTPSETax() - 0.5 * CalcSPSETax();
 var childTaxCredit = this.Calc_ChildTaxCredit();
 var NonRefCldTaxCrdt = Estimator.NonRefChildTaxCredit(Estimator.AGI, Year);
 var MaxCredit =  Math.max(0, childTaxCredit - NonRefCldTaxCrdt);
 var TotCldUnder17 =  FormatDollarAmont(Estimator.FBasic.Exempt_Dependents) +  FormatDollarAmont( Estimator.FBasic.Exempt_Under14to16);

 if ( !( MaxCredit > 0))
 {
    return 0;
 }

 RefdChildTaxCredit = Math.min( MaxCredit, [0.15 * (TotWages - RefCTC_Threshold)] );  

 if ( TotCldUnder17 > 2 )
 {
       TentativeCreditFor3OrMore = 0.5 * CalcTPSETax() + 0.5 * CalcSPSETax() + 0.0145 * ( FormatDollarAmont( Estimator.SelfWages) +  FormatDollarAmont( Estimator.SpouseWages)) + 0.062 * (Math.min(RefCTC_Threshold_Wages, Estimator.SelfWages) + Math.min(RefCTC_Threshold_Wages, Estimator.SpouseWages)) - Estimator.Calc_EIC(Estimator.AGI);
           TentativeCreditFor3OrMore = Math.max(TentativeCreditFor3OrMore, RefdChildTaxCredit);
           return(Math.max(0.0, Math.min(TentativeCreditFor3OrMore, MaxCredit)));
 }
 return (Math.max(0.0, RefdChildTaxCredit));

}


//**********************************************************************
//function: SocSecTaxableBenefit
//**********************************************************************
function SocSecTaxableBenefit()
{
    var benefits =  FormatDollarAmont(Estimator.FBasic.SocSecBen);
    var ModInc = Calc_SocSecBenInc();
    var ModAdj = FormatDollarAmont(Estimator.FBasic.DeductibleIRA) + FormatDollarAmont(Estimator.FBasic.OtherAdjToInc) + FormatDollarAmont(0.5 * CalcTPSETax());
     if ( Estimator.FilingStatus == MARRIED_JOINT_FILER) 
           {
                  ModAdj += FormatDollarAmont(0.5 * CalcSPSETax());
            }

    if  (ModAdj < ModInc) {
    
    var MaxInc = 25000;
    
    if (Estimator.FilingStatus == MARRIED_JOINT_FILER) {
          MaxInc = 32000;
    }
    
    if (FormatDollarAmont(ModInc - ModAdj) < FormatDollarAmont(MaxInc)) {
        return 0;
    }
    else 
   { 
	var ModAGI  = ModInc - ModAdj;
	var AmtToSubtract = 9000;
	if (Estimator.FilingStatus == MARRIED_JOINT_FILER) {
            	AmtToSubtract = 12000;
        	}
        	ModAGI -= MaxInc; 
	// line 11
       	 var AmtToCompare = Math.max(0, ModAGI - AmtToSubtract); 
	
      	// line 12
	var MinModAGIAmtToSubtract = Math.min( ModAGI, AmtToSubtract);
	// line 13
	MinModAGIAmtToSubtract *= 0.5;
	// line 14
	var AmtToAdd = Math.min((0.5 * benefits), MinModAGIAmtToSubtract);
	// line 15
	AmtToCompare *= 0.85;
	// line 16
	AmtToCompare += AmtToAdd;
	// line 17
	var PctOfBenefits = 0.85 * benefits;
	// line 18
	return Math.min(AmtToCompare, PctOfBenefits);             
    	}
  }
	else {
                 return 0;
	     }
} 

//******************************************************************************
//	Income for Social Security Benefits
//******************************************************************************
function Calc_SocSecBenInc()
{
         this.Inc  =   FormatDollarAmont( Estimator.SelfWages) + 
                  	FormatDollarAmont( Estimator.FBasic.QualifyingDividend) +  
			FormatDollarAmont(NetCapitalGain()) +  
            		FormatDollarAmont( Estimator.FBasic.TPIncLoss) +
            		FormatDollarAmont( Estimator.FBasic.OtherIncLoss) +
            		(0.5 * FormatDollarAmont (Estimator.FBasic.SocSecBen));

   if( Estimator.FilingStatus == MARRIED_JOINT_FILER )
   {
            this.Inc +=   FormatDollarAmont( Estimator.SpouseWages) + FormatDollarAmont(Estimator.FBasic.SPIncLoss);
   	}
          return(this.Inc);
}


//**********************************************************************************
//   Function DisplayBlankField
//       Calulation For DisplayBlankField
//
//**********************************************************************************


function DisplayBlankField()
{
         var a = "";
         return a;
}

