﻿<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 (subareaid[0].name.toUpperCase() == 'Claim Status Report'.toUpperCase()) {
                    isValid = true;
                }
        }
        catch (err) { isValid = false }

        clearInterval(_intervalVar);
        parent.Xrm.Page.getControl("topics").refresh();
        if (isValid = true) {
            var fieldsToClear = [
                "vhacrm_request_beginningdate_date"
 //               , "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"
            ];
        }
        if (isValid = false) {
            var fieldsToClear = [
                "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);
        }

        //fire the vhacrm_topicresolutionintersectionid onchange event so business rules execute
 //       parent.Xrm.Page.getAttribute("vhacrm_request_methodofdelivery_code").setValue(defaultValue);
 //       parent.Xrm.Page.getAttribute('vhacrm_topicresolutionintersectionid').fireOnChange();
        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();
    }

    $(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) {
                parent.Xrm.Page.getAttribute("ccwf_createadditionalrequest_bool").setValue(true);

                //save the record
                parent.Xrm.Page.data.entity.save();
                _intervalVar = setInterval(function () { if (!parent.Xrm.Page.data.entity.getIsDirty()) { refreshCallback(); } }, 100);
            }
            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>
