﻿$(document).ready(function () {
    getCountries();
});

$(function () {
    $('#ddlCountry').change(getProviders);
    $('#ddlProvider').attr('disabled', true);
});

function getCountries() {
    $.ajax({
        type: "POST",
        url: "/webservices/DeviceConfigurator.asmx/GetCountries",
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var countries = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $('#ddlCountry').attr('disabled', false).change(getProviders).removeOption(/./).addOption('', ' -- Select Country -- ');

            for (var i = 0; i < countries.length; i++) {
                var val = countries[i];
                var text = countries[i];
                $('#ddlCountry').addOption(val, text, false);
            }
        }
    });
}


function getProviders() {
    $.ajax({
        type: "POST",
        url: "/webservices/DeviceConfigurator.asmx/GetProvidersByCountry",
        data: "{Country: '" + $('#ddlCountry').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var providers = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $('#ddlProvider').attr('disabled', false).change(getDevices).removeOption(/./).addOption('', ' -- Select Provider -- ');

            for (var i = 0; i < providers.length; i++) {
                var val = providers[i];
                var text = providers[i];
                $('#ddlProvider').addOption(val, text, false);
            }
        }
    });
}



function getDevices() {
    $.ajax({
        type: "POST",
        url: "/webservices/DeviceConfigurator.asmx/GetDevicesByProvider",
        data: "{Provider: '" + $('#ddlProvider').val() + "', Country: '" + $('#ddlCountry').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var devices = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
            $('#device-config-link').empty();
            for (var i = 0; i < devices.length; i++) {
                $('#device-config-link').append('<a href="' + devices[i] + '"><img src="/img/device-config-go.gif" /></a>');
            }
        }
    });
}     
