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

    loadWebParts();

}

function type_OnChange(context)
{
	Xrm.Page.getAttribute("DNS_areaintersectionid").setValue(null);
	Xrm.Page.getAttribute("DNS_subareaintersectionid").setValue(null);
	Xrm.Page.getAttribute("DNS_resolutionintersectionid").setValue(null);
	Xrm.Page.getAttribute("DNS_casenotetemplateid").setValue(null);
	Xrm.Page.getAttribute("DNS_subareaintersectionid").setRequiredLevel("none");
}

function area_OnChange(context)
{
	Xrm.Page.data.entity.save();
	Xrm.Page.getAttribute("DNS_subareaintersectionid").setValue(null);
	Xrm.Page.getAttribute("DNS_casenotetemplateid").setValue(null);
	Xrm.Page.getAttribute("DNS_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.DNS_name = "Bulk Resolve on " + todaysDate;
    bulk_entityObject.DNS_lobid = { Id: '{1BD4B15A-BFBB-E511-9414-0050568DC724}', LogicalName: "DNS_lob", Name: "DNS_lobid" };

    var bulk_requests = new Array();

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

    bulk_entityObject.DNS_DNS_bulkresolution_DNS_bulkresolutionintersection_bulkresolutionid = bulk_requests;

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

    var bulk_entityCollection = "DNS_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("DNS_bulkresolution", data.d.DNS_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/DNS_/ESRParts', 'WebResource_esrenrollment|enrollment.htm,WebResource_esreligibility|eligibility.htm,WebResource_esrdemographics|demographics.htm');
}