/******************************************************************************
 *                                                                            *
 * This section of Javascript deals with page layout. ie, showing and hiding  *
 * steps and other styling. It also contains the reStart() function which     *
 * goes back to the start screen and clears all form values.                  *
 *                                                                            *
 ******************************************************************************/

// Added by Ryan 2005-06-22
// used to be in <body onLoad="..." ...> in finance_IE1_e/f.js
//window.onload = toggleVis('step0');
window.onload = reStart;

var currTop = "";

///////////////////////////////////////////////////////////////////////////////

/**
 * Global variable to hold the current language
 */

var ffk_lang = "E";

/**
 * Sets which language to use (E[nglish] or F[rench]) when displaying messages
 */
function ffk_setLang(lang)
{
    ffk_lang = lang;
}

///////////////////////////////////////////////////////////////////////////////

/**
 * This function sets all boxes to blank and returns the user to the start of the FFA kit
 */
function reStart()
{
    moveAwayFromView();
    thePressedButton('step1');
    toggleVis('step0');
    document.forms["frmSelectInfo"].elements[0].selectedIndex=0;
    document.forms["frmSelectInfo"].elements[1].selectedIndex=0;

    /** short for form frmShowValuesLiquidity */
    var FSVL = document.frmShowValuesLiquidity;
    /** short for form frmShowValuesEquity */
    var FSVE = document.frmShowValuesEquity;
    /** short for form frmShowValuesCTR */
    var FSVCTR = document.frmShowValuesCTR;

    var DF = document.forms["FinanceCalculator"]; // decprecated form?
    var DF2 = document.forms["FinanceCalculator2"];
    var DF3 = document.forms["FinanceCalculator3"];
    var DF4 = document.forms["FinanceCalculator4"];
    var DF5 = document.forms["FinanceCalculator5"];
    var DF6 = document.forms["FinanceCalculator6"];
    var DF7 = document.forms["FinanceCalculator7"];

    /////////////////////////////////////////////
    // Clear all values

    FSVL.txtLiquidityData.value = "";
    FSVL.txtProvinceData.value =  "";
    FSVL.txtEnterpriseData.value = "";

    FSVE.txtEquityData.value = "";
    FSVE.txtProvinceData.value = "";
    FSVE.txtEnterpriseData.value = "";

    FSVCTR.txtCapitalTurnOverRatioData.value = "";
    FSVCTR.txtProvinceData.value = "";
    FSVCTR.txtEnterpriseData.value = "";

    DF2.txtCurrentLiabilities.value = "";
    DF2.txtCurrentAssets.value = "";

    DF3.txtCurrentLiab.value = "";
    DF3.txtCurrentAss.value = "";
    DF3.txtLongTermAss.value="";
    DF3.txtLongTermLiab.value = "";
    DF3.txtPerCentEquity.value="";

    DF4.txtGrossIncomeA.value = "";
    DF4.txtVariableCostsA.value = "";
    DF4.txtFixedCosts.value = "";
    DF4.txtUnpaidOperatorLabour.value = "";
    DF4.txtIncomeTaxExpense.value = "";
    DF4.txtNetIncomeAfterTaxesB.value = "";
    DF4.txtNetIncomeAfterTaxesA.value = "";
    DF4.txtBeginningYearAssets.value = "";
    DF4.txtEndingYearAssets.value = "";
    DF4.txtAverageTotalAssets.value = "";
    DF4.txtAverageTotalAssetsA.value = "";
    DF4.txtReturnOnAssets.value = "";

    DF5.txtAverageTotalAssetsB.value = "";
    DF5.txtCapitalTurnOverRatio.value = "";
    DF5.txtGrossIncomeB.value = "";
    DF6.txtNetIncomeAfterTaxesC.value = "";
    DF6.txtGrossIncomeC.value = "";
    DF6.txtNetProfitMargin.value = "";
    DF7.txtGrossIncomeD.value = "";
    DF7.txtVariableCostsB.value = "";
    DF7.txtGrossMargin.value = "";
    DF7.txtGrossMarginRatio.value = "";

    DF2.txtLiquidity.value = "";
}

/**
 * This is an important global variable that comes into play when the "tips"
 * button is pressed, then the user returns to the kit, it stores the step
 * number that they were on.
 */
var theCurrElem;


/**
 * Toggles visibility of an element
 */
function toggleVis(currElem)
{
    thisDiv = document.getElementById(currElem);
    thisDiv.style.display = "";

    // I have no idea what the hell this is supposed to do, but I left it in
    // for comedic purposes - Ryan
    if(currElem == '')
    {
        currElem == theCurrElem;
    }

    for(var i = 0; i < 9; i++)
    {
            thisDiv = document.getElementById("step"+i);
            thisDiv.style.display="none";
    }

    {
        thisDiv = document.getElementById(currElem);

        if (thisDiv.style.display=="")
            thisDiv.style.display="";
        else
            thisDiv.style.display="";

        if(currElem != 'step9')
            theCurrElem = currElem;
    }
}

/**
 * Go to next step.
 */
function nextStep()
{
    if (theCurrElem == "step8" || theCurrElem == "step9")
        return;

    if (theCurrElem.substring(0, 4) == "step")
    {
        var step = theCurrElem.substring(4);
        step++;

        thePressedButton("step" + step);
    }
}

/**
 * Open glossary window
 */
function openFarmFinanceKitGlossary(location)
{
    MM_openBrWindow(lcoation, 'ffk_glossary','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=543,height=595');
}


/**
 * moves the buttonss layer into view and toggles the latest steps visibility
 */
function otherMoveToView(currElem){
    /*        thisDiv = document.getElementById(currElem);
              thisDiv.style.display="";
              toggleVis(theCurrElem);
    */
    thisDiv = document.getElementById(currElem);
    thisDiv.style.display = "none";
    document.getElementById('buttonss').style.display = "";
    toggleVis(theCurrElem);
}

/**
 * moves the buttonss layer into view and toggles step 1's visibility
 */
function moveToView(currElem)
{
    thisDiv = document.getElementById('buttonss');
    thisDiv.style.display = "";
    toggleVis('step1');
}

/**
 * moves the buttonss layer out of view and toggles step 0's visibility
 */
function moveAwayFromView()
{
    thisDiv = document.getElementById('buttonss');
    thisDiv.style.display = "none";
    toggleVis('step0');
}

function moveAwayFromView2()
{
    thisDiv = document.getElementById('buttonss');
    thisDiv.style.display="none";
    toggleVis('step9');
}

function thePressedButton(thePressedBtn)
{
    //if(thePressedButton='step0'){moveAwayFromView('buttonss');}

    domm = eval("document.getElementById('btn" + thePressedBtn + "')");
    showData(document.forms["frmSelectInfo"].elements["txtProvince"], document.forms["frmSelectInfo"].elements["txtEnterprise"]);

    domm.style.border = "1px outset #40637A";
    domm.style.font.color = "#40637A";
    domm.style.border.color = "#40637A";
    domm.style.color = "#40637A";
    domm.style.background = "#FFFFFF";

    toggleVis(thePressedBtn);

    resetOtherButtons(thePressedBtn);
}

function resetOtherButtons(thePressedBtn)
{
    domm = eval("document.getElementById('btn" + thePressedBtn + "')");

    for (var i=0; i<=8; i++)
    {
        notdomm = eval("document.getElementById('btnstep" + i + "')");

        if (notdomm != domm)
        {
            notdomm.style.border = "2px outset #ffffff";
            notdomm.style.font.color = "#000000";
            notdomm.style.color = "#000000";
            notdomm.style.background = "#c0c0c0";
        }
    }
}



/******************************************************************************
 *                                                                            *
 * This section of Javascript deals with all the business logic. It includes  *
 * calculation functions as well as other operations for each step.           *
 *                                                                            *
 ******************************************************************************/

////////////////////////////////////////////////////////////////////////////////

/**
 * Step 2: this function calculates Liquidity
 *
 * liquidity = (current assets / current liabilities)
 *
 * @param input1 current assets
 * @param input2 current liabilities
 */
function CalcLiquidity(input1, input2)
{
    var DF = document.forms["FinanceCalculator"];
    var DF2 = document.forms["FinanceCalculator2"];
    var DF3 = document.forms["FinanceCalculator3"];

    // If the user enters a value that includes commas or other characters, the
    // function "stripTheCommas" will take these commas out so that the number
    // can be used properly in the calculation.
    var currentAssets = stripTheCommas(input1.value);
    var currentLiabilities = stripTheCommas(input2.value);

    var currentRatioValue = currentAssets / currentLiabilities;

    // The function roundOffNumber will be used to take "currentRatioValue",
    // and reduce the number of significant figures in the final calculation
    var currentRatioValueRounded = roundOffNumber(currentRatioValue);

    // This last part of this function will populate the fields of later steps
    // only if these fields were not left empty in the first place
    var errorChecked;
    errorChecked=errorCheck(currentRatioValueRounded);

    if (errorChecked == "OK")
    {
        // Fill in next fields automatically
        DF3.txtCurrentLiab.value = putTheCommas(currentLiabilities);
        DF3.txtCurrentAss.value = putTheCommas(currentAssets);

        // This is the final answer to Liquidity
        DF2.txtLiquidity.value = putTheCommas(currentRatioValueRounded);
    }
}

////////////////////////////////////////////////////////////////////////////////

/**
 * Step 3
 *
 * @param inpCurrentAssets          current assets
 * @param inpLongTermAssets         long-term assets
 * @param inpCurrentLiabilities     current liabilities
 * @param inpLongTermLiabilities    long-term liabilities
 */
function CalcSolvency(inpCurrentAssets, inpLongTermAssets, inpCurrentLiabilities,
                        inpLongTermLiabilities)
{
    var DF = document.forms["FinanceCalculator"];
    var DF2 = document.forms["FinanceCalculator2"];
    var DF3 = document.forms["FinanceCalculator3"];

    var currentAssets = stripTheCommas(inpCurrentAssets.value);
    var longTermAssets = stripTheCommas(inpLongTermAssets.value);
    var currentLiabilities = stripTheCommas(inpCurrentLiabilities.value);
    var longTermLiabilities = stripTheCommas(inpLongTermLiabilities.value);

    var totalAssets = currentAssets + longTermAssets;
    var totalLiabilities = currentLiabilities + longTermLiabilities;

    var Equity=totalAssets-totalLiabilities;
    var percentEquity = Equity/totalAssets;

    var errorChecked;
    errorChecked = errorCheck(percentEquity);

    if (errorChecked == "OK")
    {
        var percentEquityRounded = roundOffNumber(percentEquity);

        // this chops off any extra figures, in case of slight rounding error
        percentEquityRounded = (percentEquityRounded * 1000) / 10;

        DF3.txtPerCentEquity.value = percentEquityRounded + "%";

        // do step 2, too - this is if the user did not have the figures for
        // their Current Assets and Current Liabilities
        var currentRatioValue = currentAssets / currentLiabilities;

        var currentRatioValueRounded = roundOffNumber(currentRatioValue);

        DF2.txtCurrentLiabilities.value = putTheCommas(currentLiabilities);
        DF2.txtCurrentAssets.value = putTheCommas(currentAssets);
    }
}
////////////////////////////////////////////////////////////////////////////////

/**
 * Step 4a
 *
 * @param inpGrossIncomeA           gross income
 * @param inpVariableCosts          variable costs
 * @param inpFixedCosts             fixed costs
 * @param inpUnpaidOperatorLabour   unpaid operator labour
 * @param inpIncomeTaxExpenses      income tax expenses
 */
function CalcNetIncomeAfterTaxes(inpGrossIncomeA, inpVariableCosts, inpFixedCosts,
                                   inpUnpaidOperatorLabour,inpIncomeTaxExpense)
{
    var DF4 = document.forms["FinanceCalculator4"];
    var DF6 = document.forms["FinanceCalculator6"];
    var DF7 = document.forms["FinanceCalculator7"];

    var GrossIncome = stripTheCommas(inpGrossIncomeA.value);
    var VariableCosts = stripTheCommas(inpVariableCosts.value);
    var FixedCosts = stripTheCommas(inpFixedCosts.value);
    var UnpaidOperatorLabour = stripTheCommas(inpUnpaidOperatorLabour.value);
    var IncomeTaxExpense = stripTheCommas(inpIncomeTaxExpense.value);

    var CostsTotal = VariableCosts + FixedCosts + UnpaidOperatorLabour + IncomeTaxExpense;
    var NetIncomeAfterTaxes = GrossIncome - CostsTotal;

    var errorChecked;
    errorChecked = errorCheck(NetIncomeAfterTaxes);

    if (errorChecked == "OK")
    {
        var NetIncomeAfterTaxes = putTheCommas(NetIncomeAfterTaxes);

        DF4.txtNetIncomeAfterTaxesA.value = NetIncomeAfterTaxes;
        DF4.txtNetIncomeAfterTaxesB.value = NetIncomeAfterTaxes;
        DF6.txtNetIncomeAfterTaxesC.value = NetIncomeAfterTaxes;
        DF7.txtVariableCostsB.value = putTheCommas(VariableCosts);
        DF4.txtNetIncomeAfterTaxesA.value = NetIncomeAfterTaxes;
        DF4.txtNetIncomeAfterTaxesB.value = NetIncomeAfterTaxes;
        DF6.txtNetIncomeAfterTaxesC.value = NetIncomeAfterTaxes;
        DF7.txtVariableCostsB.value = putTheCommas(VariableCosts);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * Step 4b
 *
 * @param inpBeggingYearAssets beginning year assets
 * @param inpEndingYearAssets  ending year assets
 */
function CalcAverageTotalAssets(inpBeginningYearAssets, inpEndingYearAssets)
{
    var BeginningYearAssets = stripTheCommas(inpBeginningYearAssets.value);
    var EndingYearAssets = stripTheCommas(inpEndingYearAssets.value);

    var DF = document.forms["FinanceCalculator"];
    var DF4 = document.forms["FinanceCalculator4"];
    var DF5 = document.forms["FinanceCalculator5"];

    var AverageTotalAssets = (BeginningYearAssets + EndingYearAssets) / 2;

    var errorChecked;
    errorChecked = errorCheck(AverageTotalAssets);

    if (errorChecked == "OK")
    {
        AverageTotalAssets = putTheCommas(AverageTotalAssets);

        DF4.txtAverageTotalAssets.value = AverageTotalAssets;
        DF4.txtAverageTotalAssetsA.value = AverageTotalAssets;
        DF5.txtAverageTotalAssetsB.value = AverageTotalAssets;
        DF4.txtReturnOnAssets.value = "";
    }

}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * Step 4c
 *
 * @param inpNetIncomeTaxesAfterTaxes   net income taxes after taxes
 * @param inpAverageTotalAssets         average total assets
 */
function CalcReturnOnAssets(inpNetIncomeAfterTaxes, inpAverageTotalAssets)
{
    var NetIncomeAfterTaxes = stripTheCommas(inpNetIncomeAfterTaxes.value);
    var AverageTotalAssets = stripTheCommas(inpAverageTotalAssets.value);

    var ReturnOnAssets = (NetIncomeAfterTaxes * 100)/ AverageTotalAssets;

    var errorChecked;
    errorChecked=errorCheck(ReturnOnAssets);

    if (errorChecked=="OK")
    {
        var ReturnOnAssetsRounded = roundOffNumber(ReturnOnAssets);
        document.FinanceCalculator4.txtReturnOnAssets.value = ReturnOnAssetsRounded +"%";
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * Step 5
 *
 * @param inpGrossIncomeB       gross income
 * @param inpAverageTotalAssets average total assets
 */
function CalcCapitalTurnOverRatio(inpGrossIncomeB, inpAverageTotalAssets)
{
    var DF5 = document.forms["FinanceCalculator5"];

    var GrossIncome = stripTheCommas(inpGrossIncomeB.value);
    var AverageTotalAssets = stripTheCommas(inpAverageTotalAssets.value);

    var CapitalTurnOverRatio = GrossIncome / AverageTotalAssets;
    var CapitalTurnOverRatioRounded = roundOffNumber(CapitalTurnOverRatio);

    errorChecked = errorCheck(CapitalTurnOverRatioRounded);

    // put commas in answer if necessary
    CapitalTurnOverRatioRounded = putTheCommas(CapitalTurnOverRatioRounded);

    DF5.txtCapitalTurnOverRatio.value = CapitalTurnOverRatioRounded;

}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * Step 6
 *
 * @param inpNetIncomeAfterTaxesC net income after taxes
 * @param inpGrossIncomeC gross income
 */
function CalcNetProfitMargin(inpNetIncomeAfterTaxesC, inpGrossIncomeC)
{
    var DF6 = document.forms["FinanceCalculator6"];

    var NetIncomeAfterTaxes = stripTheCommas(inpNetIncomeAfterTaxesC.value);
    var GrossIncome = stripTheCommas(inpGrossIncomeC.value);

    var NetProfitMargin = NetIncomeAfterTaxes/GrossIncome;

    errorChecked = errorCheck(NetProfitMargin);

    if (errorChecked == "OK")
    {
        var NetProfitMarginRounded = roundOffNumber(NetProfitMargin);
        var NetProfitMarginRounded = (NetProfitMargin * 100);

        DF6.txtNetProfitMargin.value = putTheCommas(Math.floor(NetProfitMarginRounded)) + "%";
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * Step 7
 *
 * @param inpGrossIncomeD   gross income
 * @param inpVariableCostsB variable costs
 */
function CalcGrossMarginRatio(inpGrossIncomeD,inpVariableCostsB)
{
    var DF7 = document.forms["FinanceCalculator7"];

    var GrossIncome = stripTheCommas(inpGrossIncomeD.value);
    var VariableCosts = stripTheCommas(inpVariableCostsB.value);

    var GrossMargin = GrossIncome - VariableCosts;
    var GrossMarginRatio = GrossMargin / GrossIncome;

    DF7.txtGrossMargin.value = putTheCommas(GrossMargin);

    errorChecked = errorCheck(GrossMarginRatio);

    if (errorChecked == "OK")
    {
        var GrossMarginRatioRounded = roundOffNumber(GrossMarginRatio);
        DF7.txtGrossMarginRatio.value=GrossMarginRatioRounded;
    }
}

/////////////////////////////////////////////////////////////////////////////////////////
function isKnownNetIncomeAfterTaxes(input)
{
    var DF4 = document.forms["FinanceCalculator4"];
    var DF6 = document.forms["FinanceCalculator6"];

    var theNum;

    theNum = input.value;

    DF4.txtNetIncomeAfterTaxesB.value = theNum;
    DF6.txtNetIncomeAfterTaxesC.value = theNum;
}

/////////////////////////////////////////////////////////////////////////////////////////
function isKnownGrossIncome(input){

    var DF5 = document.forms["FinanceCalculator5"];
    var DF6 = document.forms["FinanceCalculator6"];
    var DF7 = document.forms["FinanceCalculator7"];

    var theNum;

    theNum = input.value;

    DF5.txtGrossIncomeB.value = theNum;
    DF6.txtGrossIncomeC.value = theNum;
    DF7.txtGrossIncomeD.value = theNum;
}

/////////////////////////////////////////////////////////////////////////////////////////
function roundOffNumber(theNumberToRound)
{
    var theRoundedNumber = theNumberToRound;
    theRoundedNumber = theRoundedNumber * 1000;

    theRoundedNumber = Math.ceil(theRoundedNumber) / 1000;
    return theRoundedNumber;
}

/////////////////////////////////////////////////////////////////////////////////////////
function checkTheNum(input)
 {
    var oldnum;
    var newnum;
    var ch;
    oldnum = input.value;
    newnum ="";

    for (var i=0; i < oldnum.length; i++)
    {
        ch = oldnum.charAt(i);
        if (ch != ",")
            newnum += ch;
    }

    return parseFloat(newnum);
}

/////////////////////////////////////////////////////////////////////////////////////////
function checkTheDollarValue(input)
{
    input.value = checkTheNum(input);
    input.value = parseFloat(input.value);

    if (input.value == null || input.value.length == 0 || input.value == "NaN")
    {
        if (ffk_lang == "E")
            alert("Only numerals, decimals and commas are acceptable entries.");
        else
            alert("Seuls les chiffres, les décimales et les virgules sont des entrées acceptables.");

        input.value = "";
        input.focus();
    }
    else if (parseFloat(input.value) < 0)
    {
        input.value = "";
    }

    input.value = putTheCommas(input.value);
}

/////////////////////////////////////////////////////////////////////////////////////////
function putTheCommas(input)
{
    var result, len;

    input += "";
    len = input.indexOf(".");

    if (len == -1)
    {
        len = input.length;
        result = "";
    }
    else
    {
        result = input.substring(len, input.length);
    }

    while (len > 3)
    {
        result = input.substring(len-3, len) + result;
        result = "," + result;
        len -= 3;
    }

    result = input.substring(0, len) + result;
    return result;
}

/////////////////////////////////////////////////////////////////////////////////////////
function stripTheCommas(input)
{
    var newnum;
    var ch;

    newnum ="";

    for (var i=0; i<input.length; i++)
    {
        ch = input.charAt(i);
        if(ch != ",")
            newnum += ch;
    }

    return parseFloat(newnum);
}

/////////////////////////////////////////////////////////////////////////////////////////
function errorCheck(theNumber)
{
    /**
     * The value to return if everything is okay with the result of the
     * calculation passed into this function
     */
    var errorChecked = 0;

    if (isNaN(theNumber))
    {
        if (ffk_lang == "E")
            alert("Please do not leave any fields blank or enter '0'. Use estimated values where necessary, even when planning financing for a new operation.");
        else
            alert("Veuillez ne laisser aucun champ vide ou inscrivez « 0 ». Au besoin, utilisez des montants estimatifs, même lorsque vous planifiez le financement d'une nouvelle exploitation.");
    }
    else
    {
        if (theNumber == "Infinity")
        {
            if (ffk_lang == "E")
                alert("Please do not leave any fields blank or enter '0'. Use estimated values where necessary, even when planning financing for a new operation.");
            else
                alert("Veuillez ne laisser aucun champ vide ou inscrivez « 0 ». Au besoin, utilisez des montants estimatifs, même lorsque vous planifiez le financement d'une nouvelle exploitation.");
        }
        else
        {
            errorChecked = "OK";
            return errorChecked;
        }
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

var strBC, strAB, strSK, strMB, strON, strQC, strAtlantic, strCanada;

if (ffk_lang == "E")
{
    strBC = "BC";
    strAB = "AB";
    strSK = "SK";
    strMB = "MB";
    strON = "ON";
    strQC = "QC";
    strAtl = "Atlantic";
    strCan = "Canada";
}
else
{
    strBC = "C.-B.";
    strAB = "AB";
    strSK = "SK";
    strMB = "MB";
    strON = "ON";
    strQC = "QC";
    strAtl = "Atlantique";
    strCan = "Canada";
}

// Liquidity Averages
A2 = new Array(strBC,  2.129, 2.363, 2.541, 3.696, 3.411, 1.828, 1.898, 1.841, 2.634);
A0 = new Array(strAB,  2.976, 1.876, 5.147, 2.711, 4.454, 1.828, 1.898, 1.841, 2.634);
A5 = new Array(strSK,  1.870, 1.753, 2.541, 2.803, 4.454, 1.828, 1.898, 1.841, 2.634);
A3 = new Array(strMB,  1.839, 1.753, 2.166, 2.672, 4.454, 1.828, 1.898, 1.841, 2.634);
A4 = new Array(strON,  1.989, 1.718, 2.298, 2.231, 2.385, 1.828, 1.898, 1.841, 2.634);
A6 = new Array(strQC,  2.346, 1.624, 1.987, 2.477, 3.330, 1.828, 1.898, 1.841, 2.634);
A1 = new Array(strAtl, 2.477, 2.257, 2.082, 2.429, 2.929, 1.828, 1.898, 1.841, 2.634);
A7 = new Array(strCan, 2.129, 1.753, 2.541, 2.651, 3.010, 1.828, 1.898, 1.841, 2.634);

// Equity (Solvency) Averages
B2 = new Array(strBC,  83.139, 82.045, 71.940, 90.260, 83.984, 80.158, 78.166, 72.799, 65.081);
B0 = new Array(strAB,  86.479, 76.970, 84.095, 85.514, 82.332, 80.158, 78.166, 72.799, 65.081);
B5 = new Array(strSK,  80.388, 75.072, 71.940, 82.047, 82.332, 80.158, 78.166, 72.799, 65.081);
B3 = new Array(strMB,  79.821, 75.072, 75.842, 83.104, 82.332, 80.158, 78.166, 72.799, 65.081);
B4 = new Array(strON,  86.479, 76.063, 68.317, 86.981, 82.040, 80.158, 78.166, 72.799, 65.081);
B6 = new Array(strQC,  76.094, 72.589, 57.341, 77.579, 81.731, 80.158, 78.166, 72.799, 65.081);
B1 = new Array(strAtl, 82.460, 77.687, 57.629, 86.838, 79.731, 80.158, 78.166, 72.799, 65.081);
B7 = new Array(strCan, 83.139, 75.072, 71.940, 84.903, 82.347, 80.158, 78.166, 72.799, 65.081);

for (var i = 1; i < 10; i++)
{
    B0[i] = Math.round(B0[i]) / 100;
    B1[i] = Math.round(B1[i]) / 100;
    B2[i] = Math.round(B2[i]) / 100;
    B3[i] = Math.round(B3[i]) / 100;
    B4[i] = Math.round(B4[i]) / 100;
    B5[i] = Math.round(B5[i]) / 100;
    B6[i] = Math.round(B6[i]) / 100;
    B7[i] = Math.round(B7[i]) / 100;
}

// Capital Turn Over Ratio Averages
C2 = new Array(strBC,  0.164, 0.115, 0.327, 0.100, 0.168, 0.190, 0.359, 0.290, 0.575);
C0 = new Array(strAB,  0.144, 0.141, 0.236, 0.173, 0.232, 0.190, 0.359, 0.290, 0.575);
C5 = new Array(strSK,  0.199, 0.142, 0.327, 0.191, 0.232, 0.190, 0.359, 0.290, 0.575);
C3 = new Array(strMB,  0.213, 0.142, 0.393, 0.173, 0.232, 0.190, 0.359, 0.290, 0.575);
C4 = new Array(strON,  0.119, 0.130, 0.304, 0.165, 0.210, 0.190, 0.359, 0.290, 0.575);
C6 = new Array(strQC,  0.163, 0.156, 0.399, 0.246, 0.250, 0.190, 0.359, 0.290, 0.575);
C1 = new Array(strAtl, 0.168, 0.142, 0.508, 0.142, 0.361, 0.190, 0.359, 0.290, 0.575);
C7 = new Array(strCan, 0.164, 0.142, 0.327, 0.174, 0.218, 0.190, 0.359, 0.290, 0.575);


/** Basic Enterprise Categories */
var E0;

if (ffk_lang == "E")
{
    E0 = new Array("Grain and Oilseed",
                   "Dairy",
                   "Hogs",
                   "Beef",
                   "Poultry and Egg",
                   "Fruit and Nut",
                   "Vegetable",
                   "Potato",
                   "Greenhouse & Nursery");
}
else
{
    E0 = new Array("Céréales et oléagineux",
                   "Lait",
                   "Porc",
                   "B&oelig;uf",
                   "Volaille et &oelig;ufs",
                   "Fruits et de noix",
                   "Légumes",
                   "Pommes de terre",
                   "Serres et pépiniéres");
}


/**
 * This function is called when an enterprise or province is selected in
 * Step 1. It will display an alert letting the user know that national or
 * regional statistics are being used instead of provincial.
 *
 */

function enterpriseAlert()
{
    var SP = document.forms["frmSelectInfo"].elements["txtProvince"].selectedIndex;
    var SE = document.forms["frmSelectInfo"].elements["txtEnterprise"].selectedIndex;


    if (SP == 8)
        return;

    if (isCanadaData(SP, SE))
    {
        if (ffk_lang == "E")
            alert('Please note that provincial statistics are not available for ' + eval("E0" + "[" + (SE-1) + "]") + ' enterprises. The ratios displayed will be based on Canada.');
        else
            alert('Veuillez noter que les statistiques provinciales ne sont pas disponibles pour les entreprises ' + eval("E0" + "[" + (SE-1) + "]") + ". Les ratios affichés sont calculés à l'échelle du Canada.");
    }

    // Combined Poultry & Egg stats for prairies (AB, SK, MB)
    if (isPrairieData(SP, SE))
    {
        if (ffk_lang == "E")
            alert("Please note that provincial statistics are not available for poultry and egg enterprises.  The ratios displayed will be based on statistics from Alberta, Saskatchewan and Manitoba combined.");
        else
            alert("Veuillez noter que les statistiques provinciales ne sont pas disponibles pour les entreprises des secteurs avicole et ovocole. Les ratios affichés sont fondés sur les statistiques de l'Alberta, de la Saskatchewan et du Manitoba confondues.");

    }
}

/**
 * Determines whether if current data being used is Canadian data instead of provincial data
 *
 * Returns true or false
 *
 * This function is filled with magic numbers. Check out the frmSelectInfo
 * form to see what they are referring to.
 */

function isCanadaData(SP, SE)
{
    return (
            // All provinces
            //   6 - Fruit & Nut
            //   7 - Vegetable
            //   8 - Potato
            //   9 - Greenhouse & Nursery
            (SE == 6 || SE == 7 || SE == 8 || SE == 9) ||
            // 3 - BC
            //   1 - Grain and Oilseed
            //   3 - Hogs
            (SP == 3 && (SE == 1 || SE == 3)) ||
            // 4 - Manitoba
            //   2 - Dairy
            (SP == 4 && (SE == 2)) ||
            // 6 - Saskatchewan
            //   2 - Dairy
            //   3 - Hogs
            (SP == 6 && (SE == 2 || SE ==3))
           );
}

/**
 * Determines whether if current data being used is regional (prairie) data instead of provincial data
 *
 * Returns true or false
 *
 * This function is filled with magic numbers. Check out the frmSelectInfo
 * form to see what they are referring to.
 */

function isPrairieData(SP, SE)
{
    return (
            // All prairie provinces (AB, SK, MB)
            //   5 - Poultry & Egg
            (SP == 1 || SP == 4 || SP == 6) && (SE == 5)
           );
}


/**
 * This function is called on steps 2,3, and 5
 * if the enterprise selected is less than the 5th selection in the option box
 * (step 1) the value of each steps province box will match whatever province
 * the user selected because each province has distinct data available for each
 * of the enterprises as listed below:
 *
 */
function showData(theProvince,theEnterprise)
{
    /** the selected province */
    var SP = document.forms["frmSelectInfo"].elements[0].selectedIndex;
    /** the selected enterprise */
    var SE = document.forms["frmSelectInfo"].elements[1].selectedIndex;

    if (SP == 0 || SE == 0 || SP == null || SE == null)
        { }
    else
    {
        var LiquidityData = eval("A" + (SP-1) + "[" + (SE) + "]");
        var EquityData = eval("B" + (SP-1) + "[" + (SE) + "]")*100;
        var CapitalTurnOverRatioData = eval("C" + (SP-1) + "[" + (SE) + "]");

        /** show values liquidity */
        var FSVL = document.frmShowValuesLiquidity;
        /** show values equity */
        var FSVE = document.frmShowValuesEquity;
        /** show values CTR */
        var FSVCTR = document.frmShowValuesCTR;

        // Default to current province
        FSVL.txtProvinceData.value =  eval("A" + (SP-1) + "[0]");
        FSVE.txtProvinceData.value = eval("A" + (SP-1) + "[0]");
        FSVCTR.txtProvinceData.value = eval("A" + (SP-1) + "[0]");

        // Check to see if we're using Canadian data instead of provincial
        if (isCanadaData(SP, SE))
        {
            FSVL.txtProvinceData.value =  "Canada";
            FSVE.txtProvinceData.value = "Canada";
            FSVCTR.txtProvinceData.value = "Canada";
        }
        // Check to see if we're using prairie data isntead of provincial
        if (isPrairieData(SP, SE))
        {
            FSVL.txtProvinceData.value = "Prairies";
            FSVE.txtProvinceData.value = "Prairies";
            FSVCTR.txtProvinceData.value = "Prairies";
        }

        FSVL.txtLiquidityData.value = LiquidityData;
        FSVL.txtEnterpriseData.value = eval("E0" + "[" + (SE-1) + "]");

        FSVE.txtEquityData.value = EquityData + "%";
        FSVE.txtEnterpriseData.value = eval("E0" + "[" + (SE-1) + "]");

        FSVCTR.txtCapitalTurnOverRatioData.value = CapitalTurnOverRatioData;
        FSVCTR.txtEnterpriseData.value = eval("E0" + "[" + (SE-1) + "]");
    }
}

