﻿<html>
<head>
</head>
<body style="-ms-word-wrap: break-word;">
    <title>Button</title>
    <script src="ClientGlobalContext.js.aspx"></script>
    <script src="bah_SDK.REST.js" type="text/javascript"></script>
    <script src="bah_jquery1.4.1.min.js" type="text/javascript"></script>
    <link href="north52_purecss.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        html, body {
            height: 100%;
            overflow: auto;
            border-width: 0px;
        }

        body {
            padding: 0;
            margin: 0;
        }
    </style>

    <script type="text/javascript">
    //get the id of the record for which we are running
    var _parentId = null;

    //set a variable to use for the setinterval call after the save
    var _intervalVar = null;

    function refreshCallback() {
        //Check is needed to only clear certain fields based on certain criteria

        var isValid = false;
        var hasRequst = parent.Xrm.Page.getAttribute("vhacrm_requestid").getValue();
        if (hasRequst =! null){
            isValid = true;
        };
        var hasClaimStatuReport = parent.Xrm.Page.getAttribute("vhacrm_subareaintersectionid").getValue();
        try {
            if (hasClaimStatuReport[0].name.toUpperCase() == 'Claim Status Report'.toUpperCase()) {
                    isValid = true;
                }
        }
        catch (err) { isValid = false }

        var isNonCore = parent.Xrm.Page.getAttribute("vhacrm_areaintersectionid").getValue();
        try{
            if (isNonCore[0].name.toUpperCase() == 'Non-Core'.toUpperCase()) {
                parent.Xrm.Page.getAttribute("vhacrm_areaintersectionid").setValue(null);
                parent.Xrm.Page.getAttribute('vhacrm_areaintersectionid').fireOnChange();
                }
        }
        catch (err) { }

               clearInterval(_intervalVar);
               parent.Xrm.Page.getAttribute("vhacrm_noncorereasonrequest_code").setValue(null);
               parent.Xrm.Page.getAttribute('vhacrm_noncorereasonrequest_code').fireOnChange();
               parent.Xrm.Page.getAttribute("vhacrm_noncoredetailrequest_code").setValue(null);
               parent.Xrm.Page.getAttribute("vhacrm_actionintersectionid").setValue(null);
               parent.Xrm.Page.getAttribute('vhacrm_actionintersectionid').fireOnChange();
               parent.Xrm.Page.data.entity.save();

            if (isValid = true) {
            var fieldsToClear = [
                  "vhacrm_resolutionid"
                , "vhacrm_noncorevisndetailrequestid"
                , "vhacrm_request_beginningdate_date"
                , "vhacrm_request_methodofdelivery_code"
                , "vhacrm_request_endingdate_date"
                , "vhacrm_request_address1_attentionline_text"
                , "vhacrm_request_address1_line1_text"
                , "vhacrm_request_address1_city_text"
                , "vhacrm_request_address1_stateid"
                , "vhacrm_request_address1_postalcode_text"
                , "vhacrm_request_email_text"
                , "vhacrm_request_fax_text"
                , "vhacrm_faxattentionline_text"
                , "vhacrm_claimnumber_text"
                , "vhacrm_billedamount_currency"
                , "vhacrm_startdateofservice_date"
                , "vhacrm_enddateofservice_date"
                , "vhacrm_claimid"
                , "vhacrm_stationwithactivityid"
                , "vhacrm_stationtobeloadedtoid"

            ];
        }
        if (isValid = false) {
            var fieldsToClear = [
                  "vhacrm_resolutionid"
                , "vhacrm_noncorevisndetailrequestid"
                , "vhacrm_request_beginningdate_date"
                , "vhacrm_actionintersectionid"
                , "vhacrm_topicresolutionintersectionid"
                , "vhacrm_request_methodofdelivery_code"
                , "vhacrm_request_endingdate_date"
                , "vhacrm_request_address1_attentionline_text"
                , "vhacrm_request_address1_line1_text"
                , "vhacrm_request_address1_city_text"
                , "vhacrm_request_address1_stateid"
                , "vhacrm_request_address1_postalcode_text"
                , "vhacrm_request_email_text"
                , "vhacrm_request_fax_text"
                , "vhacrm_faxattentionline_text"
                , "vhacrm_claimnumber_text"
                , "vhacrm_billedamount_currency"
                , "vhacrm_startdateofservice_date"
                , "vhacrm_enddateofservice_date"
                , "vhacrm_claimid"
                , "vhacrm_stationwithactivityid"
                , "vhacrm_stationtobeloadedtoid"
            ];
        }


        for (var i = 0, l = fieldsToClear.length; i < l; i++) {
            parent.Xrm.Page.getAttribute(fieldsToClear[i]).setValue(null);
        }
        
        
        parent.Xrm.Page.getAttribute('vhacrm_subareaintersectionid').fireOnChange();
        parent.Xrm.Page.getAttribute('vhacrm_request_methodofdelivery_code').fireOnChange();
        parent.Xrm.Page.getAttribute('vhacrm_requestid').fireOnChange();
        parent.Xrm.Page.data.entity.save();


        parent.Xrm.Page.getAttribute("ccwf_createadditionalrequest_bool").setValue(true);
        parent.Xrm.Page.data.entity.save();




    }

    $(document).ready(function () {
        $('#actionButton').click(function () {
            //get the id of the record for which we are running
            _parentId = parent.Xrm.Page.data.entity.getId();

            var validform = false;
            var veteranid = parent.Xrm.Page.getAttribute("bah_veteranid").getValue();
            if (veteranid) {
                validform = true;
            }

            debugger;
            var subareaid = parent.Xrm.Page.getAttribute("vhacrm_subareaintersectionid").getValue();
            try {
                if (subareaid[0].name.toUpperCase() == 'Claim Status Report'.toUpperCase()) {
                    validform = true;
                }
            }
            catch (err) { }

            if (validform) {
                _intervalVar = setInterval(function () { if (!parent.Xrm.Page.data.entity.getIsDirty()) { refreshCallback(); } }, 200);
                parent.Xrm.Page.data.entity.save();
            }
            else {
                alert('Please perform an MVI search or create a Claim Status Report request');
            }
        });
    });
    </script>


    <button name="actionButton" id="actionButton" type="button">New Topic Same Vet</button>

</body>
</html>
