﻿jQuery(document).ready(function() {



    //This script is a copy from DSB Talk


    if (location.pathname.indexOf('optankning.asp') >= 0) {

        var strSearch = 'jCard=False';
        if (window.location.href.indexOf(strSearch) < 0) {
            jQuery('.refill_Card:eq(0)').attr('checked', true);
        }

        var thisID = jQuery('#refill_Parrent').val();
        jQuery('#' + thisID).hide();
        jQuery('#refill_Written').val(FormatNumber(jQuery('#refill_Written').val(), true));

        jQuery('#refill_Written').keyup(function() {
            if (isNaN(parseFloat(jQuery('#refill_Written').val().replace('.', '').replace(',', '.'))) || parseFloat(jQuery('#refill_Written').val().replace('.', '').replace(',', '.')) < 1) {
                jQuery('#refill_Amount').html(FormatNumber(1, true));
                jQuery('#' + thisID).val('1');
            } else {
                jQuery('#refill_Amount').html(FormatNumber(jQuery('#refill_Written').val(), true));
                jQuery('#' + thisID).val(HandleInput(jQuery('#refill_Written').val()));

            }
            SetOrderDetails();
        });

        jQuery('#refill_Written').blur(function() {
            jQuery('#refill_Written').val(FormatNumber(jQuery('#refill_Written').val(), true));

            if (parseFloat(FormatNumber(jQuery('#refill_Written').val(), true).replace('.', '').replace(',', '.')) < 1) {
                jQuery('#refill_Written').val(FormatNumber('1', true));
                jQuery('#' + thisID).val('1');
            }

            if (jQuery('.refill_Amount:checked').attr('id') == thisID) {
                GetPaymentFees(jQuery('#refill_Written').val());
            }
            else {
                GetPaymentFees(jQuery('.refill_Amount:checked').val());
                jQuery('#refill_Written').val('');
            }

        });

        jQuery('#refill_Written').click(function() {
            if (jQuery('#refill_Written').val() == '') {
                jQuery('#refill_Written').val(FormatNumber('1', true));
                jQuery('#' + thisID).val('1')
            }
            jQuery('#' + thisID).attr('checked', true);
            jQuery('#' + thisID).click();
            jQuery('#refill_Written').select();
        });

        jQuery('.refill_Amount').click(function() {

            if (jQuery('.refill_Amount:checked').attr('id') == thisID) {
                GetPaymentFees(jQuery('#refill_Written').val());
            }
            else {
                GetPaymentFees(jQuery('.refill_Amount:checked').val());
                jQuery('#refill_Written').val('');
            }

            if (jQuery(this).attr('id') == thisID) {
                jQuery('#refill_Amount').html(FormatNumber(jQuery('#refill_Written').val(), true));
                jQuery('#refill_Written').focus();
            }
            else {
                jQuery('#refill_Amount').html(FormatNumber(jQuery(this).val(), true));
                jQuery('#refill_Written').val('');

            }
            SetOrderDetails();
        });

        jQuery('.refill_Card').click(function() { SetOrderDetails(); });


        if (jQuery('.refill_Amount:checked').attr('id') == thisID) {
            GetPaymentFees(jQuery('#refill_Written').val());
        }
        else {
            GetPaymentFees(jQuery('.refill_Amount:checked').val());
            jQuery('#refill_Written').val('');
        }

        SetOrderDetails();
    }
});

function HandleInput(thisAmount) {
    var thisReturn = thisAmount;
    var commaIndex = -1;
    var dotIndex = -1;
    commaIndex = thisAmount.indexOf(',');
    dotIndex = thisAmount.indexOf('.');
    if (commaIndex > dotIndex) {
        thisReturn = thisAmount.substr(0, commaIndex);
    } else {
        if (commaIndex < dotIndex) {
            thisReturn = thisAmount.substr(0, dotIndex);
        }
    }
    thisReturn = thisReturn.replace(',', '').replace('.', '');
    return thisReturn;
}

var GetPaymentAmount = function() {
    var amount = 0;
    amount = parseFloat(jQuery('#refill_Amount').html().replace('.', '').replace(',', '.'));

    if (isNaN(amount)) {
        amount = 0;
    }
    return amount;
};

var GetPaymentFee = function(argAmount, argRefillCard) {

};

var SetOrderDetails = function() {
    if (jQuery('.refill_Amount:checked').attr('id') == jQuery('#refill_Parrent').val()) {
        jQuery('#refill_Amount').html(FormatNumber(jQuery('#refill_Written').val(), true));
    }
    else {
        jQuery('#refill_Amount').html(FormatNumber(jQuery('.refill_Amount:checked').val(), true));
    }
    jQuery('#refill_Fee').html(FormatNumber(jQuery('.refill_Card:checked').next().children('.feeInfo').html(), false));

    var total = 0;

    total = parseFloat(new String(jQuery('#refill_Amount').html()).replace('.', '').replace(',', '.'));
    total = parseFloat(total) + parseFloat(new String(jQuery('#refill_Fee').html()).replace('.', '').replace(',', '.'));

    jQuery('#refill_Total').html(FormatNumber(new String(total).replace('.', ','), false));
}

var FormatNumber = function(number, alwaysInteger) {

    var _number = parseFloat(new String(number).replace('.', '').replace(',', '.'));
    var string = '';
    var numbers;

    if (isNaN(_number)) { _number = 0; }


    _number = _number * 100;
    _number = Math.round(_number) / 100;
    _number += 0.00000001;

    numbers = (new String(_number)).split('.');

    for (var i = 1; i < numbers[0].length + 1; i++) {
        if (i % 3 == 0) {

            string = numbers[0].substr(numbers[0].length - i, 1) + string;
            if (!(numbers[0].length == i)) {
                string = '.' + string;
            }
        }
        else {
            string = numbers[0].substr(numbers[0].length - i, 1) + string;
        }
    }

    if (numbers.length == 2) {
        if (alwaysInteger) {
            string += ',00';
        }
        else {
            string += ',' + numbers[1].substr(0, 2);
        }
    }
    else { string += ',00'; }

    return string;
};


var GetPaymentFees = function(argAmount) {

    var paymentmethods = '';
    jQuery('.refill_Card').each(function() {

        if (paymentmethods.length > 0) {
            paymentmethods += ';';
        }
        paymentmethods += jQuery(this).val();


    });

    var ajaxRequest = '';
    if (location.pathname.indexOf('/optankning.asp') >= 0) {
        ajaxRequest = 'refill';
    }
    else {
        ajaxRequest = 'refillSubscription';
    }

    jQuery.ajax({
        url: '?' + ajaxRequest + '=ajax&amount=' + Math.round(parseFloat(new String(argAmount).replace('.', '').replace(',', '.'))) + '&paymentmethods=' + paymentmethods,
        async: true,
        cache: false,
        dataType: 'json',
        data: {},
        type: 'GET',
        beforeSend: function() { jQuery('#refill_Submit').attr('disabled', true); },
        error: function(XMLHttpRequest, textStatus, errorThrown) { },
        success: function(data, textStatus) {

            if (data) {

                if (data.paymentFees) {

                    for (var i = 0; i < data.paymentFees.length; i++) {
                        var paymentFee = data.paymentFees[i].paymentFee;
                        if (paymentFee.fee != null) {
                            jQuery('input[type=\'radio\'][name=\'paymentmethod\'][value=\'' + paymentFee.paymentMethod + '\']').next().children('.feeInfo').html(FormatNumber(new String(paymentFee.fee).replace('.', ','), false));
                            jQuery('input[type=\'radio\'][name=\'paymentmethod\'][value=\'' + paymentFee.paymentMethod + '\']').attr('disabled', false);
                        }
                        else {
                            jQuery('input[type=\'radio\'][name=\'paymentmethod\'][value=\'' + paymentFee.paymentMethod + '\']').attr('disabled', true);
                            jQuery('input[type=\'radio\'][name=\'paymentmethod\'][value=\'' + paymentFee.paymentMethod + '\']').next().children('.feeInfo').html('');
                        }
                    }
                }
            }

        },
        complete: function() { SetOrderDetails(); jQuery('#refill_Submit').attr('disabled', false); }
    });
};
