function form_OnLoad() {
    loadWebParts();
}

function loadWebParts() {
    unattendedSearchFromVeteran( loadAddresses, loadInsurance, loadESRWebParts, SetVeteranSidebar);
}

// replaced w/ Sidebar:
//function loadConsults(icn) {
//    displayWebPart(icn, 'IFRAME_hdr_consults', 'hdr_consults');
//}
//function loadNotes(icn) {
//    displayWebPart(icn, 'IFRAME_hdr_notes', 'hdr_notes');
//}

function loadAddresses(icn) {
    displayWebPart(icn, 'IFRAME_esr_addresses', 'esr_addresses');
}

function loadInsurance(icn) {
    displayWebPart(icn, 'IFRAME_insurance', 'esr_insurance');
}

function SetVeteranSidebar(icn) {
    //setIframeURLholder("VeteranSidebar", "vhacrm_veteransidebarurl", icn);
       getICN("IFRAME_VeteranSidebar", icn);
}


function loadESRWebParts(icn) {
    //pass icn, folder path and list of control names/resource names to the loadEsrResources function
    loadEsrResources(icn, '/webresources/vhacrm_/ESRParts', 'WebResource_PhoneNumbersGrid|PhoneNumberGrid.htm,WebResource_EmailGrid|EmailGrid.htm,WebResource_Demographics|Demographics.htm,WebResource_Enrollment|Enrollment.htm,WebResource_Eligibility|Eligibility.htm,WebResource_VAMC|VAMC.htm');
}

///takes icn, path to web resources (starting from root CRM url) and list of form control names and web resource names as arguments
///example - loadEsrResources(icn, '/webresources/vhacrm_/ESRParts', 'WebResource_enrollment|enrollment.htm,WebResource_esreligibility|eligibility.htm,WebResource_demographics|demographics.htm');
function loadEsrResources(icn, resourcepath, resourceparam) {
    SDK.REST.retrieveMultipleRecords("bah_keyvaluepair",
                              "$select=*&$filter=bah_name_text eq 'esr_endpoint'",
                              function (data) {
                                  if (data.length > 0) {
                                      //figure out the full url to the BAH esr proxy
                                      var esrEndpoint = data[0].bah_stringvalue_text;
                                      var actualEndpoint = esrEndpoint.replace('{0}', '000000' + icn + '000000'); //pad ICN with six zeroes on beginning and end

                                      //build the querystring to call the page that will query esr and display the data - note CRM requires everything be encoded and passed in a 'data' parameter
                                      var qs = 'data=' + encodeURIComponent('esr=' + encodeURIComponent(actualEndpoint));

                                      var resourcearr = resourceparam.split(',');

                                      if (resourcearr.length > 0) {
                                          for (var i = 0; i < resourcearr.length; i++) {
                                              try {
                                                  //start with assumption that resource name and control name are the same
                                                  var controlname = "";
                                                  var webresourcename = "";

                                                  //check to see if we have pipe-separated value -> control name should be first, resource name should be second
                                                  if (resourcearr[i].indexOf('|') > 0) {
                                                      controlname = resourcearr[i].split('|')[0];
                                                      webresourcename = resourcearr[i].split('|')[1];
                                                  }
                                                  else {
                                                      throw ("ESR resource target control and resource name not supplied in correct format.")
                                                  }

                                                  //get a reference to the iframe
                                                  var webresource = Xrm.Page.ui.controls.get(controlname);
                                                  if (webresource == null) {
                                                      throw ("Could not find ESR resource target control with name - '" + controlname + "'");
                                                  }

                                                  //set the iframe's url
                                                  var webresourceUrl = Xrm.Page.context.getClientUrl() + resourcepath + '/' + webresourcename + '?' + qs;
                                                  webresource.setSrc(webresourceUrl);
                                              }
                                              catch (err) { alert(err); }
                                          }
                                      }
                                  }
                              },
                              function (error) {
                                  //do nothing
                              },
                              function () {
                                  //do nothing on completion
                              }
               );
}
