﻿(function() 
    {    
        // Attach "InitForm" event handler on page load
        if (window.addEventListener) window.addEventListener("load", InitForm, false);
        else if (window.attachEvent) window.attachEvent("onload", InitForm);
        
        /////////////////////////////////////////////////
        // Adds event handlers to targeted form elements
        function InitForm()
        {
            var aElem = null;   // Holds an array of input elements
            
            // Get all input elements from current form
            aElem = document.body.getElementsByTagName("input");
            
            // Loop through the array of input elements and add event handlers only to those elements that have a custom attribute "totalType"
            for (i=0; i<aElem.length; i++)
            {
                if (aElem[i].getAttribute("totalType") != null)
                {
                    aElem[i].onchange = function(){ CalculateLineTotal(this)};
                }                
            }             
        }
        
        /////////////////////////////////////////////////////////
        // Calculates line total for the targeted input element
        function CalculateLineTotal(elem)
        {
            var totalType = elem.getAttribute("totalType");
            var total = 0;
            var resElem = null;
            var msg = "$";
            
            switch(totalType)
            {
                case "entryFee":
                    total = elem.value * 30;                   
                    break;
                case "chairFee":
                    total = elem.value * 2;                   
                    break;
                case "tableFee":
                    total = elem.value * 8;                   
                    break;
                case "CCpowerFee":
                    total = elem.value * 25;                   
                    break;
                case "CCentryFee":
                    total = elem.value * 75;                   
                    break;
                case "CCchairFee":
                    total = elem.value * 2;                   
                    break;
                case "CCtableFee":
                    total = elem.value * 8;                 
                    break;  

            }
            
            // Write the value to the span tag associated with the current input element
            resElem = elem.nextSibling.nextSibling;
            resElem.innerHTML = "$" + total.toFixed(2);
                        
            UpdateGrandTotal();            
        }
        
        ///////////////////////////////////////////////////////
        // Updates Grand Total field on the form
        function UpdateGrandTotal()
        {
            var aElem = document.getElementsByTagName("span");
            var strTemp = "";
            var nTemp = 0;
            var nGrandTotal = 0;
            var oTotal = null;
            var strResult = "";
            
            // Loop through the array of "span" elements
            for (i=0; i<aElem.length; i++)
            {
                // Collect totals from all targeted "span" elements
                if (aElem[i].getAttribute("total") == "line")
                {
                    strTemp = aElem[i].innerHTML;
                    
                    // Remove the "$" sign from the number
                    strTemp = strTemp.substring(strTemp.lastIndexOf("$")+1);      
                   
                    if (!isNaN(parseFloat(strTemp)))
                    {
                        nTemp = Number(strTemp);
                        
                        // Add each total to Grand Total
                        nGrandTotal += nTemp;
                    }
                }               
                
                // Determine which form element is our Grand Total field
                if (aElem[i].getAttribute("total") == "grand")
                {
                    oTotal = aElem[i];
                } 
            }    
            
            // Update Grand Total after we've collected all totals
            strResult = (nGrandTotal > 0) ? "$" + nGrandTotal.toFixed(2) : "";            
            oTotal.innerHTML = strResult;
        }
    })();
