I need help with this calculate script. In the first function, I cannot seem to assign a value to the global variable grandTotal for whatever reason. I want a span with id grandTotal to display it. If change it to a string it works. So i assigned a the value from the function to it.
How do I either get the global variable to accept it or add the total from the second function calculateChurch() total to it and display an updated total?
Code:
<script type="text/javascript">
var LHtotal;
var AHtotal;
var LMtotal;
var AMtotal;
var schoolTotal;
var churchTotal ;
var grandTotal;
function calculateSchool(orgType) {
var fund = parseInt(document.getElementById(orgType + 'fund').value);
var people = parseInt(document.getElementById(orgType + 'people').value);
var percent = (parseInt(document.getElementById(orgType + 'percent').value))/100;
var active = people * percent;
active = parseInt(active);
var baskets = parseInt(document.getElementById(orgType + 'baskets').value);
var numPerYear = parseInt(document.getElementById(orgType + 'numPerYear').value);
var price = 26.00;
var commision = 0.06;
var total = fund * active * baskets * numPerYear * price * commision;
total = total.toFixed(2);
LHtotal = parseFloat(document.getElementById("LHtotal").innerHTML);
AHtotal = parseFloat(document.getElementById("AHtotal").innerHTML);
LMtotal = parseFloat(document.getElementById("LMtotal").innerHTML);
AMtotal = parseFloat(document.getElementById("AMtotal").innerHTML);
//alert(LHtotal + " " + AHtotal + " " + LMtotal + " " + AMtotal);
schoolTotal = (LHtotal + AHtotal + LMtotal + AMtotal);
//alert(schoolTotal);
if(isNaN(schoolTotal)) {
schoolTotal = "";
}
if(isNaN(active)) {
active = "";
}
if(isNaN(total)) {
total = "";
}
var Ototal = parseFloat(document.getElementById("Ototal").innerHTML);
var Etotal = parseFloat(document.getElementById("Etotal").innerHTML);
var orgTotal = (Ototal + Etotal);
var orgTotal = orgTotal.toFixed(2);
grandTotal = Number(schoolTotal + orgTotal);
if(isNaN(grandTotal)) {
grandTotal = "";
}
if(isNaN(orgTotal)) {
orgTotal = "";
}
document.getElementById("orgTotal").innerHTML = orgTotal;
document.getElementById(orgType + 'total').innerHTML = total;
document.getElementById(orgType + 'active').innerHTML = active;
document.getElementById("schoolTotal").innerHTML = schoolTotal;
document.getElementById("grandTotal").innerHTML = schoolTotal;
}
function calculateChurch(orgType) {
var num = parseFloat(document.getElementById(orgType + "num").value);
var members = parseFloat(document.getElementById(orgType + "members").value);
var percent = parseFloat(document.getElementById(orgType + "percent").value)/100;
var baskets = parseFloat(document.getElementById(orgType + "baskets").value);
var fundPerYear = parseFloat(document.getElementById(orgType + "fundPerYear").value);
var total = num * members * percent * baskets * fundPerYear;
total = total.toFixed(2);
if(isNaN(total)) {
total = "";
}
document.getElementById(orgType + 'total').innerHTML = total;
var Ltotal = parseFloat(document.getElementById('Ltotal').innerHTML);
var Atotal = parseFloat(document.getElementById('Atotal').innerHTML);
churchTotal = (Ltotal + Atotal);
churchTotal = churchTotal.toFixed(2);
if(isNaN(churchTotal)) {
churchTotal = "";
}
document.getElementById("churchTotal").innerHTML = churchTotal;
document.getElementById("grandTotal").innerHTML = document.getElementById("grandTotal").innerHTML + churchTotal;
}