﻿var W2Line = new Array("07", "07", "01");
var intLine = new Array("08a", "08a", "02");
var dividendLine = new Array("09a", "09a", "N/A");
var ueLine = new Array("19", "13", "03");
var ssLine = new Array("20a", "14a", "N/A");
var educatorLine = new Array("23", "16", "N/A");
var IRALine = new Array("32", "17", "N/A");
var studentLine = new Array("33", "18", "N/A");
var tuitionLine = new Array("34", "19", "N/A");
var validateItems = new Array("", "07¡08¡19¡20¡09¡income", "3¡23¡32¡33¡34¡deduction", "51¡64¡credit", "maritalStatus¡canBeClaimed0¡age0¡state", "canBeClaimed1¡age1");
var tabs = new Array("3", "4", "5", "7", "8", "9", ueLine[0], ssLine[0].substr(0, 2), educatorLine[0], IRALine[0], studentLine[0], tuitionLine[0]);
var introPage = 0;
var lines = new Array();
var histoire = new Array();
var histPos = 0;
var cellSaveLoaded = -1;
var summaryLoaded = -1;
var EICLoaded = -1;
var cookiesLoaded = -1;
function copyRadioValues(radioFromName, radioToName) {
    var radioFrom = document.getElementsByName(radioFromName);
    var radioTo = document.getElementsByName(radioToName);
    for(var i = 0; i < radioFrom.length && i < radioTo.length; i++)
        radioTo[i].checked = radioFrom[i].checked;
}
function checkForCookies() {
    if(document.cookie != "" && document.cookie != null) {
        ck = readCookie("taxInfo")
        if(ck != "" && ck != null) {
            cellDisplay("cookiesFound", true);
            cellDisplay("intro0", false);
            cellDisplay("cookieLoad", true);
            cellDisplay("LeaveIntro", true);
            cellDisplay("introFooter", false);
            introPage = -1;
            return;
        }
    }
    cellDisplay("cookieLoad", false);
    cellDisplay("LeaveIntro", false);
}
function histAdd(tab) {
    if(getElement("MenuTab" + tab) != "" && getElement("MenuTab" + tab).indexOf("javascript:hist") != -1) return;
    if(getElement("Tab" + tab + "Main") != "" && getElement("Tab" + tab + "Main").indexOf("histBack()") != -1) return;
    if(getElement("Tab" + tab + "Num") != "" && getElement("Tab" + tab + "Num").indexOf("histForward()") != -1) return;
    if(histoire.length == histPos + 1) {
        histoire.push(tab);
        histPos++;
    } else {
        histoire[++histPos] = tab;
        histoire.length = histPos + 1;
    }
    cellDisplay("previous", histPos > 0);
}
function tabHighlight(tabNum) {
    for(var i=0; i<13; i++) {
        if(document.getElementById("MenuTab" + i).style.backgroundColor.toLowerCase() == "yellow") continue;

        if(i==tabNum) document.getElementById("MenuTab" + i).style.backgroundColor = "#3ea99f";
        else document.getElementById("MenuTab" + i).style.backgroundColor = "#addfff";
    }
}
function validate(pageNum, suppressMsg) {
    if(pageNum <= 0) return true;
    var validation = validateItems[pageNum].split("¡");
    if(getRadioValue("cantdo" + validation[validation.length - 1]) == "yes") {
        if(!suppressMsg) {
            if(pageNum == 0) alert("Sorry, you can not use this web site for your tax preparation needs yet.");
            else alert("You may use this web site for your tax preparation needs, but you'll probably get a bigger refund filing your taxes using another method.");
        }
        if(pageNum == 0) return false;
    }
    if(pageNum < 4) {
        for(var i = 0; i < validation.length - 1; i++) {
            if(getRadioValue("line" + validation[i]) == "") {
                if(!suppressMsg) {
                    alert("Required radio buttons were left unselected!" + validation[i]);
                    //setElement("line" + validation[i], "maybe");
                    document.getElementsByName("line" + validation[i]).focus();
                }
                return false;
            }
            if(getRadioValue("line" + validation[i]) != "no") {
                if(lines.length == 0 || ((lines.join("¡")).indexOf(validation[i] + "¡") == -1) && (lines[lines.length - 1] != validation[i]))
                        lines.push(validation[i]);
            }
            //if(validation[i] == W2Line[0]) setRadioValue("line63", getRadioValue("line" + W2Line[0]));
        }
    } else {
        for(var i = 0; i < validation.length; i++) {
            if(getElement(validation[i]) == "") {
                if(!suppressMsg) {
                    alert("Required dropdowns were left blank!");
                    document.getElementById(validation[i]).focus();
                }
                return false;
            }
        }
    }
    return true;
}
function emptyCellLoop(tabNum, cellType, linkText, cellType2) {
    if(cellType2) {
        if(tabNum == 10) {
            for(var n=0; n<10; n++) setElement("Empty0" + n, "<a href='javascript:createCell(\"Container0" + n + "\", \"0" + n + "\", \"" + cellType2 + "\");'>" + linkText + "</a>");
        } else {
            for(var n=0; n<10; n++) setElement("Empty" + (10*tabNum + n), "<a href='javascript:createCell(\"Container" + (10*tabNum + n) + "\", \"" + (10*tabNum + n) + "\", \"" + cellType2 + "\");'>" + linkText + "</a>");
        }
    } else {
        if(tabNum == 10) {
            for(var n=0; n<10; n++) setElement("Empty0" + n, "<a href='javascript:create" + cellType + "Cell(\"Container0" + n + "\", \"" + n + "\");'>" + linkText + "</a>");
        } else {
            for(var n=0; n<10; n++) setElement("Empty" + (10*tabNum + n), "<a href='javascript:create" + cellType + "Cell(\"Container" + (10*tabNum + n) + "\", \"" + (10*tabNum + n) + "\");'>" + linkText + "</a>");
        }
    }
}
function createMenuTab(tabNum, mainText, numText, linkURL, linkText) {
    setElement("Tab" + tabNum + "Main", mainText);
    setElement("Tab" + tabNum + "Num", ((numText=="" || numText.substring(0, 10) == "Click here" || numText.substring(0, 2) == "<a")?numText:"Click here to add new " + numText));
    if((linkURL != "") && document.getElementById("InstructionLink" + tabNum)) {
        document.getElementById("InstructionLink" + tabNum).href = linkURL;
        document.getElementById("InstructionLink" + tabNum).innerHTML = "IRS Instructions on " + linkText;
    }
    cellDisplay("MenuTab" + tabNum, true);
}
function showMenuTab() {
    var bContinue;
    for(var i = 1; i < 12; i++) {
        cellDisplay("Table" + i, false);
        cellDisplay("MenuTab" + i, false);
    }
    var j = 1;
    while(j < 11 && getElement("Tab" + j + "Main") != "" && getElement("Tab" + j + "Main") != summaryText) j++;
    var menuTabText;
    for(var i = 0; i < lines.length; i++) {
        menuTabText = "";
        bContinue = false;
        switch (lines[i]) {
            case W2Line[0]: menuTabText = "W-2s"; break;
            case intLine[0].substring(0, 2): menuTabText = "Interest Income"; break;
            case ueLine[0]: menuTabText = "Unemployment Income"; break;
            case ssLine[0].substring(0, 2): menuTabText = "Social Security Income"; break;
            case dividendLine[0].substring(0, 2): menuTabText = "Dividend Income"; break;
            case "3": menuTabText = "Dependent Information"; break;
            case educatorLine[0]: menuTabText = "Educator Deduction"; break;
            case IRALine[0]: menuTabText = "IRA Deduction"; break;
            case studentLine[0]: menuTabText = "Student Loan Deduction"; break;
            case tuitionLine[0]: menuTabText = "Tuition Deduction"; break;
        }
        for(var n = 0; n < 11; n++) {   //This loop checks to see if the table has already been set up
            if(getElement("Tab" + n + "Main") == menuTabText) {
                bContinue = true;
                cellDisplay("MenuTab" + n, true);
                break;
            }
        }
        if(bContinue) continue;
        if(menuTabText != "") {
            if(lines[i] == W2Line[0]) {
                createMenuTab(j, "W-2s", "W-2s",
                                "http://www.irs.gov/taxtopics/tc401.html", "wages, tips, compensation");
                createW2Cell("Container" + (j==10?"0":j) + "0", (j==10?"0":j) + "0");
                emptyCellLoop(j, "W2", "New W-2");
            } else if(lines[i] == IRALine[0]) {
                createMenuTab(j, "IRA Deduction", "IRA Deduction",
                                "http://www.irs.gov/taxtopics/tc451.html", "IRA Deductions");
                createIRACell("Container" + (j==10?"0":j) + "0", (j==10?"0":j) + "0");
                emptyCellLoop(j, "IRA", "New IRA Deduction");
            } else {
                switch(lines[i]) {
                    case intLine[0].substr(0, 2): 
                        createMenuTab(j, menuTabText, menuTabText + " item", "http://www.irs.gov/taxtopics/tc403.html", "interest income");
                        break;
                    case ueLine[0]:
                        createMenuTab(j, menuTabText, menuTabText + " item", "http://www.irs.gov/taxtopics/tc418.html", "unemployment income");
                        break;
                    case dividendLine[0].substr(0, 2): 
                        createMenuTab(j, menuTabText, menuTabText + " item", "http://www.irs.gov/taxtopics/tc404.html", "dividend income");
                        break;
                    case ssLine[0].substr(0, 2): 
                        createMenuTab(j, menuTabText, menuTabText + " item", "http://www.irs.gov/taxtopics/tc423.html", "social security and railroad retirement income");
                        break;
                    case "3":
                        createMenuTab(j, menuTabText, menuTabText, "http://www.irs.gov/taxtopics/tc354.html", "dependents");
                        break;
                    case educatorLine[0]:
                        createMenuTab(j, menuTabText, menuTabText, "http://www.irs.gov/taxtopics/tc458.html", "educator expense deductions");
                        break;
                    case tuitionLine[0]:
                        if(getElement("canBeClaimed0") == "No" || getElement("canBeClaimed1") == "No")
                            createMenuTab(j, menuTabText, menuTabText, "http://www.irs.gov/taxtopics/tc457.html", "tuition deductions");
                        break;
                    case studentLine[0]:
                        createMenuTab(j, menuTabText, menuTabText, "http://www.irs.gov/taxtopics/tc456.html", "student loan interest deductions");
                        break;
                }
                createCell("Container" + (j==10?"0":j) + "0", (j==10?"0":j) + "0", lines[i]);
                emptyCellLoop(j, "", "New " + menuTabText + " item", lines[i]);
            }
            cellDisplay("MenuTab" + j, true);
            j++;
        }
    }
    for(i = j; i < 11; i++) {
        if(document.getElementById("InstructionLink" + j)) {
            document.getElementById("InstructionLink" + j).href = "";
            document.getElementById("InstructionLink" + j).innerHTML = "";
        }
    }
    //Show summary tab
    if(j < 11) createMenuTab(j, summaryText, "Click here for summary and forms", "", "");
    else setElement("MenuTab" + j, summaryText);

    cellDisplay("MenuTab" + j, true);
    j++;
    //Show cookies tab
    if(j < 11) createMenuTab(j, cookiesText, "Click here to save what you've entered or get info you saved before", "", "");
    else setElement("MenuTab" + j, cookiesText);

    cellDisplay("MenuTab" + j, true);
    j++;
    //Show previous/next navigation
    if(j < 11)
        createMenuTab(j, 
            "<a href='javascript:histBack();' id='previous' style='display:none'>Previously visited page</a>",
            "<a href='javascript:histForward();' id='next' style='display:none'>Next visited page</a>", "", "");
    else 
        setElement("MenuTab" + j, "<a href='javascript:histBack();' id='previous' style='display:none'>Previously visited page</a> &nbsp; <a href='javascript:histForward();' id='next' style='display:none'>Next visited page</a>");

    cellDisplay("MenuTab" + j, true);
    cellDisplay("Menu", true);
    cellDisplay("LeaveIntro", true);
    if(document.getElementById("Table1").style.display == "none") showTab(1, false);
    histoire = new Array(1);
    cellDisplay("previous", false);
    cellDisplay("next", false);
}
function showTab(tabNum, fromHistory) {
    var bSummary = false;
    var bCookies = false;
    if(getElement("Tab" + tabNum + "Main") == summaryText || getElement("MenuTab" + tabNum) == summaryText) bSummary = true;
    if(getElement("Tab" + tabNum + "Main") == cookiesText || getElement("MenuTab" + tabNum) == cookiesText) bCookies = true;
    if(!fromHistory) histAdd(tabNum);
    if(tabNum == 0 && !fromHistory) intro(0);
    if(tabNum == 0) intro(0);
    if(bCookies && cookiesLoaded == -1) {
        alert("The scripts that handle saving and retrieving are still loading, please try again in a minute.");
        return;
    }
    if(bSummary && summaryLoaded == -1) {
        alert("The scripts that handle the summary page are still loading, please try again in a minute.");
        return;
    }
    for (var i = 0; i < 13; i++) {
        cellDisplay("Table" + i, (i == tabNum && bSummary == false && bCookies == false));
        if(i == tabNum && getElement("Tab" + i + "Num").substr(0, 5) == "Click") cellDisplay("Tab" + i + "Num", false);
        else cellDisplay("Tab" + i + "Num", true);
        if(i == tabNum) document.getElementById("MenuTab" + i).style.backgroundColor = "Yellow";
        else document.getElementById("MenuTab" + i).style.backgroundColor = "#addfff";
    }
    cellDisplay("Table11", bSummary);
    if(bCookies) cellDisplay("Table12", bCookies);
    if(bSummary) calculateAndShowSummary(0);
    else if(bCookies)  ; //Nothing to do
    else if(tabNum == 0) cellDisplay("Menu", false);
    else if(tabNum > 0 && document.getElementById("Menu").style.display == "none") showMenuTab();
    document.body.style.backgroundColor = (tabNum == 0) ? "#ffffff" : "#addfff";
    cellDisplay("initialHeader", (tabNum == 0));
    selectTab = tabNum;
    GenerateAd();
}
function intro(num, fromHistory) {
    //if(!fromHistory) histAdd("intro¡" + num);
    cellDisplay("cookiesFound", false);
    cellDisplay("introFooter", true);
    if(introPage == 0 || introPage == 6 || validate(introPage)) {
        if(num == -1 || (num == -2 && getElement("maritalStatus") != "married1")) {
            showTab(1);
            return;
        }
        if(num == -2) num = 5;
        for(var i = 0; i < 7; i++) cellDisplay("intro" + i, (i==num));
        introPage = num;
        if(num < 4) document.getElementById("continueLink").href = "javascript:intro(" + (num + 1) + ");";
        else if(num == 5) document.getElementById("continueLink").href = "javascript:intro(-1);";
        else if(num == 4) document.getElementById("continueLink").href = "javascript:intro(-2);";
        else {
            for(var i = 1; i < 4; i++) {
                if(!validate(i, true)) {
                    document.getElementById("continueLink").href = "javascript:intro(" + i + ");";
                    break;
                }
            }
        }
    }
    //showTab(0, true);
}
