﻿//////
//////  global variables
//////

var priceData = new Array(data_Cash_Load, data_Direct_Deposit, data_Pin_Purchase, data_Signature_Purchase, data_ATM_Withdraw, data_Enrollment_Costs, data_Monthly_Fee);

//Event Handlers

function pageLoad()
{
    var sel = document.getElementById("selLoadPerMonth");
    if (sel != undefined)
    {
        var times = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        loadSelectbox(sel, times, times);
    }
    
    sel = document.getElementById("selMonthlyUsage");
    if (sel != undefined)
    {
        var times = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        loadSelectbox(sel, times, times);
    }
    
    sel = document.getElementById("selFundingOption");
    if (sel != undefined)
    {
        var fundingoptions = new Array("Cash Load", "Direct Deposit");
        var fundingoptionsValue = new Array("0", "1");
        loadSelectbox(sel, fundingoptions, fundingoptionsValue);
    }
    
    sel = document.getElementById("selTransactionType")
    if (sel != undefined)
    {
        var transactionTypes = new Array("Pin Purchase", "Signature Purchase", "ATM Withdrawl");
        var transactionTypesValue = new Array("2", "3", "4");
        loadSelectbox(sel, transactionTypes, transactionTypesValue);
    }
    
    sel = document.getElementById("selCompetitor")
    if (sel != undefined)
    {
        var competitors = new Array("Western Union", "Green Dot®");
        var competitorsValue = new Array(1,2);
        loadSelectbox(sel, competitors, competitorsValue);
    }
}

function btnCompare_Click()
{
    var totalSave = document.getElementById("lblTotalSave");
    
    var selFO = document.getElementById("selFundingOption");
    var foValue = selFO.options[selFO.selectedIndex].value;
    
    var selLPM = document.getElementById("selLoadPerMonth");
    var lmpValue = selLPM.options[selLPM.selectedIndex].value;
    
    var selTRNT = document.getElementById("selTransactionType");  
    var ttValue = selTRNT.options[selTRNT.selectedIndex].value;
    
    var selMU = document.getElementById("selMonthlyUsage");
    var muValue = selMU.options[selMU.selectedIndex].value;
    
    var selCP = document.getElementById("selCompetitor");
    var cpValue = selCP.options[selCP.selectedIndex].value;
    
    var foData = priceData[foValue];
    var trntData = priceData[ttValue];
    var ecData = priceData[5];
    var mfData = priceData[6];
    
    var mgCost = (foData[0] * lmpValue + trntData[0] * muValue) * 12 + ecData[0] + mfData[0] * 12;
    var totalMF = mfData[cpValue] * 12;
    if (cpValue == 1 && foValue == 1)
    {
        totalMF = 0;
    }
        
    var cptCost = (foData[cpValue] * lmpValue + trntData[cpValue] * muValue) * 12 + ecData[cpValue] + totalMF;
    
    totalSave.innerHTML = CurrencyFormatted(cptCost-mgCost);
}


//Functions

function writeDataRow(rHeight, rBgColor, className, instName, instIndex, cAlign)
{
    document.write('<tr>');
    document.write("   <td height='" + rHeight + "' align='" + cAlign + "' bgcolor='" + rBgColor + "' class='" + className  + "'>" + instName  + "</td>");
    document.write("   <td align='center' bgcolor='" + rBgColor + "' class='" + className + "'>" + CurrencyFormatted(priceData[3][instIndex]) + "</td>");
    document.write("   <td align='center' bgcolor='" + rBgColor + "' class='" + className + "'>" + CurrencyFormatted(priceData[2][instIndex]) + "</td>");
    document.write("   <td align='center' bgcolor='" + rBgColor + "' class='" + className + "'>" + CurrencyFormatted(priceData[4][instIndex]) + "</td>");
    document.write("   <td colspan='2' align='center' bgcolor='" + rBgColor + "' class='" + className + "'>" + CurrencyFormatted(priceData[0][instIndex]) + "</td>");
    document.write("</tr>");
}
function addOption(selectbox,text,value )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function loadSelectbox(selectbox, option_texts, option_values)
{
    for (var i=0; i<option_texts.length; i++)
    {
        addOption(selectbox, option_texts[i], option_values[i]);
    }
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	//s = minus + s;
	if (amount > 0)
	    return "$" + s;
	else
	    return minus + "$" + s;
}
