﻿<html>

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

<body onload="defaultStatus()">
    
    <!--This JavaScript code is used to control the save Draft button on the CRM claims form. If a claim has the In-Process status, 
        it can be overwritten to be saved as a draft. If it has another status, the button is disabled.-->
    <script type="text/javascript">

        function defaultStatus() {

            //access mileage expense subgrid, check for an expense line item, and put value into var
            var mileage = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getTotalRecordCount();
            //alert(mileage);

            //access the appointment completed value and put it into a var
            var appointmentCompletedStatus = window.parent.Xrm.Page.getAttribute('btsss_appointmentcompleted').getValue();
            //alert(appointmentCompletedStatus);

            //get formtype
            var formType = window.parent.Xrm.Page.ui.getFormType();

            //get claim status
            var claimStatus = window.parent.Xrm.Page.getAttribute('btsss_status').getValue();

            /*setting the default claim status based on whether ot not the form is new, 
            if there are expense line items, and the value of the Appointment Completed field */

            if (claimStatus == '421750000') {//check for Saved claim status
                if (mileage != 0) {//check for expense item
                    if (appointmentCompletedStatus == true) { //check Appointment completed
                        window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750001'); // Yes = In-Process 
                    }
                    else {
                        window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750011');// No = Submitted
                    }
                }
                else {//there was no expense item
                    window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750000');// keep the claim status as saved
                }            
            }
            else if (claimStatus == '421750007') {//check for incomplete status
                if (mileage != 0) {//check for expense item
                    if (appointmentCompletedStatus == true) { //check Appointment completed
                        window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750001'); // Yes = In-Process 
                    }
                    else {
                        window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750011');// No = Submitted
                    }
                }
            }

            /* this is the code that was previously used to set status

            if (claimStatus != '421750000') {//check to see if claim status is saved
                if (formType != 1) { //check for a form that isn't new
                    if (mileage != 0) { //check to see if there is an expense line item
                        if (appointmentCompletedStatus == true) { //check Appointment completed
                            window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750001'); // Yes = In-Process 
                        }
                        else {
                            window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750011');// No = Submitted
                        }
                    }
                    else {
                        window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750007'); //If there are no expense items, the claim must be saved as Incomplete
                    }
                }
                else {
                    window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750007'); //if the form is new, it must be saved as Incomplete
                }
        }
        */
            //call checkForDefault function
            checkForSubmittedDate();

        }

        function checkForSubmittedDate() {
            var submittedDate = window.parent.Xrm.Page.getAttribute("btsss_submitteddate").getValue();//get value of the Submitted Date field
            //alert(submittedDate);
            
            if (submittedDate != null) {
                document.getElementById("draftButton").disabled = true; //if the status is not one of the default options, disable the button
            }

        }

        function draftButton() {

            //set claim status to saved
            window.parent.Xrm.Page.getAttribute("btsss_status").setValue('421750000');

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

        }



    </script>

    <button type="button" id="draftButton" onclick="draftButton()" title="Save this claim as a draft.">Save as Draft</button>

</body>
</html>
