﻿///////////////////////////////////////////////
// Variables
///////////////////////////////////////////////
var PassKeyInfo = ''

///////////////////////////////////////////////
// Control Events
///////////////////////////////////////////////
function BindEmbeddedEvents() {
    // Set embedded flag for search results
    isEmbedded = true;
    
    // Check for old version of url
    var KeyCode = ""
    if (getUrlVars()['KeyCode'] == null) {
        KeyCode = getUrlVars()['keycode'];
    }
    else {
        KeyCode = getUrlVars()['KeyCode'];
    }

    // Verify User
    VerifyPassKey(KeyCode);
     
    $('.ProductType').change(function () {
        // Hide current results
        $('#divSearchResults').hide();

        // Pull updated manufacture list
        if (PassKeyInfo != '') {
            PullManufacturers($('.ProductType:checked').val(), PassKeyInfo.ManufacturersList, GetTopManufacturers());
        }
        else {
            PullManufacturers(type, '', GetTopManufacturers());
        }

        // Reset models drop down
        $('#ddModels').html('');
    });

    $('#ddManufacturers').change(function () {
        var type = $('.ProductType:checked').val()
        var manufacturerID = $('#ddManufacturers :selected').attr('value');

        // Hide current results
        $('#divSearchResults').hide();  

        // Pull updated model list       
        PullModels(type, manufacturerID);
    });

    $('#ddModels').change(function () {
        var type = $('.ProductType:checked').val()
        var manufacturer = $('#ddManufacturers :selected').text();
        var model = $('#ddModels :selected').text();

        // Pull Results
        LoadConsumerProductSearchResults(manufacturer, model, "", "", "", "", "", true);
        $('#divSearchResults').show();
    });
};

///////////////////////////////////////////////
// Data Loaders
///////////////////////////////////////////////
function VerifyPassKey(passKey) {
    $.ajax({
        type: "POST",
        url: '/WebServices/ConsumerProductService.asmx/VerifyPassKey',
        data: "{ 'PassKey': '" + passKey + "' }",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d) {
                PullPasskeyInfo(passKey);
            }
            else {
                window.location = "/Embed/AccessDenied.aspx";
            }
        },
        error: function (xhr, textStatus, thrownError) {
            alert("/WebServices/ConsumerProductService.asmx/VerifyPassKey Service Error - Unable to pull models. TextStatus: "
                + textStatus + " ThrownError: " + thrownError);
        }
    });
};

function PullPasskeyInfo(passKey) {
    $.ajax({
        type: "POST",
        url: '/WebServices/ConsumerProductService.asmx/GetPassKeyUserInfo',
        data: "{ 'PassKey': '" + passKey + "' }",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d.length > 0) {
                PassKeyInfo = msg.d[0];
                // Initialize manufacturer list
                PullManufacturers($('.ProductType:checked').val(), PassKeyInfo.ManufacturersList, GetTopManufacturers());

                $('#divMountFinderEmbedded').show();
            }
            else {
                window.location = "/Embed/AccessDenied.aspx";
            }
        },
        error: function (xhr, textStatus, thrownError) {
            alert("/WebServices/ConsumerProductService.asmx/GetPassKeyUserInfo Service Error - Unable to pull models. TextStatus: "
                + textStatus + " ThrownError: " + thrownError);
        }
    });
};

function PullManufacturers(type, limitedList, topMaufacturers) {
    $('#divMountLoader').show();
    var optGrpTop = $('#optGrpTop');
    var optGrpAll = $('#optGrpAll');

    $.ajax({
        type: "POST",
        url: '/WebServices/ConsumerProductService.asmx/GetManufacturers',
        data: "{ 'limitedList': '" + limitedList + "', 'type': '" + type + "', 'topManufacturers': '" + topManufacturers + "' }",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (msg) {
            optGrpTop.html('');
            optGrpAll.html('');
            if (msg.d != null) {
                $.each(msg.d.TopManufacturers, function () {
                    optGrpTop.append('<option label="' + this.ManufacturerName + '" value="' + this.ManufacturerID + '">' + this.ManufacturerName + '</option>');
                });

                $.each(msg.d.FullList, function () {
                    optGrpAll.append('<option label="' + this.ManufacturerName + '" value="' + this.ManufacturerID + '">' + this.ManufacturerName + '</option>');
                });
            }
            else {
                optGrpTop.append('<option class="listHeader"> None found</option>');
                optGrpAll.append('<option class="listHeader"> None found</option>');
            }
            $('#divMountLoader').hide();
        },
        error: function (xhr, textStatus, thrownError) {
            alert("/WebServices/ConsumerProductService.asmx/GetManufacturers Service Error - Unable to pull manufacturers. TextStatus: "
                + textStatus + " ThrownError: " + thrownError);
        }
    });

//    var manufacturers = $('#ddManufacturers');
//    $.ajax({
//        type: "POST",
//        url: '/WebServices/ConsumerProductService.asmx/GetManufacturers',
//        data: "{ 'limitedList': '" + limitedList + "', 'type': '" + type + "' }",
//        contentType: "application/json;charset=utf-8",
//        dataType: "json",
//        success: function (msg) {
//            manufacturers.html('');
//            $.map(msg.d, function (item) {
//                manufacturers.append('<option value="' + item.ManufacturerID + '">' + item.ManufacturerName + '</option>');
//            });
//        },
//        error: function (xhr, textStatus, thrownError) {
//            alert("/WebServices/ConsumerProductService.asmx/GetManufacturers Service Error - Unable to pull manufacturers. TextStatus: "
//                + textStatus + " ThrownError: " + thrownError);
//        }
//    });
};

function PullModels(type, manufacturerID) {
    var models = $('#ddModels');
    $.ajax({
        type: "POST",
        url: '/WebServices/ConsumerProductService.asmx/GetConsumerProducts',
        data: "{ 'manufacturerID': '" + manufacturerID + "', 'type' : '" + type + "' }",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (msg) {
            models.html('');
            $.map(msg.d, function (item) {
                models.append('<option value="' + item.ConsumerProductID + '">' + item.Model + '</option>');
            });
        },
        error: function (xhr, textStatus, thrownError) {
            alert("/WebServices/ConsumerProductService.asmx/GetConsumerProducts Service Error - Unable to pull models. TextStatus: "
                + textStatus + " ThrownError: " + thrownError);
        }
    });
};
