﻿/// <reference path="jquery-1.4.2.min.js"/>

// ***** This file lives in both CIDS and IMACC.NET. Please keep both copies exactly identical. *****
function docReady() {
    getAdjusters($("[id*=ddlInsurer]").get(0).options[$("[id*=ddlInsurer]").get(0).selectedIndex].index);
}

function getAdjusters(pid) {

    $("body").css("cursor", "progress");
    if ($("[id*=ddlAdjuster]").attr("disabled") == false) { $("[id*=ddlAdjuster]").attr("disabled", "disabled"); }
    
    var url = 'CustomHandlers/Handler.ashx?action=GetProgramParticipants&ProgramID=' + pid;
    var xhr = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xhr = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xhr.open('GET', url, true);
    // self.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xhr.onreadystatechange = function() {
        if (self.xhr.readyState == 4) {
            // process response
            var adjusters
            if (self.xhr.responseText.length) {
                adjusters = eval('(' + self.xhr.responseText + ')');
            }
            setDdlAdjuster(adjusters);

            $("[id*=ddlAdjuster]").removeAttr("disabled");
            $("body").css("cursor", "auto");
       }
    }
    self.xhr.send(null);
}

//Clear the contents of ddlAdjuster and add the adjusters of the currently selected insurance company
function setDdlAdjuster(json) {
    var ddlAdj = $("[id*=ddlAdjuster]");
    ddlAdj.html("");
    ddlAdj.append('<option selected="selected" value=""></option>');
    if (json) {
        for (var i = 0; i < json.length; i++) {
            var o = json[i];
            var pid = o.participantId;
            var n = o.lnameFname;
            ddlAdj.append($("<option></option>").val(pid).html(n));
        }
    }

    return;
}

function getSelectedAdjuster() {
    $("#hdnAdjusterValue").val($("[id*=ddlAdjuster]").val());
    $("#hdnAdjusterText").val($("[id*=ddlAdjuster] option:selected").text());
}

