// This JavaScript code changes the Claim Status field when them Appointment Completed field changes

function isCompleted() {

    var completed = window.parent.Xrm.Page.getAttribute("btsss_appointmentcompleted").getValue();//get value of the Appointment Completed field
    var mileage = window.parent.Xrm.Page.getControl("mileageExpenses").getGrid().getTotalRecordCount();//check for expense item
    var status = window.parent.Xrm.Page.getAttribute("btsss_status").getValue();//get claim status

    if (mileage != 0) {//check for expense item
        if (status == '421750001') {//check for incomplete status
            if (completed == 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 if (status == '421750000') {//check for saved status
            if (completed == 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 if (status == '421750011') {//check for claim submitted status
            if (completed == 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(status);// keep the status the same
        }
    }
}

    /*
    This is the old code used to change the claim status based on the appointment completed field

    if (mileage != 0) { //check to see if there is an expense line item
        //check to see if Claim Status is Incomplete, Saved, or Claim Submitted
        //This will prevent claims being processed from having their status changed
        if (status == '421750001' || '421750000' || '421750011') {
            if (completed == 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
            }
        }
    }
    */