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

    loadWebParts();

}

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 bulkResolveRequests(selectedItems)
{  
    var todaysDate = new Date();
    todaysDate = (todaysDate.getMonth()+1) + "/" + todaysDate.getDate() + "/" + todaysDate.getFullYear();
    var bulk_entityObject = new Object();
    bulk_entityObject.vhacrm_name = "Bulk Resolve on " + todaysDate;
    bulk_entityObject.vhacrm_lobid = { Id: '{1BD4B15A-BFBB-E511-9414-0050568DC724}', LogicalName: "vhacrm_lob", Name: "vhacrm_lobid" };

    var bulk_requests = new Array();

    for (i = 0; i < selectedItems.length; i++) {
        var selectedItem = selectedItems[i];
        var requestObject = new Object();
        requestObject.vhacrm_requestid = { Id: selectedItem.Id, LogicalName: "vhacrm_bulkresolution", Name: "vhacrm_requestid" };
        bulk_requests.push(requestObject);
    }

    bulk_entityObject.vhacrm_vhacrm_bulkresolution_vhacrm_bulkresolutionintersection_bulkresolutionid = bulk_requests;

    var jsonEntity = window.JSON.stringify(bulk_entityObject); 

    var bulk_entityCollection = "vhacrm_bulkresolutionSet";
    var bulk_context = Xrm.Page.context;
    var serverUrl = bulk_context.getClientUrl();
    var oDataEndPoint = '/XRMServices/2011/OrganizationData.svc';
    var odataUri = serverUrl + oDataEndPoint + "/" + bulk_entityCollection;

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: odataUri,
        data: jsonEntity,
        async: true,
        beforeSend: function (XMLHttpRequest)
        {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest)
        {
            Xrm.Utility.openEntityForm("vhacrm_bulkresolution", data.d.vhacrm_bulkresolutionId);
        },
        error: function (XmlHttpRequest, textStatus, errorThrown)
        {
            alert('Fail: Ajax Error in bulkResolveRequests: ' + errorThrown);
        }
    });
}

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