﻿function spouseTotal(cell, amt, arr) {
    var prefix = cell.substr(0, 3);
    if(getElement(prefix + "earner") == "spouse")
        arr[1] += amt;
    else
        arr[0] += amt;
}
function studentLoanPhaseout(AGI, fullyDeductible, notDeductible) {
    var phaseout = notDeductible - fullyDeductible;
    if(AGI >= notDeductible) return 0;
    if(AGI <= fullyDeductible) return 1;
    return 1 - (AGI - fullyDeductible) / phaseout;
}

function IRAPhaseout(AGI, threshold, isOver50, phaseout) {
    var x = AGI;
    var retval = isOver50 ? IRALimit[1] : IRALimit[0];

    while(x > threshold) {
        retval -= 10;
        x -= isOver50 ? (phaseout / 500) : (phaseout / 400);
    }
    return retval;
}
function IRACheckAGI(AGI, IRADeduction, notDeductibleAmt, fullyDeductibleAmt, isOver50, arrayElem) {
    if(AGI >= notDeductibleAmt) {
        IRAWorksheet[arrayElem] = "";
        return 0;
    }
    if(AGI < fullyDeductibleAmt) {
        IRAWorksheet[arrayElem] = IRAWorksheet[arrayElem - 2];
        return IRADeduction;
    }
    IRAWorksheet[arrayElem] = Math.max(200, IRAPhaseout(AGI, fullyDeductibleAmt, isOver50, notDeductibleAmt - fullyDeductibleAmt));
    return Math.min(IRADeduction, IRAWorksheet[arrayElem]);
}
function calculateIRATotal(AGI, contributions, box13Checked) {
	var retval = new Array(0, 0);
	var isOver50 = new Array(false, false);
	var isOver70 = new Array(false, false);
	var arrayElem;

    if(box13Checked[0]) IRAWorksheet[0] = "X"; else IRAWorksheet[0] = "";
    if(box13Checked[1]) IRAWorksheet[1] = "X"; else IRAWorksheet[1] = "";

    isOver50[0] = (getElement("age0").indexOf("6") > -1);
    isOver50[1] = (getElement("age1").indexOf("6") > -1);

    isOver70[0] = (getElement("age0") == ">70.5");
    isOver70[1] = (getElement("age1") == ">70.5");

    if(isOver70[0]) retval[0] = 0;
    if(isOver70[1]) retval[1] = 0;

    if(isOver50[0]) {
        retval[0] = Math.min(AGI, Math.min(IRALimit[1], contributions[0]));
        IRAWorksheet[8] = IRALimit[1];
    } else {
        retval[0] = Math.min(AGI, Math.min(IRALimit[0], contributions[0]));
        IRAWorksheet[8] = IRALimit[0];
    }

    if(isOver50[1]) {
        retval[1] = Math.min(AGI, Math.min(IRALimit[1], contributions[1]));
        IRAWorksheet[9] = IRALimit[1];
    } else {
        retval[1] = Math.min(AGI, Math.min(IRALimit[0], contributions[1]));
        IRAWorksheet[9] = IRALimit[0];
    }

    var mStatus = getElement("maritalStatus");
    if(mStatus.length > 7) mStatus = mStatus.substr(0, 7);
    switch(mStatus) {
        case "single"  : arrayElem = 0; break;
        case "widow"   : arrayElem = 2; break;
        case "married" : arrayElem = 1; break;
    }

    if(!box13Checked[0] && !box13Checked[1]) {
        for (var i = 2; i < 12; i++) IRAWorksheet[i] = "";
    } else {
        if(arrayElem == 1) {
            IRAWorksheet[2] = box13Checked[0] ? IRANotDeductible[2] : IRANotDeductible[1];
            IRAWorksheet[3] = box13Checked[1] ? IRANotDeductible[2] : IRANotDeductible[1];
        } else {
            IRAWorksheet[2] = IRANotDeductible[arrayElem];
            IRAWorksheet[3] = "";
        }
    }

    if(box13Checked[0])
        retval[0] = Math.min(retval[0], IRACheckAGI(AGI, retval[0], IRANotDeductible[arrayElem], IRAFullyDeductible[arrayElem], isOver50[0], 10));
    if(box13Checked[1])
        retval[1] = Math.min(retval[0], IRACheckAGI(AGI, retval[1], IRANotDeductible[arrayElem], IRAFullyDeductible[arrayElem], isOver50[1], 11));

    if(mStatus == "married") {
        for(var i = 0; i < 2; i++) {
            if(box13Checked[i])
                retval[i] = Math.min(retval[i], IRACheckAGI(AGI, retval[i], IRANotDeductible[2], IRAFullyDeductible[2], isOver50[i], i + 10));
            else
                IRAWorksheet[i + 10] = IRAWorksheet[i + 8];
        }
	}
	return retval[0] + retval[1];
}

function tuitionTotalPhaseOut(tuition, netIncome, filingStatusNum) {
    var limits = new Array(80000, 65000, 0);
    if(getElement("canBeClaimed0") == "Yes" || getElement("canBeClaimed1") == "Yes")
        return 0;

    if(filingStatusNum == 2) {
        for (var i = 0; i < limits.length; i++)
            limits[i] *= 2;
    }
    form8917[5] = (netIncome > limits[1]) ? "X" : "";
    form8917[6] = (netIncome > limits[1]) ? "" : "X";
    for (var i = 0; i < limits.length; i++) {
        if(netIncome > limits[i]) return Math.min(tuition, 2000 * i);
    }
}

function calculateTaxableSSAmt(amt, W2Total, intTotal, divTotal, ueTotal, exemptIntTotal, educatorTotal, IRATotal, filingStatusNum) {
    ssWorksheet.length = 0;
    ssWorksheet.push(amt, amt / 2, W2Total + intTotal + divTotal + ueTotal, exemptIntTotal, amt / 2 + W2Total + intTotal + divTotal + ueTotal + exemptIntTotal, educatorTotal + IRATotal);
    var line7 = amt / 2 + W2Total + intTotal + divTotal + ueTotal + exemptIntTotal - educatorTotal - IRATotal;
    ssWorksheet.push(line7, "", "", "", "", "", "", "", "", "", "", "");
    if(line7 < 0) return 0;
    ssWorksheet[7] = filingStatusNum == 2 ? 32000 : 25000;
    var line9 = line7 - (filingStatusNum == 2 ? 32000 : 25000);
    ssWorksheet[8] = line9;
    if(line9 < 0) return 0;
    var line10 = (filingStatusNum == 2 ? 12000 : 9000);
    ssWorksheet[9] = line10;
    ssWorksheet[10] = Math.max(0, line9 - line10);
    ssWorksheet[11] = Math.min(line9, line10);
    ssWorksheet[12] = ssWorksheet[11] / 2;
    var line14 = Math.min(Math.min(line9, line10), amt) / 2;
    ssWorksheet[13] = line14;
    ssWorksheet[14] = ssWorksheet[10] * 0.85;
    ssWorksheet[15] = line14 + ssWorksheet[14];
    ssWorksheet[16] = amt * 0.85;
    ssWorksheet[17] = Math.round(Math.max(0, Math.min(ssWorksheet[15], ssWorksheet[16])));
    return ssWorksheet[17];
}

function dependentQualifies(relationType, cellNum, dependentName) {
    if(relationType == "") return "You must select the appropriate relationship type.";
    var age = getElement("n" + cellNum + "DCOB");
    age = getElement("n" + cellNum + "DYOB" + age);
    if(age == "") return "You must select the appropriate age range.";
    if(relationType == "no") return "Not a dependent since " + dependentName + " is not a relative";
    if(getElement("n" + cellNum + "support") == "0") return "Not a dependent since " + dependentName + " was not supported by you";

    var decree = getElement("n" + cellNum + "decree");
    var months = parseInt(getElement("n" + cellNum + "months"));
    if(months < 7 && decree != "canClaim") return "Not a dependent since " + dependentName + " fails the residency test";
    if(getElement("n" + cellNum + "nationality") == "0") return "Not a dependent since " + dependentName + " fails the nationality test";
    if(getElement("n" + cellNum + "filing") == "0") return "Not a dependent since " + dependentName + " filed a joint return with a spouse";

    if(relationType.substr(0, 8) == "relative" || (relationType.substr(0, 5) == "child" && getElement("adultClaim" + cellNum) == "No")) {
        if(months < 12) return "Not a dependent since " + dependentName + " did not live with you for the full year";
        if(getElement("n" + cellNum + "dependentIncome") == "No") return "Not a dependent since " + dependentName + " earned more than $" + dependentAmt;
    }
    return "";
}

function calculateDeduction(wages, stdDeduct) {
	var minExemption = 850;
	var nClaimed = 0;

    if (getElement("canBeClaimed0") == "Yes") nClaimed++;
    if (getElement("filingStatus") == "married1" && getElement("canBeClaimed1") == "Yes") nClaimed++;

    if (nClaimed > 0) {
	    if (wages <= minExemption - 300) return minExemption;
		else return Math.min(stdDeduct, wages + 300);
    } else
        return stdDeduct;
}

function calculateTax(taxableIncome, filingStatus) {
	var thresholds =  new Array(8350, 33950, 82250, 171550, 372950);
	var rates = new Array(.1, .15, .25, .28, .33, .35);
	var i, roundedIncome;
	var addend = new Array(0.0, 0.0);
	var totalTax = 0.0;

	if(filingStatus == 2 || filingStatus == 5) {
		thresholds[0] = 16700;
		thresholds[1] = 67900;
		thresholds[2] = 137050;
		thresholds[3] = 208850;
	} else if(filingStatus == 4) {
		thresholds[0] = 11950;
		thresholds[1] = 45500;
		thresholds[2] = 117450;
		thresholds[3] = 190200;
	}

	//These are from the tax table
	if(taxableIncome < 5)
		return 0;
	if(taxableIncome < 15)
		return 1;
	if(taxableIncome < 25)
		return 2;

    if(taxableIncome < 3000) {
        roundedIncome = 25 * parseInt(taxableIncome / 25);
        if(roundedIncome % 50 == 0)
            return Math.round(roundedIncome * rates[0] + 1);
        else
            return Math.round(roundedIncome * rates[0] + 1.5);
    }
    roundedIncome = 50 * parseInt(taxableIncome / 50); 
    if(roundedIncome < thresholds[0]) {
        return parseInt(roundedIncome * rates[0] + 3);
    } else if(roundedIncome < thresholds[1]) {
        if(filingStatus == 1) {
            addend[0] = 4.0;
            addend[1] = 3.5;
        } else {
            addend[0] = 3.0;
            addend[1] = 3.5;
        }
        if(roundedIncome % 100 == 0)
            return Math.round(roundedIncome * rates[1] - Math.floor(thresholds[0] * (rates[1] - rates[0])) + 5.5);
        else
            return Math.round(roundedIncome * rates[1] - Math.floor(thresholds[0] * (rates[1] - rates[0])) + 6);
    }

	totalTax = Math.ceil(thresholds[0] * rates[0] + (thresholds[1] - thresholds[0]) * rates[1]);
	if(roundedIncome < thresholds[2] && roundedIncome < 100000) {
	    if(filingStatus == 1) {
	        addend[0] = 5.5;
	        addend[1] = 6;
	    } else {
	        addend[0] = 6.5;
	        addend[1] = 6;
	    }
        if(roundedIncome % 100 == 0)
            return Math.round(totalTax + (roundedIncome - thresholds[1]) * rates[2] + addend[0]);
        else
            return Math.round(totalTax + (roundedIncome - thresholds[1]) * rates[2] + addend[1]);
	}
	totalTax += Math.ceil((thresholds[2] - thresholds[1]) * rates[2]);
	if(roundedIncome < thresholds[3] && roundedIncome < 100000)
	    return Math.round(totalTax + (roundedIncome - thresholds[2]) * rates[3] + 6);

    if(roundedIncome < thresholds[2]) {
        if(filingStatus == 4)
            return taxableIncome * rates[2] - 4937.5;
        else
            return taxableIncome * rates[2] - 7312.5;
    }

    if(roundedIncome < thresholds[3]) {
        switch(filingStatus) {
            case 1:
                return taxableIncome * rates[3] - 6021.75;
                break;
            case 4:
                return taxableIncome * rates[3] - 4937.5;
                break;
            default:
                return taxableIncome * rates[3] - 11256;
        }
    }
    if(roundedIncome < thresholds[4]) {
        switch(filingStatus) {
            case 1:
                return taxableIncome * rates[4] - 14249.25;
                break;
            case 4:
                return taxableIncome * rates[4] - 17437;
                break;
            default:
                return taxableIncome * rates[4] - 21271;
        }
    }
    switch(filingStatus) {
        case 1:
            return taxableIncome * rates[5] - 21403.25;
            break;
        case 4:
            return taxableIncome * rates[5] - 24591;
            break;
        default:
            return taxableIncome * rates[5] - 28425;
    }
}

function EICcalculator(earnedIncome, nChildren, isMarried) {
	var lowEICAmounts = new Array(2, 6, 10, 13, 17, 21, 25, 29, 33, 36, 40, 44, 
								48, 52, 55, 59, 63, 67, 71, 75, 78, 82, 86, 90,
								94, 98, 101, 105, 109, 113, 117, 120, 124, 128, 132, 136, 
								140, 143, 147, 151, 155, 159, 163, 166, 170, 174, 178, 182, 
								186, 189, 193, 197, 201, 205, 208, 212, 216, 220, 224, 228, 
								231, 235, 239, 243, 247, 251, 254, 258, 262, 266, 270, 273,
								277, 281, 285, 289, 293, 296, 300, 304, 308, 312, 316, 319, 
								323, 327, 331, 335, 339, 342, 346, 350, 354, 358, 361, 365, 
								369, 373, 377, 381, 384, 388, 392, 396, 400, 404, 407, 411,
								415, 419, 423, 426, 430, 434);
	var highEICAmounts = new Array(1, 4, 8, 12, 16, 20, 23, 27, 31, 35, 39, 42, 46, 50, 54, 58, 62, 65, 
	                            69, 73, 77, 81, 85, 88, 92, 96, 100, 104, 107, 111, 115, 119, 123, 
	                            127, 130, 134, 138, 142, 146, 150, 153, 157, 161, 165, 169, 173, 176, 180, 
	                            184, 188, 192, 195, 199, 203, 207, 211, 215, 218, 222, 226, 230, 234, 238, 
	                            241, 245, 249, 253, 257, 260, 264, 268, 272, 276, 280, 283, 287, 291, 295, 
	                            299, 303, 306, 310, 314, 318, 322, 326, 329, 333, 337, 341, 345, 348, 352, 
	                            356, 360, 364, 368, 371, 375, 379, 383, 387, 391, 394, 398, 402, 406, 410, 
	                            413, 417, 421, 425, 429, 433);

	if(nChildren >= 2) {
		if(earnedIncome > maxIncomeEIC[0])
			return 0;
		else if(isMarried == false && earnedIncome > maxIncomeEIC[1])
			return 0;
		else if(earnedIncome > 12050 && earnedIncome <= 15750)
			return 4824;
		else if(isMarried == true && earnedIncome > 15750 && earnedIncome <= 18750)
			return 4824;
		else if(earnedIncome <= 12050)
			return parseInt(earnedIncome / 50) * 20 + 10;
		else {
			var twoChildEICAmounts = new Array(4817, 4806, 4796, 4785, 4775, 4764, 4753, 4743, 4732, 4722, 
											4711, 4701, 4690, 4680, 4669, 4659, 4648, 4638, 4627, 4617,
											4606, 4595, 4585, 4574, 4564, 4553, 4543, 4532, 4522, 4511,
											4501, 4490, 4480, 4469, 4459, 4448, 4438, 4427, 4416, 4406,
											4395, 4385, 4374, 4364, 4353, 4343, 4332, 4322, 4311, 4301,
											4290, 4280, 4269, 4259, 4248, 4237, 4227, 4216, 4206, 4195,
											4185, 4174, 4164, 4153, 4143, 4132, 4122, 4111, 4101, 4090,
											4080, 4069, 4058, 4048, 4037, 4027, 4016, 4006, 3995, 3985,
											3974, 3964, 3953, 3943, 3932, 3922, 3911, 3901, 3890, 3879,
											3869, 3858, 3848, 3837, 3827, 3816, 3806, 3795, 3785, 3774,
											3764, 3753, 3743, 3732, 3722, 3711, 3700, 3690, 3679, 3669,
											3658, 3648, 3637, 3627, 3616, 3606, 3595, 3585, 3574, 3564,
											3553, 3542, 3532, 3521, 3511, 3500, 3490, 3479, 3469, 3458,
											3448, 3437, 3427, 3416, 3406, 3395, 3385, 3374, 3363, 3353,
											3342, 3332, 3321, 3311, 3300, 3290, 3279, 3269, 3258, 3248,
											3237, 3227, 3216, 3206, 3195, 3184, 3174, 3163, 3153, 3142,
											3132, 3121, 3111, 3100, 3090, 3079, 3069, 3058, 3048, 3037,
											3027, 3016, 3005, 2995, 2984, 2974, 2963, 2953, 2942, 2932,
											2921, 2911, 2900, 2890, 2879, 2869, 2858, 2848, 2837, 2826,
											2816, 2805, 2795, 2784, 2774, 2763, 2753, 2742, 2732, 2721,
											2711, 2700, 2690, 2679, 2669, 2658, 2647, 2637, 2626, 2616,
											2605, 2595, 2584, 2574, 2563, 2553, 2542, 2532, 2521, 2511,
											2500, 2489, 2479, 2468, 2458, 2447, 2437, 2426, 2416, 2405,
											2395, 2384, 2374, 2363, 2353, 2342, 2332, 2321, 2310, 2300,
											2289, 2279, 2268, 2258, 2247, 2237, 2226, 2216, 2205, 2195,
											2184, 2174, 2163, 2153, 2142, 2131, 2121, 2110, 2100, 2089,
											2079, 2068, 2058, 2047, 2037, 2026, 2016, 2005, 1995, 1984,
											1974, 1963, 1952, 1942, 1931, 1921, 1910, 1900, 1889, 1879,
											1868, 1858, 1847, 1837, 1826, 1816, 1805, 1795, 1784, 1773,
											1763, 1752, 1742, 1731, 1721, 1710, 1700, 1689, 1679, 1668,
											1658, 1647, 1637, 1626, 1616, 1605, 1594, 1584, 1573, 1563,
											1552, 1542, 1531, 1521, 1510, 1500, 1489, 1479, 1468, 1458,
											1447, 1436, 1426, 1415, 1405, 1394, 1384, 1373, 1363, 1352,
											1342, 1331, 1321, 1310, 1300, 1289, 1279, 1268, 1257, 1247,
											1236, 1226, 1215, 1205, 1194, 1184, 1173, 1163, 1152, 1142,
											1131, 1121, 1110, 1100, 1089, 1078, 1068, 1057, 1047, 1036,
											1026, 1015, 1005, 994, 984, 973, 963, 952, 942, 931,
											921, 910, 899, 889, 878, 868, 857, 847, 836, 826,
											815, 805, 794, 784, 773, 763, 752, 742, 731, 720,
											710, 699, 689, 678, 668, 657, 647, 636, 626, 615,
											605, 594, 584, 573, 563, 552, 541, 531, 520, 510,
											499, 489, 478, 468, 457, 447, 436, 426, 415, 405,
											394, 383, 373, 362, 352, 341, 331, 320, 310, 299,
											289, 278, 268, 257, 247, 236, 226, 215, 204, 194,
											183, 173, 162, 152, 141, 131, 120, 110, 99, 89,
											78, 68, 57, 47, 36, 25, 15, 5);
			var arrayElement;
			if(isMarried)
				arrayElement = parseInt((earnedIncome-18750)/50);
			else
				arrayElement = parseInt((earnedIncome-15750)/50);
			return twoChildEICAmounts[arrayElement];
		}
	} else if(nChildren == 1) {
		if(earnedIncome > maxIncomeEIC[2])
			return 0;
		else if(isMarried == false && earnedIncome > maxIncomeEIC[3])
			return 0;
		else if(earnedIncome > 8550 && earnedIncome <= 15750)
			return 2917;
		else if(isMarried == true && earnedIncome > 15750 && earnedIncome <= 18750)
			return 2917;
		else if(earnedIncome <= 8550)
			return parseInt(earnedIncome / 50) * 17 + 9;
		else {
			var x = parseInt(earnedIncome / 50);
			if(isMarried) {
				if(x >= 375 && x <= 464)
					return 5912 - 8 * x;
				if(x >= 465 && x <= 564)
					return 5913 - 8 * x;
				if(x >= 565 && x <= 664)
					return 5914 - 8 * x;
				if(x >= 665 && x <= 738)
					return 5915 - 8 * x;
				if(x == 739)
					return 4;
			} else {
				if(x >= 315 && x <= 404)
					return 5432 - 8 * x;
				if(x >= 405 && x <= 504)
					return 5433 - 8 * x;
				if(x >= 505 && x <= 604)
					return 5434 - 8 * x;
				if(x >= 605 && x <= 678)
					return 5435 - 8 * x;
				if(x == 679)
					return 4;
			}
		}
	}
									
	if(earnedIncome > 0 && earnedIncome < 5650)
		return lowEICAmounts[parseInt(earnedIncome/50)];
	if(earnedIncome >= 5650 && earnedIncome < 7200)
		return 438;
	if(isMarried == true && earnedIncome >= 5650 && earnedIncome < 10200)
		return 438;
	if(isMarried == false && earnedIncome >= 7200 && earnedIncome < maxIncomeEIC[5])
		return highEICAmounts[parseInt((maxIncomeEIC[5]-0.01-earnedIncome)/50)];
	if(isMarried == true && earnedIncome >= 10200 && earnedIncome < maxIncomeEIC[4])
		return highEICAmounts[parseInt((maxIncomeEIC[4]-0.01-earnedIncome)/50)];
	return 0;
}

function calculateEIC(wages, combat, interest, unemployment, children) {
	var isMarried = false;
	var wc = wages + combat;
	var wiu = wages + interest + unemployment;

    if(getElement("maritalStatus").substring(0, 7) == "married")
	    isMarried = true;

    if(getElement("canBeClaimed0") == "Yes" || getElement("residency0") == "No" ||
        (getElement("age0") != "25to49" && getElement("age0") != "50to64"))
        return 0;

    if(isMarried == true && (getElement("canBeClaimed1") == "Yes" || getElement("residency1") == "No" ||
        (getElement("age1") != "25to49" && getElement("age1") != "50to64")))
        return 0;
        
	if(children >= 2) {
		if(wiu < 14850 || (wiu < 16850 && isMarried == true))
		    return Math.max(EICcalculator(wages, children, false), EICcalculator(wc, children, false));
		else if(wiu >= 14600 && (wages < maxIncomeEIC[1] || (isMarried == true && wages < maxIncomeEIC[0])))
		    return Math.max(EICcalculator(wages, children, isMarried), EICcalculator(wiu, children, isMarried));
		else
			return 0;
	} else if(children == 1) {
		if(wiu < 14850 || (wiu < 16850 && isMarried == true))
		    return Math.max(EICcalculator(wages, children, false), EICcalculator(wc, children, false));
		else if(wiu >= 14600 && (wages < maxIncomeEIC[3] || (isMarried == true && wages < maxIncomeEIC[2])))
		    return Math.max(EICcalculator(wages, children, isMarried), EICcalculator(wiu, children, isMarried));
		else
			return 0;
	} else {            //No kiddies
		if(wiu < 7000 || (wiu < 9000 && isMarried == true))
		    return Math.max(EICcalculator(wages, children, false), EICcalculator(wc, children, false));
		else if(wiu >= 6400 && (wages < maxIncomeEIC[5] || (isMarried == true && wages < maxIncomeEIC[4])))
		    return Math.max(EICcalculator(wages, children, isMarried), EICcalculator(wiu, children, isMarried));
		else
			return 0;
	}
}

function changeSummaryLineNumbers(formType) {
    for(var i = 0; i < 18; i++) cellDisplay("noEZ" + i, (formType != 2));

    cellDisplay("EZonly0", (formType == 2));
    cellDisplay("EZonly1", (formType == 2));
    setElement("heading", "Line number on form 1040" + (formType==1 ? "A" : (formType==2 ? "EZ" : "")));
    setElement("totalIncomeLine", totalIncomeLine[formType]);
    setElement("totDeductionLine", totDeductionLine[formType]);
    setElement("AGILine", AGILine[formType]);
    setElement("over65Line", over65Line[formType]);
    setElement("propTaxLine", propTaxLine[formType]);
    setElement("stdDeductLine", stdDeductLine[formType]);
    setElement("Line41Label", Line41[formType]);
    setElement("Line41Description", "Subtract line " + stdDeductLine[formType] + " from line " + AGILine[formType]);
    setElement("exemptionLine", exemptionLine[formType]);
    setElement("taxIncomeLine", taxIncomeLine[formType]);
    setElement("taxLine", taxLine[formType]);
    setElement("childCreditLine", childCreditLine[formType]);
    setElement("taxLine2", taxLine2[formType]);
    setElement("taxDescription2", "Subtract line " + childCreditLine[formType] + " from line " + taxLine[formType]);
    setElement("taxLine3", taxLine3[formType]);
    setElement("paymentLine", paymentLine[formType]);
    setElement("refundLine", refundLine[formType]);
    setElement("oweLine", oweLine[formType]);
    setElement("summaryW2Line", W2Line[formType]);
    setElement("summaryIntLine", intLine[formType]);
    setElement("summaryExemptIntLine", exemptIntLine[formType]);
    setElement("summaryDivLine", dividendLine[formType]);
    setElement("summaryQualDivLine", qualDividendLine[formType]);
    setElement("summaryUELine", ueLine[formType]);
    setElement("summarySSLine", ssLine[formType]);
    setElement("summarySSTaxLine", ssLine[formType].replace("a", "b"));
    setElement("summaryEducatorLine", educatorLine[formType]);
    setElement("summaryIRALine", IRALine[formType]);
    setElement("summaryStudentLine", studentLine[formType]);
    setElement("summaryTuitionLine", tuitionLine[formType]);
    setElement("summaryAdvEICLine", advEICLine[formType]);
    setElement("summaryWithholdLine", withholdLine[formType]);
    setElement("EICLine", EICLine[formType]);
    setElement("summaryCombatLine", combatLine[formType]);
    if(formType==2) setElement("filingStatusNum", "N/A");
}

function calculateAndShowSummary() {
    var elementId;
    var cellNum;
    var W2Total = 0;
    var intTotal = 0;
    var exemptIntTotal = 0;
    var divTotal = 0;
    var qualDivTotal = 0;
    var ueTotal = 0;
    var ssTotal = 0;
    var ssTaxable = 0;
    var educatorVal = new Array(0, 0);
    var IRAVal = new Array(0, 0);
    var studentVal = 0;
    var tuitionVal = 0;
    var box13Checked = new Array(false, false);
    var withholdTotal = 0;
    var advEICTotal = 0;
    var combatTotal = 0;
    var blindOver65Total = 0;
    var dependentTotal = 0;
    var dependentChildTotal = 0;
    var dependentInHouse = 0;
    var dependentElsewhere = 0;
    var childCredit = 0;
    var fullYearChild = false;
    var HOHDependent = false;
    var amt, sDeduction, filingStatusNum;
    var canFile1040A = true;
    var canFile1040EZ = true;
    var showSchedule1 = false;
    var showForm8917 = false;
    var dollarsInterest = new Array();
    var dollarsDividend = new Array();
    var studentName = "";
    var studentSSN = "";
    dollars1040EZ.length = 0;
    dollars1040A.length = 0;
    dependentInfo.length = 0;
    dollarsScheduleEIC.length = 0;
    studentWorksheet.length = 0;
    form8917.length = 0;
    form8917.push("", "", "", "", "", "", "");
    cellDisplay("summaryCannotDo", false);
    cellDisplay("summaryView", false);
    cellDisplay("summaryPreview", true);
    changeSummaryLineNumbers(1);

    var mStatus = getElement("maritalStatus");
    if(mStatus.substr(0, 7) == "married") {
        filingStatusNum = 2;
        setElement("filingStatus", "Married Filing Jointly");
    } else if(mStatus == "widow" && getElement("residencyPay") == "Yes" && fullYearChild == true) {
        filingStatusNum = 5;
        setElement("filingStatus", "Qualifying Widow(er)");
        canFile1040EZ = false;
    } else if(mStatus == "single" && (getElement("parentPay") == "Yes" || (getElement("residencyPay") == "Yes" && HOHDependent == true))) {
        filingStatusNum = 4;
        setElement("filingStatus", "Head of Household");
        canFile1040EZ = false;
    } else {
        filingStatusNum = 1;
        setElement("filingStatus", "Single");
    }
    dollars1040A.push((filingStatusNum == 1) ? "X" : "");
    dollars1040A.push((filingStatusNum == 2) ? "X" : "");
    dollars1040A.push((filingStatusNum == 4) ? "X" : "");
    dollars1040A.push((filingStatusNum == 5) ? "X" : "");
    setElement("filingStatusNum", filingStatusNum);

    if(getElement("canBeClaimed0") == "No") {
        dependentTotal++;
        dollars1040A.push("X");
    } else
        dollars1040A.push("");

    if(getElement("blind0") == "Yes") blindOver65Total++;
    if(getElement("age0").indexOf("70") > -1) blindOver65Total++;

    if(mStatus == "married1") {
        if(getElement("canBeClaimed1") == "No") {
            dependentTotal++;
            dollars1040A.push("X");
        } else
            dollars1040A.push("");

        if(getElement("blind1") == "Yes") blindOver65Total++;
        if(getElement("age1").indexOf("70") != -1) blindOver65Total++;
    } else
        dollars1040A.push("");
    dollars1040A.push(dependentTotal);

    if(blindOver65Total > 0) canFile1040EZ = false;

    for(var i=0; i < document.forms[0].elements.length; i++) {
        elementId = document.forms[0].elements[i].id;
        cellNum = elementId.substr(1, 2);
        if(elementId.substr(0, 1) == "n" && isNaN(cellNum) == false) {
            amt = parseMoney(getElement(elementId));
            if(elementId.substr(3) == "SSN") {
                var relation = getElement("n" + cellNum + "relation");
                var months = parseInt(getElement("n" + cellNum + "months"));
                var decree = getElement("n" + cellNum + "decree");
                var age = getElement("n" + cellNum + "DCOB");
                age = getElement("n" + cellNum + "DYOB" + age);
                var YOB = getElement("n" + cellNum + "DCOB");
                YOB += getElement("n" + cellNum + "DYOB" + YOB, true);
                if(isNaN(age)) YOB = ""; else YOB = YOB.substr(0, 4);
                var support = getElement("n" + cellNum + "support");
                var cc = false;
                var gender = document.getElementById("n" + cellNum + "gender").selectedIndex;
                var relationType;
                var dependentSSN = getElement(elementId);
                    
                if(months == 12 && (relation == "child1" || relation == "child6"))
                    fullYearChild = true;

                if(relation.substr(0, 5) == "child" && support == "1" && months > 6)
                    if(age.length == 1 || age < "18" || getElement("adultClaim" + cellNum) != "No")
                        HOHDependent = true;
                if(dependentQualifies(relation, elementId.substr(1, 2), "") == "") {
                    dependentTotal++;
                    canFile1040EZ = false;
                    dependentInfo.push(getElement("n" + cellNum + "name"));
                    dollarsScheduleEIC.push(getElement("n" + cellNum + "name"));
                    relationType = getElement("n" + cellNum + "relation", true);
                    if(relationType.indexOf("/") != -1) {
                        var relations = relationType.split("/");
                        if(relations.length != gender)
                            relationType = relations[gender];
                    }
                    dependentInfo.push(relationType);
                    if(relation.substr(0, 5) == "child")
                        if(age.length == 1 || age <"17")
                            if(support != "0")
                                if(months > 6)
                                    cc = true;
                    if(cc) childCredit++;
                    dependentInfo.push(cc ? "X" : "");
                    pushSSNontoArray(dependentSSN, dependentInfo);
                    pushSSNontoArray(dependentSSN, dollarsScheduleEIC);
                    dollarsScheduleEIC.push(YOB);
                    if(YOB == "") {
                        if(getElement("adultClaim" + cellNum) == "YesS") {
                            dollarsScheduleEIC.push("X", "", "", "");
                        } else if(getElement("adultClaim" + cellNum) == "YesD") {
                            dollarsScheduleEIC.push("", "X", "X", "");
                        } else {
                            dollarsScheduleEIC.push("", "X", "", "X");
                        }
                    } else {
                        dollarsScheduleEIC.push("", "", "", "");
                    }
                    dollarsScheduleEIC.push(relationType);
                    dollarsScheduleEIC.push(months);
                    if(relation.substr(0, 5) == "child") dependentChildTotal++;
                    else dollarsScheduleEIC.length = dollarsScheduleEIC.length - 6;
                    if(months < 7) dependentElsewhere++;
                    else dependentInHouse++;
                }
            } else if(!isNaN(amt)) {
                switch(elementId.substr(3)) {
                    case W2Line[0]:
                        W2Total += amt;
                        if(getElement("n" + cellNum + "ret") == true) {
                            if(getElement("n" + cellNum + "earner") == "spouse")
                                box13Checked[1] = true;
                            else
                                box13Checked[0] = true;
                        }
                        break;
                    case intLine[0]:
                        intTotal += amt;
                        dollarsInterest.push(getElement("n" + cellNum + "name"));
                        dollarsInterest.push(amt);
                        break;
                    case exemptIntLine[0]: exemptIntTotal += amt; break;
                    case ueLine[0]: ueTotal += amt; break;
                    case ssLine[0]: ssTotal += amt; break;
                    case dividendLine[0]: 
                        divTotal += amt;
                        dollarsDividend.push(getElement("n" + cellNum + "name"));
                        dollarsDividend.push(amt);
                        if(getElement("n" + cellNum + "ownership") == "2") showSchedule1 = true;
                        break;
                    case qualDividendLine[0]: 
                        if(getElement("ownership" + cellNum) <= "1") {
                            divTotal -= amt;
                            qualDivTotal += amt;
                        }
                        if(getElement("n" + cellNum + "ownership") == "2") showSchedule1 = true;
                        break;
                    case advEICLine[0]: advEICTotal += amt; break;
                    case withholdLine[0]: withholdTotal += amt; break;
                    case combatLine[0]: combatTotal += amt; break;
                    case educatorLine[0]:
                        if((getElement("educatorGrade" + cellNum) + getElement("educatorHours" + cellNum)).indexOf("No") == -1)
                            spouseTotal(elementId, amt, educatorVal); break;
                    case IRALine[0]:
                        if(getElement("n" + cellNum + "IRADeductible") == "true")
                            spouseTotal(elementId, amt, IRAVal);
                        break;
                    case studentLine[0]: 
                        if((getElement("studentDependent" + cellNum) + getElement("studentProgram" + cellNum)).indexOf("0") == -1
                            && (getElement("studentCover" + cellNum) + getElement("studentLoad" + cellNum)).indexOf("No") == -1)
                            studentVal += amt;
                        break;
                    case tuitionLine[0]:
                        if(getElement("n" + cellNum + "courseStart") == "Deductible") {
                            tuitionVal += amt;
                            studentName = getElement("n" + cellNum + "earner", true);
                            studentSSN = getElement("n" + cellNum + "earner");
                            form8917.push(studentName);
                            if(studentSSN != studentName) pushSSNontoArray(studentSSN, form8917);
                            else pushSSNontoArray("", form8917);
                            form8917.push(amt);
                        }
                        break;
                }
            }
        }
    }
    form8917[0] = tuitionVal;
    if(intTotal >= 1500 || divTotal >= 1500) showSchedule1 = true;
    dollars1040A.push(dependentInHouse, dependentElsewhere, dependentTotal);
    dollars1040EZ.push(Math.round(W2Total));
    dollars1040A.push(Math.round(W2Total));
    dollars1040EZ.push(Math.round(intTotal));
    dollars1040A.push(Math.round(intTotal), Math.round(exemptIntTotal));
    dollars1040A.push(Math.round(divTotal), Math.round(qualDivTotal));
    ueTotal = Math.round(Math.max(0, ueTotal - 2400));  //For 2009 only
    dollars1040EZ.push(ueTotal);
    dollars1040A.push(ueTotal);
    if(ssTotal > 0) {
        canFile1040EZ = false;
        ssTaxable = Math.round(calculateTaxableSSAmt(ssTotal, W2Total, intTotal, divTotal, 
                                ueTotal, exemptIntTotal, 
                                Math.min(educatorLimit, educatorVal[0]) + Math.min(educatorLimit, educatorVal[1]), 
                                calculateIRATotal(W2Total + intTotal + ueTotal + divTotal, IRAVal, box13Checked), filingStatusNum));
    }
    dollars1040A.push(ssTotal);
    dollars1040A.push(ssTaxable);
    var totIncome = Math.round(W2Total) + Math.round(intTotal) + ueTotal + Math.round(divTotal) + ssTaxable;
    form8917[1] = totIncome;
    var netIncome = totIncome;
    dollars1040EZ.push(netIncome);
    dollars1040A.push(netIncome);
    IRAWorksheet[4] = netIncome;
    var educatorTotal = Math.min(educatorLimit, educatorVal[0]) + Math.min(educatorLimit, educatorVal[1]);
    IRAWorksheet[5] = Math.round(educatorTotal);
    dollars1040A.push(Math.round(educatorTotal));
    if(educatorTotal > 0) canFile1040EZ = false;
    netIncome -= educatorTotal;
    IRAWorksheet[6] = netIncome;
    if(filingStatusNum == 2) IRAWorksheet[7] = netIncome; else IRAWorksheet[7] = "";

    var IRATotal = 0;
    if(IRAVal[0] + IRAVal[1] > 0) {
        IRATotal = calculateIRATotal(netIncome, IRAVal, box13Checked);
        if(IRATotal > 0) canFile1040EZ = false;
    }
    dollars1040A.push(Math.round(IRATotal));
    netIncome -= IRATotal;
    
    var studentTotal = Math.round(Math.min(studentLimit, studentVal));
    studentWorksheet.push(studentTotal);
    if(studentTotal > 0) {
        canFile1040EZ = false;
        studentWorksheet.push(totIncome, netIncome);
        if(mStatus == "married1") {
            studentTotal = Math.round(studentTotal * studentLoanPhaseout(netIncome, studentLoanFullyDeductible[1], studentLoanNotDeductible[1]));
            studentWorksheet.push(studentLoanFullyDeductible[1]);
            studentWorksheet.push(studentLoanNotDeductible[1]);
        } else {
            studentTotal = Math.round(studentTotal * studentLoanPhaseout(netIncome, studentLoanFullyDeductible[0], studentLoanNotDeductible[0]));
            studentWorksheet.push(studentLoanFullyDeductible[0]);
            studentWorksheet.push(studentLoanNotDeductible[0]);
        }
    }

    dollars1040A.push(Math.round(studentTotal));
    netIncome -= studentTotal;
    form8917[3] = netIncome;
    form8917[2] = totIncome - netIncome;
    tuitionVal = tuitionTotalPhaseOut(tuitionVal, netIncome, filingStatusNum);
    form8917[4] = tuitionVal;
    if(tuitionVal > 0) {
        canFile1040EZ = false;
        showForm8917 = true;
    }
    netIncome -= tuitionVal;
    dollars1040A.push(Math.round(tuitionVal));
    dollars1040A.push(Math.round(educatorTotal) + Math.round(IRATotal) + Math.round(studentTotal) + Math.round(tuitionVal));
    dollars1040A.push(netIncome, blindOver65Total);
    if(getElement("age0").indexOf("70") > -1) dollars1040A.push("X");
    else dollars1040A.push("");
    if(getElement("blind0") == "Yes") dollars1040A.push("X");
    else dollars1040A.push("");
    if(mStatus == "married1") {
        if(getElement("age1").indexOf("70") != -1) dollars1040A.push("X");
        else dollars1040A.push("");
        if(getElement("blind1") == "Yes") dollars1040A.push("X");
        else dollars1040A.push("");
    } else {
        dollars1040A.push("");
        dollars1040A.push("");
    }

    if(filingStatusNum == 2 || filingStatusNum == 5)
        sDeduction = calculateDeduction(W2Total, stdDeduction[1]);
    else if(filingStatusNum == 4)
        sDeduction = calculateDeduction(W2Total, stdDeduction[2]);
    else
        sDeduction = calculateDeduction(W2Total, stdDeduction[0]);

    if(getElement("ownHome") == "Yes" && isNaN(getElement("propTax")) == false && getElement("propTax") > 0) {
        setElement("propTaxCheck", true);
        dollars1040A.push("X");
        canFile1040EZ = false;
        if(mStatus == "married1")
            sDeduction += (blindOver65Total * 1050 + Math.min(1000, parseMoney(getElement("propTax"))));
        else
            sDeduction += (blindOver65Total * 1350 + Math.min(500, parseMoney(getElement("propTax"))));
    } else {
        setElement("propTaxCheck", false);
        dollars1040A.push("");
        if(mStatus == "married1")
            sDeduction += (blindOver65Total * 1050);
        else
            sDeduction += (blindOver65Total * 1350);
    }
    dollars1040A.push(sDeduction);

    if(intTotal > 1500 || advEICTotal > 0 || divTotal > 0 || qualDivTotal > 0) canFile1040EZ = false;

    setElement("summaryW2Total", Math.round(W2Total));
    setElement("summaryIntTotal", Math.round(intTotal));
    setElement("summaryExemptIntTotal", Math.round(exemptIntTotal));
    setElement("summaryDivTotal", Math.round(divTotal));
    setElement("summaryQualDivTotal", Math.round(qualDivTotal));
    setElement("summaryUETotal", Math.round(ueTotal));
    setElement("summarySSTotal", Math.round(ssTotal));
    setElement("summarySSTaxTotal", ssTaxable);
    setElement("totalIncome", totIncome);
    setElement("summaryEducatorTotal", Math.round(educatorTotal));
    setElement("summaryIRATotal", Math.round(IRATotal));
    setElement("summaryStudentTotal", Math.round(studentTotal));
    setElement("summaryTuitionTotal", Math.round(tuitionVal));
    setElement("totalDeductions", totIncome - netIncome);
    if(netIncome > maxIncome) {
        cellDisplay("summaryCannotDo", true);
        cellDisplay("summaryPreview", false);
        return;
    } 
    setElement("AGI", netIncome);
    setElement("blindOver65", blindOver65Total);
    setElement("stdDeduction", sDeduction);
    netIncome = Math.max(0, netIncome - sDeduction);
    setElement("Line41", netIncome);
    dollars1040A.push(netIncome);
    setElement("dependentAmt", dependentTotal * dependentAmt);
    dollars1040A.push(dependentTotal * dependentAmt);
    setElement("deductExempt", sDeduction + dependentTotal * dependentAmt);
    dollars1040EZ.push(Math.round(sDeduction + dependentTotal * dependentAmt));
    setElement("taxableIncome", (netIncome = Math.max(0, netIncome - (dependentTotal * dependentAmt))));
    dollars1040EZ.push(Math.round(netIncome));
    dollars1040A.push(Math.round(netIncome));
    if(netIncome > 100000) canFile1040EZ = false;
    var taxLiability = calculateTax(netIncome, filingStatusNum);
    setElement("taxLiability", taxLiability);
    dollars1040A.push(taxLiability);
    setElement("childCreditScript", "<a href='javascript:popChildCreditSheet(" + childCredit + ", " + taxLiability + ", 1);'>View Child Credit Worksheet</a>");
    childCredit = Math.min(taxLiability, childCredit * childCreditAmt);
    cellDisplay("childCreditLink", (childCredit > 0));
    if(childCredit > 0) {
        if(filingStatusNum == 2 || netIncome <= 75000) {
            setElement("childCredit", childCredit);
            taxLiability -= childCredit;
        } else
            setElement("childCredit", "Must fill out Pub 972");
    } else setElement("childCredit", "0");
    dollars1040A.push(getElement("childCredit"), getElement("childCredit"));
    setElement("tax2", taxLiability);
    dollars1040A.push(taxLiability);
    setElement("summaryAdvEICTotal", advEICTotal);
    dollars1040A.push(advEICTotal);
    setElement("tax3", (taxLiability += advEICTotal));
    dollars1040A.push(taxLiability);
    setElement("summaryWithholdTotal", withholdTotal);
    dollars1040EZ.push(Math.round(withholdTotal));
    dollars1040A.push(Math.round(withholdTotal));
    if(W2Total < maxIncomeEIC[4-2*dependentChildTotal] && intTotal + exemptIntTotal + divTotal < 2950)
        var EICAmt = calculateEIC(W2Total, combatTotal, intTotal, ueTotal, dependentChildTotal);
    else
        EICAmt = 0;

    setElement("summaryEICAmount", EICAmt);
    dollars1040EZ.push(Math.round(EICAmt));
    dollars1040A.push(Math.round(EICAmt));
    setElement("summaryCombatTotal", Math.round(combatTotal));
    dollars1040EZ.push(Math.round(combatTotal));
    dollars1040A.push(Math.round(combatTotal));
    withholdTotal += EICAmt;
    dollars1040EZ.push(Math.round(withholdTotal));
    dollars1040A.push(Math.round(withholdTotal));
    dollars1040EZ.push(Math.round(taxLiability));
    dollars1040EZ.push(Math.round(Math.max(0, withholdTotal - taxLiability)));
    dollars1040EZ.push(Math.round(Math.max(0, taxLiability - withholdTotal)));
    dollars1040A.push(Math.round(Math.max(0, withholdTotal - taxLiability)));
    dollars1040A.push(Math.round(Math.max(0, withholdTotal - taxLiability)));
    dollars1040A.push(Math.round(Math.max(0, taxLiability - withholdTotal)));
    dollars1040EZ.push((getElement("canBeClaimed0") == "No") ? "" : "X");
    dollars1040EZ.push((getElement("maritalStatus") != "married1" || getElement("canBeClaimed1") == "No") ? "" : "X");
    IRAWorksheet[12] = Math.round(W2Total) + Math.round(combatTotal);
    IRAWorksheet[13] = IRAVal[0];
    IRAWorksheet[14] = IRAVal[1];
	if(IRAWorksheet[10] == "" || IRAVal[0] == 0) IRAWorksheet[15] = "0"; else IRAWorksheet[15] = Math.min(IRAWorksheet[10], Math.min(IRAWorksheet[12], IRAWorksheet[13]));
	if(IRAWorksheet[11] == "" || IRAVal[1] == 0) IRAWorksheet[16] = "0"; else IRAWorksheet[16] = Math.min(IRAWorksheet[11], Math.min(IRAWorksheet[12], IRAWorksheet[14]));

    setElement("totalPayments", withholdTotal);
    if(withholdTotal >= taxLiability) {
        setElement("refund", withholdTotal - taxLiability);
        setElement("owed", "");
    } else {
        setElement("refund", "");
        setElement("owed", taxLiability - withholdTotal);
    }

    if(canFile1040EZ) changeSummaryLineNumbers(2);
    cellDisplay("showIfOldOrBlind", (blindOver65Total > 0));
    if(blindOver65Total > 0) {
        setElement("oldBlind2", (getElement("blind0") == "Yes"));
        setElement("oldBlind1", (getElement("age0").indexOf("70") > -1));
        setElement("oldBlind4", (mStatus == "married1" && getElement("blind1") == "Yes"));
        setElement("oldBlind3", (mStatus == "married1" && getElement("age1").indexOf("70") > -1));
    }
    cellDisplay("summaryView", true);
    cellDisplay("schedule1Link", showSchedule1);
    cellDisplay("scheduleEICLink", EICAmt > 0 && dollarsScheduleEIC.length > 0);
    cellDisplay("form8917Link", showForm8917);
    cellDisplay("SSWorksheet", ssTotal > 0);
    cellDisplay("DeductionWorksheet", (IRAVal[0] + IRAVal[1] + studentTotal > 0));
    if(showSchedule1) {
        dollarsSchedule1.length = 0;
        for (var i = 0; i < dollarsInterest.length; i++) dollarsSchedule1.push(dollarsInterest[i]);
        while (dollarsSchedule1.length < 20) dollarsSchedule1.push("");
        dollarsSchedule1.push(Math.round(intTotal));
        dollarsSchedule1.push(Math.round(intTotal));
        for (var i = 0; i < dollarsDividend.length; i++) dollarsSchedule1.push(dollarsDividend[i]);
        while (dollarsSchedule1.length < 42) dollarsSchedule1.push("");
        dollarsSchedule1.push(Math.round(divTotal));
    }
    cellDisplay("summaryPreview", false);
}

function popChildCreditSheet(qualChildren, taxesOwed, formNum) {
    var taxLn = taxLine[formNum];
    var HTML = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title></head>";
    if(taxLn.length > 2) taxLn = parseInt(taxLn.substr(taxLn.length - 2));
    else taxLn = parseInt(taxLn);
    HTML += "<body><h3>Child Tax Credit Worksheet</h3><h5>For your records only</h5><table border='0'><tr>";
    HTML += "<td colspan='3'>1. Number of qualifying children <u>" + qualChildren + "</u> X $" + childCreditAmt + ". Enter the result.</td>";
    HTML += "<td style='border-width:thin; border-style:solid;text-align:right'><tt>1</tt></td><td style='border-width:thin; border-style:solid;text-align:right'>" + (qualChildren * childCreditAmt) + "</td></tr>";
    HTML += "<tr><td>2. Enter the amount from Form " + formName[formNum] + ", line " + taxLn;
    HTML += "</td><td style='border-width:thin; border-style:solid;text-align:right'><tt>2</tt></td><td style='border-width:thin; border-style:solid;text-align:right'>" + taxesOwed + "</td></tr>";
    HTML += "<tr><td>3. Add the amounts from Form " + formName[formNum] + ":</td></tr>";
    HTML += "<tr><td> &nbsp; Line " + (taxLn + 1) + " &nbsp; <u>0</u></td></tr>";
    HTML += "<tr><td> &nbsp; Line " + (taxLn + 2) + " &nbsp; <u>0</u></td></tr>";
    HTML += "<tr><td> &nbsp; Line " + (taxLn + 3) + " &nbsp; <u>0</u></td></tr>";
    HTML += "<tr><td> &nbsp; Line " + (taxLn + 4) + " &nbsp; <u>0</u></td>";
    if(formNum == 0) HTML += "</tr><tr><td> &nbsp; Line " + (taxLn + 5) + " &nbsp; <u>0</u></td>";
    HTML += "<td style='border-width:thin; border-style:solid;text-align:right'><tt>3</tt></td><td style='border-width:thin; border-style:solid;text-align:right'>0</td></tr>";
    HTML += "<tr><td>4. Are the amounts on line 2 and 3 the same?</td></tr>";
    HTML += "<tr><td><b>Yes</b><br/>You cannot take this credit because there is no tax<br/>to reduce. However you may be able to take the<br/><b>additional child tax credit.</b>If you complete the form 8812.</td></tr>";
    HTML += "<tr><td colspan='3'><b>No.</b> Subtract line 3 from line 2.</td><td style='border-width:thin; border-style:solid;text-align:right'><tt>4</tt></td><td style='border-width:thin; border-style:solid;text-align:right'>" + taxesOwed + "</td></tr>";
    HTML += "<tr><td colspan='2'>5. Is the amount on line 1 more than the amount on line 4?<br/><b>Yes.</b> Enter the amount on line 4.<br/>Also you may be able to take the<br/><b>additional child tax credit.</b>If you complete the form 8812.<br/><b>No.</b> Enter the amount from Line 1.</td>";
    HTML += "<td><b>This is your child tax credit.</b></td><td style='border-width:thin; border-style:solid;text-align:right'><tt>5</tt></td><td style='border-width:thin; border-style:solid;text-align:right'><div style='font-size:xx-large;font-weight:bold'>";
    HTML += Math.min(qualChildren * childCreditAmt, taxesOwed) + "</div>Enter this amount on<br/>Form ";
    HTML += formName[formNum] + ", line " + childCreditLine[formNum] + "</td></tr></table>";
    HTML += "</body>";
    var taxForm = window.open("", "Child Tax Credit Worksheet", "");
    taxForm.document.write(HTML);
    taxForm.document.close();
}

function popDeductionWorksheet() {
    var HTML = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title></head><body>";
    var phaseIn = new Array(0, 0, 0, 0);
    var multiplier;
    if(IRAWorksheet[13] + IRAWorksheet[14] > 0) {
        phaseIn[0] = IRANotDeductible[0] - IRAFullyDeductible[0];
        phaseIn[1] = IRANotDeductible[2] - IRAFullyDeductible[2];
        phaseIn[2] = Math.round(IRALimit[0] * 100 / phaseIn[0]);
        phaseIn[3] = Math.round(IRALimit[1] * 100 / phaseIn[0]);
        HTML += "<h3>IRA Deduction Worksheet</h3><h5>For your records only</h5><table border='1'>";
        HTML += "<tr><td colspan='2'></td><td><b>Your IRA</b></td><td><b>Spouse's IRA</b></td></tr>";
        HTML += "<tr><td colspan='2'>1a. Were you covered by a retirement plan?</td><td style='text-align:right'>" + IRAWorksheet[0] + "</td><td></td></tr>";
        HTML += "<tr><td colspan='3'>1b. If married filing jointly, was your spouse covered by a retirement plan?</td><td style='text-align:right'>" + IRAWorksheet[1] + "</td></tr>";
        HTML += "<tr><td colspan='4'><b>Next.</b> If you checked 'No' on line 1a (and 'No' on line 1b if married filing jointly), skip lines 2 through 6, enter the applicable amount below on line 7a (and line 7b if applicable), and go to line 8.<br />";
        HTML += "<ul><li>$" + IRALimit[0] + ", if under 50 at the end of " + currentTaxYear + ".</li>";
        HTML += "<li>$" + IRALimit[1] + ", if age 50 or older but under age 70 1/2 at the end of " + currentTaxYear + ".</li></ul>Otherwise, go to line 2.</td></tr>";
        HTML += "<tr><td colspan='2'>2. Enter the amount shown below that applies to you.<br />";
        HTML += "<ul><li>Single or head of household, enter $" + IRANotDeductible[0] + "</li>";
        HTML += "<li>Qualifying widow(er) enter $" + IRANotDeductible[2] + "</li>";
        HTML += "<li>Married filing jointly, enter $" + IRANotDeductible[2] + " in both columns. But if you checked 'No' on either line 1a or line 1b, enter $" + IRANotDeductible[1] + " for the person who was not covered by a plan</li></ul></td>";
        HTML += "<td style='text-align:right'>" + IRAWorksheet[2] + "</td><td style='text-align:right'>" + IRAWorksheet[3] + "</td></tr>";
        HTML += "<tr><td>3. Enter the amount from Form 1040A, line 15</td><td style='text-align:right'>" + IRAWorksheet[4] + "</td><td></td><td></td><tr>";
        HTML += "<tr><td>4. Enter the amount, if any, from Form 1040A, line 16</td><td style='text-align:right'>" + IRAWorksheet[5] + "</td><td></td><td></td></tr>";
        HTML += "<tr><td colspan='2'>5. Subtract line 4 from line 3. If married filing jointly, enter the result in both columns</td><td>" + IRAWorksheet[6] + "</td><td>" + IRAWorksheet[7] + "</td></tr>";
        HTML += "<tr><td colspan='2'>6. Is the amount on line 5 less than the amount on line 2?<br />";
        if(IRAWorksheet[6] >= IRAWorksheet[2] && IRAWorksheet[7] >= IRAWorksheet[3]) HTML += "<b>√</b>";
        HTML += "<b>No</b> None of your IRA contributions are deductible. For details on nondeductible IRA contributions, see Form 8606.<br/>";
        if(IRAWorksheet[6] < IRAWorksheet[2] || IRAWorksheet[7] < IRAWorksheet[3]) HTML += "<b>√</b>";
        HTML += "<b>Yes</b> Subtract line 5 from line 2 in each column. Follow the instruction below that applies to you.<br/>";
        HTML += "<ul><li>If single or head of household, and the result is $" + phaseIn[0] + " or more, enter the applicable amount below on line 7 for that column and go to line 8.<br />";
        HTML += "i. $" + IRALimit[0] + " if under 50 at the end of " + currentTaxYear + ".<br />";
        HTML += "ii. $" + IRALimit[1] + " if age 50 or older but under age 70 1/2 at the end of " + currentTaxYear + ".<br />Otherwise, go to line 7.</li>";
        HTML += "<li>If married filing jointly or qualifying widow(er) and the result is $" + phaseIn[1] + " or more ($" + phaseIn[0] + " or more in the column for the IRA of a person who was not covered by a retirement plan), enter the applicable amount below on line 7 for that column and go to line 8.<br />";
        HTML += "i. $" + IRALimit[0] + " if under 50 at the end of " + currentTaxYear + ".<br />";
        HTML += "ii. $" + IRALimit[1] + " if age 50 or older but under age 70 1/2 at the end of " + currentTaxYear + ".<br />Otherwise, go to line 7.</li></ul></td><td>";
        if(IRAWorksheet[2] > IRAWorksheet[6]) HTML += IRAWorksheet[8];
        HTML += "</td><td>";
        if(IRAWorksheet[3] > IRAWorksheet[7]) HTML += IRAWorksheet[9];
        HTML += "</td></tr>";
        HTML += "<tr><td colspan='2'>7. Multiply lines 6a and 6b by the percentage below that applies to you. If the result is not a multiple of $10, increase it to the next multiple of $10 (for example, increase $490.30 to $500). If the result is $200 or more, enter the result. But if it is less than $200, enter $200.<br />";
        HTML += "<ul><li>Single or head of household, multiply by " + phaseIn[2] + "% (." + phaseIn[2] + ") (or by " + phaseIn[3] + "% (." + phaseIn[3] + ") in the column for the IRA of a person who is age 50 or older at the end of " + currentTaxYear + ")</li>";
        phaseIn[2] /= 2;
        phaseIn[3] /= 2;
        HTML += "<li>Married filing jointly or qualifying widow(er), multiply by " + phaseIn[2] + "% (." + phaseIn[2] + ") (or by " + phaseIn[3] + "% (." + phaseIn[3] + ") in the column for the IRA of a person who is age 50 or older at the end of " + currentTaxYear + "). But if you checked 'No' on either 1a or 1b, then in the column for the IRA of the person who was not covered by a retirement plan, multiply by " + (2 * phaseIn[2]) + "% (." + (2 * phaseIn[2]) + ") (or by " + (2 * phaseIn[3]) + "% (." + (2 * phaseIn[3]) + ") in the column for the IRA of a person who is age 50 or older at the end of " + currentTaxYear + ")</li></ul></td>";
        HTML += "<td>" + IRAWorksheet[10] + "</td><td>" + IRAWorksheet[11] + "</td></tr>";
        HTML += "<tr><td>8. Enter the amount from form 1040A, line 7. Include any non-taxable combat pay. This amount should be reported in box 12 of form W-2 with code Q</td><td>" + IRAWorksheet[12] + "</td><td></td><td></td></tr>";
        HTML += "<tr><td colspan='4'><b><font size='+1'>CAUTION</font></b>If married filing jointly and line 8 is less than $" + (2 * IRALimit[0]) + " ($" + (IRALimit[0] + IRALimit[1]) + " if one spouse is age 50 or older at the end of " + currentTaxYear + "; $" + (2 * IRALimit[1]) + " if both spouses are age 50 or older at the end of " + currentTaxYear + "), <b>stop here</b> and see Pub. 590 to figure your IRA deduction.</td></tr>";
        HTML += "<tr><td colspan='2'>9. Enter traditional IRA contributions made, or that will be made by " + thisYearsDueDate + ", " + nextTaxYear + ", for " + currentTaxYear + " to your IRA on line 9a and to your spouse’s IRA on line 9b</td>";
        HTML += "<td>" + IRAWorksheet[13] + "</td><td>" + IRAWorksheet[14] + "</td></tr>";
        HTML += "<tr><td colspan='2'>10. On line 10a, enter the smallest of line 7a, 8, or 9a. On line 10b, enter the smallest of line 7b, 8, or 9b. This is the most you can deduct. Add the amounts on lines 10a and 10b and enter the total on Form 1040A, line 17. Or, if you want, you can deduct a smaller amount and treat the rest as a nondeductible contribution (see Form 8606)</td>";
        HTML += "<td>" + IRAWorksheet[15] + "</td><td>" + IRAWorksheet[16] + "</td></tr></table>";
    }
    if(studentWorksheet[0] > 0) {
        HTML += "<h3>Student Loan Interest Deduction Worksheet</h3><h5>For your records only</h5><table border='1'>";
        HTML += "<tr><td colspan='2'>1. Enter total interest you paid in " + currentTaxYear + " on qualified student loans. Do not enter more than $2,500.</td><td>" + studentWorksheet[0] + "</td></tr>";
        HTML += "<tr><td>2. Enter the amount from Form 1040A, line 15</td><td>" + studentWorksheet[1] + "</td><td></td></tr>";
        HTML += "<tr><td>3. Enter the total of the amounts from Form 1040A, lines 16 and 17</td><td>" + (studentWorksheet[1] - studentWorksheet[2]) + "</td><td></td></tr>";
        HTML += "<tr><td>4. Subtract line 3 from line 2</td><td>" + studentWorksheet[2] + "</td><td></td></tr>";
        HTML += "<tr><td>5. Enter the amount shown below for your filing status.<br />";
        HTML += "Single, head of household or qualifying widow(er) --- " + studentLoanFullyDeductible[0] + "<br />";
        HTML += "Married, filing jointly --- " + studentLoanFullyDeductible[1] + "</td>";
        HTML += "<td>" + studentWorksheet[3] + "</td><td></td></tr>";
        HTML += "<tr><td>6. Is the amount on line 4 more than the amount on line 5?<br />";
        HTML += "<table border='0'><tr><td>";
        if(studentWorksheet[2] <= studentWorksheet[3]) HTML += "<b>√</b>";
        HTML += "<b>No.</b> Skip lines 6 and 7, enter -0- on line 8, and go to line 9.</td></tr>";
        HTML += "<tr><td>";
        if(studentWorksheet[2] > studentWorksheet[3]) HTML += "<b>√</b>";
        HTML += "<b>Yes.</b> Subtract line 5 from line 4.</td></tr></table></td><td>" + Math.max(0, studentWorksheet[2] - studentWorksheet[3]) + "</td><td></td></tr>";
        HTML += "<tr><td colspan='2'>7. Divide line 6 by $" + (studentLoanNotDeductible[0] - studentLoanFullyDeductible[0]) + "($" + (studentLoanNotDeductible[1] - studentLoanFullyDeductible[1]) + " if married filing jointly). Enter the result as a decimal (rounded to at least 3 places). If the result is 1.000 or more, enter 1.000.</td><td>";
        multiplier = Math.round(1000000 * Math.max(Math.min((studentWorksheet[2] - studentWorksheet[3]) / (studentWorksheet[4] - studentWorksheet[3]), 1), 0)) / 1000000;
        HTML += multiplier;
		HTML += "</td></tr>";
		HTML += "<tr><td colspan='2'>8. Multiply line 1 by line 7</td><td>" + Math.round(multiplier * studentWorksheet[0]) + "</td></tr>";
		HTML += "<tr><td colspan='2'>9. <b>Student loan interest deduction.</b> Subtract line 8 from line 1. Enter the result here and on Form 1040A, line 18</td><td>" + Math.round((1 - multiplier) * studentWorksheet[0]) + "</td></tr></table>";
    }
    HTML += "</body>";
    var taxForm = window.open("", "Deduction Worksheets", "");
    taxForm.document.write(HTML);
    taxForm.document.close();
}

function popSSWorksheet() {
    var HTML = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title></head><body>";
    HTML += "<h3>Social Security Income Worksheet</h3><h5>For your records only</h5><table border='1'>";
    HTML += "<tr><td>1. Enter the total amount from <b>the net benefits box</b> of <b>all</b> your <b>Forms SSA-1099</b> and <b>Forms RRB-1099</b>.<br/>Also, enter this amount on Form 1040A, line 14a</td><td style='text-align:right'>" + ssWorksheet[0] + "</td><td></td></tr>";
    HTML += "<tr><td colspan='2'>2. Enter one-half of line 1.</td><td style='text-align:right'>" + ssWorksheet[1] + "</td></tr>";
    HTML += "<tr><td colspan='2'>3. Enter the total of the amounts from Form 1040A, lines 7, 8a, 9a, 10, 11b, 12b, and 13</td><td style='text-align:right'>" + ssWorksheet[2] + "</td></tr>";
    HTML += "<tr><td colspan='2'>4. Enter the amount, if any, from Form 1040A, line 8b</td><td style='text-align:right'>" + ssWorksheet[3] + "</td></tr>";
    HTML += "<tr><td colspan='2'>5. Add lines 2, 3, and 4</td><td style='text-align:right'>" + ssWorksheet[4] + "</td></tr>";
    HTML += "<tr><td colspan='2'>6. Enter the total of the amounts from Form 1040A, lines 16 and 17</td><td style='text-align:right'>" + ssWorksheet[5] + "</td></tr>";
    HTML += "<tr><td colspan='2'>7. Subtract line 6 from line 5, if the amount is less than 0, enter 0 here and on Form 1040A, line 14b.</td><td style='text-align:right'>" + ssWorksheet[6] + "</td></tr>";
    HTML += "<tr><td colspan='2'>8. If you are married filing jointly, enter $32,000, otherwise enter $25,000.</td><td style='text-align:right'>" + ssWorksheet[7] + "</td></tr>";
    HTML += "<tr><td colspan='2'>9. Subtract line 8 from line 7, if the amount is less than 0, enter 0 here and on Form 1040A, line 14b.</td><td style='text-align:right'>" + ssWorksheet[8] + "</td></tr>";
    HTML += "<tr><td colspan='2'>10. If you are married filing jointly, enter $12,000, otherwise enter $9,000.</td><td style='text-align:right'>" + ssWorksheet[9] + "</td></tr>";
    HTML += "<tr><td colspan='2'>11. Subtract line 10 from line 9, if the amount is less than 0, enter 0.</td><td style='text-align:right'>" + ssWorksheet[10] + "</td></tr>";
    HTML += "<tr><td colspan='2'>12. Enter the <b>smaller</b> of line 9 or line 10.</td><td style='text-align:right'>" + ssWorksheet[11] + "</td></tr>";
    HTML += "<tr><td colspan='2'>13. Enter one-half of line 12.</td><td style='text-align:right'>" + ssWorksheet[12] + "</td></tr>";
    HTML += "<tr><td colspan='2'>14. Enter the <b>smaller</b> of line 2 or line 13.</td><td style='text-align:right'>" + ssWorksheet[13] + "</td></tr>";
    HTML += "<tr><td colspan='2'>15. Multiply line 11 by 85% (.85).</td><td style='text-align:right'>" + ssWorksheet[14] + "</td></tr>";
    HTML += "<tr><td colspan='2'>16. Add lines 14 and 15</td><td style='text-align:right'>" + ssWorksheet[15] + "</td></tr>";
    HTML += "<tr><td colspan='2'>17. Multiply line 1 by 85% (.85).</td><td style='text-align:right'>" + ssWorksheet[16] + "</td></tr>";
    HTML += "<tr><td colspan='2'>18. <b>Taxable social security benefits.</b> Enter the <b>smaller</b> of line 16 or line 17. Also enter this amount on Form 1040A, line 14b.</td><td style='text-align:right'>" + ssWorksheet[17] + "</td></tr>";
    HTML += "</body>";
    var taxForm = window.open("", "Social Security Income Worksheet", "");
    taxForm.document.write(HTML);
    taxForm.document.close();
}