﻿$(document).ready(function () {
    var EnqSrcId = $('select[name="EnqSrcId"]');
    $(EnqSrcId).html('').hide();

    $('#EnqSrcWrapper').html('<div class="enq-category-padding"><select id="EnqCategory" onchange="UpdateEnqSrcList();" class="select-orig"></select></div>');
    $('#EnqSrcWrapper').append(EnqSrcId);
    styleSelects();
});

var EnqCategoryList = null;
function UpdateEnqCategoryList(enqSrcID_) {
    var country = $('[name="PaxAddress.CountryCode"]').val();
    if (enqSrcID_ == undefined || enqSrcID_ == null || enqSrcID_ == 0) { enqSrcID_ = ''; }

    $('#EnqSrc-Container').show();
        
    $.ajax({
        url: '/Services/Enquiry/GetEnqCategoryList',
        data: ({ CountryCode: country }),
        success: function (result) {
            if (result.length > 0) {
                BindListOptions(result, enqSrcID_);
            } else {
                BindEmptyOptions();
            }
        },
        error: (function (result) {
            BindEmptyOptions();
        })
    });
}

function BindListOptions(data_, enqSrcID_) {
    EnqCategoryList = data_;

    var enqCategoryHTML = '<option value="">Select from list</option>';

    for (var x = 0; x < data_.length; ++x) {
        enqCategoryHTML = enqCategoryHTML + '<option value="' + data_[x].EnqCategoryID + '">' + data_[x].Description + '</option>';
    }

    $('#EnqCategory').html(enqCategoryHTML);
    styleSelects();
    DisableEnqSrcId();

    if (enqSrcID_ != '') {
        for (var x = 0; x < EnqCategoryList.length; ++x) {
            for (var y = 0; y < EnqCategoryList[x].EnqSrcs.length; ++y) {
                if (EnqCategoryList[x].EnqSrcs[y].EnqSrcID == enqSrcID_) {
                    $('#EnqCategory').val(EnqCategoryList[x].EnqCategoryID);
                    break;
                }
            }
        }
        UpdateEnqSrcList(enqSrcID_);
    }

    if (EnqCategoryList.length > 0) {
        MarkAsRequired();
    } else {
        MarkAsNotRequired();
    }
}

function UpdateEnqSrcList(enqSrcID_) {

    styleSelects();

    if (enqSrcID_ == undefined || enqSrcID_ == null || enqSrcID_ == 0) { ercSrcId_ = ''; }

    var EnqCategoryID = $('#EnqCategory').val();
    var enqSrcHTML = '<option value="">Please specify...</option>';

    for (var x = 0; x < EnqCategoryList.length; ++x) {
        if (EnqCategoryList[x].EnqCategoryID == EnqCategoryID) {
            for (var y = 0; y < EnqCategoryList[x].EnqSrcs.length; ++y) {
                enqSrcHTML = enqSrcHTML + '<option value="' + EnqCategoryList[x].EnqSrcs[y].EnqSrcID + '">' + EnqCategoryList[x].EnqSrcs[y].EnqDescription + '</option>';
            }
            break;
        }
    }

    if (EnqCategoryID != '') {
        $('select[name="EnqSrcId"]').html(enqSrcHTML).val(enqSrcID_)
        MarkAsRequired();
        styleSelects();
    } else {
        DisableEnqSrcId();
    }
}

function BindEmptyOptions() {
    $('#EnqCategory').html('');
    styleSelects();
    DisableEnqSrcId();
    MarkAsNotRequired();
    $('#EnqSrc-Container').hide();
}

function MarkAsRequired() {
    $('select[name="EnqSrcId"]').addClass('required_field');
    $('#EnqCategory').addClass('required_field');
}

function MarkAsNotRequired() {
    $('select[name="EnqSrcId"]').removeClass('required_field');
    $('#EnqCategory').removeClass('required_field');
}

function DisableEnqSrcId() {
    var EnqSrcId = $('select[name="EnqSrcId"]');
    $(EnqSrcId).html('<option value=""></option>').val('');

    styleSelects();
    $('.select-box-wrapper[for-select="' + $(EnqSrcId).attr('id') +'"]').hide();
}
