﻿<html>

<head>
    <meta charset="utf-8" />
</head>

<body>

    <!--This Javascript code adds up the expense types requested and places the total in a new field.-->
    <script type="text/javascript">

        function totalExpenses() {

            var filteredRecordCount = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getTotalRecordCount();
            //alert(filteredRecordCount);

            //Access the mileage subgrid and pull out the cost column
            var mileageRows = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getRows().get(0).getData();
            var mileageEntity = mileageRows.getEntity();
            var mileageAttributes = mileageEntity.getAttributes();
            var mileageCost = mileageAttributes.get("btsss_costrequestedmileage").getValue();
            //alert(mileageCost);      

            //remove the $ from the cost strings
            while (mileageCost.charAt(0) === '$') {
                mileageCost = mileageCost.substr(1);
            }

            //Checks to see if there are other rows on the sub-grid and, if yes, pulls out their cost column
            //This section of code is just for testing purporses - the travel clerk will not have multiple mileage expenses for 1 claim
            if (filteredRecordCount > 1) {
                mileageRows1 = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getRows().get(1).getData();
                    var mileageEntity1 = mileageRows1.getEntity();
                    var mileageAttributes1 = mileageEntity1.getAttributes();
                    var mileageCost1 = mileageAttributes1.get("btsss_costrequestedmileage").getValue();

                    while (mileageCost1.charAt(0) === '$') {
                        mileageCost1 = mileageCost1.substr(1);
                    } 
            }
            else {
                mileageCost1 = 0;
            }

            //This test code will now work if the subgrid is set to only display 2 rows
            //other rows can be found, but not accessed unless you are on the subgrid pages that display those records
            if (filteredRecordCount > 2) {
                mileageRows2 = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getRows().get(2).getData();
                var mileageEntity2 = mileageRows2.getEntity();
                var mileageAttributes2 = mileageEntity2.getAttributes();
                var mileageCost2 = mileageAttributes2.get("btsss_costrequestedmileage").getValue();

                while (mileageCost2.charAt(0) === '$') {
                    mileageCost2 = mileageCost2.substr(1);
                }
            }
            else {
                mileageCost2 = 0;
            }
            //alert(mileageCost1);
            //end testing code


            //change strings to float for calculations
            mileageCost = parseFloat(mileageCost);
            mileageCost1 = parseFloat(mileageCost1);
            mileageCost2 = parseFloat(mileageCost2);

            //calculates the total
            var total = mileageCost + mileageCost1 + mileageCost2;
            //alert(total);

            //sets total into Total Cost Requested field
            window.parent.Xrm.Page.getAttribute("btsss_claimtotalreimbursementcostrequested").setValue(total);

            //saves the form
            window.parent.Xrm.Page.data.entity.save();
        }

    </script>

    <button type="button" onclick="totalExpenses()" title="Calculate the Total">
        Calculate Total
    </button>

</body>
</html>