var _intervalVar = null;

function form_OnLoad() {
	
	if(Xrm.Page.ui.getFormType() == 4){
		Xrm.Page.getControl("WebResource_submithecalert").setVisible(false);
		Xrm.Page.getControl("WebResource_completecasenote").setVisible(false);
		Xrm.Page.getControl("WebResource_mvisearch").setVisible(false);
	}
	checkVeteran();
}

	


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

function firstname_OnChange() { refresh.firstname_OnChange(); }

function lastname_OnChange() { refresh.lastname_OnChange(); }

function dob_OnChange() { refresh.dob_OnChange(); }

function ssn_OnChange() { refresh.ssn_OnChange(); }

function edipi_OnChange() { refresh.edipi_OnChange(); }

function gender_OnChange() { refresh.gender_OnChange(); }




function hecalerttype_OnChange(context) {
    Xrm.Page.getAttribute("vhacrm_overridetype_code").setValue(null);
}


function checkVeteran() {
	var noveteran = Xrm.Page.getAttribute("vhacrm_noveteran_bool").getValue();
    if (Xrm.Page.getAttribute("vhacrm_veteranid").getValue()){
		var vetValue = Xrm.Page.getAttribute("vhacrm_veteranid").getValue();
		if(vetValue[0].id.toUpperCase() != '{1B8680E1-8D87-E611-9422-0050568DADE6}') 
		{
	        Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo").setVisible(true);
		    Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo_input").setVisible(false);
			Xrm.Page.ui.tabs.get("tab_veteransearch").setDisplayState("collapsed");
		}
		else{
			Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo").setVisible(false);
			Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo_input").setVisible(true);
			Xrm.Page.ui.tabs.get("tab_veteransearch").setDisplayState("collapsed");		
		}
    }
    else if (noveteran == true) {
        Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo_input").setVisible(true);
		Xrm.Page.ui.tabs.get("tab_veteransearch").setDisplayState("collapsed");
	}
	else{
		Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo_input").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_hecalertinformation").sections.get("section_veteraninfo").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_veteransearch").setDisplayState("expanded");
    }
}

var refresh = {
    firstname_OnChange: function () {
        refreshMVI();
    },

    lastname_OnChange: function () {
        refreshMVI();
    },

    dob_OnChange: function () {
        refreshMVI();
    },

    ssn_OnChange: function () {
        refreshMVI();
    },

    edipi_OnChange: function () {
        refreshMVI();
    },

    gender_OnChange: function () {
        refreshMVI();
    },


}

function refreshMVI() {
    try {
        var webResArea = Xrm.Page.ui.controls.get("WebResource_mvisearch");
        webResArea.setSrc(webResArea.getSrc());
    }
    catch (e) {
        alert('Error in refreshMVI(): ' + e.Message);
    }
}
var mvi = {
    personSelected: function (id, name) {
        var veteran = new Array();
        veteran[0] = new Object();
        veteran[0].id = id;
        veteran[0].name = name;
        veteran[0].entityType = "contact";

        Xrm.Page.getAttribute("vhacrm_veteranid").setValue(veteran);
        Xrm.Page.getAttribute("vhacrm_veteranid").fireOnChange();
    }
}
