﻿<html><head></head><body style="overflow-wrap: break-word;">﻿<meta><style type="text/css">
:root #header + #content > #left > #rlblock_left
{ display: none !important; }</style>﻿








    <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 _intervalVar3 = 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"
                           , "vhacrm_noncorereasonrequest_code"
                           , "vhacrm_noncoredetailrequest_code"
                           , "vhacrm_resolutionid"
                           , "vhacrm_noncorevisndetailrequestid"
                          // , "vhacrm_vetconsultidstempfield_text"
                          // , "vhacrm_vetappidtempfield_text"
						  // , "vhacrm_appointmentdatetime_date"
            ];

            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);
            //If Claim Status Report, clear several additioanal fields
            var subareaid = parent.Xrm.Page.getAttribute("vhacrm_subareaintersectionid").getValue();
            //try {
              //  if (subareaid[0].name.toUpperCase() == 'Claim Status Report'.toUpperCase()) {
                //    validform = true;
                  //  var CCfieldsToClear = [
                    //                       "vhacrm_areaintersectionid"
                      //                     , "vhacrm_subareaintersectionid"
                        //                   , "vhacrm_actionintersectionid"
                    //];
                    //for (var i = 0, l = fieldsToClear.length; i < l; i++) {
                  //      parent.Xrm.Page.getAttribute(CCfieldsToClear[i]).setValue(null);
            //        }
                    //save the record
           //         parent.Xrm.Page.data.entity.save();
           //         _intervalVar3 = setInterval(function () { if (!parent.Xrm.Page.data.entity.getIsDirty()) { secondSaveCallback(); } }, 100);
            //    }
           // }
           // catch (err) { }


        }

        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;

                var validform = true;
                var tin = parent.Xrm.Page.getAttribute("ccwf_tin_text").getValue();
                var facility = parent.Xrm.Page.getAttribute("bah_facilityid").getValue();
                var provFac = parent.Xrm.Page.getAttribute("ccwf_providerfacility_text").getValue();
                var subarea = parent.Xrm.Page.getAttribute("vhacrm_subareaintersectionid").getValue();
                var VISNNum = parent.Xrm.Page.getAttribute("bah_visnid").getValue();
                var validationmessage = 'You must supply values for '
                try {
                    if (subarea[0].name.toUpperCase() == 'Claim Status Report'.toUpperCase()) {
                        if (!VISNNum) {
                            validform = false;
                            validationmessage += 'VISN, ';
                        }
                    }
                    else {
                        if (!facility) {
                            validform = false;
                            validationmessage += 'Home Facility, ';
                        }
                    }
                }
                catch (err) { }
                if (!tin) {
                    if (!provFac) {
                        validform = false;
                        validationmessage += 'TIN, ';
                    }
                }



                if (validform) {

                    //parent.Xrm.Page.data.entity.save();
                    clearInterval(_intervalVar);
                    clearInterval(_intervalVar2);
                    clearInterval(_intervalVar3);
                    _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;
                    }

                    //            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');
                    //            }
                }
                else {
                    validationmessage = validationmessage.slice(0, -2) + '.';
                    alert(validationmessage);
                }

            });
        });
    </script>


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





</body></html>