function form_OnLoad() {
    if (Xrm.Page.ui.getFormType() != 1) { loadLOBForm(); }

    if (Xrm.Page.ui.getFormType() == 4) {
        Xrm.Page.getControl("WebResource_CreateCorrespondence").setVisible(false);
        Xrm.Page.getControl("WebResource_completenotes").setVisible(false);
    }
    loadWebParts();

}

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

function type_OnChange(context)
{
	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");
}

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);
}

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 actionIntersection_OnChange() {
    var actionIntersection = Xrm.Page.getAttribute("vhacrm_actionintersectionid").getValue();

    if (actionIntersection != null) {
        if (actionIntersection[0].id.toString() == "{E3152D4D-C7E4-E611-947B-0050568D63D9}" && checkOwnedByTier3()) {
            Xrm.Page.getAttribute("vhacrm_casenotes_memo").setRequiredLevel("required");
            Xrm.Page.ui.setFormNotification("Case note is a required field.", "INFO", 'CaseNoteRequired');
        }
        else {
            Xrm.Page.getAttribute("vhacrm_casenotes_memo").setRequiredLevel("none");
            Xrm.Page.ui.clearFormNotification('CaseNoteRequired');
        }
    }
    else {
        Xrm.Page.getAttribute("vhacrm_casenotes_memo").setRequiredLevel("none");
        Xrm.Page.ui.clearFormNotification('CaseNoteRequired');
    }

}