function checkOwnedByTier3() {
    var ownedByTier3 = (Xrm.Page.getAttribute("vhacrm_ownedbytier3").getValue() == 1) ? true : false;

    return ownedByTier3;
}

function loadLOBForm() {

    var HDFORMID = "836e163c-5f4e-4abb-a75b-5e8e9c70deb4";
    var HBFORMID = "d8e13fa0-e09f-482b-ac15-d7d212ff323e";
    var ROFORMID = "73b31959-329b-4967-ab19-cf115109c91c";
    var NCCHVFORMID = "e3e6071c-c046-4deb-90ad-f9ae69a5e3b5";
    var HECFORMID = "498612d2-6cb0-450b-a285-21acec2bb22f";
    var HDTIER3FORMID = "a0f95efb-0448-4233-9fb1-969f2010d25a";
    var formId = ROFORMID;
    var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();

    if (Xrm.Page.ui.getFormType() != 1) {
        var currentFormName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
        var lob = Xrm.Page.getAttribute("vhacrm_lobid").getValue();

        if (checkOwnedByTier3() == true) {
            formId = HDTIER3FORMID;
        }
        else {
            switch (lob[0].name) {
                case "Help Desk":
                    if (Xrm.Page.ui.formSelector.items.get(HDFORMID) != null) {
                        formId = HDFORMID;
                    }
                    break;
                case "Health Benefits":
                    if (Xrm.Page.ui.formSelector.items.get(HBFORMID) != null) {
                        formId = HBFORMID;
                    }
                    break;
                case "HRC":
                    if (Xrm.Page.ui.formSelector.items.get(HBFORMID) != null) {
                        formId = HBFORMID;
                    }
                    break;
                case "NCCHV":
                    if (Xrm.Page.ui.formSelector.items.get(NCCHVFORMID) != null) {
                        formId = NCCHVFORMID;
                    }
                    break;
                case "HEC":
                    if (Xrm.Page.ui.formSelector.items.get(HECFORMID) != null) {
                        formId = HECFORMID;
                    }
                    break;
                case "EED":
                    if (Xrm.Page.ui.formSelector.items.get(HECFORMID) != null) {
                        formId = HECFORMID;
                    }
                    break;

                default:
                    formId = ROFORMID;
                    break;
            }
        }

        if (Xrm.Page.ui.formSelector.items.get(formId) != null && currentFormId != formId) {
            Xrm.Page.ui.formSelector.items.get(formId).navigate();
        }
    }
}

function preventAutoSave(context) {
    var eventArgs = context.getEventArgs();

    if (eventArgs.getSaveMode() == 70) {
        eventArgs.preventDefault();
    }
}

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

}