{% extends "::base.html.twig" %} 

{% block body %}

        <div id="override_javascript_alert"></div>

        <form id="clientform" action="{{ path('DorcasTabithaBundle_finance_update', { 'client_id' : finance.client.clientid } ) }}" method="post" {{ form_enctype(form) }} class="dorcas">
            {% include 'DorcasTabithaBundle:Finance:financelayout.html.twig' %}
        </form>

{% endblock %}

{% block javascripts %}

{{ parent() }}

<script type="text/javascript">

window.alert = function(message) {
	$('#override_javascript_alert').html(message).dialog({
		modal:true,
		title:'Tabitha Validation Message',
		buttons: {
			'OK':function(){
				$(this).dialog('close');
			}
		}
	});
};

function calculate(field)
{

// ToDo:  merge income and expense
// Changes:  Changed to run without input variable passed

//var oForm = field.form;
//var list = oForm.getElementsByTagName("div");
var list = document.getElementsByTagName("div");
var totalIncome = 0;
var totalIncomeValue = 0;

for(var i=0;i<list.length;i++){
   if(list[i].className == 'financeinput'){
    if(list[i].parentNode.id == 'topLeftCenter' || list[i].parentNode.id == 'topRight'){
//      list[i].style.backgroundColor = 'black';
      totalIncomeValue = Number(list[i].childNodes[1].value);
     if( isNaN(totalIncomeValue)){
        list[i].childNodes[1].value = 0;
        list[i].childNodes[1].focus();
        alert("&#8227 Please enter whole number only, no <br />&nbsp; dollar sign.");
      }
      totalIncome = totalIncome + totalIncomeValue;
 //     oForm.form_totalIncome.value = totalIncome;
        var fieldTotalIncome = list.totalincome.children.form_totalIncome;
        fieldTotalIncome.value = totalIncome;
//      oForm.form_totalIncome.value = totalIncome.toFixed(2);
         }}}


var totalExpenses = 0;
var totalExpensesValue = 0;

for(var i=0;i<list.length;i++){
   if(list[i].className == 'financeinput'){
    if(list[i].parentNode.id == 'bottomLeftCenter' || list[i].parentNode.id == 'bottomRight'){
//      list[i].style.backgroundColor = 'black';
      totalExpensesValue = Number(list[i].childNodes[1].value);
     if( isNaN(totalExpensesValue)){      
        list[i].childNodes[1].value = 0;
        list[i].childNodes[1].focus();
        alert("&#8227 Please enter whole number only, no <br />&nbsp;  dollar sign.");
      }
      totalExpenses = totalExpenses + totalExpensesValue;
 //     oForm.form_totalExpenses.value = totalExpenses;
        var fieldTotalExpenses = list.totalexpense.children.form_totalExpenses;
        fieldTotalExpenses.value = totalExpenses;
//      oForm.form_totalExpenses.value = totalExpenses.toFixed(2);
         }}}

    var deficit = 0;
    var fieldDeficit = list.surplusdeficit.children.form_deficit;
    fieldDeficit.value = totalIncome - totalExpenses;
    //oForm.form_deficit.value = totalIncome - totalExpenses;
    // change the color to red if negative
    if( fieldDeficit.value < 0 )
    {
        fieldDeficit.style.color="red";
    }
    else
    {
        fieldDeficit.style.color="black";
    }

}


window.onload = calculate(document); 

</script>

    
{% endblock %}
