﻿function errorIfNotNumeric(field, otherChar) {
    var commaSep = false;
    var leadingDollar = false;
    var numValue = "";
    var newValue = "";
    var nDecimals = 0;
    numValue = field.value;
    if(otherChar){
        if(numValue.indexOf(otherChar)) {
            numValue = numValue.replace(otherChar, "");
            commaSep = true;
        }
    } else {
        if(numValue.substr(0, 1) == "$") {
            leadingDollar = true;
            numValue = numValue.substr(1);
        }
        if(numValue.indexOf(",") > -1) {
            numValue = numValue.replace(",", "");
            commaSep = true;
        }
    }
    if(isNaN(numValue)) {
        alert("Unrecognizable number input");
        var txtBox = document.getElementById(field.id);
        txtBox.select();
        txtBox.focus();
        return false;
    }
    if(otherChar == "-") {      //SSN Format
        newValue = numValue;
        if(commaSep) {
            if(numValue.length >= 5) newValue = numValue.substr(0, 3) + "-" + numValue.substr(3, 2) + "-" + numValue.substr(5);
            if(numValue.length == 4) newValue = "--" + numValue;
        }
    } else {                    //Default is number format
        numValue = Math.round(numValue * 100) / 100;
        newValue += numValue;
        var i = newValue.indexOf(".");
        if(i != -1)
            while(newValue.length < i + 3) newValue += "0";
        else
            i = newValue.length;
        if(commaSep) {
            i -= 3;
            while(i > 0) {
                newValue = newValue.substr(0, i) + "," + newValue.substr(i);
                i -= 3;
            }
        }
        if(leadingDollar) newValue = "$" + newValue;
    }
    field.value = newValue;
}
function parseMoney(value) {
    if(value=="" || value == true || value == false) return 0;
    if(!isNaN(value)) return parseFloat(value);
    var retval = value.replace("$", "");
    retval = retval.replace(",", "");
    return isNaN(retval) ? 0 : parseFloat(retval);
}
function cellDisplay(cell, show) {
    if (show == true) {
        document.getElementById(cell).style.display = "";
    } else {
        document.getElementById(cell).style.display = "none";
    }
}

function encryptDecrypt(inp, maskBadChars) {
    var vals = new Array(31, 30, 29, 28, 27, 26, 25, 23, 22, 21, 19, 15, 14, 13, 11, 7);
    var retval = "";
    var x;
    for(var i = 0; i < inp.length; i++) {
        if(inp.charCodeAt(i) == 176) retval += String.fromCharCode(59 ^ vals[i % vals.length]);
        else if(inp.charCodeAt(i) == 177) retval += String.fromCharCode(96 ^ vals[i % vals.length]);
        else if(inp.charCodeAt(i) == 178) retval += String.fromCharCode(61 ^ vals[i % vals.length]);
        else {
            x = String.fromCharCode(inp.charCodeAt(i) ^ vals[i % vals.length]);
            if(maskBadChars == true) {
                if(x == ";") x = String.fromCharCode(176);
                else if(x == "¡") x = String.fromCharCode(177);
                else if(x == "=") x = String.fromCharCode(178);
            }
            retval += x;
        }
    }
    return retval;
}

function pwdCompare() {
    var badChar;
    if(document.getElementById("password2").style.display == "") {
        for (var i = 128; i < 130; i++) {
            badChar = String.fromCharCode(i);
            if((getElement("pwd1") + getElement("pwd2")).indexOf(badChar) > -1) {
                alert("The " + badChar + " character may not be used in a password");
                setElement("pwd1", getElement("pwd1").replace(badChar, ""));
                setElement("pwd2", getElement("pwd2").replace(badChar, ""));
                return;
            }
        }
        cellDisplay("pwdMatch", (getElement("pwd1") != getElement("pwd2")));
    }
}

function selectState() {
    if(getElement("state") != "") return;   //Don't mess with the user's selection
    var zip = getElement("zip");
    if(zip.length < 5) return;
    zip = zip.substr(0, 3);
    if(isNaN(zip)) return;
    var nzip = parseInt(zip, 10);
    var state = "";
    switch(true) {
        case nzip == 4 || nzip == 63: state = "NY"; break;
        case nzip < 8 || nzip == 9: state = "PR"; break;
        case nzip < 28 || nzip == 55: state = "MA"; break;
        case nzip < 30: state = "RI"; break;
        case nzip < 39: state = "NH"; break;
        case nzip < 50: state = "ME"; break;
        case nzip < 60: state = "VT"; break;
        case nzip < 70: state = "CT"; break;
        case nzip < 90: state = "NJ"; break;
        case nzip < 150: state = "NY"; break;
        case nzip < 197: state = "PA"; break;
        case nzip < 200: state = "DE"; break;
        case nzip < 206: state = "DC"; break;
        case nzip < 220: state = "MD"; break;
        case nzip < 247: state = "VA"; break;
        case nzip < 270: state = "WV"; break;
        case nzip < 290: state = "NC"; break;
        case nzip < 300: state = "SC"; break;
        case nzip < 320: state = "GA"; break;
        case nzip == 340: break;
        case nzip < 350: state = "FL"; break;
        case nzip < 370: state = "AL"; break;
        case nzip < 386: state = "TN"; break;
        case nzip < 400: state = "MS"; break;
        case nzip < 430: state = "KY"; break;
        case nzip < 460: state = "OH"; break;
        case nzip < 480: state = "IN"; break;
        case nzip < 500: state = "MI"; break;
        case nzip < 530: state = "IA"; break;
        case nzip < 550: state = "WI"; break;
        case nzip < 570: state = "MN"; break;
        case nzip < 580: state = "SD"; break;
        case nzip < 590: state = "ND"; break;
        case nzip < 600: state = "MT"; break;
        case nzip < 630: state = "IL"; break;
        case nzip < 660: state = "MO"; break;
        case nzip < 680: state = "KS"; break;
        case nzip < 700: state = "NE"; break;
        case nzip < 715: state = "LA"; break;
        case nzip < 730: state = "AR"; break;
        case nzip < 750: state = "OK"; break;
        case nzip == 755: break;
        case nzip < 800 || nzip == 885: state = "TX"; break;
        case nzip < 820: state = "CO"; break;
        case nzip < 832: state = "WY"; break;
        case nzip < 840: state = "ID"; break;
        case nzip < 850: state = "UT"; break;
        case nzip < 870: state = "AZ"; break;
        case nzip < 889: state = "NM"; break;
        case nzip < 900: state = "NV"; break;
        case nzip < 962: state = "CA"; break;
        case nzip == 968: state = "HI"; break;
        case nzip < 969: break;
        case nzip < 980: state = "OR"; break;
        case nzip < 995: state = "WA"; break;
        default: state = "AK"; break;
    }
    if(state != "") setElement("state", state);
}

function pushSSNontoArray(SSN, arr) {
    if(SSN.length > 0) {
        SSN = SSN.replace("-", "");
        if(SSN.length == 4) arr.push("", "", SSN);
        else {
            arr.push(SSN.substr(0, 3));
            if(SSN.length > 3) arr.push(SSN.substr(3, 2));
            else arr.push("");
            if(SSN.length > 5) arr.push(SSN.substr(5, 4));
            else arr.push("");
        }
    } else arr.push("", "", "");
}

function popInstructions(selectBox) {
    window.open("http://www.irs.gov/taxtopics/tc" + getElement(selectBox) + ".html");
}

function popForm(img, dollars, tops, lefts, heights, widths, sizes, reqFields, formName, d2, t2, l2, h2, w2, s2, r2) {
    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>";
    HTML += "<body><img src='" + img + "' alt='Be sure to set your margins to .5 inches all the way around and clear your header and footer.' style='position:absolute;top:0px;left:0px;width:700px;height:885px' />";
    if(!inSeason)
        HTML += "<div style='position:absolute;top:0px;left:0px;width:700px;height:30px;background:white;font-size:24px;'>Not a valid tax form, currently in test mode!</div>";
    for (var i = 0; i < dollars.length; i++) {
        if(reqFields[i] == true || isNaN(dollars[i]) == true || dollars[i] > 0) {
            HTML += "<div style='position:absolute;top:" + tops[i] + "px;left:" + lefts[i] + "px;width:" + widths[i] + "px;height:" + heights[i] + "px;background:white;font-size:" + sizes[i];
            if(isNaN(dollars[i])) HTML += "px;text-align:left'>" + dollars[i] + "</div>";
            else HTML += "px;text-align:right'><tt>" + dollars[i] + "</tt></div>";
        }
    }
    if(d2) {
        for (var i=0; i < d2.length; i++) {
            if(r2[i] == true || isNaN(d2[i]) == true || d2[i] > 0) {
                HTML += "<div style='position:absolute;top:" + t2[i] + "px;left:" + l2[i] + "px;width:" + w2[i] + "px;height:" + h2[i] + "px;background:white;font-size:" + s2[i];
                HTML += "px;text-align:left'><tt>" + d2[i] + "</tt></div>";
            }
        }
    }
    HTML += "</body>";
    var taxForm = window.open("", formName, "");
    taxForm.document.write(HTML);
    taxForm.document.close();
}

function pop1040EZ() {
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!"))
        popForm("1040EZ.jpg", dollars1040EZ, top1040EZ, left1040EZ, height1040EZ, width1040EZ, size1040EZ, required1040EZ, "1040EZ");
}

function pop1040A1() {
    var len = dependentInfo.length;
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!")) {
        if(len == 0)
            popForm("1040A1.jpg", dollars1040A.slice(0, 25), top1040A.slice(0, 25), left1040A.slice(0, 25), 
                    height1040A.slice(0, 25), width1040A.slice(0, 25), size1040A.slice(0, 25), 
                    required1040A.slice(0, 25), "1040A");
        else
            popForm("1040A1.jpg", dollars1040A.slice(0, 25), top1040A.slice(0, 25), left1040A.slice(0, 25),
                    height1040A.slice(0, 25), width1040A.slice(0, 25), size1040A.slice(0, 25),
                    required1040A.slice(0, 25), "1040A", dependentInfo, dtop1040A, dleft1040A,
                    dheight1040A, dwidth1040A, dsize1040A, drequired1040A);
    }
}

function pop1040A2() {
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!"))
        popForm("1040A2.jpg", dollars1040A.slice(24), top1040A.slice(25), left1040A.slice(25), 
                height1040A.slice(25), width1040A.slice(25), size1040A.slice(25), 
                required1040A.slice(25), "1040A, page 2");
}

function popSchedule1() {
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!"))
        popForm("Schedule1.jpg", dollarsSchedule1, topSchedule1, leftSchedule1, 
                heightSchedule1, widthSchedule1, sizeSchedule1, 
                requiredSchedule1, "Schedule 1");
}

function popScheduleEIC() {
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!")) {
        if(dollarsScheduleEIC.length > 22) {
            if(dollarsScheduleEIC[4] == "") {
                for(var i = dollarsScheduleEIC.length - 7; i > 22; i -= 11) {
                    if(dollarsScheduleEIC[i] != "") {
                        for(var j = 0; j < 11; j++) dollarsScheduleEIC[j] = dollarsScheduleEIC[i+j-4];
                        if(i + 11 <= dollarsScheduleEIC.length) {
                            for(var j = 0; j < 11; j++)
                                dollarsScheduleEIC[i+j-4] = dollarsScheduleEIC[dollarsScheduleEIC.length+j-11];
                        }
                        dollarsScheduleEIC.length -= 11;
                    }
                }
            }
            if(dollarsScheduleEIC[14] == "") {
                for(var i = dollarsScheduleEIC.length - 7; i > 22; i -= 11) {
                    if(dollarsScheduleEIC[i] != "") {
                        for(var j = 0; j < 11; j++) dollarsScheduleEIC[j+11] = dollarsScheduleEIC[i+j-4];
                        if(i + 11 <= dollarsScheduleEIC.length) {
                            for(var j = 0; j < 11; j++)
                                dollarsScheduleEIC[i+j-4] = dollarsScheduleEIC[dollarsScheduleEIC.length+j-11];
                        }
                        dollarsScheduleEIC.length -= 11;
                    }
                }
            }
            dollarsScheduleEIC.length = 22;
        }
        popForm("ScheduleEIC.jpg", dollarsScheduleEIC, topScheduleEIC, leftScheduleEIC, 
                heightScheduleEIC, widthScheduleEIC, sizeScheduleEIC, 
                requiredScheduleEIC, "Schedule EIC");
    }
}

function popForm8917() {
    if(inSeason == true || confirm("Warning!  This form is only viewable for testing purposes do not print it out!")) {
        if(form8917.length > 22) form8917.length = 22;
        popForm("Form8917.jpg", form8917,
                top8917, left8917, height8917, width8917, size8917, required8917, "Form 8917");
    }
}

function showHist(arrayElem) {
    var func = histoire[arrayElem];
    var func2 = func.split("¡");
    if(func2[0] == "showTab") showTab(func2[1], true);
    else if(func2[0] = "intro") intro(func2[1], true);
}
function histBack() {
    if(histPos > 0) showHist(--histPos);
    cellDisplay("previous", histPos > 0);
    cellDisplay("next", histPos < histoire.length - 1);
}
function histForward() {
    if(histPos < histoire.length - 1) showHist(++histPos);
    cellDisplay("previous", histPos > 0);
    cellDisplay("next", histPos < histoire.length - 1);
}

function writeOption(val, txt) {
    return "<option value='" + val + "'>" + txt + "</option>";
}
function createContainer(cellId, emptyCellId, emptyCellScript, emptyCellText, filledCellId, entryCellId, cellSummaryLine) {
    var blank = "<table><tr><td id='" + emptyCellId + "'><a href='" + emptyCellScript + ";'>" + emptyCellText + "</a></td></tr>";
    blank += "<tr><td id='" + filledCellId + "' style='display:none'>";
    blank += "<table><tr>";
    for (var i = 0; i < cellSummaryLine.length; i++)
        blank += "<td>" + cellSummaryLine[i] + "</td>";
    blank += "</tr></table>";
    blank += "</td></tr><tr><td id='" + entryCellId + "' style='display:none'></td></tr></table>";
    setElement(cellId, blank);
    cellDisplay(cellId, true);
}
function primaryOrSpouse(id, msg) {
    if(getElement("maritalStatus") == "married1") {
        var retval = "<tr><td>" + msg + "</td><td><select id='" + id + "'>";
        var primary = getElement("filerNameInput0");
        var spouse = getElement("filerNameInput1");
        retval += writeOption("primary", ((primary=="") ? "Primary Taxpayer" : primary));
        retval += writeOption("spouse", ((spouse=="") ? "Spouse" : spouse));
        retval += "</td></tr>";
        return retval;
    } else
        return "<tr><td></td><td><input type='hidden' id='" + id + "' value='primary'/></td></tr>";
}
function primarySpouseDependent(id, msg) {
    var retval = "<tr><td>";
    var dep = new Array();
    var primary = getElement("filerNameInput0");
    var spouse = getElement("filerNameInput1");
    if(primary == "") primary = "Primary Taxpayer";
    if(spouse == "") spouse = "Spouse";
    for (var i = 0; i < 10; i++)
        if(dependents[i] != null) dep.push(dependents[i]);

    if(getElement("maritalStatus") == "married1") {
        retval += msg + "</td><td><select id='" + id + "'>";
        retval += writeOption(primary, primary);
        retval += writeOption(spouse, spouse);
    } else if (dep.length == 0)
        return retval + "</td><td><input type='hidden' id='" + id + "' value='" + primary + "'/></td></tr>";
    else {
        retval += msg + "</td><td><select id='" + id + "'>";
        retval += writeOption(primary, primary);
    }

    for (var i = 0; i < dep.length; i++) {
        if(dep[i].indexOf("¡") == -1) retval += writeOption(dep[i], dep[i]);
        else {
            var temp = dep[i].split("¡");
            retval += writeOption(temp[1], temp[0]);
        }
    }

    retval += "</select></td></tr>";
    return retval;
}
function createW2Container(num) {
    var sumLine = new Array("<div id='employer" + num + "'></div>", "Wages: ", "<div id='wages" + num + "' style='text-align:right'></div>", "Withholding: ", "<div id='withholding" + num + "' style='text-align:right'></div>");
    createContainer("W2Container" + num, "W2Empty" + num, "javascript:createW2Cell(\"W2Entry" + num + "\", \"0" + num + "\")", "Add new W-2", "W2Filled" + num, "W2Entry" + num, sumLine);
}
function createW2Cell(cell, num) {
    var blank = "<table>" + primaryOrSpouse("n" + num + "earner", "Who earned these wages");
    blank += "<tr><td>Employer name (optional) </td><td><input type='text' id='n" + num + "name' size='30'/></td></tr>";
    blank += "<tr><td>Wages<br/>(from box 1 of W-2)</td><td><input type='text' id='n" + num + W2Line[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Federal Tax Withheld<br/>(from box 2 of W-2)</td><td><input type='text' id='n" + num + withholdLine[0] + "' size='15'" +errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Advance EIC Payments<br/>(from box 9 of W-2)</td><td><input type='text' id='n" + num + advEICLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Combat Pay<br/>(from box 14 code Q)</td><td><input type='text' id='n" + num + combatLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Is box 13, retirement pay, checked? </td><td><input type='checkbox' id='n" + num + "ret'></td></tr>";
    blank += saveLine1 + "W2(\"" + cell + saveLine2 + "W2(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "09") {
        var next = (parseInt(num, 10) + 1).toString();
        createW2Container(next);
    }
}
function createIntContainer(num) {
    var sumLine = new Array("<div id='bank" + num + "'></div>", "Interest: ", "<div id='interest" + num + "' style='text-align:right'></div>", "<div id='exemptLabel" + num + "'>Exempt Interest: </div>", "<div id='exemptInt" + num + "' style='text-align:right'></div>");
    createContainer("intContainer" + num, "intEmpty" + num, "javascript:createIntCell(\"intEntry" + num + "\", \"1" + num + "\")", "Add new Interest item", "intFilled" + num, "intEntry" + num, sumLine);
}
function createIntCell(cell, num) {
    var blank = "<table><tr><td>Bank name (optional) </td><td><input type='text' id='n" + num + "name' size='30'/></td></tr>";
    blank += "<tr><td>Interest Income<br/>(from box 1 of 1099-Int)</td><td><input type='text' id='n" + num + intLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Tax Exempt Interest<br/>(from box 3 of 1099-Int)</td><td><input type='text' id='n" + num + exemptIntLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Federal Tax Withheld<br/>(from box 4 of 1099-Int)</td><td><input type='text' id='n" + num + withholdLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += saveLine1 + "Int(\"" + cell + saveLine2 + "Int(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "19") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createIntContainer(next);
    }
}
function createUEContainer(num) {
    var sumLine = new Array("<div id='state" + num + "'></div>", "Unemployment: ", "<div id='UE" + num + "' style='text-align:right'></div>", "Withholding: ", "<div id='withholdUE" + num + "' style='text-align:right'></div>");
    createContainer("ueContainer" + num, "ueEmpty" + num, "javascript:createUECell(\"ueEntry" + num + "\", \"2" + num + "\")", "Add new Unemployment item", "ueFilled" + num, "ueEntry" + num, sumLine);
}
function createUECell(cell, num) {
    var blank = "<table><tr><td>State name (optional) </td><td><input type='text' id='n" + num + "name' size='30'/></td></tr>";
    blank += "<tr><td>Unemployment Income<br/>(from box 1 of 1099-G)</td><td><input type='text' id='n" + num + ueLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Federal Tax Withheld<br/>(from box 4 of 1099-G)</td><td><input type='text' id='n" + num + withholdLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += saveLine1 + "UE(\"" + cell + saveLine2 + "UE(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "29") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createUEContainer(next);
    }
}
function createSSContainer(num) {
    var sumLine = new Array("<div id='ssEarner" + num + "'><div>", "Net SS Benefits: ", "<div id='ss" + num + "' style='text-align:right'></div>", "Withholding: ", "<div id='withholdSS" + num + "' style='text-align:right'></div>");
    createContainer("ssContainer" + num, "ssEmpty" + num, "javascript:createSSCell(\"ssEntry" + num + "\", \"6" + num + "\")", "Add new Social Security income", "ssFilled" + num, "ssEntry" + num, sumLine);
}
function createSSCell(cell, num) {
    var blank = "<table>" + primaryOrSpouse("n" + num + "earner", "Who earned this Social Security income");
    blank += "<tr><td>Type of benefit: </td><td><select id='n" + num + "type'>" + writeOption("SS", "Social Security (form SSA 1099)") + writeOption("RR", "Railroad Retirement (form RRB 1099)") + "</select></td></tr>";
    blank += "<tr><td>Net Benefits<br/>(from box 5 of SSA-1099 or box 7 of RRB-1099)</td><td><input type='text' id='n" + num + ssLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Federal Tax Withheld<br/>(from box 6 of SSA-1099 or box 9 of RRB-1099)</td><td><input type='text' id='n" + num + withholdLine[0] + "' size='15'" +errorIfNotNum + "></td></tr>";
    blank += saveLine1 + "SS(\"" + cell + saveLine2 + "SS(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "69") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createSSContainer(next);
    }
}
function createDividendContainer(num) {
    var sumLine = new Array("<div id='payer" + num + "'></div>", "Dividend: ", "<div id='dividend" + num + "' style='text-align:right'></div>", "<div id='qualifiedLabel" + num + "'>Qualified Dividend: </div>", "<div id='qualDiv" + num + "' style='text-align:right'></div>")
    createContainer("divContainer" + num, "divEmpty" + num, "javascript:createDividendCell(\"divEntry" + num + "\", \"3" + num + "\")", "Add new Dividend item", "divFilled" + num, "divEntry" + num, sumLine);
}
function createDividendCell(cell, num) {
    var blank = "<table><tr><td>Payer's name (optional) </td><td><input type='text' id='n" + num + "name' size='30'/></td></tr>";
    blank += "<tr><td>Ordinary Dividends<br/>(from box 1a of 1099-Div)</td><td><input type='text' id='n" + num + dividendLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Qualified Dividends<br/>(from box 1b of 1099-Div)</td><td><input type='text' id='n" + num + qualDividendLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Federal Tax Withheld<br/>(from box 4 of 1099-Div)</td><td><input type='text' id='n" + num + withholdLine[0] + "' size='15'" + errorIfNotNum + "></td></tr>";
    blank += "<tr><td>Please choose one: </td><td><select id='n" + num + "ownership'>" + writeOption("1", "Stock owned 61+ days and dividend not received as a nominee") + writeOption("2", "You received the dividend as a nominee") + writeOption("3", "You owned the stock 60 days or less") + "</select></td></tr>";
    blank += "<tr><td colspan='2'><div id='qualDivWarn" + num + "' style='color:Red;display:none'>This dividend might not be qualified.  Contact a tax professional.</td></tr>";
    blank += saveLine1 + "Div(\"" + cell + saveLine2 + "Div(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "39") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createDividendContainer(next);
    }
}
function showConfirmIRA(num) {
    if(getElement("n" + num + "IRADate") == "lastEarly")
        setElement("n" + num + "confirmIRA", "Was this IRA deducted on last year's return? </td><td><select id='n" + num + "IRADeductible'>" + writeOption("true", "No") + writeOption("false", "Yes") + "</select>");
    else if (getElement("n" + num + "IRADate") == "thisYear")
        setElement("n" + num + "confirmIRA", "Will this IRA be deducted on this year's return, not next year's? </td><td><select id='n" + num + "IRADeductible'>" + writeOption("true", "Yes") + writeOption("false", "No") + "</select>");
    else
        setElement("n" + num + "confirmIRA", "<input type='hidden' id='n" + num + "IRADeductible' value='true'/>");
}
function createDeductionContainer(num) {
    var deductblank = "<table><tr><td id='deductEmpty" + num + "'>Add new <select id='n4" + num + "deductType' onchange='javascript:createDeductionCell(\"deductEntry" + num + "\", \"4" + num + "\");'><option></option>" + writeOption("educator", "Educator Expense") + writeOption("IRA", "IRA") + writeOption("student", "Student Loan") + writeOption("tuition", "Tuition and Fees") + "</select> deduction</td></tr>";
    deductblank += "<tr><td id='deductFilled" + num + "' style='display:none'>";
    deductblank += "<table><tr><td><div id='deductType" + num + "'></div></td>";
    deductblank += "<td><div id='deductAmt" + num + "' style='text-align:right'></div></td></tr></table></td></tr>";
    deductblank += "<tr><td id='deductEntry" + num + "' style='display:none'></td></tr></table>";
    setElement("deductContainer" + num, deductblank);
    cellDisplay("deductContainer" + num, true);
}
function createDeductionCell(cell, num) {
    var deductType = getElement("n" + num + "deductType");
    var deductTypeLong;
    var blank = "<table>" + primaryOrSpouse("n" + num + "earner", "Which person contributed to this " + deductType + " deduction?");
    if(deductType == "") return;
    if(deductType == "IRA") {
        deductTypeLong = "IRA";
        blank += "<tr><td>Date of Contribution: </td><td><select id='n" + num + "IRADate' onclick='showConfirmIRA(" + num + ")'><option></option>" + writeOption("lastEarly", "January 1 to " + lastYearsDueDate + "," + currentTaxYear) + writeOption("lastLate", "During " + currentTaxYear + " but AFTER " + lastYearsDueDate) + writeOption("thisYear", "January 1 to " + thisYearsDueDate + ", " + nextTaxYear) + "</select></td></tr>";
        blank += "<tr><td id='n" + num + "confirmIRA' colspan='2'><input type='hidden' id='n" + num + "IRADeductible' value='true'/></td></tr>";
        blank += "<tr><td>IRA deduction amount: </td><td><input type='text' id='n" + num + IRALine[0] + "' size='15'" + errorIfNotNum + "/></td></tr>";
    } else if(deductType == "student") {
        deductTypeLong = "Student Loan Interest";
        blank = "<table><tr><td>Who was the loan taken out on behalf of? </td><td><select id='n" + num + "studentDependent'>" + writeOption("1", "Yourself or your spouse") + writeOption("2", "A dependent in the year the loan was taken out") + writeOption("3", "A person who would have been your dependent, but they filed a joint return") + writeOption("4", "A person who would have been your dependent if they had earned less money") + writeOption("5", "Person would be your dependent, but you were dependent on a tax return") + writeOption("0", "None of the above apply, I cannot take this deduction") + "</select></td></tr>";
        blank += "<tr><td>Did the student loan cover tuition, fees, room, board or books? </td><td><select id='n" + num + "studentCover'>" + writeOption("Yes", "Yes") + writeOption("No", "No") + "</select></td></tr>";
        blank += "<tr><td>What type of program was the student enrolled in? </td><td><select id='n" + num + "studentProgram'>" + writeOption("1", "Degree or Certificate program at a College, University or Vocational School") + writeOption("2", "A for credit study abroad program approved by the school") + writeOption("0", "None of the above apply, I cannot take this deduction") + "</select></td></tr>";
        blank += "<tr><td>Did the student carry at least half the normal full-time course load? </td><td><select id='n" + num + "studentLoad'>" + writeOption("Yes", "Yes") + writeOption("No", "No") + "</select></td></tr>";
        blank += "<tr><td>Student Loan interest deduction amount<br/>(From box 1 of form 1098-E)</td><td><input type='text' id='n" + num + studentLine[0] + "' size='15'" + errorIfNotNum + "/></td></tr>";
    } else if(deductType == "educator") {
        deductTypeLong = "Educator Expense";
        blank += "<tr><td>Was this person a teacher in Kindergarten through 12th grade? </td><td><select id='n" + num + "educatorGrade'>" + writeOption("Yes", "Yes") + writeOption("No", "No") + "</select></td></tr>";
        blank += "<tr><td>Did this person work at least 900 hours during the year? </td><td><select id='n" + num + "educatorHours'>" + writeOption("Yes", "Yes") + writeOption("No", "No") + "</select></td></tr>";
        blank += "<tr><td>Educator Expense deduction amount: </td><td><input type='text' id='n" + num + educatorLine[0] + "' size='15'" + errorIfNotNum + "/></td></tr>";
    } else if(deductType == "tuition") {
        deductTypeLong = "Tuition and Fees";
        if(getElement("canBeClaimed0") == "Yes" || getElement("canBeClaimed1") == "Yes") {
            alert("Tuition and Fees can not be deducted if you can be claimed on another person's tax return!");
            return;
        } else {
            blank = "<table>" + primarySpouseDependent("n" + num + "earner", "Student name: ");
            blank += "<tr><td>Tuition and Fees amount: </td><td><input type='text' id='n" + num + tuitionLine[0] + "' size='15'" + errorIfNotNum + "/></td></tr>";
            blank += "<tr><td>Course start date: </td><td><select id='n" + num + "courseStart'>" + writeOption("Deductible", "January 1, " + currentTaxYear + " to March 31, " + nextTaxYear) + writeOption("NotDeductible", "Outside of this date range and not deductible.") + "</select></td></tr>";
            blank += "<tr><td colspan='2'>This should be out of pocket expenses, not covered by scholarships</td></tr>";
        }
    }
    blank += "<tr><td id='" + deductType + "Warn" + num + "' style='color:Red;display:none' colspan='2'>Your " + deductTypeLong + " amount exceeds the IRS' maximum deductible amount</td></tr>";
    blank += saveLine1 + "Deduct(\"" + cell + saveLine2 + "Deduct(\"" + cell + saveLine3;
    setElement("deductEmpty" + num.substr(1,1), "New " + deductTypeLong + " deduction<input type='hidden' id='n" + num + "deductType' value='" + deductType + "'>");
    setElement(cell, blank);
    cellDisplay(cell, true);
    if (num != "49") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createDeductionContainer(next);
    }
}
function writeAgeRangeOption(minAge, maxAge, minDate, maxDate, twoDigitYear) {
    var retval, minYear, maxYear, linkWord;

    minYear = parseInt(parseFloat(currentTaxYear) - maxAge);
    maxYear = parseInt(parseFloat(currentTaxYear) - minAge);
    if(maxDate != "" && maxDate.indexOf("January") != -1)
        maxYear++;
    if(minDate != "" && minDate.indexOf("July") != -1)
        minYear++;

    if(twoDigitYear == true) {
        maxYear = maxYear.toString().substr(2);
        minYear = minYear.toString().substr(2);
    }

    if(maxDate == "") maxDate = maxYear;
    if(minDate == "") minDate = minYear;

    if(maxAge == 500) {
        if(maxDate != maxYear) maxDate = maxDate + ", " + (maxYear + 1);
        retval = writeOption(">" + minAge, maxDate + " or earlier (older than " + minAge + ")");
    } else if (minAge == 0 && maxAge != 0) {
        if(minDate != minYear) minDate = minDate + ", " + (minYear + 1);
        retval = writeOption("<" + maxAge, minDate + " or later (younger than " + maxAge + ")");
    } else {
        if(minDate != minYear) minDate = minDate + ", " + minYear;
        if(maxDate != maxYear) maxDate = maxDate + ", " + maxYear;
        if(minAge == maxAge) {
            if(minDate == maxDate)
                retval = writeOption(minAge, minDate + " (" + minAge + ")");
            else
                retval = writeOption(minAge, minDate + " to " + maxDate + " (" + minAge + ")");
        } else {
            if(maxAge - minAge == 1)
                linkWord = " or ";
            else
                linkWord = " to ";

            retval = writeOption(minAge + "to" + maxAge, minDate + linkWord + maxDate + " (" + minAge + linkWord + maxAge + ")");
        }
    }
    return retval;
}
function msChange() {
    var marStatus = getElement("maritalStatus");
    if(marStatus == "married1") {
        if(document.getElementById("filerEmpty1").style.display == "none" && document.getElementById("filerFilled1").style.display == "none")
            cellDisplay("filerEmpty1", true);
    } else {
        cellDisplay("filerFilled1", false);
        cellDisplay("filerEmpty1", false);
    }
    cellDisplay("showIfSingleOrWidowed", (marStatus == "single" || marStatus == "widow"));
}
function claimChange(num) {
    cellDisplay("showIfUnsure" + num, (getElement("canBeClaimed" + num) == "Unsure"));
}
function homeOwnChange() {
    cellDisplay("showIfHomeOwner", (getElement("ownHome") == "Yes"));
}
function dYOBChange(num) {
    var dCOB = getElement("n" + num + "DCOB");
    var dYOB;
    cellDisplay("n" + num + "DYOB19", false);
    cellDisplay("n" + num + "DYOB20", false);
    if(dCOB == "") return;
    cellDisplay("n" + num + "DYOB" + dCOB, true);
    dYOB = getElement("n" + num + "DYOB" + dCOB);
    if(dYOB == "") return;
    if(dYOB == ">24") {
        setElement("n" + num + "showAdultDependent", "<td>Is this person permanently and totally disabled: </td><td><select id='adultClaim" + num + "'>" + writeOption("YesD", "Yes") + writeOption("No", "No") + "</select></td>");
        cellDisplay("n" + num + "showAdultDependent", true);
    } else if (dYOB == "19to23") {
        setElement("n" + num + "showAdultDependent", "<td>Please select the best option</td><td><select id='adultClaim" + num + "'>" + writeOption("YesS", "This person was a full-time student for any part of 5 months") + writeOption("YesD", "This person is permanently and totally disabled:") + writeOption("No", "None of the above apply") + "</select></td>");
        cellDisplay("n" + num + "showAdultDependent", true);
    } else {
        setElement("n" + num + "showAdultDependent", "<td><input type='hidden' id='adultClaim" + num + "' value='Yes'/></td>");
        cellDisplay("n" + num + "showAdultDependent", false);
    }
}
function createDependentContainer(num) {
    var deductblank = "<table><tr><td id='dependentEmpty" + num + "'><a href='javascript:createDependentCell(\"dependentEntry" + num + "\", \"5" + num + "\");'>Add new dependent</a></td></tr>";
    deductblank += "<tr><td id='dependentFilled" + num + "' style='display:none'>";
    deductblank += "<table><tr><td><div id='dependentName" + num + "'></div></td>";
    deductblank += "<td><div id='dependentInfo" + num + "0'></div></td>";
    deductblank += "<td><div id='dependentInfo" + num + "1'></div></td></tr></table></td></tr>";
    deductblank += "<tr><td id='dependentEntry" + num + "' style='display:none'></td></tr></table>";
    setElement("dependentContainer" + num, deductblank);
    cellDisplay("dependentContainer" + num, true);
}
function createDependentCell(cell, num) {
    var blank = "<table><tr><td>Dependent name (optional here, required for return) </td><td><input type='text' id='n" + num + "name' size='30'/></td></tr>";
    blank += "<tr><td>Dependent SSN: (optional here, required for return)</td><td><input type='text' id='n" + num + "SSN' size='15'/></td></tr>";
    blank += "<tr><td>Dependent Gender: </td><td><select id='n" + num + "gender'>" + writeOption("Female", "Female") + writeOption("Male", "Male") + "<option selected></option></select></td></tr>";
    blank += "<tr><td>Relation to primary taxpayer OR spouse: </td><td>";
    blank += "<select id='n" + num + "relation'><option></option>" + writeOption("child1", "Daughter/Son/Child") + writeOption("child2", "Foster Daughter/Foster Son/Foster Child") + writeOption("child3", "Granddaughter/Grandson/Grandchild") + writeOption("child4", "Niece/Nephew") + writeOption("child5", "Sister/Brother/Sibling") + writeOption("child6", "Stepdaughter/Stepson/Stepchild") + writeOption("child7", "Stepsister/Stepbrother/Stepsibling") + writeOption("child8", "Half Sister/Half Brother/Half Sibling") + writeOption("relative1", "Mother/Father/Parent") + writeOption("relative2", "Grandmother/Grandfather/Grandparent") + writeOption("relative3", "Aunt/Uncle") + writeOption("relative4", "Other Relative") + writeOption("no", "Not a relative");
    blank += "</select></td></tr><tr><td>Dependent Year of Birth: </td><td><select id='n" + num + "DCOB' onChange='dYOBChange(" + num + ")'><option></option>";
    blank += writeOption("19", "19") + writeOption("20", "20") + "</select>";
    blank += "<select id='n" + num + "DYOB20' style='display:none' onChange='dYOBChange(" + num + ")'><option></option>";
    for (var i = 0; i < 19 && i < parseInt(currentTaxYear) - 1999; i++)
        blank += writeAgeRangeOption(i, i, "", "", true);
    blank += "</select>";
    blank += "<select id='n" + num + "DYOB19' style='display:none' onChange='dYOBChange(" + num + ")'><option></option>";
    for (var i = parseInt(currentTaxYear) - 1999; i < 19; i++)
        blank += writeAgeRangeOption(i, i, "", "", true);
    blank += writeAgeRangeOption(19, 23, "", "", true) + writeAgeRangeOption(24, 500, "", "", true) + "</select></td></tr>";
    blank += "<tr><td id='n" + num + "showAdultDependent' style='display:none' colspan='2'></td></tr>";
    blank += "<tr><td>How many months did the dependent live with you? </td><select id='n" + num + "months'>" + writeOption("12", "12 months") + writeOption("12", "Was born in " + currentTaxYear + " and lived with taxpayer") + writeOption("12", "Died in " + currentTaxYear + " and lived with taxpayer");
    for(var i = 11; i >= 0; i--) blank += writeOption(i, i + " months");
    blank += "</select><br/>(Include time away at school or vacation and partial months)</td></tr>";
    blank += "<tr><td>Is there a divorce decree stating who can claim this dependent? </td><select id='n" + num + "decree'>" + writeOption("No", "No") + writeOption("canClaim", "Yes, it states I can claim this dependent") + writeOption("cantClaim", "Yes, it states somebody else will claim this dependent") + "</select></td></tr>";
    blank += "<tr><td>Dependent Support Status: </td><td><select id='n" + num + "support'>" + writeOption("1", "Taxpayer provided over 50% of support for dependent") + writeOption("2", "Taxpayer provided less than 50% of support, but claim them due to a legal agreement") + writeOption("0", "None of the above apply and I can't claim this dependent") + "</select></td></tr>";
    blank += "<tr><td>Dependent Nationality Status: </td><td><select id='n" + num + "nationality'>" + writeOption("1", "U.S. citizen or U.S. national or U.S. resident alien") + writeOption("2", "Canadian or Mexican resident") + writeOption("3", "Adopted child of U.S. Citizen or U.S. national") + writeOption("0", "None of the above apply") + "</select></td></tr>";
    blank += "<tr><td>Did this person earn less than $" + dependentAmt + " in " + currentTaxYear + "? </td><td><select id='n" + num + "income'>" + writeOption("Yes", "Yes") + writeOption("No", "No") + "</select></td></tr>";
    blank += "<tr><td>Dependent Filing Status: </td><td><select id='n" + num + "filing'>" + writeOption("1", "This person did not file a tax return") + writeOption("2", "Filed as a single taxpayer") + writeOption("0", "Married and filed a joint return") + writeOption("3", "Other Filing Status") + "</select></td></tr>";
    blank += "<tr><td colspan='2'><div id='dependentWarn" + num + "' style='color:Red;display:none'></td></tr>";
    blank += saveLine1 + "Dependent(\"" + cell + saveLine2 + "Dependent(\"" + cell + saveLine3;
    setElement(cell, blank);
    cellDisplay(cell, true);
    if(num != "59") {
        var next = ((parseInt(num, 10) + 1) % 10).toString();
        createDependentContainer(next);
    }
}