function form_OnLoad() {
    if (Xrm.Page.ui.getFormType() != 1) { loadLOBForm(); }
    var LOBValue = Xrm.Page.getAttribute("vhacrm_lobid").getValue();
    if (LOBValue) {
        if (LOBValue[0].name == "EED") {
            Xrm.Page.ui.tabs.get("tab_general").sections.get("section_verificationmethods").setVisible(true);
            Xrm.Page.ui.tabs.get("tab_general").sections.get("section_correspondencebutton").setVisible(true);
			Xrm.Page.ui.tabs.get("tab_verificationmethodslist").setVisible(true);
        }
        else {
            Xrm.Page.ui.tabs.get("tab_general").sections.get("section_verificationmethods").setVisible(false);
            Xrm.Page.ui.tabs.get("tab_general").sections.get("section_correspondencebutton").setVisible(false);
			Xrm.Page.ui.tabs.get("tab_verificationmethodslist").setVisible(false);
        }
    }
	var vetValue = Xrm.Page.getAttribute("customerid").getValue();
   Xrm.Page.ui.controls.get("IFRAME_addresses").setSrc("/WebResources/vhacrm_/ESRParts/Blank.htm");
    loadWebParts();
    checkAreas(false);

   
  
}


function form_OnSave(context) {
    preventAutoSave(context);
}

function type_OnChange()
{
	Xrm.Page.getAttribute("vhacrm_areaintersectionid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_subareaintersectionid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_resolutionintersectionid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_casenotetemplateid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_subareaintersectionid").setRequiredLevel("none");
	Xrm.Page.data.save().then(type_Callback(), function (err) { alert(err); });
}

function area_OnChange(context)
{
	Xrm.Page.data.entity.save();
	Xrm.Page.getAttribute("vhacrm_subareaintersectionid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_casenotetemplateid").setValue(null);
	Xrm.Page.getAttribute("vhacrm_subareaintersectionid").setRequiredLevel("none");
	N52.Clientside.ExecuteFormulas(context, 'Q5i'); 
}

function resolution_OnChange()
{
	Xrm.Page.data.entity.save();
}

function caseNoteTemplate_OnChange(context)
{
    N52.Clientside.ExecuteFormulas(context, 'lSq');
	Xrm.Page.data.entity.save();
}

function loadWebParts() {
    unattendedSearchFromRelatedRecord("customerid", loadAssociations, loadESRWebParts, loadAddresses);
}

function loadAssociations(icn) {
    displayWebPart(icn, 'IFRAME_associations', 'esr_associations_endpoint');
}

function loadESRWebParts(icn) {
    //pass icn, folder path and list of control names/resource names to the loadEsrResources function
    loadEsrResources(icn, '/webresources/vhacrm_/ESRParts', 'WebResource_esrenrollment|enrollment.htm,WebResource_esreligibility|eligibility.htm,WebResource_esrdemographics|demographics.htm');
}

function loadAddresses(icn) {
    displayWebPart(icn, 'IFRAME_addresses', 'esr_addresses_endpoint');
}

function type_Callback() {
    checkAreas(false);
    Xrm.Page.getAttribute("vhacrm_areaintersectionid").setValue(null);

}

function checkAreas(sync) {

    var typeintersectionid = Xrm.Page.getAttribute("vhacrm_typeintersectionid").getValue();
    if (typeintersectionid) {
        var oDataSetName = "vhacrm_areaintersection";
        var filter = "vhacrm_typeintersectionid/Id eq (guid'" + typeintersectionid[0].id.toString() + "') and statecode/Value eq 0";
        var columns = "*";

        if (sync) {
            var data = retrieveMultipleSync(oDataSetName, columns, filter);
            if (data != null) {
                if (data.length > 0) {
                    Xrm.Page.getAttribute('vhacrm_areaintersectionid').setRequiredLevel('required');
                }
                else {
                    Xrm.Page.getAttribute('vhacrm_areaintersectionid').setRequiredLevel('none');
                }
            }
        }
        else {
            var querystring = "$select=" + columns + "&$filter=" + filter;
            SDK.REST.retrieveMultipleRecords(
				oDataSetName,
				querystring,
				function (data) {
				    if (data.length > 0) {
				        Xrm.Page.getAttribute('vhacrm_areaintersectionid').setRequiredLevel('required');
				    }
				    else {
				        Xrm.Page.getAttribute('vhacrm_areaintersectionid').setRequiredLevel('none');
				    }
				},
				function (error) {
				    alert(error);
				    //do nothing
				},
				function () {
				    //do nothing on completion
				}
			);
        }
    }
}

function retrieveMultipleSync(odataSetName, select, filter) {
    // Get Server URL
    var serverUrl = "";
    if (Xrm.Page.context.getClientUrl) {
        //Post UR 12
        serverUrl = Xrm.Page.context.getClientUrl();
    }
    else {
        //Pre UR 12
        serverUrl = Xrm.Page.context.getServerUrl();
    }
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + 'Set' + "?";
    if (select) {
        odataUri += "$select=" + select;
    }
    if (filter) {
        odataUri += "&" + "$filter=" + filter;
    }
    var service = getRequestObject();
    if (service != null) {
        service.open("GET", odataUri, false);
        service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
        service.setRequestHeader("Accept", "application/json,text/javascript, */*");
        service.send(null);
        var requestResults = eval('(' + service.responseText + ')').d;
        return requestResults;
    }
}

