﻿
var timer = null;

function GetActivities(searchCriteria, agentID, schemeGroupID) 
{
    if (searchCriteria) 
    {
        Index.GetActivities(searchCriteria, agentID, schemeGroupID, GetActivities_CallBack);
    }
    else 
    {
        //clear the states dropdown
        matchList.style.visibility = "hidden";
    }
}

function GetActivities_CallBack(response) 
{
    //if the server side code threw an exception
    if (response.error != null) 
    {
        alert(response.error); //we should probably do better than this
        //return;
    }
    var activities = response.value;
    
    
    //if we didn't get what we expect, or didn't get any matches
    if (activities == null || typeof (activities) != "object") 
    { 
        return;
    }
    var lst = document.getElementById("lstActivities");
  
    lst.options.length = 0; //reset the states dropdown

    if (activities.Rows != null) 
    {
        for (var i = 0; i < activities.Rows.length; ++i) 
        {
            lst.options[lst.options.length] = new Option(activities.Rows[i].Name, activities.Rows[i].ID);
        }
    }
    else 
    {
        for (var i = 0; i < activities.length; ++i) 
        {
            lst.options[lst.options.length] = new Option(activities[i].Name, activities[i].ID);
        }
    }
}

function ActivityKeyUp() 
{
    if (timer != null) 
    {
        clearTimeout(timer);
    }

    timer = setTimeout(ActivityKeyUp_next, 100);
}

function ActivityKeyUp_next() 
{
    var ele = document.getElementById("txtActivitySearch");
    if (ele.value.length >= 2) {

        var agentID = document.getElementById("hidAgentID");
        var schemeGroupID = document.getElementById("hidSchemeGroupID");

        GetActivities(ele.value, agentID.value, schemeGroupID.value);
    }
    else 
    {
        var lst = document.getElementById("lstActivities");
        lst.options.length = 0; //reset the states dropdown           
    }
}		