﻿<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;
               var _intervalVar2 = null;
               var _firstCallbackCalled = false;

               function firstSaveCallback(){
                              if(_firstCallbackCalled)
                                             return;

                              _firstCallbackCalled = true;
                              clearInterval(_intervalVar);
                              parent.Xrm.Page.ui.controls.get("vhacrm_requestid").setDisabled(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_requestid"
                                             , "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);
                              }
                              //alert(parent.Xrm.Page.getAttribute("vhacrm_requestid").getValue());

                              //fire the onchange events so business rules execute
                              parent.Xrm.Page.getAttribute('vhacrm_actionintersectionid').fireOnChange();
                              parent.Xrm.Page.getAttribute('vhacrm_requestid').fireOnChange();

                              //save the record
                              parent.Xrm.Page.data.entity.save();
                              _intervalVar2 = setInterval(function () { if (!parent.Xrm.Page.data.entity.getIsDirty()) { secondSaveCallback(); } }, 100);
               }

    function secondSaveCallback() {
        clearInterval(_intervalVar2);
                              parent.Xrm.Page.ui.controls.get("vhacrm_requestid").setDisabled(true);
        parent.Xrm.Page.getControl("topics").refresh();
        $(parent.Xrm.Page.ui.controls.get('WebResource_ccwf_mvisearch').getObject()).contents().find('#SocialSecurityTextBox').focus();

        //alert("secondSaveCallback");
    }

    $(document).ready(function () {
        $('#actionButton').click(function () {
                                             debugger;
                                             clearInterval(_intervalVar);
                                             clearInterval(_intervalVar2);
                                             _firstCallbackCalled = false;
            //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;
            }
            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) {
                //Initial Save the record
                parent.Xrm.Page.data.entity.save();
                _intervalVar = setInterval(function () { if (!parent.Xrm.Page.data.entity.getIsDirty()) { firstSaveCallback(); } }, 100);

                // clear out the MVI Search fields:
                var webres = parent.Xrm.Page.ui.controls.get("WebResource_ccwf_mvisearch").getObject().contentWindow.document;
                webres.getElementById("SocialSecurityTextBox").value = null;
                webres.getElementById("FirstNameTextBox").value = null;
                webres.getElementById("LastNameTextBox").value = null;
                webres.getElementById("BirthMonthTextBox").value = null;
                webres.getElementById("BirthDayTextBox").value = null;
                webres.getElementById("BirthYearTextBox").value = null;
                webres.getElementById("AddMiddleNameTextBox").value = null;
                webres.getElementById("AddGenderTextBox").value = null;
                webres.getElementById("AddMMNTextBox").value = null;
                webres.getElementById("AddPhoneNoTextBox").value = null;
                webres.getElementById("AddHomeStreetTextBox").value = null;
                webres.getElementById("AddHomeCityTextBox").value = null;
                webres.getElementById("AddHomeStateTextBox").value = null;
                webres.getElementById("AddHomeZipTextBox").value = null;
                webres.getElementById("AddBirthCityTextBox").value = null;
                webres.getElementById("AddBirthStateTextBox").value = null;

            }
            else {
                alert('Please perform an MVI search or create a Claim Status Report request');
            }
        });
    });
    </script>


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



</body>
</html>
