using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using Telerik.Web.UI;
using System.Web.Script.Serialization;

[ScriptService]
public class Countries : WebService
{
    [WebMethod]
    public RadComboBoxItemData[] GetCountries(object context)
    {
        IDictionary<string, object> contextDictionary = (IDictionary<string, object>)context;

        DataTable table = new DataTable();
        table.Columns.Add("ID");
        table.Columns.Add("Name");


        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {
            RadComboBoxItemData itemData = new RadComboBoxItemData();
            itemData.Text = row["Name"].ToString();
            itemData.Value = row["ID"].ToString();
            result.Add(itemData);
        }

        return result.ToArray();
    }

    [WebMethod]
    public RadComboBoxData GetCuntriesNames(RadComboBoxContext context)
    {
        DataTable data = new DataTable();
        data.Columns.Add("ID");
        data.Columns.Add("Name");

        if (context.Text == "1")
        {
            data.Rows.Add(new string[] { "1", "England" });
            data.Rows.Add(new string[] { "2", "France" });
        }
        else
        {
            data.Rows.Add(new string[] { "1", "New Zealand" });
        }

        List<RadComboBoxItemData> result = new List<RadComboBoxItemData>(context.NumberOfItems);
        RadComboBoxData comboData = new RadComboBoxData();
        try
        {

            int itemsPerRequest = 10;
            int itemOffset = context.NumberOfItems;
            int endOffset = itemOffset + itemsPerRequest;
            if (endOffset > data.Rows.Count)
            {
                endOffset = data.Rows.Count;
            }
            if (endOffset == data.Rows.Count)
            {
                comboData.EndOfItems = true;
            }
            else
            {
                comboData.EndOfItems = false;
            }
            result = new List<RadComboBoxItemData>(endOffset - itemOffset);
            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBoxItemData itemData = new RadComboBoxItemData();
                itemData.Text = data.Rows[i]["Name"].ToString();
                itemData.Value = data.Rows[i]["ID"].ToString();

                result.Add(itemData);
            }

            if (data.Rows.Count > 0)
            {
                comboData.Message = String.Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString());
            }
            else
            {
                comboData.Message = "No matches";
            }
        }
        catch (Exception e)
        {
            comboData.Message = e.Message;
        }

        comboData.Items = result.ToArray();
        return comboData;

    }
}
