﻿function prepareToEatCookie() {
    var x = document.cookie;
    if(x.indexOf("pwd=") > 0) {
        cellDisplay("password", true);
        cellDisplay("clear", true);
        cellDisplay("go", false);
        cellDisplay("pwd2", false);
        cellDisplay("pwdMatch", false);
        cellDisplay("forgot", true);
    } else {
        eatCookie();
    }
}

function eatCookie() {
    if(confirm("Are you sure you want to delete the saved information?")) {
        document.cookie = "taxInfo=;expires=Thu, 01-Jan-1970 00:00:01 GMT;";
        //document.cookie = "perm=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
        cellDisplay("cookieLoad", false);
    }
}

function writeCookie(temp) {
    var ck = "";
    if(temp != "")
        if(temp.substr(0, 1) == "¡")
            temp = temp.substr(1);

        ck += "taxInfo=" + temp + ";expires=" + cookieExpire.toString();
/*    if(temp != "" && perm != "")
        ck += ";";
    if(perm != "")
        ck += "perm=" + perm + ";expires=" + cookiePerm.toString(); */
    document.cookie = ck;
    alert("Your information has been saved.");
}

function saveInformationToCookie() {
    var x, elementId, cellNum;
    var maritalStatus = getElement("maritalStatus");
    var temp = "";
    var pwd = getElement("pwd1");
    x = getElement("filerNameInput0");
    if(x == "") {
        alert("Please fill in your name on the filing status tab.  This information will help identify you when you retrieve the information.");
        return;
    }
    if(document.cookie != "" && document.cookie != null)
        if(!confirm("Do you want to overwrite the existing saved cookie?")) return;

    document.cookie = "test=pass";

    if(document.cookie == "" || document.cookie == null) {
        alert("Your browser has disabled cookies; your information cannot be saved.");
        return;
    }
    document.cookie = "test=; expires=Thu, 01-Jan-70 00:00:01 GMT;";

    if(pwd != "" && pwd == getElement("pwd2"))
        temp += "pwd=" + encryptDecrypt(pwd, true) + "¡";
    x = getElement("filerNameInput0");
    temp += "filerNameInput0=" + encryptDecrypt(x, true) + "¡";
    if(maritalStatus == "married1") {
        x = getElement("filerNameInput1");
        temp += "filerNameInput1=" + encryptDecrypt(x, true) + "¡";
    }
    for(var i=0; i < validateItems.length; i++) {
        var items = validateItems[i].split("¡");
        for(var j=0; j < items.length; j++) {
            if(!isNaN(items[j]))
                temp += "line" + items[j] + "=" + encryptDecrypt(getElement("line" + items[j])) + "¡";
        }
    }
    temp += "maritalStatus=" + encryptDecrypt(maritalStatus, true) + "¡";
    temp += "canBeClaimed0=" + encryptDecrypt(getElement("canBeClaimed0"), true) + "¡";
    temp += "blind0=" + encryptDecrypt(getElement("blind0"), true) + "¡";
    temp += "age0=" + encryptDecrypt(getElement("age0"), true) + "¡";
    temp += "residency0=" + encryptDecrypt(getElement("residency0"), true) + "¡";
    temp += "zip=" + encryptDecrypt(getElement("zip"), true) + "¡";
    temp += "state=" + encryptDecrypt(getElement("state"), true) + "¡";
    x = getElement("propTax");
    if(x != "") temp += "propTax=" + encryptDecrypt(x, true) + "¡";
    if(maritalStatus == "married1") {
        temp += "canBeClaimed1=" + encryptDecrypt(getElement("canBeClaimed1"), true) + "¡";
        temp += "blind1=" + encryptDecrypt(getElement("blind1"), true) + "¡";
        temp += "age1=" + encryptDecrypt(getElement("age1"), true) + "¡";
        temp += "residency1=" + encryptDecrypt(getElement("residency1"), true) + "¡";
    }
    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) {
            if(elementId.indexOf("SSN") == -1) {
                x = (getElement(elementId)).toString();
                if(x != "" && (isNaN(x) == true || parseFloat(x) != 0)) {
                    x = encryptDecrypt(x, true);
                    if(temp.length + x.length + elementId.length > 4049) {
                        writeCookie(temp);
                        return;
                    }
                    temp += elementId + "=" + x + "¡";
                }
            }
        }
    }
    writeCookie(temp);
    return;
}

function createPassword() {
    cellDisplay("password", true);
    cellDisplay("password2", true);
    cellDisplay("pwd2", true);
    cellDisplay("pwdMatch", false);
    cellDisplay("clear", false);
    cellDisplay("go", false);
    cellDisplay("forgot", false);
    cellDisplay("questions", false);
}

function prepareToReadCookie() {
    var x = document.cookie;
    if(x.indexOf("pwd=") > 0) {
        cellDisplay("password", true);
        cellDisplay("go", true);
        cellDisplay("pwd2", false);
        cellDisplay("pwdMatch", false);
        cellDisplay("forgot", true);
    } else {
        read_Cookie();
    }
}

function nextAvailableTab() {
    for(var i=1; i<11; i++)
        if(getElement("Tab" + i + "Main") == "") return i;
    for(var i=11; i<14; i++)
        if(getElement("MenuTab" + i) == "") return i;
}

function read_Cookie() {
    var x = document.cookie.replace("taxInfo=", "¡");//.replace("perm=", "¡");      //Strip off taxInfo=
    x = x.split("¡");
    var y = new Array();
    var z;
    var pos;
    var W2Cells = new Array();
    var intCells = new Array();
    var UECells = new Array();
    var SSCells = new Array();
    var divCells = new Array();
    var IRACells = new Array();
    var educatorCells = new Array();
    var tuitionCells = new Array();
    var studentCells = new Array();
    var depCells = new Array();
    var menuCells = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    var bMarried = false;
    //First loop, just see if the password is right
    for(var i = 0; i < x.length; i++) {
        pos = x[i].indexOf("=");
        if(pos > 0) {
            y.length = 0;
            y.push(x[i].substr(0, pos));
            y.push(x[i].substr(pos + 1));
            y[1] = encryptDecrypt(y[1], false);
            if(y[0] == "pwd" || y[0] == "¡pwd") {
                if(getElement("pwd1") != y[1]) {
                    alert("Incorrect password");
                    return;
                } else {
                    setElement("pwd1", y[1]);
                    setElement("pwd2", y[1]);
                }
            }
        }
    }
    //Second loop, see which cells we need to create
    for(var i = 0; i < x.length; i++) {
        if(x[i].substr(0, 1) == "n" && !isNaN(x[i].substr(1, 2))) {
            z = x[i].substr(1, 2);
            if(x[i].substr(3, W2Line[0].length) == W2Line[0]) {
                if(W2Cells.length > 0)
                    createMenuTab(W2Cells[0].substr(0, 1), "W-2s", "",
                                        "http://www.irs.gov/taxtopics/tc401.html", "wages, tips, compensation");

                createW2Cell("Container" + z, z);
                W2Cells.push(z);
            }
            if(x[i].substr(3,intLine[0].length) == intLine[0]) {
                if(intCells.length > 0)
                    createMenuTab(intCells[0].substr(0, 1), "Interest Income", "",
                                        "http://www.irs.gov/taxtopics/tc403.html", "interest income");

                createIntCell("Container" + z, z);
                intCells.push(z);
            }
            if(x[i].substr(3,dividendLine[0].length) == dividendLine[0]) {
                if(divCells.length > 0)
                    createMenuTab(divCells[0].substr(0, 1), "Dividend Income", "",
                                        "http://www.irs.gov/taxtopics/tc404.html", "dividend income");

                createDividendCell("Container" + z, z);
                divCells.push(z);
            }
            if(x[i].substr(3,ueLine[0].length) == ueLine[0]) {
                if(UECells.length > 0)
                    createMenuTab(UECells[0].substr(0, 1), "Unemployment Income", "",
                                        "http://www.irs.gov/taxtopics/tc418.html", "unemployment income");

                createUECell("Container" + z, z);
                UECells.push(z);
            }
            if(x[i].substr(3,ssLine[0].length) == ssLine[0]) {
                if(SSCells.length > 0)
                    createMenuTab(SSCells[0].substr(0, 1), "Social Security Income", "",
                                        "http://www.irs.gov/taxtopics/tc423.html", "social security and railroad retirement income");

                createSSCell("Container" + z, z);
                SSCells.push(z);
            }
            if(x[i].substr(3,educatorLine[0].length) == educatorLine[0]) {
                if(educatorCells.length > 0)
                    createMenuTab(educatorCells[0].substr(0, 1), "Educator Deduction", "",
                                        "http://www.irs.gov/taxtopics/tc458.html", "educator expense deductions");

                createEducatorCell("Container" + z, z);
                educatorCells.push(z);
            }
            if(x[i].substr(3,IRALine[0].length) == IRALine[0]) {
                if(IRACells.length > 0)
                    createMenuTab(IRACells[0].substr(0, 1), "IRA Deduction", "",
                                        "http://www.irs.gov/taxtopics/tc451.html", "IRA Deductions");

                createIRACell("Container" + z, z);
                IRACells.push(z);
            }
            if(x[i].substr(3,studentLine[0].length) == studentLine[0]) {
                if(studentCells.length > 0)
                    createMenuTab(studentCells[0].substr(0, 1), "Student Loan Deduction", "",
                                        "http://www.irs.gov/taxtopics/tc456.html", "student loan interest deductions");

                createStudentCell("Container" + z, z);
                studentCells.push(z);
            }
            if(x[i].substr(3,tuitionLine[0].length) == tuitionLine[0]) {
                if(tuitionCells.length > 0)
                    createMenuTab(tuitionCells[0].substr(0, 1), "Tuition Deduction", "",
                                        "http://www.irs.gov/taxtopics/tc457.html", "tuition deductions");

                createTuitionCell("Container" + z, z);
                tuitionCells.push(z);
            }
            if(x[i].substr(3,8) == "relation") {
                if(depCells.length > 0)
                    createMenuTab(depCells[0].substr(0, 1), "Dependent Information", "",
                                        "http://www.irs.gov/taxtopics/tc354.html", "dependents");

                createDependentCell("Container" + z, z);
                depCells.push(z);
            }
        } else if(x[i].substr(0, 4) == "line" && !isNaN(x[i].substr(4, 2))) {
            z = x[i].substr(4, 2);
            pos = x[i].indexOf("=");
            if(pos > 0) {
                y.length = 0;
                y.push(x[i].substr(0, pos));
                y.push(x[i].substr(pos + 1));
                y[1] = encryptDecrypt(y[1], false);
                if(y[1] == "yes" || y[1] == "maybe") {
                    switch(z) {
                        case W2Line[0] : menuCells[0] = 1; break;
                        case intLine[0].substr(0, 2) : menuCells[1] = 1; break;
                        case ueLine[0] : menuCells[2] = 1; break;
                        case ssLine[0].substr(0, 2) : menuCells[3] = 1; break;
                        case dividendLine[0].substr(0, 2) : menuCells[4] = 1; break;
                        case "3" : menuCells[5] = 1; break;
                        case educatorLine[0] : menuCells[6] = 1; break;
                        case IRALine[0] : menuCells[7] = 1; break;
                        case studentLine[0] : menuCells[8] = 1; break;
                        case tuitionLine[0] : menuCells[9] = 1; break;
                    }
                }
            }
        }
    }
    if(menuCells[0] == 1 && W2Cells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "W-2s", "Click here to add new W-2s",
                                "http://www.irs.gov/taxtopics/tc401.html", "wages, tips, compensation");
        createW2Cell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "W2", "New W-2");
    }
    if(menuCells[1] == 1 && intCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Interest Income", "Click here to add new Interest Income item",
                                "http://www.irs.gov/taxtopics/tc403.html", "interest income");
        createIntCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Interest item", intLine[0].substr(0, 2));
    }
    if(menuCells[2] == 1 && UECells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Unemployment Income", "Click here to add new Unemployment Income item",
                                "http://www.irs.gov/taxtopics/tc418.html", "unemployment income");
        createUECell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Unemployment Income item", ueLine[0]);
    }
    if(menuCells[3] == 1 && SSCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Social Security Income", "Click here to add new Social Security Income item",
                                "http://www.irs.gov/taxtopics/tc423.html", "social security and railroad retirement income");
        createSSCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Social Security item", ssLine[0].substr(0, 2));
    }
    if(menuCells[4] == 1 && divCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Dividend Income", "Click here to add new Dividend Income item",
                                "http://www.irs.gov/taxtopics/tc404.html", "dividend income");
        createDividendCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Dividend item", dividendLine[0]);
    }
    if(menuCells[5] == 1 && depCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Dependent Information", "Dependent Information item",
                                "http://www.irs.gov/taxtopics/tc354.html", "dependents");
        createDependentCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Dependent Information item", "3");
    }
    if(menuCells[6] == 1 && educatorCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Educator Deduction", "Click here to add new Educator Deduction item",
                                "http://www.irs.gov/taxtopics/tc458.html", "educator expense deductions");
        createEducatorCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Educator Deduction item", educatorLine[0]);
    }
    if(menuCells[7] == 1 && IRACells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "IRA Deduction", "Click here to add new IRA Deduction item",
                                "http://www.irs.gov/taxtopics/tc451.html", "IRA deductions");
        createIRACell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "IRA", "New IRA Deduction");
    }
    if(menuCells[8] == 1 && studentCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Student Loan Deduction", "Click here to add new Student Loan Deduction item",
                                "http://www.irs.gov/taxtopics/tc456.html", "student loan expense deductions");
        createStudentCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Student Loan Deduction item", studentLine[0]);
    }
    if(menuCells[9] == 1 && tuitionCells.length == 0) {
        z = nextAvailableTab();
        createMenuTab(z, "Tuition Deduction", "Click here to add new Tuition Deduction item",
                                "http://www.irs.gov/taxtopics/tc457.html", "tuition deductions");
        createTuitionCell("Container" + (z==10?"0":z) + "0", (z==10?"0":z) + "0");
        emptyCellLoop(z, "", "New Tuition Deduction item", tuitionLine[0]);
    }
    for(var i = 0; i < x.length; i++) {
        pos = x[i].indexOf("=");
        if(pos > 0) {
            y.length = 0;
            y.push(x[i].substr(0, pos));
            y.push(x[i].substr(pos + 1));
            y[1] = encryptDecrypt(y[1], false);
            if(y[0] == "pwd" || y[0] == "¡pwd") continue;

            setElement(y[0], y[1]);
            switch(y[0]) {
                case "maritalStatus": msChange(); bMarried = (y[1] == "married1"); break;
                case "canBeClaimed0": claimChange(0); cellSaveFiler("primary", false); break;
                case "canBeClaimed1": claimChange(1); cellSaveFiler("spouse", false); break;
                case "filerNameInput0": cellSaveFiler("primary", false); break;
                case "filerNameInput1": cellSaveFiler("spouse", false); break;
            }
        }
    }
    for(var i = 0; i < W2Cells.length; i++) cellSaveW2("Container" + W2Cells[i], false);
    for(var i = 0; i < intCells.length; i++) cellSaveInt("Container" + intCells[i], false);
    for(var i = 0; i < UECells.length; i++) cellSaveUE("Container" + UECells[i], false);
    for(var i = 0; i < divCells.length; i++) cellSaveDiv("Container" + divCells[i], false);
    for(var i = 0; i < SSCells.length; i++) cellSaveSS("Container" + SSCells[i], false);
    for(var i = 0; i < depCells.length; i++) cellSaveDependent("Container" + depCells[i], false);
    for(var i = 0; i < IRACells.length; i++) cellSaveIRA("Container" + IRACells[i], false);
    for(var i = 0; i < educatorCells.length; i++) cellSaveEducator("Container" + educatorCells[i], false);
    for(var i = 0; i < studentCells.length; i++) cellSaveStudent("Container" + studentCells[i], false);
    for(var i = 0; i < tuitionCells.length; i++) cellSaveTuition("Container" + tuitionCells[i], false);

    //Show summary tab
    if((z=nextAvailableTab()) < 11) createMenuTab(z, summaryText, "Click here for summary and forms", "", "");
    else setElement("MenuTab" + z, summaryText);

    cellDisplay("MenuTab" + z, true);
    //Show cookies tab
    if((z=nextAvailableTab()) < 11) createMenuTab(z, cookiesText, "Click here to save what you've entered or get info you saved before");
    else setElement("MenuTab" + z, cookiesText);

    cellDisplay("MenuTab" + z, true);
    //Show previous/next navigation
    if((z=nextAvailableTab()) < 11)
        createMenuTab(z, 
            "<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" + z, "<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" + z, true);
    
    cellDisplay("password", false);
    cellDisplay("Menu", true);
    showTab(1, false);
    histoire = new Array(1);
    cellDisplay("previous", false);
    cellDisplay("next", false);
    alert("Your information has been restored.");
}

function forgotPassword() {
    var q = new Array();
    var a = new Array();
    var inc = new Array();
    var pos;
    var x = document.cookie.replace("taxInfo=", "¡");
    var y = new Array();
    x = x.split("¡");
    for(var i = 0; i < x.length; i++) {
        pos = x[i].indexOf("=");
        if(pos > 0) {
            y.length = 0;
            y.push(x[i].substr(0, pos));
            y.push(x[i].substr(pos + 1));
            y[1] = encryptDecrypt(y[1], false);
            switch(y[0]) {
                case "filerNameInput0":
                    q.push("What did you list as the primary taxpayer's name?");
                    a.push("fïlér" + y[1] + "fïlér");
                    break;
                case "filerNameInput1":
                    q.push("What did you list as the spouse's name?");
                    a.push("spôüsé" + y[1] + "spôüsé");
                    break;
            }
        }
    }
    for(var i = 0; i < x.length; i++) {
        pos = x[i].indexOf("=");
        if(pos > 0) {
            y.length = 0;
            y.push(x[i].substr(0, pos));
            y.push(x[i].substr(pos + 1));
            y[1] = encryptDecrypt(y[1], false);
            if(y[0].indexOf("name") > 0) {
                if(y[0].indexOf("n0") == 0) {
                    q.push("Name one of your employers");
                    a.push("émplôÿér" + y[1] + "émplôÿér");
                } else if(y[0].indexOf("n5") == 0) {
                    q.push("Name one of your dependents");
                    a.push("dépéñdéñt" + y[1] + "dépéñdéñt");
                }
            } else if(y[0].indexOf(W2Line[0]) > 0 && y[0].substr(0, 1) == "n") {
                inc.push(y[1]);
            }
        }
    }
    if(q.length < 3) {
        var ql = q.length;
        for(var i = ql; i < ql + inc.length; i++) {
            q.push("What amount of wages did you list on one of your W-2s?");
            a.push("wâgés" + inc[i-ql] + "wâgés");
        }
    }
    var allAns = "";
    for (var i = 0; i < q.length; i++) {
        if(i < 3) {
            cellDisplay("qr" + (i+1), true);
            setElement("q" + (i+1), q[i]);
        }
        allAns += (a[i] + " ");
    }
    setElement("allAnswers", allAns);
    cellDisplay("questions", true);
}

function checkAnswers() {
    var correctAnswers = 0, wrongAnswers = 0, answer;
    var allAns = getElement("allAnswers").toLowerCase();
    for(var i = 1; i <= 3; i++) {
        if(document.getElementById("qr" + i).style.display == "") {
            question = getElement("q" + i);
            answer = getElement("a" + i).toLowerCase();
            if(question.indexOf("primary") != -1) answer = "fïlér" + answer + "fïlér";
            else if(question.indexOf("spouse") != -1) answer = "spôüsé" + answer + "spôüsé";
            else if(question.indexOf("employer") != -1) answer = "émplôÿér" + answer + "émplôÿér";
            else if(question.indexOf("dependent") != -1) answer = "dépéñdéñt" + answer + "dépéñdéñt";
            else if(question.indexOf("wages") != -1) answer = "wâgés" + answer + "wâgés";
            if(allAns.indexOf(answer) > -1) correctAnswers++;
            else wrongAnswers++;
        }
    }
    if(correctAnswers > wrongAnswers) {
        document.cookie = document.cookie.replace("pwd=", "");
        read_Cookie();
        setElement("pwd1", "");
        setElement("pwd2", "");
    } else alert("Sorry, those answers are not correct");
}
function displayCookie() {
    //alert(document.cookie);
    setElement("cookieShow", document.cookie);
    cellDisplay("cookieShow", true);
}
cookiesLoaded = 1;
