﻿//Called by Master Page
//Loads readonly fields
function body_onload()
{
    ddlOccupatiionSelectionChanged();
}
//ctl00$MainContentPlaceHolder$
//Event fires when the user clicks the checkbox beside the expense description
//Then sets the textbox value with the urban or rural value
//Sets the page values with calculated values
function cbExpense_CheckedChanged_js(control)
{
    var controlID = control.id;
    var cbID = controlID.substring(37, controlID.length);
    var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBox" + cbID);
    
    if (tb != null)
    {            
        if (window.document.getElementById("ctl00_MainContentPlaceHolder_CheckBox" + cbID).checked)
        {
            var ddl = window.document.getElementById("ctl00_MainContentPlaceHolder_ddlUrbanRural");
            var selValue = ddl.options[ddl.selectedIndex].value;
            if (selValue == "Urbain")
            {
                var hfUrban = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldUrban" + cbID);
                tb.value = addCommas(hfUrban.value);
            }
            else
            {
                var hfRural = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldRural" + cbID);
                tb.value = addCommas(hfRural.value);
            }
        }
        else
        {
            tb.value = "";
        }
    }
    setPageValues();
}

function occNameEnable()
{
var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxPay");
var oc = window.document.getElementById("ctl00_MainContentPlaceHolder_ddlOccupation");           
 
 oc.disabled = tb.value.length > 0
           
}

//Event fires when user enters their own salary
function TextBoxYourOwnMonthlyPay_TextChanged()        
{
    var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxPay");
    if (tb.value.length > 0)
    {
        if (IsNumeric(tb.value))
        {
            setPageValues();
        }
        else
        {
            alert("'votre propre valeur valeur pour salaire mensuel' n'est pas un nombre valide. Veuillez remplacer la valeur par un nombre valide et essayer encore !");
        }
    }
    else if (tb.value.length == 0)
    {
        setPageValues();    
    }
}
//Event fires when user changes a value for their own expense category
function tbExpense_TextChanged_js(control)
{
    setCheckBox(control);
    var controlID = control.id;
    var tb = window.document.getElementById(controlID);
    if (tb.value.length > 1)
    {
        if (IsNumeric(tb.value))
        {
            setPageValues();
        }
        else
        {
            alert("Votre valeur de dépenses n'est pas un nombre valide. Veuillez remplacer la valeur par un nombre valide et essayer encore !");
        }
    }
    else
    {
        setPageValues();
    }
}
//Called by tbExpense_TextChanged_js
//Checks or unchecks checkbox depending on the value in
//corresponding textbox
function setCheckBox(control)
{
    var controlID = control.id;
    var tbID = controlID.substring(36, controlID.length);
    var cbControl = window.document.getElementById("ctl00_MainContentPlaceHolder_CheckBox" + tbID);
    if (cbControl != null)
    {
        if (control.value.length > 0)
        {
            cbControl.checked = true;
        }
        else
        {
            cbControl.checked = false;
        }
    }
}
//Event fires when user changes occupations
//Scrapes occupation and salary from selected value and sets
//values in hidden fields
function ddlOccupatiionSelectionChanged()
{
    var hfOccupation = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldOccupation");
    var tbOccupation = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxChosenOccupation");
    var hfSalary = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldSalary");
    var ddl = window.document.getElementById("ctl00_MainContentPlaceHolder_ddlOccupation");
    var selValue = ddl.options[ddl.selectedIndex].value;
    if (selValue == "-99")
    {
        hfOccupation.value = "";
        tbOccupation.value = "";
        hfSalary.value = 0;
    }
    else
    {
        var occupation = selValue.substring(0, selValue.indexOf("$"))
        hfOccupation.value = occupation;
        tbOccupation.value = occupation;
        var salary = selValue.substring(selValue.indexOf("$") + 1)
        hfSalary.value = salary;
    }
    setPageValues();
}
//Event is fired when the user changes the selection on the rural urban drop down list
//The logic cycles through all the expenses and when the program finds a checked
//value the program sets the corresponding textbox with the value from the matching
//hidden field
function ddlUrbanRuralSelectionChanged()
{
    index = 1;
    while(true)
    {
        //The highest checkbox id is 57
        if (index == 58)
        {
            break;
        }
        else
        {
            var cb = window.document.getElementById("ctl00_MainContentPlaceHolder_CheckBox" + index.toString());
            if (cb == null)
            {
                index++;
            }
            else
            {
                if (cb.checked)
                {
                    var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBox" + index.toString());
                    var hfUrban = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldUrban" + index.toString());
                    var hfRural = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldRural" + index.toString());
                
                    var ddl = window.document.getElementById("ctl00_MainContentPlaceHolder_ddlUrbanRural");
                    var selValue = ddl.options[ddl.selectedIndex].value;
                    if (selValue == "Urbain")
                    {
                        //Check if user has replaced default values
                        if (tb.value.replace(",","") == hfRural.value)
                        {
                            tb.value = addCommas(hfUrban.value);
                        }
                    }
                    else
                    {
                        //Check if user has replaced default values
                        if (tb.value.replace(",","") == hfUrban.value)
                        {
                            tb.value = addCommas(hfRural.value);
                        }
                    }
                }                
                index++;
            }
        }
    }                    
    this.setPageValues();
}
//Get Salary and expense / calculate remaing salary and set the textboxes on the page        
function setPageValues()
{
    var salary = getSalary_js();
    var expenses = getTotalExpenses_js();
    var remainingSalary = salary - expenses;
    
    var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxRemainingBudget");
    tb.value = addCommas(remainingSalary.toString());
    
    tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxMonthlyDifference");
    tb.value = addCommas(remainingSalary.toString());            
    
    tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxTakeHomePay");
    tb.value = addCommas(salary.toString());            
    
    tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxTotalExpenses1");
    tb.value = addCommas(expenses.toString());            
    
    tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxTotalExpenses2");
    tb.value = addCommas(expenses.toString());            
    
    tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxTotalMonthlyExpenses");
    tb.value = addCommas(expenses.toString());        
}

//Return Salary from own salary text box or from selected value in drop down list
function getSalary_js()
{
    returnSalary = 0;
    var hfSalary = window.document.getElementById("ctl00_MainContentPlaceHolder_HiddenFieldSalary");
    var tbOwnSalary = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxPay");
    
    //This means the user has nulled the field
    if (tbOwnSalary.value.length == 0)
    {
        returnSalary = parseFloat(hfSalary.value);
    }
    else
    {
        //This means the user has prefixed with a $
        if (tbOwnSalary.value.length > 0 && IsNumeric(tbOwnSalary.value))
        {
            var salaryValue = tbOwnSalary.value.replace(",", "");
            returnSalary = parseFloat(salaryValue);
        }
    }
    return returnSalary;
}
//Calculates the total expense amounts for the current page.
function getTotalExpenses_js()
{
    returnExpenses = 0;
    var expense;
    index = 1;
    while(true)
    {
        //The highest checkbox id is 57
        if (index == 58)
        {
            break;
        }
        else
        {
            var cb = window.document.getElementById("ctl00_MainContentPlaceHolder_CheckBox" + index.toString());
            if (cb == null)
            {
                index++;
            }
            else
            {
                if (cb.checked)
                {
                    var tb = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBox" + index.toString());
                    if (tb.value.length > 0 && IsNumeric(tb.value))
                    {
                        returnExpenses = returnExpenses + parseFloat(tb.value.replace(",", ""));
                    }
                }                
                index++;
            }
        }
    }
    
    //Make sure to add their own expense items to the returned expense amount            
    var tbOwn = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxExp1");
    if (tbOwn.value.length > 0 && IsNumeric(tbOwn.value))
    {
        returnExpenses = returnExpenses + parseFloat(tbOwn.value.replace(",", ""));
    }
    tbOwn = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxExp2");
    if (tbOwn.value.length > 0 && IsNumeric(tbOwn.value))
    {
        returnExpenses = returnExpenses + parseFloat(tbOwn.value.replace(",", ""));
    }
    tbOwn = window.document.getElementById("ctl00_MainContentPlaceHolder_TextBoxExp3");
    if (tbOwn.value.length > 0 && IsNumeric(tbOwn.value))
    {
        returnExpenses = returnExpenses + parseFloat(tbOwn.value.replace(",", ""));
    }
    
    return returnExpenses;
}
function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
     Char = sText.charAt(i); 
     if (ValidChars.indexOf(Char) == -1) 
     {
        IsNumber = false;
     }
  }
  return IsNumber;
}   