﻿$(document).ready(function () {
    if ($("#RoleChooser").size() > 0) {
        $("#RoleChooser").change(function () {
            var agentID = "";
            var type = "";
            var length = 0;
            if ($(this).val().indexOf('_', 0) > 0) {
                length = $(this).val().indexOf('_', 0);
                agentID = $(this).val().substring(length + 1, $(this).val().length);
                $("#agentIDSelector").val(agentID);
            }
            else {
                length = $(this).val().length;
            }
            type = $(this).val().substring(0, length);
            ImpersonateChooser(type);
            //inizializzo l'autocomplete per gli agenti
            $("input#agentAutoComplete").autocomplete({
                source: function (request, response) {
                    // define a function to call your Action (assuming UserController)        
                    $.ajax({
                        url: '/Order/GetAgents',
                        type: "POST",
                        dataType: "json",
                        // query will be the param used by your action method            
                        data: { query: request.term },
                        success: function (data) {
                            response($.map(eval(data), function (item) {
                                return { label: item.Name, value: item.ID };
                            }))
                        }
                    })
                },
                minLength: 2, // require at least one character from the user
                delay: 500,
                focus: function (event, ui) {
                    $(this).val(ui.item.label);
                    return false;
                },
                select: function (event, ui) {
                    $("#agentIDSelector").val(ui.item.value);
                    $(this).val(ui.item.label);
                    return false;
                }
            });
            //inizializzo l'autocomplete per gli agenti
            $("input#customerAutoComplete").autocomplete({
                source: function (request, response) {
                    // define a function to call your Action (assuming UserController)        
                    $.ajax({
                        url: '/Order/GetCustomers',
                        type: "POST",
                        dataType: "json",
                        // query will be the param used by your action method            
                        data: { query: request.term, agentID: agentID },
                        success: function (data) {
                            response($.map(eval(data), function (item) {
                                return { label: item.Name + ' (' + item.City + ')', value: item.ID };
                            }))
                        }
                    })
                },
                minLength: 1, // require at least one character from the user
                delay: 500,
                focus: function (event, ui) {
                    $(this).val(ui.item.label);
                    return false;
                },
                select: function (event, ui) {
                    $("#customerIDSelector").val(ui.item.value);
                    $(this).val(ui.item.label);
                    return false;
                }
            });
        });
    }

});

function GenericError() {
    alert("Oooops...something went wrong!!");
}

function ImpersonateChooser(type) {
    //alert(type);
    if (type == "AGENT") {
        $("#agentSelector").hide();
        $("#customerSelector").show();
        $("#customerIDSelector").val('');
        $("#customerAutoComplete").val('');
        
    }
    else if (type == "CUSTOMER") {
        $("#agentSelector").hide();
        $("#agentAutoComplete").val('');
        $("#agentIDSelector").val('');
        $("#customerSelector").show();
    }
    else {
        $("#agentSelector").hide();
        $("#customerSelector").hide();
        $("#agentIDSelector").val('');
        $("#customerIDSelector").val('');
        $("#agentAutoComplete").val('');
        $("#customerAutoComplete").val('');
    }
}


