/*
 * Copyright 2008 Squawkfox
 * All rights reserved.
 */


var count = 1;

function AddRow() {
    count++;
    
    var row = document.createElement("tr");
    
    var counterTD = document.createElement("td");
    counterTD.innerHTML = count;
    row.appendChild(counterTD);
    
    var itemTd = document.createElement("td");
    var itemInput = document.createElement("input");
    itemInput.setAttribute("id", "item" + count);
    itemInput.setAttribute("type", "text");
    itemInput.setAttribute("class", "itemInput");
    itemInput.setAttribute("size", "32");
    itemTd.appendChild(itemInput);
    row.appendChild(itemTd);
    
    var amountTd = document.createElement("td");
    var amountInput = document.createElement("input");
    amountInput.setAttribute("id", "amount" + count);
    amountInput.setAttribute("type", "text");
    amountInput.setAttribute("class", "amountInput");
    amountInput.setAttribute("size", "6");
    amountTd.appendChild(amountInput);
    row.appendChild(amountTd);
    
    var weightTd = document.createElement("td");
    var weightInput = document.createElement("input");
    weightInput.setAttribute("id", "weight" + count);
    weightInput.setAttribute("type", "text");
    weightInput.setAttribute("class", "weightInput");
    weightInput.setAttribute("size", "6");
    weightTd.appendChild(weightInput);
    row.appendChild(weightTd);
    
    var tableBody = document.getElementById("entryTableBody");
    tableBody.appendChild(row);
}


function CalcAverage() {
    var total = 0;
    for (var i = 1; i <= count; i++) {
        var itemAmount = document.getElementById("amount" + i).value;
        
        if (isNaN(itemAmount)) {
            continue;
        }
        
        total += (itemAmount * ReadWeightingAsNumber(i) / 100);
    }
    
    return total;
}

function VerifyInput() {
    document.getElementById("message").innerHTML = "";
    var total = 0;
    for (var i = 1; i <= count; i++) {
        total += ReadWeightingAsNumber(i);
    }
    
    if (total != 100) {
        document.getElementById("message").innerHTML = "Portfolio % total must add up to 100%. Total currently is " + total + "%";
        return false;
    }
    
    return true;
}


function ReadWeightingAsNumber(rowNumber) {
    var value = document.getElementById("weight" + rowNumber).value;
    if (isNaN(value)) {
        return 0;
    }
    
    if (value < 1) {
        value = value * 100;
    }
    
    return (value * 1);
}

function ReadTotalPortfolioAsNumber() {
    var value = document.getElementById("portfolioValue").value;
    if (isNaN(value)) {
        return 0;
    }
    
    return value * 1;
    
}

function ClearCalculator() {
    count = 1;
    
    tableBody = document.getElementById('entryTableBody');
    
    while (tableBody.getElementsByTagName("tr").length > 1) {
        nodeToDelete = tableBody.getElementsByTagName("tr")[tableBody.getElementsByTagName("tr").length - 1];        
        tableBody.removeChild(nodeToDelete);
    }
    
    document.getElementById('item1').value = "";
    document.getElementById('amount1').value = "";
    document.getElementById('weight1').value = "";
    
    document.getElementById('portfolioValue').value = 0;
    document.getElementById('total').innerHTML = "0";
    document.getElementById('expense').innerHTML = "0.00";
}


function ShowWeightedAverage() {
    VerifyInput();
    var averageMER = CalcAverage();
    document.getElementById("total").innerHTML = Round(averageMER, 4);
    document.getElementById("expense").innerHTML = Round(averageMER * ReadTotalPortfolioAsNumber() / 100, 2);
}


var popupWindow;
function ShowReport(reportFile) {
    ShowWeightedAverage();
    popupWindow = window.open(reportFile, "reportWindow");
}


function ReportPopupCallback() {
    var doc = popupWindow.document;
    doc.getElementById('AverageMERContainer').innerHTML = document.getElementById('total').innerHTML;
    doc.getElementById('YearlyExpenseContainer').innerHTML = document.getElementById('expense').innerHTML;
    
    for (var i = 1; i <= count; i++) {
        var fundName = document.getElementById('item' + i).value;
        var mer = document.getElementById('amount' + i).value;
        var proportion = document.getElementById('weight' + i).value;
        
        var tableBody = doc.getElementById('FundListBody');
        
        var row = doc.createElement("tr");
        
        var indexTd = doc.createElement("td");
        indexTd.innerHTML = i;
        row.appendChild(indexTd);
        
        var fundNameTd = doc.createElement("td");
        if (fundName.length > 0) {
            fundNameTd.innerHTML = fundName;
        }
        else {
            fundNameTd.innerHTML = "&nbsp;";
        }
        row.appendChild(fundNameTd);
        
        var merTd = doc.createElement("td");
        merTd.innerHTML = mer;
        if (merTd.innerHTML.length == 0) {
            merTd.innerHTML = 0;
        }
        row.appendChild(merTd);
        
        var proportionTd = doc.createElement("td");
        if (proportion.length > 0) {
            proportionTd.innerHTML = proportion;
        }
        else {
            proportionTd.innerHTML = "0";
        }
        row.appendChild(proportionTd);
        
        var investmentTd = doc.createElement("td");
        investmentTd.innerHTML = FormatCurrency(document.getElementById('portfolioValue').value * proportion / 100);
        row.appendChild(investmentTd);
             
        var expenseTd = doc.createElement("td");
        expenseTd.innerHTML = FormatCurrency(document.getElementById('portfolioValue').value * proportion / 100 * mer / 100);
        row.appendChild(expenseTd);
                        
        tableBody.appendChild(row);
    }
    
    
    doc.getElementById('TotalMER').innerHTML = document.getElementById('total').innerHTML;
    doc.getElementById('TotalInvestment').innerHTML = FormatCurrency(document.getElementById('portfolioValue').value);
    doc.getElementById('TotalExpense').innerHTML = FormatCurrency(document.getElementById('expense').innerHTML);
    
}


function FormatCurrency(value) {
    return "$" + Round(value, 2);
}


function Round(value, decimals) {
    if (decimals <= 0) {
        return Math.round(value);
    }
    
    var mult = Math.pow(10, decimals);
 
    return ((Math.round(mult * value)) / mult);
}
