HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //proc/self/cwd/nueva/modules/roja45quotationsprofree/views/js/roja45quotationsadmin.js
/**
 * roja45quotationsadmin.js.
 *
 * @author    Roja45
 * @copyright 2016 Roja45
 * @license   license.txt
 *
 * 2016 ROJA45 - All rights reserved.
 *
 * DISCLAIMER
 * Changing this file will render any support provided by us null and void.
 */

$(document).ready(function() {
    // Init all events
    roja45quotationsprofree.initialiseEvents();
    note_box = $('#note-detail-div');
    note_box.dialog({
        autoOpen:false,
        title:"Title",
        show: "fade",
        hide: "fade"
    });

    $('#container-quotations').on('click', '#claimRequest', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_claimrequest,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.claimRequest(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#releaseRequest', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_releaserequest,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.releaseRequest(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#resetCart', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_resetcart,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.resetCart(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#createCustomerAccount', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_createcustomeraccount,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.createCustomerAccount(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });


    $('#container-quotations').on('click', '.sendCustomerQuotation', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.sendCustomerQuotation(target);
    });

    $('#container-quotations').on('click', '#saveQuotation', function(e) {
        roja45global.toggleModal();
    });

    $('#container-quotations').on('click', '#addQuotationToOrder', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.addQuotationToOrder(target);
    });

    $('#container-quotations').on('click', '#sendCustomerMessage', function(e) {
        e.preventDefault();
        var editor = tinyMCE.get('final-quotation-response');
        editor.setContent('');
        tinyMCE.triggerSave();
        $('#quotation-response-panel').slideDown();
        $('#sendQuotationToCustomer').removeAttr('disabled');
        $('#cancelSendQuotationToCustomer').removeAttr('disabled');
        $('#sendQuotationToCustomer').attr('data-message-type', 'sendMessageToCustomer');
        $('#reponse-selection-panel .panel-body').show();
        $(document).scrollTo('#sendQuotationToCustomer');
    });

    $('#container-quotations').on('click', '#sendMessageForm', function(e) {
        tinyMCE.triggerSave();
    });

    $('#container-quotations').on('click', '#loadMessageTemplate', function(e) {
        e.preventDefault();
        roja45quotationsprofree.loadMessageTemplate();
    });

    $('#container-quotations').on('click', '#add_quotation_product', function(e) {
        $('.cancel_product_change_link:visible').trigger('click');
        $('.add_product_quotation_fields').show();
        $('.quotation_action .btn-group').hide();
        $('#new_quotation_product').slideDown('fast', function () {
            $('#add_product_product_name').focus();
            scroll_if_anchor('#new_product');
        });
        e.preventDefault();
    });

    $('#container-quotations').on('click', '#submitAddProductToQuotation', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.submitAddProductToQuotation(target);
        /*
        $.confirm({
            text: roja45quotationspro_txt_addproduct,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitAddProductToQuotation(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
        */
    });

    $('#container-quotations').on('click', '.delete_product_quotation_line', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_deleteproduct,
            title: roja45quotationspro_txt_confirm,
            confirm: function () {
                roja45quotationsprofree.submitDeleteProductFromQuotation(target);
            },
            cancel: function () {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '.edit_product_quotation_change_link', function(e) {
        roja45quotationsprofree.editProductLine(e);
    });

    $('#container-quotations').on('click', '.cancel_product_quotation_change_link', function(e) {
        e.preventDefault();
        current_product = null;
        $('.edit_product_quotation_fields').hide();
        $('.add_product_quotation_fields').show();
        var element_list = $('.customized-' + $(this).parent().parent().find('.edit_product_id_order_detail').val());
        if (!element_list.length)
            element_list = $(this).closest('.product-line-row');
        element_list.find('td .product_price_show').show();
        element_list.find('td .product_quantity_show').show();
        element_list.find('td .product_price_edit').hide();
        element_list.find('td .product_quantity_edit').hide();
        //element_list.find('td.product_invoice').hide();
        //element_list.find('td.cancelCheck').show();
        //element_list.find('td.cancelQuantity').show();
        element_list.find('.edit_product_quotation_change_link').parent().show();
        element_list.find('button.submitProductQuotationChange').hide();
        element_list.find('.cancel_product_quotation_change_link').hide();
        //element_list.find('td.total_product').html(current_total);
    });

    $('#container-quotations').on('click', 'button.submitProductQuotationChange', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.submitProductLineChange(target);
    });

    $('#container-quotations').on('click', '#add_quotation_charge', function(e) {
        $('.quotation_action').fadeOut();
        $('.panel-charges #charges_form').slideDown();
        e.preventDefault();
    });

    $('#container-quotations').on('click', '#cancel_add_charge', function(e) {
        $('#charges_form').slideUp();
        if ($('#charges_table .charge_row').length > 0)
            has_charge = 1;
        else
            has_charge = 0
        if (has_charge)
            $('.charges-vouchers').show();
        $('.quotation_action').fadeIn();
        e.preventDefault();
    });

    $('#container-quotations').on('click', 'button.submit-new-charge', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.submitNewCharge(target);
        /*
        $.confirm({
            text: roja45quotationspro_txt_addcharge,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitNewCharge(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
        */
    });

    $('#container-quotations').on('click', '.submit-delete-charge', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_deletecharge,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitDeleteCharge(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '.add_discount', function(e) {
        e.preventDefault();
        $('.quotation_action').fadeOut();
        $('.panel-vouchers,#voucher_form').slideDown();
    });

    $(document).on('click', '#quotationspro_createorder', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_createorder,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitNewCustomerOrder(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#cancel_add_voucher', function(e) {
        $('#voucher_form').slideUp();

        if ($('#discount_table .discount_row').length > 0)
            has_voucher = 1;
        else
            has_voucher = 0
        if (has_voucher)
            $('.panel-vouchers').show();
        $('.quotation_action').fadeIn();
        e.preventDefault();
    });

    $('#container-quotations').on('click', 'button.submitNewVoucher', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.submitAddDiscount(target);
        /*
        $.confirm({
            text: roja45quotationspro_txt_adddiscount,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitAddDiscount(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
        */
    });

    $('#container-quotations').on('click', 'a.submitDeleteVoucher', function(e) {
        e.preventDefault();
        var target = $(e.target);
        $.confirm({
            text: roja45quotationspro_txt_deletediscount,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitDeleteDiscount(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#submitQuotationNote', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.submitAddNote(target);
        /*
        $.confirm({
            text: roja45quotationspro_txt_addnote,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.submitAddNote(target);
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
        */
    });

    $('#container-quotations').on('click', '.btn.btn-set-status', function(e) {
        $.ajax({
            type: 'GET',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'submitSetQuotationStatus',
                'id_roja45_quotation': id_roja45_quotation,
                'id_status': $('#quotation_status').val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                    /*
                    $('#current_status').fadeOut('400', function() {
                        $('#current_status').text(data.name);
                        $('#current_status').css('background-color', data.color);
                        $('#current_status').fadeIn();
                    });
                    */
                } else {
                    jAlert(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                //roja45global.toggleModal();
            }
        });
    });

    $('#container-quotations').on('click', '#sendQuotationToCustomer', function(e) {
        e.preventDefault();
        $.confirm({
            text: roja45quotationspro_txt_sendcustomerquotation,
            title: roja45quotationspro_txt_confirm,
            confirm: function() {
                roja45quotationsprofree.sendQuotationToCustomer();
            },
            cancel: function() {
                // nothing to do
            },
            confirmButton: roja45quotationspro_confirmbutton,
            cancelButton: roja45quotationspro_cancelbutton,
            post: true,
            confirmButtonClass: "btn-danger",
            cancelButtonClass: "btn-default",
            dialogClass: "bootstrap modal-dialog" // Bootstrap classes for large modal
        });
    });

    $('#container-quotations').on('click', '#cancelSendQuotationToCustomer', function(e) {
        e.preventDefault();
        var target = $(e.target);
        roja45quotationsprofree.cancelSendQuotationForm(target);
    });

    $('#container-quotations').on('click', '#addQuotationToAccount', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'GET',
            url: quotationspro_link+query,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'addQuotationToAccount',
                'id_roja45_quotation': id_roja45_quotation
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                    $('#quotation-pipeline .in-cart.label-danger').fadeOut('fast', function () {
                        $('#quotation-pipeline .in-cart.label-success').fadeIn();
                    });
                } else {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected+'\n'+data.responseText);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    });

    $(document).on('click', '#quotationspro_request_dialog .cross, #quotationspro_request_dialog .continue, .quotationspro_request_dialog_overlay', function (e) {
        e.preventDefault();
        roja45quotationsprofree.closeDialog();
    });

    $('#container-quotations').on('change','input.product_quotation_quantity',function(e) {
        roja45quotationsprofree.editProductRefreshTotal($(this));
    });

    $('#container-quotations').unbind('keyup').on('keyup', 'input.product_quotation_price_tax_excl', function(e) {
        var price_tax_excl = parseFloat($(this).val());
        if (price_tax_excl < 0 || isNaN(price_tax_excl))
            price_tax_excl = 0;

        var tax_rate = current_product.tax_rate / 100 + 1;
        $(this).closest('tr.product-line-row').find('.product_quotation_price_tax_incl').val(ps_round(price_tax_excl * tax_rate, 2));

        // Update total product
        roja45quotationsprofree.editProductRefreshTotal($(this));
    });

    $('#container-quotations').unbind('keyup').on('keyup', 'input.product_quotation_price_tax_incl', function(e) {
        var price_tax_incl = parseFloat($(this).val());
        if (price_tax_incl < 0 || isNaN(price_tax_incl)) {
            price_tax_incl = 0;
        }

        var tax_rate = current_product.tax_rate / 100 + 1;
        $(this).closest('tr.product-line-row').find('.product_quotation_price_tax_excl').val(ps_round(price_tax_incl / tax_rate, 2));

        // Update total product
        roja45quotationsprofree.editProductRefreshTotal($(this));
    });

    roja45quotationsprofree.tinySetup();
});


roja45quotationsprofree = {
    tinySetup : function (config)
    {
        if (typeof tinyMCE === 'undefined') {
            setTimeout(function() {
                roja45quotationsprofree.tinySetup(config);
            }, 100);
            return;
        }
        tinyMCE.init({
            selector: ".rte" ,
            mode : "textareas",
            theme : "advanced",
            plugins : "safari,pagebreak,style,layer,table,advimage,advlink,inlinepopups,media,searchreplace,contextmenu,paste,directionality,fullscreen",
            theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
            theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,,|,forecolor,backcolor",
            theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,|,ltr,rtl,|,fullscreen",
            theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,pagebreak",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : false,
            content_css : "/themes/default-bootstrap/css/global.css",
            document_base_url : "/",
            width: "600",
            height : "500",
            font_size_style_values : "8pt, 10pt, 12pt, 14pt, 18pt, 24pt, 36pt",
            template_external_list_url : "lists/template_list.js",
            external_link_list_url : "lists/link_list.js",
            external_image_list_url : "lists/image_list.js",
            media_external_list_url : "lists/media_list.js",
            elements : "nourlconvert",
            entity_encoding: "raw",
            convert_urls : false,
            language : "en",
            setup: function (editor) {
                editor.on('BeforeSetContent', function (contentEvent) {
                    contentEvent.content = contentEvent.content.replace(/\r?\n/g, '<br />');
                })
            }
        });
    },

    initialiseEvents : function()
    {
        $('select#product_quotation_id_product_attribute').unbind('change');
        $('select#product_quotation_id_product_attribute').change(function() {
            $('#add_product_quotation_price_tax_incl').val(current_product.combinations[$(this).val()].price_tax_incl);
            $('#add_product_quotation_price_tax_excl').val(current_product.combinations[$(this).val()].price_tax_excl);
            roja45quotationsprofree.addProductRefreshTotal();
        });

        $('#cancelAddProductToQuotation').off('click').on('click', function() {
            $('.quotation_action .btn-group').show();
            $('#new_quotation_product').slideUp('fast');
        });

        $('#customer_main_address').off('change').on('change', function(e) {
            roja45global.toggleModal();
            $.ajax({
                type: 'GET',
                url: quotationspro_link,
                cache: false,
                dataType: 'json',
                data : {
                    'ajax': 1,
                    'token': token,
                    'action': 'submitSetCustomerAddress',
                    'id_roja45_quotation': id_roja45_quotation,
                    'id_address': $(this).val(),
                },
                success : function(data)
                {
                    if (data.result && data.result=='success')
                    {
                        roja45global.displaySuccessMsg(data.message);
                    }
                    else
                        jAlert(data.errors);
                },
                error: function(data) {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                },
                complete: function() {
                    roja45global.toggleModal();
                }
            });
        });

        $('#discount_type').off('change').on('change',function() {
            // Percent type
            if ($(this).val() == 1)
            {
                $('#discount_value_field').show();
                $('#discount_currency_sign').hide();
                $('#discount_value_help').hide();
                $('#discount_percent_symbol').show();
                $('#with_tax').show();
                $('#with_tax').val(1);
                $('#with_tax').removeAttr('disabled');
            }
            // Amount type
            else if ($(this).val() == 2)
            {
                $('#discount_value_field').show();
                $('#discount_percent_symbol').hide();
                $('#discount_value_help').show();
                $('#discount_currency_sign').show();
                $('#with_tax').show();
                $('#with_tax').val(2);
                $('#with_tax').attr('disabled', 'disabled');
            }
            // Free shipping
            else if ($(this).val() == 3)
            {
                $('#discount_value_field').hide();
                $('#with_tax').hide();
            }
        });

        $('#apply_discount').off('change').on('change', function(e) {
            if ($(this).val() == 1)
            {
                $('#discount_form_select_product').hide();
            } else {
                $('#discount_form_select_product').show();
            }
        });

        $('#charge_type').off('change').on('change', function(e) {
            if ( $(this).val() == 'shipping' ) {
                $('#charge-method-block').show();
                $('#charge_method').val(2);
                $('#charge-method-block').hide();
                $('#charge-name-block').hide();
                $('#carriers-block').show();
                $('#available-taxes-block').hide();
                $('#charge_value').addClass('disabled');
                $('#charges_warning').hide();
            } else if ( $(this).val() == 'general' ) {
                $('#charge-method-block').show();
                $('#charge-name-block').show();
                $('#charge-method-block').show();
                $('#carriers-block').hide();
                $('#available-taxes-block').hide();
                $('#charge_name').val('');
                $('#charge_value').val('0.00');
                $('#charge_value').removeClass('disabled');
                $('#charges_warning').slideDown();
            } else if ( $(this).val() == 'handling' ) {
                $('#charge-method-block').show();
                $('#charge_method').val(2);
                $('#charge-method-block').hide();
                $('#charge-name-block').show();
                $('#charge_name').val('');
                $('#carriers-block').hide();
                $('#available-taxes-block').hide();
                $('#charge_value').val('0.00');
                $('#charge_value').removeClass('disabled');
                $('#charges_warning').slideDown();
            } else {

            }
        }).trigger('change');

        $('#carriers').off('change').on('change', function(e) {
            var selected = $(this).val();
            var name = $(this).find('option[value='+selected+']').attr('data-name');
            var rate = $(this).find('option[value='+selected+']').attr('data-rate');
            $('#charge_name').val(name);
            if (rate) {
                $('#charge_value').val(rate);
            } else {
                $('#charge_value').val(0.0);
            }
        }).trigger('change');

        $('#charge_type').val('shipping').change();

        $('#charge_method').off('change').on('change', function() {
            // Percent type
            if ($(this).val() == 1)
            {
                $('charge_value_field').show();
                $('#charge_currency_sign').hide();
                $('#charge_value_help').hide();
                $('#charge_percent_symbol').show();
            }
            // Amount type
            else if ($(this).val() == 2)
            {
                $('#charge_value_field').show();
                $('#charge_percent_symbol').hide();
                $('#charge_value_help').show();
                $('#charge_currency_sign').show();
            }
        }).trigger('change');

        $('input[name=ROJA45_QUOTATIONSPRO_ENABLE_TAXES]').off('click').on('click', function(e) {
            var checked = $('input[name=ROJA45_QUOTATIONSPRO_ENABLE_TAXES]:checked').val();
            var id_country = $('#tax_country').val();
            if ($('#tax_country').val() <= 0)
            {
                jAlert(txt_enable_taxes_country_missing);
                return false;
            }
            $.ajax({
                type: 'GET',
                url: quotationspro_link,
                cache: false,
                dataType: 'json',
                data : {
                    'ajax': 1,
                    'token': token,
                    'action': 'submitEnableTaxes',
                    'id_roja45_quotation': id_roja45_quotation,
                    'enable_taxes': checked,
                },
                beforeSend : function() {
                    roja45global.toggleModal();
                },
                success : function(data)
                {
                    if (data.result) {
                        // update quotation table.
                        $('#quotation_panel').empty();
                        $('#quotation_panel').append(data.view);
                        roja45quotationsprofree.initialiseEvents();
                    } else {
                        jAlert(data.errors);
                    }
                },
                error: function(data) {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                },
                complete: function() {
                    roja45global.toggleModal();
                }
            });
        });

        $('#quote_currency').off('change').on('change', function(e) {
            roja45quotationsprofree.submitSetCurrency($(this));
        });

        $('#tax_country').off('change').on('change', function(e) {
            roja45quotationsprofree.submitSetCountry($(this));
        });

        $('#tax_state').off('change').on('change', function(e) {
            // get states.
            roja45quotationsprofree.submitSetState($(this));
        });

        $('#quote_language').off('change').on('change', function(e) {
            roja45quotationsprofree.submitSetLanguage($(this));
        });

        $('#valid_for').off('change').on('change', function(e) {
            roja45quotationsprofree.submitSetValidForDays($(this));
        });

        $('.quotation-note-link').each(function(key, value) {
            $(this).off('click').on('click', function(e) {
                e.preventDefault();
                var html = $(this).parent().find('.note-detail-div');
                note_box.html(html.html());
                note_box.dialog('option', 'title', html.data('received'));

                setTimeout( function()
                {
                    note_box.dialog('open');
                }, 200);

            });
        });

        $('.delete-quotation-note-link').each(function(key, value) {
            $(this).off('click').on('click', function(e) {
                e.preventDefault();
                var element = $(this);
                $.ajax({
                    type: 'POST',
                    url: quotationspro_link,
                    cache: false,
                    dataType: 'json',
                    data : {
                        'ajax': 1,
                        'token': token,
                        'action': 'deleteQuotationNote',
                        'id_roja45_quotation': id_roja45_quotation,
                        'id_roja45_quotation_note': $(this).attr('data-id-roja45-quotation-note'),
                    },
                    beforeSend : function() {
                        roja45global.toggleModal();
                    },
                    success : function(data)
                    {
                        if (data.result && data.result=='success') {
                            roja45global.displaySuccessMsg(data.response);
                            window.location = data.redirect;
                            /*
                            $('#notes_table').fadeOut('fast', function() {
                                $('#notes_table').html(data.view);
                                $('#notes_table').show();
                            });*/
                        } else if(data.result && data.result=='error') {
                            roja45global.displayErrorMsg(data.errors);
                        }
                    },
                    error: function(data) {
                        roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                    },
                    complete: function() {
                        //roja45global.toggleModal();
                    }
                });
            });
        });

        $('.delete-customer-message').each(function(key, value) {
            $(this).off('click').on('click', function(e) {
                e.preventDefault();
                alert('Delete not implemented');
            });
        });
        
        roja45quotationsprofree.initProductLineEvents();
    },

    initProductLineEvents : function() {
        $("#addProductToQuotationProductSearch").autocomplete(quotationspro_link,
            {
                minChars: 3,
                max: 10,
                width: 500,
                selectFirst: false,
                scroll: false,
                dataType: "json",
                highlightItem: true,
                formatItem: function(data, i, max, value, term) {
                    return value;
                },
                parse: function(data) {
                    var products = new Array();
                    if (typeof(data.products) != 'undefined')
                        for (var i = 0; i < data.products.length; i++)
                            products[i] = { data: data.products[i], value: data.products[i].name };
                    return products;
                },
                extraParams: {
                    ajax: true,
                    token: token,
                    action: 'searchProducts',
                    id_lang: id_lang,
                    id_currency: id_currency,
                    product_search: function() { return $('#addProductToQuotationProductSearch').val(); }
                }
            }
        ).result(function(event, data, formatted) {
            if (!data) {
                $('#new_quotation_product input, #new_quotation_product select').each(function() {
                    if ($(this).attr('id') != 'addProductToQuotationProductSearch')
                        $('#new_quotation_product input, #new_quotation_product select, #new_quotation_product button').attr('disabled', true);
                });
            } else {
                var ele = $('#addProductToQuotationProductSearch').closest('tr.product-line-row');
                $('#new_quotation_product input, #new_quotation_product select, #new_quotation_product button').removeAttr('disabled');
                // Keep product variable
                current_product = data;
                $('#addProductToQuotationProductSearch').val(data.name);
                ele.find('.product_quotation_id').val(data.id_product);
                ele.find('.product_quotation_quantity').val(1);
                ele.find('.product_quotation_price_tax_incl').val(data.price_tax_incl);
                ele.find('.product_quotation_price_tax_excl').val(data.price_tax_excl);
                ele.find('.product_wholesale_price').html(data.wholesale_price);
                roja45quotationsprofree.editProductRefreshTotal($('#submitAddProductToQuotation'));

                if (current_product.combinations.length !== 0) {
                    // Reset combinations list
                    $('select#product_quotation_id_product_attribute').html('');
                    var defaultAttribute = 0;
                    $.each(current_product.combinations, function() {
                        $('select#product_quotation_id_product_attribute').append('<option value="'+this.id_product_attribute+'"'+(this.default_on == 1 ? ' selected="selected"' : '')+'>'+this.attributes+'</option>');
                        if (this.default_on == 1)
                        {
                            defaultAttribute = this.id_product_attribute;
                        }
                    });
                    // Show select list
                    $('#product_quotation_id_product_attribute').show();
                } else {
                    // Reset combinations list
                    $('select#product_quotation_id_product_attribute').html('');
                    $('select#product_quotation_id_product_attribute').html('');
                    // Hide select list
                    $('#product_quotation_id_product_attribute').hide();
                }
            }
        });
    },

    closeDialog : function() {
        $('.quotationspro_request_dialog_overlay').removeClass('roja45quotationsprofree-darken-background');
        $('.quotationspro_request_dialog_overlay').hide();
        $('#quotationspro_request_dialog').fadeOut('fast');
        $('#quotationspro_request_dialog').off('click');
        $('#quotationspro_request_dialog').off('keypress');
    },

    sendQuotationToCustomer : function() {
        tinyMCE.triggerSave();
        var serializedForm = $('#sendMessageForm').serialize();
        var url = $('#sendMessageForm').attr('action') + '&ajax=1&token='+token+'&action=submitSendMessageForm&id_roja45_quotation='+id_roja45_quotation;
        var query = '&ajax=1&token='+token+'&action=submitSendMessageForm&id_roja45_quotation='+id_roja45_quotation;
        $.ajax({
            type: 'POST',
            url: quotationspro_link+query,
            cache: false,
            dataType: 'json',
            data : serializedForm,
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                    $('#addQuotationToCart').fadeIn();
                    $('.quotationOptionsDropdown').fadeIn();
                    $('#quotation-pipeline .quote-sent.label-danger').fadeOut('fast', function () {
                        $('#quotation-pipeline .quote-sent.label-success').fadeIn();
                    });
                    var editor = tinyMCE.get('final-quotation-response');
                    editor.setContent('');
                    $('#quotation-response-panel').slideUp();
                    $('#sendQuotationToCustomer').attr('disabled', true);
                    $('#cancelSendQuotationToCustomer').attr('disabled', true);
                    $('#current_status').fadeOut(400, function() {
                        $('#current_status').text(data.status.status);
                        $('#current_status').css('background-color', data.status.color);
                        $('#current_status').fadeIn();
                    });
                    $('#quotationspro_buttons').fadeOut(400, function() {
                        $('#quotationspro_buttons').empty();
                        $('#quotationspro_buttons').append(data.buttons);
                        $('#quotationspro_buttons').fadeIn();
                    });
                } else {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected+'\n'+data.responseText);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    cancelSendQuotationForm : function(target) {
        var editor = tinyMCE.get('final-quotation-response');
        editor.setContent('');
        $('#quotation-response-panel').slideUp();
        $('#sendQuotationToCustomer').attr('disabled', true);
        $('#cancelSendQuotationToCustomer').attr('disabled', true);
    },

    submitSetCurrency : function(target) {
        var id_roja45_quotation = parseInt($('input[name=id_roja45_quotation]').val());
        if (id_roja45_quotation) {
            $.ajax({
                type: 'POST',
                url: quotationspro_link,
                cache: false,
                dataType: 'json',
                data : {
                    'ajax': 1,
                    'token': token,
                    'action': 'setCurrency',
                    'id_roja45_quotation': id_roja45_quotation,
                    'id_currency': target.val()
                },
                beforeSend : function() {
                    roja45global.toggleModal();
                },
                success : function(data)
                {
                    if (data.result) {
                        roja45global.displaySuccessMsg(roja45quotationspro_success);
                        $('#quotation_panel').empty();
                        $('#quotation_panel').append(data.view);
                        roja45quotationsprofree.initialiseEvents();
                    } else {
                        roja45global.displayErrorMsg(data.errors);
                    }
                },
                error: function(data) {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected+'\n'+data.responseText);
                },
                complete: function() {
                    roja45global.toggleModal();
                }
            });
        }
    },

    submitSetCountry : function(target) {
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'setCountry',
                'id_roja45_quotation': id_roja45_quotation,
                'id_country': target.val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    if (data.reload) {
                        window.location = data.reload;
                    } else {
                        $('#tax_state').empty();
                        $('#tax_state').append('<option value="0" selected="selected">&nbsp;</option>');
                        if (data.states.length==0) {
                            $('#tax_state').prop('disabled', true);
                        } else {
                            $.each( data.states, function (key, value) {
                                $('#tax_state').append('<option value="'+value.id_state+'">'+value.name+'</option>');
                            });
                            $('#tax_state').prop('disabled', false);
                        }
                        roja45quotationsprofree.initialiseEvents();
                    }
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                } else {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected+'\n'+data.responseText);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitSetState : function(target) {
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'setState',
                'id_roja45_quotation': id_roja45_quotation,
                'id_state': target.val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    if (data.reload) {
                        window.location = data.reload;
                    }
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                    /*
                    $('#quotation_panel').empty();
                    $('#quotation_panel').append(data.view);
                    roja45quotationsprofree.initialiseEvents();*/
                } else {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected+'\n'+data.responseText);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitSetLanguage : function(target) {
        $.ajax({
            type: 'GET',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'setLanguage',
                'id_roja45_quotation': id_roja45_quotation,
                'id_lang': target.val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    if (data.reload) {
                        window.location = data.reload;
                    }
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                } else  {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
                roja45quotationsprofree.initialiseEvents();
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitSetValidForDays : function(target) {
        $.ajax({
            type: 'GET',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'setValidForDays',
                'id_roja45_quotation': id_roja45_quotation,
                'valid_for': target.val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    roja45global.displaySuccessMsg(roja45quotationspro_success);
                } else  {
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                }
                roja45quotationsprofree.initialiseEvents();
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitAddNote : function(target) {
        if ($('#customer_note #noteContent').val() <= 0)
        {
            roja45global.displayErrorMsg(txt_add_note_no_note_content);
            return false;
        }
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'submitQuotationNote',
                'id_roja45_quotation': id_roja45_quotation,
                'note': $('#customer_note #noteContent').val(),
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                //roja45global.toggleModal();
            }
        });
    },

    submitAddDiscount : function(target) {
        if ($('#voucher_form #discount_name').val() <= 0)
        {
            roja45global.displayErrorMsg(txt_add_discount_no_discount_name);
            return false;
        }

        if ($('#voucher_form #discount_value').val() <= 0)
        {
            roja45global.displayErrorMsg(txt_add_discount_no_discount_value);
            return false;
        }

        var discount_name = target.closest('#voucher_form').find('#discount_name').val();
        var discount_type = target.closest('#voucher_form').find('#discount_type').val();
        var discount_value = target.closest('#voucher_form').find('#discount_value').val();
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'submitAddDiscount',
                'id_roja45_quotation': id_roja45_quotation,
                'discount_name': discount_name,
                'discount_type': discount_type,
                'discount_value': discount_value,
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
               // roja45global.toggleModal();
            }
        });
    },

    submitDeleteDiscount : function(target) {
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'submitDeleteVoucher',
                'id_roja45_quotation': id_roja45_quotation,
                'id_roja45_quotation_charge': target.attr('data-id-roja45-quotation-charge')
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    $('#quotation_panel').empty();
                    $('#quotation_panel').append(data.view);
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
        return false;
    },

    editProductLine : function(e) {
        e.preventDefault();
        var target = $(e.target)
        var id_roja45_quotation_product = target.closest('tr.product-line-row').find('input.product_quotation_id').val()
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'loadQuotationProduct',
                'id_roja45_quotation': id_roja45_quotation,
                'id_roja45_quotation_product':id_roja45_quotation_product
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    $('.quotation_action').fadeOut();
                    $('.add_product_quotation_fields').hide();
                    $('.edit_product_quotation_fields').show();
                    $('.cancel_product_quotation_change_link:visible').trigger('click');
                    roja45quotationsprofree.closeAddProduct();
                    current_product = data.product;
                    var element_list = $('.customized-' + target.closest('tr.product-line-row').find('.product_quotation_id').val());
                    if (!element_list.length)
                        element_list = target.closest('.product-line-row');
                    var current_total = element_list.find('td.total_product').html();
                    //element_list.find('td .product_price_show').hide();
                    element_list.find('td .product_price_show').hide();
                    element_list.find('td .product_quantity_show').hide();
                    element_list.find('td .product_price_edit').show();
                    element_list.find('td .product_quantity_edit').show();
                    element_list.find('td.cancelCheck').hide();
                    element_list.find('td.cancelQuantity').hide();
                    $('td.quotation_action').attr('colspan', 3);
                    $('th.edit_product_quotation_fields').show();
                    //$('th.edit_product_quotation_fields').attr('colspan',  2);
                    //element_list.find('td.quotation_action').attr('colspan', 1);
                    element_list.find('.edit_product_quotation_change_link').parent().hide();
                    element_list.find('button.submitProductQuotationChange').show();
                    element_list.find('.cancel_product_quotation_change_link').show();
                    $('.quotation_action').fadeIn();
                } else if(data.result && data.result=='error') {
                    $('.add_product_quotation_fields, .quotation_action').show();
                    $('.edit_product_quotation_fields').hide();
                    roja45global.displayErrorMsg([data.error]);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitAddProductToQuotation : function(target) {
        if ($('input#addProductToQuotationProductSearch').val() == 0)
        {
            roja45global.displayErrorMsg(txt_add_product_no_product);
            return false;
        }

        if ($('input#add_product_quotation_product_quantity').val() == 0)
        {
            roja45global.displayErrorMsg(txt_add_product_no_product_quantity);
            return false;
        }

        if ($('input#add_product_quotation_product_price_excl').val() == 0)
        {
            roja45global.displayErrorMsg(txt_add_product_no_product_price);
            return false;
        }

        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'addProductToQuotation',
                'id_roja45_quotation': id_roja45_quotation,
                'id_product': $('#new_quotation_product input.product_quotation_id').val(),
                'id_product_attribute': $('#new_quotation_product input.product_quotation_id_product_attribute').val(),
                'product_quantity': $('#new_quotation_product input.product_quotation_quantity').val()
            },
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                roja45global.displayErrorMsg("Unable to add the product to the quotation.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function() {
                //roja45global.toggleModal();
            }
        });
    },

    submitDeleteProductFromQuotation : function(target) {
        roja45global.toggleModal();
        var id_product = target.closest('.product-line-row').find('td .product_id').val();
        var id_roja45_quotation_product = target.closest('tr.product-line-row').find('input.product_quotation_id').val()
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'deleteProductOnQuotation',
                'id_roja45_quotation': id_roja45_quotation,
                'id_roja45_quotation_product': id_roja45_quotation_product,
                'id_product': id_product,
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                jAlert("Unable to add the product to the quotation.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitNewCharge : function(target) {
        var action = $('#charge_type').val();
        if (action=='shipping') {
            action = 'submitShippingCharge';
        } else if ('general' || 'handling') {
            action = 'submitNewCharge';
        }
        if ($('#charges_form #charge_name').val().length == 0)
        {
            jAlert(txt_add_charge_no_charge_name);
            return false;
        }
        if ($('#charges_form #charge_value').val().length == 0)
        {
            jAlert(txt_add_charge_no_charge_value);
            return false;
        }

        var charge_name = target.closest('#charges_form').find('#charge_name').val();
        var charge_type = target.closest('#charges_form').find('#charge_type').val();
        var charge_method = target.closest('#charges_form').find('#charge_method').val();
        var charge_value = target.closest('#charges_form').find('#charge_value').val();
        var carriers = target.closest('#charges_form').find('#carriers').val();
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': action,
                'id_roja45_quotation': id_roja45_quotation,
                'charge_name': charge_name,
                'charge_value': charge_value,
                'charge_method': charge_method,
                'carriers': carriers,
            },
            beforeSend: function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                    //updateAmounts(data.quotation);
                    //roja45quotationsprofree.updateChargeForm(data.charge_table);
                    //roja45quotationsprofree.updateTotals(data.quotation_details);
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                jAlert(roja45_quotationspro_error_unexpected+"\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function() {
                //roja45global.toggleModal();
            }
        });
        return false;
    },

    submitDeleteCharge : function(target) {
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'submitDeleteCharge',
                'id_roja45_quotation': id_roja45_quotation,
                'id_roja45_quotation_charge': target.closest('tr.charge_row').attr('data-id-quotation-charge')
            },
            beforeSend: function() {
                roja45global.toggleModal();
            },
            success : function(data)
            {
                if (data.result && data.result=='success')
                {
                    roja45quotationsprofree.updateTotals(data.quotation_details);
                    roja45quotationsprofree.updateChargeForm(data.charge_table);
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                roja45global.toggleModal();
            }
        });
    },

    submitNewCustomerOrder : function(target) {
        roja45global.toggleModal();
        $('#quotationspro_form').submit();
    },

    submitProductLineChange : function(target) {
        var id_roja45_quotation_product = target.closest('tr.product-line-row').find('td .product_quotation_id').val();
        var product_price = target.closest('tr.product-line-row').find('td .product_price').val();
        var product_quotation_quantity = target.closest('tr.product-line-row').find('td .product_quotation_quantity').val();
        if (product_quotation_quantity <= 0)
        {
            roja45global.displayErrorMsg(txt_add_product_no_product_quantity);
            return false;
        }
        if (target.closest('tr.product-line-row').find('td .product_price').val() <= 0)
        {
            roja45global.displayErrorMsg(txt_add_product_no_product_price);
            return false;
        }
        roja45global.toggleModal();
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data : {
                'ajax': 1,
                'token': token,
                'action': 'editProductOnQuotation',
                'id_roja45_quotation': id_roja45_quotation,
                'id_roja45_quotation_product': id_roja45_quotation_product,
                'product_price': product_price,
                'product_quotation_quantity': product_quotation_quantity,
            },
            success : function(data)
            {
                if (data.result && data.result=='success') {
                    roja45global.displaySuccessMsg(data.response);
                    window.location = data.redirect;
                    /*
                    $('#quotation_panel').empty();
                    $('#quotation_panel').append(data.view);
                    roja45quotationsprofree.initialiseEvents();
                    */
                } else if(data.result && data.result=='error') {
                    roja45global.displayErrorMsg(data.errors);
                }
            },
            error: function(data) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            complete: function() {
                //roja45global.toggleModal();
            }
        });
        return false;
    },

    stopAjaxQuery : function () {
        if (typeof(ajaxQueries) == 'undefined')
            ajaxQueries = new Array();
        for(i = 0; i < ajaxQueries.length; i++)
            ajaxQueries[i].abort();
        ajaxQueries = new Array();
    },

    updateQuotationTotals : function() {

        // Get all discount lines
        // Add them up

        $('.quotation_total_discounts');

        $('.quotation_total_charges');


        $('.quotation_total_exc');
        $('.quotation_total_inc');
    },

    /*
    addProductRefreshTotal : function()
    {
        var quantity = parseInt($('#add_product_quotation_quantity').val());
        if (quantity < 1|| isNaN(quantity))
            quantity = 1;
        if (use_taxes)
            var price = parseFloat($('#product_quotation_price_tax_incl').val());
        else
            var price = parseFloat($('#product_quotation_price_tax_excl').val());

        if (price < 0 || isNaN(price))
            price = 0;
        var total = roja45quotationsprofree.makeTotalProductCaculation(quantity, price);
        $('#add_product_quotation_total').html(formatCurrency(total, currency_format, currency_sign, currency_blank));
    },
*/
    editProductRefreshTotal : function(element)
    {
        element = element.closest('tr.product-line-row');
        var element_list = [];

        var quantity = parseInt(element.find('td input.product_quotation_quantity').val());
        if (quantity < 1 || isNaN(quantity))
            quantity = 1;
        if (use_taxes)
            var price = parseFloat(element.find('td input.product_quotation_price_tax_incl').val());
        else
            var price = parseFloat(element.find('td input.product_quotation_price_tax_excl').val())

        if (price < 0 || isNaN(price))
            price = 0;

        // Customized product
        /*
        if (element_list.length)
        {
            var qty = 0;
            $.each(element_list, function(i, elm) {
                if($(elm).find('.edit_product_quotation_quantity').length)
                {
                   // qty += parseInt($(elm).find('.edit_product_quotation_quantity').val());
                    qty += quantity;
                    subtotal = roja45quotationsprofree.makeTotalProductCaculation($(elm).find('.edit_product_quantity').val(), price);
                    $(elm).find('.total_product').html(formatCurrency(subtotal, currency_format, currency_sign, currency_blank));
                }
            });

            var total = roja45quotationsprofree.makeTotalProductCaculation(qty, price);
            element.find('td.total_product').html(formatCurrency(total, currency_format, currency_sign, currency_blank));
            element.find('td.productQuantity').html(qty);
        }
        else
        {
            var total = roja45quotationsprofree.makeTotalProductCaculation(quantity, price);
            element.find('td.total_product').html(formatCurrency(total, currency_format, currency_sign, currency_blank));
        }
        */
        var total = roja45quotationsprofree.makeTotalProductCaculation(quantity, price);
        element.find('td.total_product').html(formatCurrency(total, currency_format, currency_sign, currency_blank));
    },

    makeTotalProductCaculation : function(quantity, price)
    {
        return Math.round(quantity * price * 100) / 100;
    },

    updateProductsTable : function(view)
    {
        html = $(view);
        html.find('#quotationProducts').hide();
        //$('tr#new_invoice').hide();
        $('#new_quotation_product').hide();

        // Initialize fields
        roja45quotationsprofree.closeAddProduct();

        $('#quotationProducts tr.product-line-row').remove();
        $('#quotationProducts tbody #new_quotation_product').before(html);
        /*
        $('tr#new_quotation_product').before(html);
        html.find('td').each(function() {
            if (!$(this).is('.product_invoice'))
                $(this).fadeIn('slow');
        });
        */
    },

    updateAmounts : function(quotation)
    {
        $('#total_products td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_products_wt), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        $('#total_discounts td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_discount), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        if (quotation.total_discount > 0)
            $('#total_discounts').slideDown('slow');
        $('#total_wrapping td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_wrapping), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        if (quotation.total_wrapping_tax_incl > 0)
            $('#total_wrapping').slideDown('slow');
        $('#total_shipping td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_shipping), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        $('#total_order td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_to_pay), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        $('#total_quotation td.amount').fadeOut('slow', function() {
            $(this).html(formatCurrency(parseFloat(quotation.total_to_pay_wt), currency_format, currency_sign, currency_blank));
            $(this).fadeIn('slow');
        });
        $('.alert').slideDown('slow');
        $('#product_number').fadeOut('slow', function() {
            var old_quantity = parseInt($(this).html());
            $(this).html(old_quantity + 1);
            $(this).fadeIn('slow');
        });
    },

    closeAddProduct : function()
    {
        $('#new_quotation_product').slideUp();
        // Initialize fields
        $('#new_quotation_product select, #new_quotation_product input').each(function() {
            if (!$(this).is('.button'))
                $(this).val('')
        });
        $('#product_quotation_product_quantity').val('1');
        $('#product_quotation_product_attribute_id option').remove();
        $('#product_quotation_id_product_attribute').hide();
        current_product = null;
    },

    updateDiscountForm : function(discount_form_html)
    {
        $('#discount_table').html(discount_form_html);

        $('#voucher_form').slideUp();

        if( $('#discount_table .discount_row').length > 0) {
            has_voucher = 1;
        } else {
            has_voucher = 0;
        }

        $('.quotation_action').fadeIn();
    },

    updateChargeForm : function(charge_form_html) {
        $('#charges_table').html(charge_form_html);

        $('#charges_form').slideUp();

        if ($('#charges_table .charge_row').length > 0) {
            has_charges = 1;
        } else {
            has_charges = 0;
        }

        $('.quotation_action').fadeIn();
    },

    updateProductLines : function(product_lines)
    {
        $('#quotationProducts').fadeOut('fast', function() {
            $('#quotationProducts tbody').html(product_lines);
            $('#quotationProducts').fadeIn();
            roja45quotationsprofree.initProductLineEvents();
        });
    },

    updateTotals : function(product_lines)
    {
        $('#totals_panel').fadeOut('fast', function() {
            $('#totals_panel').html(product_lines);
            $('#totals_panel').fadeIn();
        });
    },

    updateNotesForm : function(charge_form_html)
    {
        $('#notes_table').html(charge_form_html);

        if( $('#notes_table .note_row').length > 0) {
            has_notes = 1;
        } else {
            has_notes = 0;
        }
    },

    createCustomerAccount : function(ele) {
        roja45global.toggleModal();
        var query = 'ajax=1&token=' + token + '&action=createCustomerAccount&id_roja45_quotation=' + id_roja45_quotation + '&firstname=' + ele.attr('data-customer-firstname') + '&lastname=' + ele.attr('data-customer-lastname') + '&email=' + ele.attr('data-customer-email');
        var ajax_query = $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: query,
            success: function (data) {
                if (data.result && data.result == 'success') {
                    //$('#createCustomerAccount').attr('disabled', 'disabled');
                    //$('#createQuotationResponse').removeAttr('disabled');
                    $('#createCustomerAccount').fadeOut('fast', function(e) {
                        $('#sendCustomerQuotation').fadeIn();
                        $('.customerOptionsDropdown').fadeIn();
                    });
                    $('#quotation-pipeline .user-account.label-danger').fadeOut('fast', function () {
                        $('#quotation-pipeline .user-account.label-success').fadeIn();
                    });
                    $('.add_customer_address_link').removeClass('hidden');
                    $('#customer_no_main_address').val(txt_no_addresses_available);
                    roja45global.displaySuccessMsg(data.message);
                } else if (data.result && data.result == 'error') {
                    roja45global.displayErrorMsg(data.error);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                jAlert(roja45_quotationspro_error_createaccount+"\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    },

    addQuotationToOrder : function(target) {
        $('.quotationspro_request_dialog_overlay').addClass('roja45quotationsprofree-darken-background');
        $('#quotationspro_request_dialog .page-heading').html($('input[name=DIALOG_HEADING]').val());

        $('.quotationspro_request_dialog_overlay').css('width','100%');
        $('.quotationspro_request_dialog_overlay').css('height','100%');
        $('.quotationspro_request_dialog_overlay').show();

        $('#quotationspro_request_dialog').fadeIn('fast');
    },

    sendCustomerQuotation : function() {
        var editor = tinyMCE.get('final-quotation-response');
        editor.setContent('');
        tinyMCE.triggerSave();
        //var query = 'ajax=1&token=' + token + '&action=loadCustomerQuotation&viewroja45_quotation&id_roja45_quotation=' + id_roja45_quotation + '&template=' + $( "#select_quotation_answer" ).val()
        $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: {
                'action' : 'loadCustomerQuotation',
                'ajax' : 1,
                'token' : token,
                'id_roja45_quotation' : id_roja45_quotation,
                'template' : $( "#select_quotation_answer" ).val()
            },
            beforeSend: function() {
                roja45global.toggleModal();
            },
            success: function (data) {
                if (data.result && data.result == 'success') {
                    var editor = tinyMCE.get('final-quotation-response');
                    editor.setContent(data.content);
                    $('#quotation-response-panel').slideDown();
                    $('#sendQuotationToCustomer').removeAttr('disabled');
                    $('#cancelSendQuotationToCustomer').removeAttr('disabled');
                    $(document).scrollTo('#sendQuotationToCustomer', 500);
                } else {
                    if (data.errors) {
                        $.each(data.errors, function(index, value) {
                            roja45global.displayErrorMsg(value);
                        })
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                jAlert("Unable to load answer.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    },

    loadMessageTemplate : function() {
        roja45global.toggleModal();
        var query = 'ajax=1&token=' + token + '&action=loadMessageTemplate&viewroja45_quotation&id_roja45_quotation=' + id_roja45_quotation + '&template=' + $( "#select_quotation_answer" ).val()
        var ajax_query = $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: query,
            success: function (json) {
                if (json.result) {

                    if (json.result == 'success') {
                        var editor = tinyMCE.get('final-quotation-response');
                        editor.setContent(json.content);
                        $('#quotation-response-panel').slideDown();
                        $('#sendQuotationToCustomer').removeAttr('disabled');
                        $('#cancelSendQuotationToCustomer').removeAttr('disabled');
                    } else {

                    }

                } else
                    jAlert(json.error);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                jAlert("Unable to load answer.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    },

    claimRequest : function() {
        roja45global.toggleModal();
        var query = 'ajax=1&token=' + token + '&action=submitClaimQuotation&id_roja45_quotation=' + id_roja45_quotation;
        var ajax_query = $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: query,
            success: function (data) {
               if (data.result) {
                   if (data.result == 'success') {
                       $('#quotation-owner').html(data.employee_name);
                       $('#customer_employee').val(data.employee_name);
                       $('#quotation-owner').css("color", "green");
                       $('#claimRequest').fadeOut(400, function() {
                           $('#releaseRequest').fadeIn();
                       });
                       $('#quotationspro_buttons').fadeOut(400, function() {
                           $('#quotationspro_buttons').empty();
                           $('#quotationspro_buttons').append(data.buttons);
                           $('#quotationspro_buttons').fadeIn();
                       });
                   } else {
                       roja45global.displayErrorMsg(data.error);
                   }
                } else
                    jAlert(data.error);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                roja45global.displayErrorMsg(roja45_quotationspro_error_unabletoclaim+"\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    },

    releaseRequest : function() {
        roja45global.toggleModal();
        var query = 'ajax=1&token=' + token + '&action=submitReleaseQuotation&id_roja45_quotation=' + id_roja45_quotation;
        var ajax_query = $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: query,
            success: function (data) {
               if (data.result) {
                   if (data.result == 'success') {
                       $('#quotation-owner').html(data.unassigned);
                       $('#customer_employee').val(data.unassigned);
                       $('#quotation-owner').css("color", "red");
                       $('#releaseRequest').fadeOut(400, function() {
                           $('#claimRequest').fadeIn();
                       });
                       $('#quotationspro_buttons').fadeOut(400, function() {
                           $('#quotationspro_buttons').empty();
                           $('#quotationspro_buttons').append(data.buttons);
                           $('#quotationspro_buttons').fadeIn();
                       });
                   } else {
                       roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                   }
                } else
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                jAlert("Unable to claim the quotation.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    },

    resetCart : function() {
        var query = 'ajax=1&token=' + token + '&action=submitResetCart&id_roja45_quotation=' + id_roja45_quotation;
        var ajax_query = $.ajax({
            type: 'POST',
            url: quotationspro_link,
            cache: false,
            dataType: 'json',
            data: query,
            beforeSend : function() {
                roja45global.toggleModal();
            },
            success: function (data) {
                if (data.result) {
                    if (data.result == 'success') {
                        $('.cart-badge').fadeOut();
                    } else {
                        roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
                    }
                } else
                    roja45global.displayErrorMsg(roja45_quotationspro_error_unexpected);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                jAlert("Unable to claim the quotation.\n\ntextStatus: '" + textStatus + "'\nerrorThrown: '" + errorThrown + "'\nresponseText:\n" + XMLHttpRequest.responseText);
            },
            complete: function () {
                roja45global.toggleModal();
            }
        });
    }
}