﻿var QueryGetDetailedQuoteSummaryUrl = mvcCommonControllerUrl + "/QueryGetDetailedQuoteSummary";
var CheckLHCLoadOptionsUrl = mvcCommonControllerUrl + "/CheckLHCLoadOptions";
var ValidateDetailedQuoteInputsUrl = mvcCommonControllerUrl + "/ValidateDetailedQuoteInputs";

var OmnitureDetailedQuoteTracker =
{
    _buttonNameShowPopup: '',
    _buttonNameGetQuote: '',

    init: function(buttonNameShowPopup, buttonNameGetQuote) {
        OmnitureDetailedQuoteTracker._buttonNameShowPopup = buttonNameShowPopup;
        OmnitureDetailedQuoteTracker._buttonNameGetQuote = buttonNameGetQuote;
    },

    showPopup: function() {
        if (OmnitureDetailedQuoteTracker.isButtonClickedDefined() && (OmnitureDetailedQuoteTracker._buttonNameShowPopup != '')) {
            ButtonClicked(OmnitureDetailedQuoteTracker._buttonNameShowPopup);
        }
    },

    getQuote: function() {
        if (OmnitureDetailedQuoteTracker.isButtonClickedDefined() && (OmnitureDetailedQuoteTracker._buttonNameGetQuote != '')) {
            ButtonClicked(OmnitureDetailedQuoteTracker._buttonNameGetQuote);
        }
    },

    isButtonClickedDefined: function() {
        return (typeof ButtonClicked == 'function');
    }
}

function GetDetailedQuote() {

    var timeoutId = FreezePremiumDisplay();
    HidePrintButton();

    //build the url
    var _coverStartDate = $("#txtCoverStartDate").val();
    var _rebate = $('.FedRebate:checked').attr("value");
    var _directDebitDiscount = $('.PaymentFrequencySelector:checked').attr("value");
    var _applicantDateOfBirth = $('#txtApplicantDateOfBirth').val();
    var _partnerDateOfBirth = $('#txtPartnerDateOfBirth').val();
    var _applicantLHC = $('.ApplicantLHC:checked').attr("value");
    var _parterLHC = $('.PartnerLHC:checked').attr("value");
    var _excessLevel = $('.ExcessLevel:checked').attr("value");
    var _hospitalCode = $('#HospitalCodeHidden').val();
    var _extrasCode = $('#ExtrasCodeHidden').val();
    var _combCode = $('#CombinationCodeHidden').val();
    var _packageCode = $('#ProductPackageCode').val();
    var _notloaded = $('#DataNotLoaded').val();
    var _hasExcess = $('#HasExcessHidden').val();

    $.ajax({
        type: "POST",
        url: QueryGetDetailedQuoteSummaryUrl,
        data: ({ coverStartDate: _coverStartDate,
            rebate: _rebate,
            directDebitType: _directDebitDiscount,
            applicantDOB: _applicantDateOfBirth,
            partnerDOB: _partnerDateOfBirth,
            applicantLHC: _applicantLHC,
            partnerLHC: _parterLHC,
            excessLevel: _excessLevel,
            hospitalCode: _hospitalCode,
            extraCode: _extrasCode,
            combCode: _combCode,
            packageCode: _packageCode,
            hasExcess: _hasExcess
        }),
        async: true,
        success: function(msg) {
            if (msg.toString().length == 0) {
                if (_notloaded == 'True') {
                    UnFreezePremiumDisplay();
                }

                ClearTimeoutError(timeoutId);
                return false;
            }
            else {
                $('#DetailedQuoteSummaryDisplay').html(msg);
                UnFreezePremiumDisplay();
                ClearTimeoutError(timeoutId);                
                DisplayPrintButton();
                OmnitureDetailedQuoteTracker.getQuote();
            }
        },
        error: function(request, error) {
            //600 is custome code for session time out
            if (request.status == 600) {
                window.location = homeAddressUrl;
            }
            else {
                window.location = generalErrorUrl;
            }
        }
    });
}

function FreezePremiumDisplay() {
    $('#DetailedQuoteSummaryDisplay').fadeTo(250, 0.3);
    
    // if it takes more then 5 secs to load, the loading will be dismissed automatically
    setTimeout(function() {
        if ($('.waiting').is(':visible')) { $('.DetailedEntry').unblock(); };
    }, 5000);

    // the loading will only appear if the waiting time is longer than 1 sec
    return setTimeout(function() {
        $('.DetailedEntry').block({ message: $('.waiting'), css: { border: 'none'} });
    }, 1000);
}

function ClearTimeoutError(timeoutId) {
    // clear the timer
    clearTimeout(timeoutId);
    // dismiss the loading bar
    $('.DetailedEntry').unblock();
}

function UnFreezePremiumDisplay() {
    $('#DetailedQuoteSummaryDisplay').fadeTo(250, 1.0);
}

function DisplayPrintButton() {
    var salesChannelType = $('#SalesChannelType').val();
    if (salesChannelType != 'Affiliate') {
        $('.But').show();
    }
}

function HidePrintButton() {
    $('#PrintButton').hide();
}

function CheckLHCOptions() {

    var _applicantDateOfBirth = $('#txtApplicantDateOfBirth').val();
    var _partnerDateOfBirth = $('#txtPartnerDateOfBirth').val();
    var _hospitalCode = $('#HospitalCodeHidden').val();
    var _extrasCode = $('#ExtrasCodeHidden').val();

    $.ajax({
        type: "POST",
        url: CheckLHCLoadOptionsUrl,
        data: ({ hospitalCode: _hospitalCode,
            extraCode: _extrasCode,
            applicantDoB: _applicantDateOfBirth,
            partnerDob: _partnerDateOfBirth
        }),
        async: false,
        success: function(msg) {
            var object = JSON.parse(msg);
            if (object.applicantLHCRequired != undefined) {
                if (object.applicantLHCRequired == true) {
                    $(".applicantLHC").slideDown("fast");
                }
                else {
                    $(".applicantLHC").slideUp("fast");
                }
            }
            if (object.partnerLHCRequired != undefined) {
                if (object.partnerLHCRequired == true) {
                    $(".partnerCover").slideDown("fast");
                }
                else {
                    $(".partnerCover").slideUp("fast");
                }
            }
        },
        error: function(request, error) {
        }
    });
}

function ValidateInputs() {

    var _coverStartDate = $("#txtCoverStartDate").val();
    var _applicantDateOfBirth = $('#txtApplicantDateOfBirth').val();
    var _partnerDateOfBirth = $('#txtPartnerDateOfBirth').val();

    $.ajax({
        type: "POST",
        url: ValidateDetailedQuoteInputsUrl,
        data: ({ coverStartDate: _coverStartDate,
            applicantDoB: _applicantDateOfBirth,
            partnerDob: _partnerDateOfBirth
        }),
        async: false,
        success: function(msg) {
            var object = JSON.parse(msg);
            $(".CoverStartDateError").html(object.coverStartDate);
            $(".ApplicantDateOfBirthError").html(object.applicantDoB);
            $(".PartnerDateOfBirthError").html(object.partnerDob);
        },
        error: function(request, error) {
        }
    });
}

function CoverStartDateChanged() {
    ValidateInputs();
    GetDetailedQuote();
}

function DateOfBirthChanged() {
    CheckLHCOptions();
    ValidateInputs();
    GetDetailedQuote();
}



