var temporary_data = [];
var temp_data = [];
var tempry_data = [];
var temporaryy_data  = [];
var temp_storage = [];
var temprary_data = [];
var check_total = 0;
var check_subtotal = 0;
var error_trap1 = 0;
var with_driver = '';
var temporary_entry = [];
var temporary_entryy = [];

jQuery(function($) {
    $('#restau_continue_payment').on('submit', function(event) { 
        event.preventDefault();
        var id = this.id;
        $('body').loading();
        $('.modall').css('z-index', '0');
    
        if ( id == 'restau_continue_payment_authorize') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+ "&type=restau&action=easync_validation_authorize", 
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if (!data.success) {
                    remove_error();
                    if (data.errors.menu_ids) {
                        $('#restau_continue_payment_authorize #tab').append('<div class="error error-pick-item active">' + data.errors.menu_ids + '</div>'); 
                    }
                }else{
                    /*menu ids*/
                    temporary_data[0]  = data['name'];
                    temporary_data[1]  = data['email_add'];
                    temporary_data[2]  = data['phone_no'];
                    temporary_data[3]  = data['branch'];
                    temporary_data[4]  = data['guest_no'];
                    temporary_data[5]  = data['table_no'];
                    temporary_data[6]  = data['timeslot'];
                    temporary_data[7]  = data['picked_date'];
                    temporary_data[8]  = data['reserved'];
                    temporary_entry    = [];
                    temporary_entry[0] = [];
                    temporary_entry[1] = [];
                    temporary_entry[2] = [];
                    temporary_entry[3] = [];
                    temporary_entry[4] = [];
                    temporary_entry[5] = [];
                    temporary_data[14] = data['reference_num'];
                    temporary_data[15] = data['amount_to_pay'];
                    temporary_data[16] = data['coupon_id'];
                    temporary_data[17] = data['paypal_dis_price'];
                    temporary_data[18] = data['paypal_dis'];
                    temporary_data[19] = data['item_qty'];
                    temporary_data[20] = data['table_ids'];
    
                    $.each( data.items, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="item_name_'+(key+1)+'" value="'+value+'">');
                      temporary_entry[0][key] = value;
                    });
                    $.each( data.item_qtys, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="item_number_'+(key+1)+'" value="'+value+'">');
                      temporary_entry[1][key]  = value;  
                    });
                    $.each( data.item_prices, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="amount_'+(key+1)+'" value="'+value+'">');
                      temporary_entry[2][key]  = (value / temporary_entry[1][key]);  
                    });
    
                    temporary_entry[3] = data['amount_to_pay'];
                    temporary_entry[4] = data['item_qty'];
                    item_qty = temporary_entry[4];
                    temporary_entry[5].push(data['amount_to_pay']);
                    var tot_amount = Number(data['amount_to_pay'].replace(/[^0-9\.]+/g, ""));
                    tot_amount = tot_amount.toFixed(2);
                    $('.pay_amount .amount-placeholder .amt_toPay_restau').text(tot_amount);
    
                    $('.restau-authorize-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'   : '#payment_authorize_restau',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });
                    $('.restau-authorize-payment').click();
                    $('.restau-authorize-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.restau-authorize-payment').removeAttr('data-dismiss');
                    $('.restau-authorize-payment').removeAttr('data-toggle');
                    $('.restau-authorize-payment').removeAttr('data-targett');
                    $('.restau-authorize-payment').removeAttr('data-backdrop');
                    $('.restau-authorize-payment').removeAttr('data-keyboard');
    
                }
            });
        }
        if ( id == 'restau_continue_payment_stripe') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+ "&type=restau&action=easync_validation_stripe", 
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if (!data.success) {
                    remove_error();
                    if (data.errors.menu_ids) {
                        $('#restau_continue_payment #tab').append('<div class="error error-pick-item active">' + data.errors.menu_ids + '</div>'); 
                    }
                }else{
                    /*menu ids*/
                    temporary_data[0]   = data['name'];
                    temporary_data[1]   = data['email_add'];
                    temporary_data[2]   = data['phone_no'];
                    temporary_data[3]   = data['branch'];
                    temporary_data[4]   = data['guest_no'];
                    temporary_data[5]   = data['table_no'];
                    temporary_data[6]   = data['timeslot'];
                    temporary_data[7]   = data['picked_date'];
                    temporary_data[8]   = data['reserved'];
                    temporary_entryy    = [];
                    temporary_entryy[0] = [];
                    temporary_entryy[1] = [];
                    temporary_entryy[2] = [];
                    temporary_entryy[3] = [];
                    temporary_entryy[4] = [];
                    temporary_entryy[5] = [];
                    temporary_data[14]  = data['reference_num'];
                    temporary_data[15]  = data['amount_to_pay'];
                    temporary_data[16]  = data['coupon_id'];
                    temporary_data[17]  = data['paypal_dis_price'];
                    temporary_data[18]  = data['paypal_dis'];
                    temporary_data[19]  = data['item_qty'];
                    temporary_data[20]  = data['table_ids'];
    
                    $.each( data.items, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="item_name_'+(key+1)+'" value="'+value+'">');
                      temporary_entryy[0][key] = value;
                    });
                    $.each( data.item_qtys, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="item_number_'+(key+1)+'" value="'+value+'">');
                      temporary_entryy[1][key]  = value;  
                    });
                    $.each( data.item_prices, function( key, value ) {
                      $('.sync_payment_display').prepend('<input type="hidden" name="amount_'+(key+1)+'" value="'+value+'">');
                      temporary_entryy[2][key]  = (value / temporary_entryy[1][key]);  
                    });
                    temporary_entryy[3] = data['amount_to_pay'];
    
                    temporary_entryy[4] = data['item_qty'];
                    item_qty = temporary_entryy[4];
    
                    temporary_entryy[5].push(data['amount_to_pay']);
    
                    $('.restau-stripe-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'   : '#confirm_stripe',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });
                    
                    $('.restau-stripe-payment').click();
                    $('.restau-stripe-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.restau-stripe-payment').removeAttr('data-dismiss');
                    $('.restau-stripe-payment').removeAttr('data-toggle');
                    $('.restau-stripe-payment').removeAttr('data-targett');
                    $('.restau-stripe-payment').removeAttr('data-backdrop');
                    $('.restau-stripe-payment').removeAttr('data-keyboard');
    
                    var formData = {
                        'type'                 : 'restau',
                        'name'                 : temporary_data[0],
                        'email'                : temporary_data[1],
                        'phone_no'             : temporary_data[2],
                        'branch'               : temporary_data[3],
                        'guest_no'             : temporary_data[4],
                        'table_no'             : temporary_data[5],
                        'timeslot'             : temporary_data[6],
                        'picked_date'          : temporary_data[7],
                        'menu_ids'             : temporary_data[8],
                        'reference_number'     : temporary_data[14],
                        'amount_to_pay_restau' : temporary_data[15],
                        'coupon_id'            : temporary_data[16],
                        'item_prices'          : temporary_data[17],
                        'menu_reserved'        : temporary_data[18],
                        'item_qty_each'        : temporary_data[19],
                        'table_ids'            : temporary_data[20],
                        'action'               :'easync_session_store_stripe',
                    };
    
                    $.ajax({
                        type        : 'POST',
                        url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                        data        : formData, 
                        dataType    : 'json', 
                        encode      : true
                    }).done(function(data) {
                        if (!data.success) {
                                console.log('Something wrong!');
                        } else {
                            temporary_entry[4] = 'success';
                        }
                    });
                }
            });
        }   
        if (id == 'submit_reservation') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+ "&type=restau&action=easync_validation_offline", 
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
    
                var formdata = {
                    'type'             : 'restau',
                    'picked_date'      : data.picked_date,
                    'timeslot'         : data.timeslot,
                    'branch'           : data.branch,
                    'guest_no'         : data.guest_no,
                    'table_no'         : data.table_no,
                    'name'             : data.name,
                    'phone'            : data.phone_no,
                    'email'            : data.email_add,
                    'paypal_dis_price' : data.paypal_dis_price,
                    'paypal_dis'       : data.paypal_dis,
                    'item_qty'         : data.item_qty,
                    'facility_request' : data.facility_request,
                    'other_req'        : data.other_req,
                    'reserved'         : data.reserved,
                    'menu_ids'         : data.menu_ids,
                    'table_ids'        : data.table_ids,
                    'coupon_id'        : data.coupon_id,
                    'amount_to_pay'    : data.amount_to_pay,
                    'reference_number' : data.reference_num,
                    'action'           :'easync_submit_reservation',
                }
    
                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                    data        : formdata, 
                    dataType    : 'json', 
                    encode      : true
                }).done(function(data) {
    
                    let qr_data1 = "Name: " + data['curr']['name'] + '\n'; 
                    let qr_data2 = "Phone Number: " + data['curr']['phone'] + '\n';
                    let qr_data3 = "Email: " + data['curr']['email'] + '\n';
                    let qr_data4 = "Reservation Date: " + data['curr']['picked_date'] + '\n';
                    let qr_data5 = "Timeslot: " + data['curr']['timeslot'] + '\n';
                    let qr_data6 = "Number of Guest: " + data['curr']['guest_no'] + '\n';
                    let qr_data7 = "Number of Tables: " + data['curr']['table_no'] + '\n';
                    let qr_data9 = "Branch: " + data['curr']['branch'] + '\n';
                    let qr_data10 = "Reference Number: " + data['curr']['reference_number'] + '\n';
                    let qr_data11 = "Amount to Pay: " + data['currency'] + " " + data['curr']['amount_to_pay'];
                    let details = qr_data1.concat(qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data9, qr_data10, qr_data11);
    
                    let qr;
                    (function () {
                        qr = new QRious({
                            element: document.querySelector("img#qrCode"),
                            size: 250
                            // value: "eaSYNC",
                        });
                    })();
    
                    const qrText = details;
                    qr.set({
                        value: qrText,
                    });
    
                    var regex = /(<([^>]+)>)/ig;
                    var content = details;
                    var result = content.replace(regex, "");
    
                    var image = jQuery('.qrCode_container #qrCode').attr('src');
                    jQuery('#qrCode_image').attr('href', image);
    
                    if(data.error){
                        $('#restau_customer_payment').modal('hide');
                        $('#errorModal').modal('show');
                    }else{
                        $('#qrCode_modal').modal('show');
                        $('#restau_customer_payment').modal('hide');
                        $('#restau_menu_info').modal('hide');						        		
                    }
                });
            });
        }
    });

    $('#car_continue_payment').on('submit', function(event) {
        event.preventDefault();
        $('body').loading();
        $('.modall').css('z-index', '0');
        var id = this.id;
        var is_require = jQuery('.payment .is_required').val();
        var data_value = "";
        var data_value_stripe = "";
        var data_value_authorize = "";
        

        if (is_require == 'on') {
            var allowedExtensions = /(\.jpg|\.jpeg|\.png)$/i;
            var file1 = document.getElementById("file1").value;
            var file2 = document.getElementById("file2").value;
            var image_empty1 = '';
            var image_empty2 = '';
            if(document.getElementById("file1").files.length == 0 ) {
                image_empty1 = 'no-file';
            }
            else if(!allowedExtensions.exec(file1)){
                image_empty1 = 'invalid-file';
            }
            if(document.getElementById("file2").files.length == 0) {
                image_empty2 = 'no-file';
            }
            else if(!allowedExtensions.exec(file2) ){
                image_empty2 = 'invalid-file';
            }

            data_value_stripe = "&type=car&with_driver="+with_driver+"&file_empty1="+image_empty1+"&file_empty2="+image_empty2+"&file1="+file1+"&file2="+file2+"&is_required="+is_require+"&action=easync_validation_stripe";
            data_value_authorize = "&type=car&with_driver="+with_driver+"&file_empty1="+image_empty1+"&file_empty2="+image_empty2+"&file1="+file1+"&file2="+file2+"&is_required="+is_require+"&action=easync_validation_authorize";
            data_value_offline = "&type=car&with_driver="+with_driver+"&file_empty1="+image_empty1+"&file_empty2="+image_empty2+"&file1="+file1+"&file2="+file2+"&is_required="+is_require+"&action=easync_validation_offline";
        } else {
            data_value_stripe = "&type=car&with_driver="+with_driver+"&is_required="+is_require+"&action=easync_validation_stripe";
            data_value_authorize = "&type=car&with_driver="+with_driver+"&is_required="+is_require+"&action=easync_validation_authorize";
            data_value_offline = "&type=car&with_driver="+with_driver+"&is_required="+is_require+"&action=easync_validation_offline";
        }

        var formData = new FormData(this);         
        formData.append('type', 'car'); 
        formData.append('with_driver', with_driver); 

        if (id == 'car_continue_payment_stripe') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+data_value_stripe,
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if (!data.success) {
                    remove_error();
                    // alert(data.errors.file);
                    if (data.errors.firstname) {
                        $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                    }else{
                        $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                    }
                    if (data.errors.lastname) {
                        $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                    }else{
                        $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>');
                    }
                    if (data.errors.phone) {
                        $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                    }else{
                        $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>');
                    }
                    if (data.errors.email) {
                        $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                    }else{
                        $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>');
                    }

                    if (is_require == "on") {
                        if (data.errors.driver_name) {
                            $('.personal-info.driver-name').append('<div class="error error-driver-name active">' + data.errors.driver_name + '</div>'); 
                        }else{
                            $('.personal-info.driver-name').append('<div class="error error-driver-name ok active"> OK </div>');
                        }
                        if (data.errors.driver_phone) {
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number active">' + data.errors.driver_phone + '</div>'); 
                        }else{
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number ok active"> OK </div>');
                        }
                        if (data.errors.file1 ) {
                            $('#filediv1').append('<div class="error error-driver-image active">' + data.errors.file1 + '</div>'); 
                        }else{
                            $('#filediv1').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
                        if (data.errors.file2 ) {
                        $('#filediv2').append('<div class="error error-driver-image active">' + data.errors.file2 + '</div>'); 
                        }else{
                            $('#filediv2').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
    
                        if (data.errors.driver_name ||  data.errors.driver_phone || data.errors.file1 || data.errors.file2 ) {
                            $('.modall').animate({scrollTop: $('.modal-bodyy .sync_with_driver_container #filediv1').offset().top}, 200);
                        }
                    }

                    if (data.errors.firstname ||  data.errors.lastname || data.errors.phone || data.errors.email ) {
                        $('.modall').animate({scrollTop: $('.modal-header').offset().top }, 200);
                    }
                }else{
                    //pick date//
                    temporary_data[1]  = data.date_pick;
                    temporary_data[2]  = data.pick_time;
                    temporary_data[3]  = data.date_return;
                    temporary_data[4]  = data.return_time;
                    temporary_data[5]  = data.pick_location;
                    temporary_data[6]  = data.number_days;
                    temporary_data[7]  = data.with_or_out_driver;
                    temporary_data[8]  = data.firstname;
                    temporary_data[9]  = data.lastname;
                    temporary_data[10] = data.phone;
                    temporary_data[11] = data.email;
                    temporary_data[12] = data.driver_name;
                    temporary_data[13] = data.driver_phone;
                    temporary_data[19] = data.coupon_id_car;
                    temporary_data[20] = data.amount_to_pay_car;
                    temporary_data[21] = data.facility_request;
                    temporary_data[22] = data.other_req;
                    temporary_data[23] = data.reference_number;
                    temporary_data[24] = data.return_days;
                    temporary_data[25] = data.check_require;

                    jQuery('.sync_payment_display input:first-child').val(car_name);
                    jQuery('.sync_payment_display input:nth-child(2)').val(Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, "")));
    
                    temporary_entry    = [];
                    temporary_entry[0] = car_name;
                    temporary_entry[1] = Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, "")) / data.number_days;
                    temporary_entry[2] = data.number_days;
                    temporary_entry[3] = Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, ""));

                    remove_error();

                    $('.car-stripe-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'   : '#confirm_stripe',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });

                    $('.car-stripe-payment').click();
                    $('.car-stripe-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.car-stripe-payment').removeAttr('data-dismiss');
                    $('.car-stripe-payment').removeAttr('data-toggle');
                    $('.car-stripe-payment').removeAttr('data-targett');
                    $('.car-stripe-payment').removeAttr('data-backdrop');
                    $('.car-stripe-payment').removeAttr('data-keyboard');

                }

                var formData = new FormData();
                formData.append('type', 'car');
                formData.append('with_driver', with_driver);  
                formData.append('car_id', temporary_data[0]);
                formData.append('date_pick', temporary_data[1]);
                formData.append('pick_time', temporary_data[2]);
                formData.append('date_return', temporary_data[3]);
                formData.append('return_time', temporary_data[4]);
                formData.append('pick_location', temporary_data[5]);
                formData.append('number_days', temporary_data[6]);
                formData.append('with_or_out_driver', temporary_data[7]);
                formData.append('firstname', temporary_data[8]);
                formData.append('lastname', temporary_data[9]);
                formData.append('phone', temporary_data[10]);
                formData.append('email', temporary_data[11]);

                if (temporary_data[25] == 'on') {
                    formData.append('driver_name', temporary_data[12]);
                    formData.append('driver_phone', temporary_data[13]);//
                    formData.append('driver_license_image1', $('.sync_components #filediv1 input[type=file]').val().split('\\').pop());
                    formData.append('driver_license_image2', $('.sync_components #filediv2 input[type=file]').val().split('\\').pop());
                }
                
                formData.append('coupon_id_car', temporary_data[19]);
                formData.append('amount_to_pay_car', temporary_data[20]);
                formData.append('facility_request', temporary_data[21]);
                formData.append('other_req', temporary_data[22]);
                formData.append('reference_number', temporary_data[23]);
                formData.append('action', 'easync_session_store_stripe');

                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                    data        : formData, 
                    dataType    : 'json', 
                    encode      : true,
                    cache       : false,
                    processData : false,
                    contentType : false
                }).done(function(data) {
                    $('body').loading('stop');
                    $('.modall').css('z-index', '');
                    if (!data.success) {
                        console.log('Something wrong!');
                    }else{
                        temporary_entry[4] = 'success';
                    }
                });
            });
        }

        if ( id == 'car_continue_payment_authorize') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+data_value_authorize,
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if (!data.success) {
                    remove_error();
                    // alert(data.errors.file);
                    if (data.errors.firstname) {
                        $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                    }else{
                        $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                    }
                    if (data.errors.lastname) {
                        $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                    }else{
                        $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>');
                    }
                    if (data.errors.phone) {
                        $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                    }else{
                        $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>');
                    }
                    if (data.errors.email) {
                        $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                    }else{
                        $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>');
                    }

                    if (is_require == "on") {
                        if (data.errors.driver_name) {
                            $('.personal-info.driver-name').append('<div class="error error-driver-name active">' + data.errors.driver_name + '</div>'); 
                        }else{
                            $('.personal-info.driver-name').append('<div class="error error-driver-name ok active"> OK </div>');
                        }
                        if (data.errors.driver_phone) {
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number active">' + data.errors.driver_phone + '</div>'); 
                        }else{
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number ok active"> OK </div>');
                        }
                        if (data.errors.file1 ) {
                            $('#filediv1').append('<div class="error error-driver-image active">' + data.errors.file1 + '</div>'); 
                        }else{
                            $('#filediv1').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
                        if (data.errors.file2 ) {
                        $('#filediv2').append('<div class="error error-driver-image active">' + data.errors.file2 + '</div>'); 
                        }else{
                            $('#filediv2').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
    
                        if (data.errors.driver_name ||  data.errors.driver_phone || data.errors.file1 || data.errors.file2 ) {
                            $('.modall').animate({scrollTop: $('.modal-bodyy .sync_with_driver_container #filediv1').offset().top}, 200);
                        }
                    }

                    if (data.errors.firstname ||  data.errors.lastname || data.errors.phone || data.errors.email ) {
                        $('.modall').animate({scrollTop: $('.modal-header').offset().top }, 200);
                    }
                }
                else{
                    temporary_data[1]  = data.date_pick;
                    temporary_data[2]  = data.pick_time;
                    temporary_data[3]  = data.date_return;
                    temporary_data[4]  = data.return_time;
                    temporary_data[5]  = data.pick_location;
                    temporary_data[6]  = data.number_days;
                    temporary_data[7]  = data.with_or_out_driver;
                    temporary_data[8]  = data.firstname;
                    temporary_data[9]  = data.lastname;
                    temporary_data[10] = data.phone;
                    temporary_data[11] = data.email;
                    temporary_data[12] = data.driver_name;
                    temporary_data[13] = data.driver_phone;
                    temporary_data[19] = data.coupon_id_car;
                    temporary_data[20] = data.amount_to_pay_car;
                    temporary_data[21] = data.facility_request;
                    temporary_data[22] = data.other_req;
                    temporary_data[23] = data.reference_number;
                    temporary_data[24] = data.return_days;
                    temporary_data[25] = data.check_require;

                    var amt_car = Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, ""));
                    amt_car = amt_car.toFixed(2);
                    jQuery('.sync_payment_display input:first-child').val(car_name);
                    jQuery('.sync_payment_display input:nth-child(2)').val(Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, "")));
                    jQuery('.pay_amount .amount-placeholder .amt_toPay_car').text(amt_car);
    
                    temporary_entry    = [];
                    temporary_entry[0] = car_name;
                    jQuery('.authorize_info_car #car_name').val(car_name);
                    temporary_entry[1] = Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, "")) / data.number_days;
                    temporary_entry[2] = data.number_days;
                    temporary_entry[3] = Number(data.amount_to_pay_car.replace(/[^0-9\.]+/g, ""));

                    remove_error();

                    $('.car-authorize-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'    : '#payment_authorize_car',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });

                    $('.car-authorize-payment').click();
                    $('.car-authorize-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.car-authorize-payment').removeAttr('data-dismiss');
                    $('.car-authorize-payment').removeAttr('data-toggle');
                    $('.car-authorize-payment').removeAttr('data-targett');
                    $('.car-authorize-payment').removeAttr('data-backdrop');
                    $('.car-authorize-payment').removeAttr('data-keyboard');
                }
            });
        }

        if (id == 'car_submit_reservation') { 
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+data_value_offline, 
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if(!data.success) {
                    remove_error();
                    if (data.errors.firstname) {
                        $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                    }else{
                        $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                    }
                    if (data.errors.lastname) {
                        $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                    }else{
                        $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>');
                    }
                    if (data.errors.phone) {
                        $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                    }else{
                        $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>');
                    }
                    if (data.errors.email) {
                        $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                    }else{
                        $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>');
                    }

                    if (is_require == "on") {
                        if (data.errors.driver_name) {
                            $('.personal-info.driver-name').append('<div class="error error-driver-name active">' + data.errors.driver_name + '</div>'); 
                        }else{
                            $('.personal-info.driver-name').append('<div class="error error-driver-name ok active"> OK </div>');
                        }
                        if (data.errors.driver_phone) {
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number active">' + data.errors.driver_phone + '</div>'); 
                        }else{
                            $('.personal-info.driver-phone').append('<div class="error error-driver-number ok active"> OK </div>');
                        }
                        if (data.errors.file1 ) {
                            $('#filediv1').append('<div class="error error-driver-image active">' + data.errors.file1 + '</div>'); 
                        }else{
                            $('#filediv1').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
                        if (data.errors.file2 ) {
                            $('#filediv2').append('<div class="error error-driver-image active">' + data.errors.file2 + '</div>'); 
                        }else{
                            $('#filediv2').append('<div class="error error-driver-image ok active"> OK </div>');
                        }
    
                        if (data.errors.driver_name ||  data.errors.driver_phone || data.errors.file1 || data.errors.file2 ) {
                            $('.modall').animate({scrollTop: $('.modal-bodyy .sync_with_driver_container #filediv1').offset().top}, 200);
                        }
                    }

                    if (data.errors.firstname ||  data.errors.lastname || data.errors.phone || data.errors.email ) {
                        $('.modall').animate({scrollTop: $('.modal-header').offset().top }, 200);
                    }

                } else {

                    var formdata = {
                        'type'                : 'car',
                        'car_id'              : temporary_data[0],
                        'date_pick'           : data.date_pick,
                        'date_return'         : data.date_return,
                        'number_days'         : data.number_days,
                        'guest_number'        : data.number_guest,
                        'room_number'         : data.number_room,
                        'firstname'           : data.firstname,
                        'lastname'            : data.lastname,
                        'phone'               : data.phone,
                        'email'               : data.email,
                        'facility_request'    : data.facility_request,
                        'other_req'           : data.other_req,
                        'pick_location'       : data.pick_location,
                        'pick_time'           : data.pick_time,
                        'return_time'         : data.return_time,
                        'coupon_id_car'       : data.coupon_id_car,
                        'amount_to_pay_car'   : data.amount_to_pay_car,
                        'reference_number'    : data.reference_number,
                        'with_without_driver' : data.with_or_out_driver,
                        'driver_name'         : data.driver_name,
                        'driver_phone'        : data.driver_phone,
                        'action'              :'easync_submit_reservation',
                    }

                    $.ajax({
                        type        : 'POST',
                        url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                        data        : formdata, 
                        dataType    : 'json', 
                        encode      : true
                    }).done(function(data) {
                        let qr_data = "Firstname: " + data['curr']['firstname'] + '\n';
                        let qr_data1 = "Lastname: " + data['curr']['lastname'] + '\n';
                        let qr_data2 = "Phone Number: " + data['curr']['phone'] + '\n';
                        let qr_data3 = "Email: " + data['curr']['email'] + '\n';
                        let qr_data4 = "Pickup Date: " + data['curr']['date_pick'] + '\n';
                        let qr_data5 = "Return Date: " + data['curr']['date_return'] + '\n';
                        let qr_data6 = "Facility Request: " + data['curr']['facility_request'] + '\n';
                        let qr_data7 = "Other Reqs: " + data['curr']['other_req'] + '\n';
                        let qr_data8 = "Reference Number: " + data['curr']['reference_number'] + '\n';
                        let qr_data9 = "Amount to Pay: " + data['curr']['amount_to_pay_car'] + '\n';
                        let details = qr_data.concat(qr_data1, qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data8, qr_data9);

                        let qr;
                        (function () {
                            qr = new QRious({
                                element: document.querySelector("img#qrCode"),
                                size: 250
                                // value: "eaSYNC",
                            });
                        })();

                        const qrText = details;
                        qr.set({
                            value: qrText,
                        });

                        var regex = /(<([^>]+)>)/ig;
                        var content = details;
                        var result = content.replace(regex, "");

                        var image = jQuery('.qrCode_container #qrCode').attr('src');
                        jQuery('#qrCode_image').attr('href', image);

                        if(data.error){
                            $('#errorModal').modal('show');
                        }else{
                            // location = data.redirect;	
                            $('#qrCode_modal').modal('show');
                            $('#car_customer_info').modal('hide');
                        }
                    });
                }
            });
        }
    });

    $('#continue_payment').on('submit', function(event) {
        $('body').loading();
        $('.modall').css('z-index', '0');

        var id = this.id;

        if (id == 'continue_payment_stripe') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+"&type=hotel&action=easync_validation_stripe",
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if (!data.success) {
                    remove_error();
                        if (data.errors.firstname) {
                            $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                        }else{
                            $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                        }
                        if (data.errors.lastname) {
                            $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                        }else{
                            $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>'); 
                        }
                        if (data.errors.phone) {
                            $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                        }else{
                            $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>'); 
                        }
                        if (data.errors.email) {
                            $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                        }else{
                            $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>'); 
                        }
                        $('.modall').animate({scrollTop: $('.modal-header').offset().top}, 200);
                }else{
                    // arrival date//
                    temp_data[1]  = data.date_arrive;
                    temp_data[2]  = data.date_departure;
                    temp_data[3]  = data.night_number;
                    temp_data[4]  = data.number_guest;
                    temp_data[5]  = data.number_room;
                    temp_data[6]  = data.firstname;
                    temp_data[7]  = data.lastname;
                    temp_data[8]  = data.phone;
                    temp_data[9]  = data.email;
                    temp_data[10] = data.facility_request;
                    temp_data[11] = data.other_req;
                    temp_data[18] = data.coupon_id;
                    temp_data[19] = data.amount_to_pay;
                    temp_data[20] = data.reference_number;
    
                    jQuery('.sync_payment_display input:first-child').val(room_title);
                    jQuery('.sync_payment_display input:nth-child(2)').val(Number(data.amount_to_pay.replace(/[^0-9\.]+/g, "")));
                    
                    temporary_entry    = [];
                    temporary_entry[0] = room_title;
                    temporary_entry[1] = Number(data.amount_to_pay.replace(/[^0-9\.]+/g, "")) / data.night_number;
                    temporary_entry[2] = data.night_number;
                    temporary_entry[3] = Number(data.amount_to_pay.replace(/[^0-9\.]+/g, ""));
                    
                    remove_error();
    
                    $('.hotel-stripe-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'   : '#confirm_stripe',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });
                    $('.hotel-stripe-payment').click();
                    $('.hotel-stripe-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.hotel-stripe-payment').removeAttr('data-dismiss');
                    $('.hotel-stripe-payment').removeAttr('data-toggle');
                    $('.hotel-stripe-payment').removeAttr('data-targett');
                    $('.hotel-stripe-payment').removeAttr('data-backdrop');
                    $('.hotel-stripe-payment').removeAttr('data-keyboard');
    
                }

                var formData = {
                    'type'                  : 'hotel',
                    'room_id'               : temp_data[0],
                    'arrival_date'          : temp_data[1],
                    'departure_date'        : temp_data[2],
                    'night_number'          : temp_data[3],
                    'guest_number'          : temp_data[4],
                    'room_number'           : temp_data[5],
                    'firstname'             : temp_data[6],
                    'lastname'              : temp_data[7],
                    'phone'                 : temp_data[8],
                    'email'                 : temp_data[9],
                    'facility_request'      : temp_data[10],
                    'other_req'             : temp_data[11],
                    'coupon_id'             : temp_data[18],
                    'amount_to_pay'         : temp_data[19],
                    'reference_number'      : temp_data[20],
                    'action'                :'easync_session_store_stripe',
                };

                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                    data        : formData, 
                    dataType    : 'json', 
                    encode      : true
                }).done(function(data) {
                    $('body').loading('stop');
                    $('.modall').css('z-index', '');
                    if (!data.success) {
                        console.log('Something wrong!');
                    }else{
                        temporary_entry[4] = 'success';
                    }
                });
            });
        }

        if ( id == 'continue_payment_authorize') {
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+"&type=hotel&action=easync_validation_authorize",
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if(!data.success) {
                    remove_error();
                    if (data.errors.firstname) {
                        $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                    }else{
                        $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                    }
                    if (data.errors.lastname) {
                        $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                    }else{
                        $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>'); 
                    }
                    if (data.errors.phone) {
                        $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                    }else{
                        $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>'); 
                    }
                    if (data.errors.email) {
                        $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                    }else{
                        $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>'); 
                    }
                    $('.modall').animate({scrollTop: $('.modal-header').offset().top}, 200);
                }
                else{
                    //arrival date//
                    tempry_data[1]   = data.date_arrive;
                    tempry_data[2]   = data.date_departure;
                    tempry_data[3]   = data.night_number;
                    tempry_data[4]   = data.number_guest;
                    tempry_data[5]   = data.number_room;
                    tempry_data[6]   = data.firstname;
                    tempry_data[7]   = data.lastname;
                    tempry_data[8]   = data.phone;
                    tempry_data[9]   = data.email;
                    tempry_data[10] = data.facility_request;
                    tempry_data[11]  = data.other_req;
                    tempry_data[22]  = data.coupon_id;
                    tempry_data[23] = data.amount_to_pay;
                    tempry_data[24] = data.reference_number;

                    var amt = Number(data.amount_to_pay.replace(/[^0-9\.]+/g, ""));
                    amt = amt.toFixed(2);
                    jQuery('.authorize_info #room_name').val(room_title);
                    jQuery('.pay_amount .amount-placeholder .amt_toPay').text(amt);

                    temporary_entry    = [];
                    temporary_entry[0] = room_title;
                    temporary_entry[1] = Number(data.amount_to_pay.replace(/[^0-9\.]+/g, "")) / data.night_number;
                    temporary_entry[2] = data.night_number;
                    temporary_entry[3] = Number(data.amount_to_pay.replace(/[^0-9\.]+/g, ""));
                    
                    remove_error();

                    $('.hotel-authorize-payment').attr({
                        'type'           : 'button',
                        'data-dismiss'   : 'modal',
                        'data-toggle'    : 'modal',
                        'data-targett'   : '#payment_authorize',
                        'data-backdrop'  : 'static',
                        'data-keyboard'  : 'false' 
                    });
                    $('.hotel-authorize-payment').click();
                    $('.hotel-authorize-payment').attr({
                        'type'           : 'submit'
                    });
                    $('.hotel-authorize-payment').removeAttr('data-dismiss');
                    $('.hotel-authorize-payment').removeAttr('data-toggle');
                    $('.hotel-authorize-payment').removeAttr('data-targett');
                    $('.hotel-authorize-payment').removeAttr('data-backdrop');
                    $('.hotel-authorize-payment').removeAttr('data-keyboard');
                }
            });
        }

        if (id == 'submit_reservation') { 
            $.ajax({
                type        : 'POST',
                url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
                data        : $(this).serialize()+"&type=hotel&action=easync_validation_offline", 
                dataType    : 'json', 
                encode      : true
            }).done(function(data) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                if(!data.success) {
                    remove_error();
                    if (data.errors.firstname) {
                        $('.personal-info.firstname').append('<div class="error error-firstname active">' + data.errors.firstname + '</div>'); 
                    }else{
                        $('.personal-info.firstname').append('<div class="error error-firstname ok active"> OK </div>');
                    }
                    if (data.errors.lastname) {
                        $('.personal-info.lastname').append('<div class="error error-lastname active">' + data.errors.lastname + '</div>'); 
                    }else{
                        $('.personal-info.lastname').append('<div class="error error-lastname ok active"> OK </div>'); 
                    }
                    if (data.errors.phone) {
                        $('.personal-info.phone').append('<div class="error error-phone active">' + data.errors.phone + '</div>'); 
                    }else{
                        $('.personal-info.phone').append('<div class="error error-phone ok active"> OK </div>'); 
                    }
                    if (data.errors.email) {
                        $('.personal-info.email-address').append('<div class="error error-email-address active">' + data.errors.email + '</div>'); 
                    }else{
                        $('.personal-info.email-address').append('<div class="error error-email-address ok active"> OK </div>'); 
                    }
                    $('.modall').animate({scrollTop: $('.modal-header').offset().top}, 200);
                } else {

                    var formdata = {
                        'type'                  : 'hotel',
                        'room_id'               : tempry_data[0],
                        'arrival_date'          : data.date_arrive,
                        'departure_date'        : data.date_departure,
                        'night_number'          : data.night_number,
                        'guest_number'          : data.number_guest,
                        'room_number'           : data.number_room,
                        'firstname'             : data.firstname,
                        'lastname'              : data.lastname,
                        'phone'                 : data.phone,
                        'email'                 : data.email,
                        'facility_request'      : data.facility_request,
                        'other_req'             : data.other_req,
                        'coupon_id'             : data.coupon_id,
                        'amount_to_pay'         : data.amount_to_pay,
                        'reference_number'      : data.reference_number,
                        'action'                :'easync_submit_reservation',
                    }

                    $.ajax({
                        type        : 'POST',
                        url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                        data        : formdata, 
                        dataType    : 'json', 
                        encode      : true
                    }).done(function(data) {
                        let qr_data = "Firstname: " + data['curr']['firstname'] + '\n';
                        let qr_data1 = "Lastname: " + data['curr']['lastname'] + '\n';
                        let qr_data2 = "Phone Number: " + data['curr']['phone'] + '\n';
                        let qr_data3 = "Email: " + data['curr']['email'] + '\n';
                        let qr_data4 = "Hotel Arrival Date: " + data['curr']['arrival_date'] + '\n';
                        let qr_data5 = "Hotel Departure Date: " + data['curr']['departure_date'] + '\n';
                        let qr_data6 = "Duration: " + data['curr']['night_number'] + '\n';
                        let qr_data7 = "Number of Guest: " + data['curr']['guest_number'] + '\n';
                        let qr_data8 = "Room Number: " + data['curr']['room_number'] + '\n';
                        let qr_data9 = "Facility Request: " + data['curr']['facility_request'] + '\n';
                        let qr_data10 = "Other Reqs: " + data['curr']['other_req'] + '\n';
                        let qr_data11 = "Reference Number: " + data['curr']['reference_number'] + '\n';
                        let qr_data12 = "Amount to Pay: " + data['curr']['amount_to_pay'];
                        let details = qr_data.concat(qr_data1, qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data8, qr_data9, qr_data10, qr_data11, qr_data12);

                        let qr;
                        (function () {
                            qr = new QRious({
                                element: document.querySelector("img#qrCode"),
                                size: 250
                                // value: "eaSYNC",
                            });
                        })();

                        const qrText = details;
                        qr.set({
                            value: qrText,
                        });

                        var regex = /(<([^>]+)>)/ig;
                        var content = details;
                        var result = content.replace(regex, "");

                        var image = jQuery('.qrCode_container #qrCode').attr('src');
                        jQuery('#qrCode_image').attr('href', image);

                        if(data.error){
                            $('#errorModal').modal('show');
                        }else{
                            // location = data.redirect;	
                            $('#qrCode_modal').modal('show');
                            $('#customer_info').modal('hide');
                        }
                    });
                }
            });
        }

        event.preventDefault();
    });

    $('#authorize-payment').on('submit', function(event) {
        temporary_entry[4] = 'fail';
        $('body').loading();
        $('.modall').css('z-index', '1');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
            data        : $(this).serialize()+"&type=hotel-authorize&action=easync_validation_authorize",
            dataType    : 'json', 
            encode      : true
        }).done(function(data) {
            if (!data.success) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                remove_error();
                if (data.errors.NameOnCard) {
                    $('.authorize-info.NameOnCard').append('<div class="error error-NameOnCard active">' + data.errors.NameOnCard + '</div>'); 
                }else{
                    $('.authorize-info.NameOnCard').append('<div class="error error-NameOnCard ok active"> OK </div>');
                }
                if (data.errors.CreditCardNumber) {
                    $('.authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber active">' + data.errors.CreditCardNumber + '</div>'); 
                }else{
                    $('.authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber ok active"> OK </div>'); 
                }
                if (data.errors.expiryDate) {
                    $('.authorize-info.expiryDate').append('<div class="error error-expiryDate active">' + data.errors.expiryDate + '</div>'); 
                }else{
                    $('.authorize-info.expiryDate').append('<div class="error error-expiryDate ok active"> OK </div>'); 
                }
                if (data.errors.SecurityCode) {
                    $('.authorize-info.SecurityCode').append('<div class="error error-SecurityCode active">' + data.errors.SecurityCode + '</div>'); 
                }else{
                    $('.authorize-info.SecurityCode').append('<div class="error error-SecurityCode ok active"> OK </div>'); 
                }
                if (data.errors.address) {
                    $('.authorize-info.address').append('<div class="error error-address active">' + data.errors.address + '</div>'); 
                }else{
                    $('.authorize-info.address').append('<div class="error error-address ok active"> OK </div>');
                }
                if (data.errors.state) {
                    $('.authorize-info.state').append('<div class="error error-state active">' + data.errors.state + '</div>'); 
                }else{
                    $('.authorize-info.state').append('<div class="error error-state ok active"> OK </div>'); 
                }
                if (data.errors.city) {
                    $('.authorize-info.city').append('<div class="error error-city active">' + data.errors.city + '</div>'); 
                }else{
                    $('.authorize-info.city').append('<div class="error error-city ok active"> OK </div>'); 
                }
                if (data.errors.country) {
                    $('.authorize-info.country').append('<div class="error error-country active">' + data.errors.country + '</div>'); 
                }else{
                    $('.authorize-info.country').append('<div class="error error-country ok active"> OK </div>'); 
                }
                if (data.errors.ZIPCode) {
                    $('.authorize-info.ZIPCode').append('<div class="error error-ZIPCode active">' + data.errors.ZIPCode + '</div>'); 
                }else{
                    $('.authorize-info.ZIPCode').append('<div class="error error-ZIPCode ok active"> OK </div>'); 
                }
                $('.modall').animate({scrollTop: $('.authorize-info.address').offset().top}, 200);

            }
            else if(data.error == 'Charge Error.') {
                remove_error();
                $('#charge_error_modal').modal('show');
                $('#payment_authorize').modal('hide');
                console.log('Something wrong!');
            }
            else{

                var temp_path = '#Checkout #authorize-payment';
                tempry_data[12] = $(temp_path+' input[name="NameOnCard"]').val();
                tempry_data[13] = $(temp_path+' input[name="cc_no"]').val();
                tempry_data[14] = $(temp_path+' input[name="expiry"]').val();
                tempry_data[15] = $(temp_path+' input[name="cvv"]').val();
                tempry_data[16] = $(temp_path+' input[name="address"]').val();
                tempry_data[17] = $(temp_path+' input[name="state"]').val();
                tempry_data[18] = $(temp_path+' input[name="city"]').val();
                tempry_data[19] = $(temp_path+' input[name="country"]').val();
                tempry_data[20] = $(temp_path+' input[name="zip"]').val();
                tempry_data[21] = $(temp_path+' input[name="easync_payment_nonce"]').val();
                

                var formData = {
                    'type'                  : 'hotel',
                    'room_id'               : tempry_data[0],
                    'arrival_date'          : tempry_data[1],
                    'departure_date'        : tempry_data[2],
                    'night_number'          : tempry_data[3],
                    'guest_number'          : tempry_data[4],
                    'room_number'           : tempry_data[5],
                    'firstname'             : tempry_data[6],
                    'lastname'              : tempry_data[7],
                    'phone'                 : tempry_data[8],
                    'email'                 : tempry_data[9],
                    'facility_request'      : tempry_data[10],
                    'other_req'             : tempry_data[11],
                    'NameOnCard'            : tempry_data[12],
                    'cc_no'                 : tempry_data[13],
                    'expiry'                : tempry_data[14],
                    'cvv'                   : tempry_data[15],
                    'address'               : tempry_data[16],
                    'state'                 : tempry_data[17],
                    'city'                  : tempry_data[18],
                    'country'               : tempry_data[19],
                    'zip'                   : tempry_data[20],
                    'easync_payment_nonce'  : tempry_data[21],
                    'coupon_id'             : tempry_data[22],
                    'amount_to_pay'         : tempry_data[23],
                    'reference_number'      : tempry_data[24],
                    'action'                :'authorize_save',
                };

                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/session-store.php',
                    data        : formData, 
                    dataType    : 'json', 
                    encode      : true
                }).done(function(data) {

                    $('body').loading('stop');
                    $('.modall').css('z-index', '');

                    let qr_data = "Firstname: " + data[0] + '\n';
                    let qr_data1 = "Lastname: " + data[1] + '\n';
                    let qr_data2 = "Phone Number: " + data[3] + '\n';
                    let qr_data3 = "Email: " + data[2] + '\n';
                    let qr_data4 = "Hotel Arrival Date: " + data[4] + '\n';
                    let qr_data5 = "Hotel Departure Date: " + data[5] + '\n';
                    let qr_data6 = "Duration: " + data[6] + '\n';
                    let qr_data7 = "Number of Guest: " + data[7] + '\n';
                    let qr_data8 = "Room Number: " + data[8] + '\n';
                    let qr_data9 = "Facility Request: " + data[9] + '\n';
                    let qr_data10 = "Other Reqs: " + data[10] + '\n';
                    let qr_data11 = "Reference Number: " + data[12] + '\n';
                    let qr_data12 = "Amount to Pay: " + data[11] + '\n';
                    let details = qr_data.concat(qr_data1, qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data8, qr_data9, qr_data10, qr_data11, qr_data12);

                    let qr;
                    (function () {
                        qr = new QRious({
                            element: document.querySelector("#qrCode_modal_auth img#qrCode"),
                            size: 250
                            // value: "eaSYNC",
                        });
                    })();

                    const qrText = details;
                    qr.set({
                        value: qrText,
                    });

                    var regex = /(<([^>]+)>)/ig;
                    var content = details;
                    var result = content.replace(regex, "");

                    var image = $('#qrCode_modal_auth .qrCode_container #qrCode').attr('src');
                    $('#qrCode_modal_auth #qrCode').attr('href', image);
                    
                    if (data[13] == 'Success') {
                        $('#qrCode_modal_auth').modal('show');
                        $('#payment_authorize').modal('hide');

                    }else{
                        $('#charge_error_modal').modal('show');
                        $('#payment_authorize').modal('hide');
                        console.log('Something wrong!');
                    }
                });
            }
        });

        event.preventDefault();
    });

    $('#authorize-payment-car').on('submit', function(event) {
        temporary_entry[4] = 'fail';
        $('body').loading();
        $('.modall').css('z-index', '1');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
            data        : $(this).serialize()+"&type=car-authorize&action=easync_validation_authorize",
            dataType    : 'json', 
            encode      : true
        }).done(function(data) {
            if (!data.success) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                remove_error();
                if (data.errors.NameOnCard) {
                    $('#authorize-payment-car .authorize-info.NameOnCard').append('<div class="error error-NameOnCard active">' + data.errors.NameOnCard + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.NameOnCard').append('<div class="error error-NameOnCard ok active"> OK </div>');
                }
                if (data.errors.CreditCardNumber) {
                    $('#authorize-payment-car .authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber active">' + data.errors.CreditCardNumber + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber ok active"> OK </div>'); 
                }
                if (data.errors.expiryDate) {
                    $('#authorize-payment-car .authorize-info.expiryDate').append('<div class="error error-expiryDate active">' + data.errors.expiryDate + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.expiryDate').append('<div class="error error-expiryDate ok active"> OK </div>'); 
                }
                if (data.errors.SecurityCode) {
                    $('#authorize-payment-car .authorize-info.SecurityCode').append('<div class="error error-SecurityCode active">' + data.errors.SecurityCode + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.SecurityCode').append('<div class="error error-SecurityCode ok active"> OK </div>'); 
                }
                if (data.errors.address) {
                    $('#authorize-payment-car .authorize-info.address').append('<div class="error error-address active">' + data.errors.address + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.address').append('<div class="error error-address ok active"> OK </div>');
                }
                if (data.errors.state) {
                    $('#authorize-payment-car .authorize-info.state').append('<div class="error error-state active">' + data.errors.state + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.state').append('<div class="error error-state ok active"> OK </div>'); 
                }
                if (data.errors.city) {
                    $('#authorize-payment-car .authorize-info.city').append('<div class="error error-city active">' + data.errors.city + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.city').append('<div class="error error-city ok active"> OK </div>'); 
                }
                if (data.errors.country) {
                    $('#authorize-payment-car .authorize-info.country').append('<div class="error error-country active">' + data.errors.country + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.country').append('<div class="error error-country ok active"> OK </div>'); 
                }
                if (data.errors.ZIPCode) {
                    $('#authorize-payment-car .authorize-info.ZIPCode').append('<div class="error error-ZIPCode active">' + data.errors.ZIPCode + '</div>'); 
                }else{
                    $('#authorize-payment-car .authorize-info.ZIPCode').append('<div class="error error-ZIPCode ok active"> OK </div>'); 
                }
                $('.modall').animate({scrollTop: $('#authorize-payment-car .authorize-info.address').offset().top}, 200);

            }else{

                var temp_path = '#Checkout #authorize-payment-car';
                temporaryy_data[0] = $(temp_path+' input[name="NameOnCard"]').val();
                temporaryy_data[1] = $(temp_path+' input[name="cc_no"]').val();
                temporaryy_data[2] = $(temp_path+' input[name="expiry"]').val();
                temporaryy_data[3] = $(temp_path+' input[name="cvv"]').val();
                temporaryy_data[4] = $(temp_path+' input[name="address"]').val();
                temporaryy_data[5] = $(temp_path+' input[name="state"]').val();
                temporaryy_data[6] = $(temp_path+' input[name="city"]').val();
                temporaryy_data[7] = $(temp_path+' input[name="country"]').val();
                temporaryy_data[8] = $(temp_path+' input[name="zip"]').val();

                var formData = {
                    'type'                  : 'car',
                    'with_driver'           : with_driver,
                    'car_id'                : temporary_data[0],
                    'date_pick'             : temporary_data[1],
                    'pick_time'             : temporary_data[2],
                    'date_return'           : temporary_data[3],
                    'return_time'           : temporary_data[4],
                    'pick_location'         : temporary_data[5],
                    'number_days'           : temporary_data[6],
                    'with_or_out_driver'    : temporary_data[7],
                    'firstname'             : temporary_data[8],
                    'lastname'              : temporary_data[9],
                    'phone'                 : temporary_data[10],
                    'email'                 : temporary_data[11],
                    'NameOnCard'            : temporaryy_data[0],
                    'cc_no'                 : temporaryy_data[1],
                    'expiry'                : temporaryy_data[2],
                    'cvv'                   : temporaryy_data[3],
                    'address'               : temporaryy_data[4],
                    'state'                 : temporaryy_data[5],
                    'city'                  : temporaryy_data[6],
                    'country'               : temporaryy_data[7],
                    'zip'                   : temporaryy_data[8],
                    'driver_name'           : temporary_data[12],
                    'driver_phone'          : temporary_data[13],
                    'driver_license_image1' : $('.sync_components #filediv1 input[type=file]').val().split('\\').pop(),
                    'driver_license_image2' : $('.sync_components #filediv2 input[type=file]').val().split('\\').pop(), 
                    'coupon_id_car'         : temporary_data[19],
                    'amount_to_pay_car'     : temporary_data[20],
                    'facility_request'      : temporary_data[21],
                    'other_req'             : temporary_data[22],
                    'reference_number'      : temporary_data[23],
                    'check_if_require'      : temporary_data[25],
                    'car_name'              : car_name,
                    'action'                :'authorize_save_car',
                };

                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,
                    data        : formData, 
                    dataType    : 'json', 
                    encode      : true
                }).done(function(data) {

                    $('body').loading('stop');
                    $('.modall').css('z-index', '');

                    let qr_data = "Firstname: " + data[0] + '\n';
                    let qr_data1 = "Lastname: " + data[1] + '\n';
                    let qr_data2 = "Phone Number: " + data[3] + '\n';
                    let qr_data3 = "Email: " + data[2] + '\n';
                    let qr_data4 = "Pickup Date: " + data[4] + '\n';
                    let qr_data5 = "Return Date: " + data[5] + '\n';
                    let qr_data6 = "Facility Request: " + data[6] + '\n';
                    let qr_data7 = "Other Reqs: " + data[7] + '\n';
                    let qr_data8 = "Reference Number: " + data[9] + '\n';
                    let qr_data9 = "Amount to Pay: " + data[8] + '\n';
                    let details = qr_data.concat(qr_data1, qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data8, qr_data9);

                    let qr;
                    (function () {
                        qr = new QRious({
                            element: document.querySelector("img#qrCode_car"),
                            size: 250
                            // value: "eaSYNC",
                        });
                    })();

                    const qrText = details;
                    qr.set({
                        value: qrText,
                    });

                    var regex = /(<([^>]+)>)/ig;
                    var content = details;
                    var result = content.replace(regex, "");

                    var image = jQuery('.qrCode_container #qrCode_car').attr('src');
                    jQuery('#qrCode_image').attr('href', image);
                    
                    if (data[10] == 'Success') {
                        $('#qrCode_modal_auth_car').modal('show');
                        $('#payment_authorize_car').modal('hide');

                    }else{
                        $('#charge_error_modal').modal('show');
                        $('#payment_authorize_car').modal('hide');
                        console.log('Something wrong!');
                    }
                });
            }
        });

        event.preventDefault();
    });

    $('#authorize-payment-restau').on('submit', function(event) {
        temporary_entry[4] = 'fail';
        $('body').loading();
        $('.modall').css('z-index', '1');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/validation.php',
            data        : $(this).serialize()+"&type=restau-authorize&action=easync_validation_authorize",
            dataType    : 'json', 
            encode      : true
        }).done(function(data) {
            if (!data.success) {
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                remove_error();
                if (data.errors.NameOnCard) {
                    $('#authorize-payment-restau .authorize-info.NameOnCard').append('<div class="error error-NameOnCard active">' + data.errors.NameOnCard + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.NameOnCard').append('<div class="error error-NameOnCard ok active"> OK </div>');
                }
                if (data.errors.CreditCardNumber) {
                    $('#authorize-payment-restau .authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber active">' + data.errors.CreditCardNumber + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.CreditCardNumber').append('<div class="error error-CreditCardNumber ok active"> OK </div>'); 
                }
                if (data.errors.expiryDate) {
                    $('#authorize-payment-restau .authorize-info.expiryDate').append('<div class="error error-expiryDate active">' + data.errors.expiryDate + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.expiryDate').append('<div class="error error-expiryDate ok active"> OK </div>'); 
                }
                if (data.errors.SecurityCode) {
                    $('#authorize-payment-restau .authorize-info.SecurityCode').append('<div class="error error-SecurityCode active">' + data.errors.SecurityCode + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.SecurityCode').append('<div class="error error-SecurityCode ok active"> OK </div>'); 
                }
                if (data.errors.address) {
                    $('#authorize-payment-restau .authorize-info.address').append('<div class="error error-address active">' + data.errors.address + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.address').append('<div class="error error-address ok active"> OK </div>');
                }
                if (data.errors.state) {
                    $('#authorize-payment-restau .authorize-info.state').append('<div class="error error-state active">' + data.errors.state + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.state').append('<div class="error error-state ok active"> OK </div>'); 
                }
                if (data.errors.city) {
                    $('#authorize-payment-restau .authorize-info.city').append('<div class="error error-city active">' + data.errors.city + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.city').append('<div class="error error-city ok active"> OK </div>'); 
                }
                if (data.errors.country) {
                    $('#authorize-payment-restau .authorize-info.country').append('<div class="error error-country active">' + data.errors.country + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.country').append('<div class="error error-country ok active"> OK </div>'); 
                }
                if (data.errors.ZIPCode) {
                    $('#authorize-payment-restau .authorize-info.ZIPCode').append('<div class="error error-ZIPCode active">' + data.errors.ZIPCode + '</div>'); 
                }else{
                    $('#authorize-payment-restau .authorize-info.ZIPCode').append('<div class="error error-ZIPCode ok active"> OK </div>'); 
                }
                $('.modall').animate({scrollTop: $('#authorize-payment-restau .authorize-info.address').offset().top}, 200);

            }else{

                var temp_path = '#Checkout #authorize-payment-restau';
                temp_storage[0] = $(temp_path+' input[name="NameOnCard"]').val();
                temp_storage[1] = $(temp_path+' input[name="cc_no"]').val();
                temp_storage[2] = $(temp_path+' input[name="expiry"]').val();
                temp_storage[3] = $(temp_path+' input[name="cvv"]').val();
                temp_storage[4] = $(temp_path+' input[name="address"]').val();
                temp_storage[5] = $(temp_path+' input[name="state"]').val();
                temp_storage[6] = $(temp_path+' input[name="city"]').val();
                temp_storage[7] = $(temp_path+' input[name="country"]').val();
                temp_storage[8] = $(temp_path+' input[name="zip"]').val();

                var formData = {
                    'type'                 : 'restau',
                    'name'                 : temporary_data[0],
                    'email'                : temporary_data[1],
                    'phone_no'             : temporary_data[2],
                    'branch'               : temporary_data[3],
                    'guest_no'             : temporary_data[4],
                    'table_no'             : temporary_data[5],
                    'timeslot'             : temporary_data[6],
                    'picked_date'          : temporary_data[7],
                    'menu_ids'             : temporary_data[8],
                    'NameOnCard'           : temp_storage[0],
                    'cc_no'                : temp_storage[1],
                    'expiry'               : temp_storage[2],
                    'cvv'                  : temp_storage[3],
                    'address'              : temp_storage[4],
                    'state'                : temp_storage[5],
                    'city'                 : temp_storage[6],
                    'country'              : temp_storage[7],
                    'zip'                  : temp_storage[8],
                    'reference_number'     : temporary_data[14],
                    'amount_to_pay_restau' : temporary_data[15],
                    'coupon_id'            : temporary_data[16],
                    'item_prices'          : temporary_data[17],
                    'menu_reserved'        : temporary_data[18],
                    'item_qty_each'        : temporary_data[19],
                    'table_ids'            : temporary_data[20],
                    'action'               :'authorize_save_restau',
                };

                $.ajax({
                    type        : 'POST',
                    url         : easync_admin_ajax_directory.ajaxurl,
                    data        : formData, 
                    dataType    : 'json', 
                    encode      : true
                }).done(function(data) {

                    $('body').loading('stop');
                    $('.modall').css('z-index', '');

                    let qr_data1 = "Name: " + data[0] + '\n'; 
                    let qr_data2 = "Phone Number: " + data[2] + '\n';
                    let qr_data3 = "Email: " + data[1] + '\n';
                    let qr_data4 = "Reservation Date: " + data[6] + '\n';
                    let qr_data5 = "Timeslot: " + data[7] + '\n';
                    let qr_data6 = "Number of Guest: " + data[4] + '\n';
                    let qr_data7 = "Number of Tables: " + data[5] + '\n';
                    let qr_data9 = "Branch: " + data[3] + '\n';
                    let qr_data10 = "Reference Number: " + data[9] + '\n';
                    let qr_data11 = "Amount to Pay: " + data[11] + " " + data[8];
                    let details = qr_data1.concat(qr_data2, qr_data3, qr_data4, qr_data5, qr_data6, qr_data7, qr_data9, qr_data10, qr_data11);

                    let qr;
                    (function () {
                        qr = new QRious({
                            element: document.querySelector("#qrCode_modal_auth_restau img#qrCode"),
                            size: 250
                        });
                    })();

                    const qrText = details;
                    qr.set({
                        value: qrText,
                    });

                    var regex = /(<([^>]+)>)/ig;
                    var content = details;
                    var result = content.replace(regex, "");

                    var image = jQuery('#qrCode_modal_auth_restau .qrCode_container #qrCode').attr('src');
                    jQuery('#qrCode_modal_auth_restau #qrCode').attr('href', image);
                    
                    if (data[10] == 'Success') {
                        $('#qrCode_modal_auth_restau').modal('show');
                        $('#payment_authorize_restau').modal('hide');

                    }else{
                        $('#charge_error_modal').modal('show');
                        $('#payment_authorize_restau').modal('hide');
                        console.log('Something wrong!');
                    }
                });

            }
        });

        event.preventDefault();
    });

    jQuery(document).on('submit', '#sync_restau_availability', function(event) {
        jQuery('body').loading();
        jQuery('.modall').css('z-index', '0');
        var sun = jQuery("#sync_restau_availability #sunday").is(':checked'); 
        var mon = jQuery("#sync_restau_availability #monday").is(':checked');
        var tue = jQuery("#sync_restau_availability #tuesday").is(':checked');
        var wed = jQuery("#sync_restau_availability #wednesday").is(':checked'); 
        var thu = jQuery("#sync_restau_availability #thursday").is(':checked');
        var fri = jQuery("#sync_restau_availability #friday").is(':checked');
        var sat = jQuery("#sync_restau_availability #saturday").is(':checked');
    
        jQuery.ajax({
            type        : 'POST',
            dataType    : 'json', 
            url         : easync_admin_ajax_directory.ajaxurl,
            data        : { action: 'easync_disable_dates', sunday: sun, monday: mon, tuesday: tue, wednesday: wed, thursday: thu, friday: fri, saturday: sat }, 
            success: function(data){
                jQuery('body').loading('stop');
                jQuery('.modall').css('z-index', '');
                sweetfb();
            }
        });
        event.preventDefault();
    });

    $('#sync_hotel_early_check').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        check_val = $("#sync_hotel_checkin_switch").val();
        price = $("#sync_early_checkPrice").val();
        check_late_val = $("#sync_hotel_checkout_switch").val();
        late_price = $("#sync_late_checkPrice").val();
        $.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "easync_early_check_in", check_value: check_val, checkin_price: price, late_value: check_late_val, checkout_price: late_price  },
            success: function(data){
                if (data == "false") {
                    console.log('Something wrong!');
                }
                $('body').loading('stop');
                $('.modall').css('z-index', '');
                sweetfb();
            }
        });
        event.preventDefault();
        
    });

    $('#update_cpn').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_update_coupon", 
            dataType    : 'json', 
            encode      : true,
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb();
            location.reload();
        });
        event.preventDefault();
    });

    $('#update_car_cpn').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_update_car_coupon", 
            dataType    : 'json', 
            encode      : true,
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb();
            location.reload();
        });
        event.preventDefault();
    });

    $('#generate_cpn').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_save_coupon", 
            dataType    : 'json', 
            encode      : true,
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb_reload();
            // location.reload();
        });
        event.preventDefault();
    });

    $('#generate_cpn_car').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_save_coupon_car", 
            dataType    : 'json', 
            encode      : true,
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb_reload();
            // location.reload();
        });
        event.preventDefault();
    });

    $('#generate_cpn_restau').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_save_coupon_restau", 
            dataType    : 'json', 
            encode      : true,
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb_reload();
            // location.reload();
        });
        event.preventDefault();
    });
    
    $(document).on('click', '#choice #use_now', function () {
        var currency = $('.payment input[name="sync_currency_code"]').val();
        var coup_code = $(".add_coupon #coupon_field").val();

        $.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "use_coupon", cpn_code: coup_code },
            success: function(data){

                if( data == "invalid" ) {
                    $('.alert-danger.error-coupon').remove();
                    $('.add_coupon').after('<p class="alert alert-danger error-coupon active"> Invalid Coupon Code. </p>'); 
                } else {
                    var final_total = subtotal - data[0]['coupon_value'];
                    final_total  = final_total.toFixed(2);
                    final_total  = final_total.toString().replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");
                    var discount = data[0]['coupon_value'];
                    $('.alert-danger.error-coupon').remove();
                    $('#customer_info .customer-info .second-row .sync_components .book-summary-total .total_amount p .discount_amount').text('- '+currency+' '+discount);
                    $('#customer_info .customer-info .second-row .sync_components .book-summary-total .total_amount p .final_amount').text(currency+' '+final_total);
                    $('#customer_info .customer-info .second-row .sync_components .book-summary-payment .payment .amount_to_pay').val(final_total);
                    $('#customer_info .customer-info .second-row .sync_components .book-summary-payment .payment .coupon_id').val(data[0]['id']);
                    
                    $(".total_amount p .discount_amount").show(); 
                    $(".total_amount p .final_amount").show();
                    $("#customer_info .customer-info .second-row .sync_components .add_coupon #choice #use_now").hide();
                    $("#customer_info .customer-info .second-row .sync_components .add_coupon #choice #cancel").hide();
                    $("#customer_info .customer-info .second-row .sync_components .add_coupon #choice #remove").show();
                    $("#customer_info .customer-info .second-row .sync_components .add_coupon #coupon_field").prop('readonly', true);
                    $("#customer_info .customer-info .second-row .sync_components #view_coupons").hide();
                }
            }
        });
    });

    $(document).on('click', '#choice #use_now_car', function () {
        var currency = $('.payment input[name="sync_currency_code"]').val();
        var coup_code = $(".add_coupon_car #coupon_field_car").val();

        $.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "use_coupon_car", cpn_code: coup_code },
            success: function(data){

                if (data == 'invalid') {
                    $('.alert-danger.error-coupon').remove();
                    $('.add_coupon_car').after('<p class="alert alert-danger error-coupon active"> Invalid Coupon Code. </p>'); 
                } else {
                    var final_total = Number(subtotal) + Number(total_deposit) + Number(tax_value) + Number(fees_value);
                    var final_total_sum = final_total - data[0]['coupon_value'];
                    var disc = Number(data[0]['coupon_value']);
                    disc = disc.toFixed(2);

                    final_total_sum  = final_total_sum.toFixed(2);
                    final_total_sum  = final_total_sum.toString().replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");

                    $('.alert-danger.error-coupon').remove();
                    $('#car_customer_info .customer-info .second-row .sync_components .book-summary-total .total_amount_car p .discount_amount_car').text('- '+currency+' '+ disc);
                    $('#car_customer_info .customer-info .second-row .sync_components .book-summary-total .total_amount_car p .final_amount_car').text(currency+' '+final_total_sum);
                    $('#car_customer_info .customer-info .second-row .sync_components .book-summary-payment .payment .amount_to_pay_car').val(final_total_sum);
                    $('#car_customer_info .customer-info .second-row .sync_components .book-summary-payment .payment .coupon_id_car').val(data[0]['id']);
                    $('#car_customer_info .customer-info .second-row .sync_components .book-summary-total p .final_noDiscount_car').hide();
                    $(".total_amount_car p .discount_amount_car").show(); 
                    $(".total_amount_car p .final_amount_car").show();
                    $("#car_customer_info .customer-info .second-row .sync_components .add_coupon_car #choice #use_now_car").hide();
                    $("#car_customer_info .customer-info .second-row .sync_components .add_coupon_car #choice #cancel_car").hide();
                    $("#car_customer_info .customer-info .second-row .sync_components .add_coupon_car #choice #remove_car").show();
                    $("#car_customer_info .customer-info .second-row .sync_components .book-summary-total .total_text_car .discount_lbl").show();
                    $("#car_customer_info .customer-info .second-row .sync_components .add_coupon_car #coupon_field_car").prop('readonly', true);
                    $("#car_customer_info .customer-info .second-row .sync_components #view_coupons_car").hide();
                }

            }
        });
    });

    $(document).on('click', '#choice #use_now_restau', function () {
        var currency = $('.payment input[name="sync_currency_code"]').val();
        var coup_code = $(".add_coupon_restau #coupon_field_restau").val();

        $.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: { action: "use_coupon_restau", cpn_code: coup_code },
            success: function(data){

                if (data == 'invalid') {
                    $('.alert-danger.error-coupon').remove();
                    $('.add_coupon_restau').after('<p class="alert alert-danger error-coupon active"> Expired/Invalid Coupon Code. </p>'); 
                } else {
                    var subtotal = $('#restau_continue_payment .total_amount_restau p .sync_price_money_format').text();
                    var final_total_sum = Number(subtotal) - data[0]['coupon_value'];
                    var disc = Number(data[0]['coupon_value']);
                    disc = disc.toFixed(2);

                    final_total_sum  = final_total_sum.toFixed(2);
                    final_total_sum  = final_total_sum.toString().replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",");

                    $('.alert-danger.error-coupon').remove();
                    $('#restau_menu_info .customer-info .second-row .sync_components .book-summary-total .total_amount_restau p .discount_amount_restau').text('- '+currency+' '+ disc);
                    $('#restau_menu_info .customer-info .second-row .sync_components .book-summary-total .total_amount_restau p .final_amount_restau').text(currency+' '+final_total_sum);
                    $('#restau_menu_info .customer-info .second-row .sync_components .book-summary-payment .payment #amount_to_pay_restau').val(final_total_sum);
                    $('#restau_menu_info .customer-info .second-row .sync_components .book-summary-payment .payment .coupon_id_restau').val(data[0]['id']);
                    $(".total_amount_restau p .discount_amount_restau").show(); 
                    $(".total_amount_restau p .final_amount_restau").show();
                    $("#restau_menu_info .customer-info .second-row .sync_components .add_coupon_restau #choice #use_now_restau").hide();
                    $("#restau_menu_info .customer-info .second-row .sync_components .add_coupon_restau #choice #cancel_restau").hide();
                    $("#restau_menu_info .customer-info .second-row .sync_components .add_coupon_restau #choice #remove_restau").show();
                    $("#restau_menu_info .customer-info .second-row .sync_components .book-summary-total .total_text_restau .discount_lbl").show();
                    $("#restau_menu_info .customer-info .second-row .sync_components .add_coupon_restau #coupon_field_restau").prop('readonly', true);
                    $("#restau_menu_info .customer-info .second-row .sync_components #view_coupons_restau").hide();
                }

            }
        });
    });

    var disableDates = [];
    var indexes      = [];
    var disableDays  = "";
    var now          = new Date();
    $(document).ready(function () {
        $.ajax({
            type        : 'GET',
            url         : easync_admin_ajax_directory.ajaxurl,
            data        : "action=easync_get_dates", 
            dataType    : 'json', 
            encode      : true
        }).done(function(data) {
            if (!data) {
                    console.log('Something wrong!');
            }else{
                for (let i = 0; i < data.length-1; i++) {
                    disableDates.push(data[i]);
                }
                var days_list = data[data.length-1];
                for (j = 0; j < days_list.length; j++) {
                    if (days_list[j] == 'true') {
                        indexes.push(j);
                    }
                }
                disableDays = indexes.join();
                
                $('.docs-datepicker-container').datepicker({
                    inline: true,
                    zIndex: 2048,
                    startDate: now,
                    todayHighlight: true,
                    datesDisabled: disableDates,
                    daysOfWeekDisabled: disableDays,
                }).on('changeDate', function(e){
                    $('.docs-date').val(e.format('mm/dd/yyyy'))
                });
            }
        });
    });

    $('#sync_car_tax_fees').on('submit', function(event){
        $('body').loading();
        $('.modall').css('z-index', '0');
        $.ajax({
            type        : 'POST',
            url         : easync_admin_ajax_directory.ajaxurl,//sync_plugin_directory.pluginsUrl +'/easync/settings-save.php',
            data        : $(this).serialize()+ "&action=easync_taxes_and_fees_car", 
            dataType    : 'json', 
            encode      : true
        }).done(function(data) {
            if (!data.success) {
                console.log('Something wrong!');
            }
            $('body').loading('stop');
            $('.modall').css('z-index', '');
            sweetfb();
        });
        event.preventDefault();
    });
});

jQuery(document).on('submit', '#sync_hotel_room_rates', function (e) {
    var id = jQuery('#sync_hotel_rooms').val();
    var name = jQuery('.event_name_fld').val();
    var rate = jQuery('.room_rate').val();
    var start = jQuery('#start_date').val();
    var end = jQuery('#end_date').val();

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "hotel_room_rate", id: id, event_name_fld: name, room_rate: rate, start_date: start, end_date: end},
        success: function(data){
            location.reload();
        }
    });
        
    e.preventDefault();
});

jQuery(document).on('click', '#event_table .btn-update-event', function() {
    var id = this.id;
    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "display_room_rate", id: id},
        success: function(data){
            jQuery('#update_sync_hotel_rooms').val(data[1]).change();
            jQuery('.update_event_name_fld').val(data[0].name);
            jQuery('.update_room_rate').val(data[0].rate);
            jQuery('#update_start_date').val(data[0].start_date);
            jQuery('#update_end_date').val(data[0].end_date);
            jQuery('#sync_update_room_rates .btn-update').attr('id', data[0].id);

            jQuery('#sync_update_event').modal('show'); 
        }
    });
});

jQuery(document).on('submit', '#sync_update_room_rates', function (e) {
    
    var rate_id = jQuery('#sync_update_room_rates .btn-update').attr('id');
    var name = jQuery('.update_event_name_fld').val();
    var rate = jQuery('.update_room_rate').val();
    var start = jQuery('#update_start_date').val();
    var end = jQuery('#update_end_date').val();

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "update_room_rate", event_name_fld: name, room_rate: rate, start_date: start, end_date: end, rate_id: rate_id},
        success: function(data){
            location.reload();
        }
    });
    e.preventDefault();

});

jQuery(document).on('click', '.btn-end-event', function () {

    var id = jQuery('.rate_details .btn-end-event').attr('id');

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "check_rate_exist", rate_id: id},
        success: function(data){
            if (data[0] == 'true') {
                jQuery("#sync_cannot_delete").modal("show");
            }
            if ( data[0] == 'false') {
                jQuery('#sync_confirm_end .confirm_end').attr('id', data[1]);
                jQuery("#sync_confirm_end").modal("show");
            }
        }
    });

});

jQuery(document).on('submit', '#sync_hotel_settings', function (e) {
    e.preventDefault();

    var fname_req = jQuery("#fname_require").is(':checked');
    var fname_disp =  jQuery("#fname_display").is(':checked')
    var lname_req =  jQuery("#lname_require").is(':checked');
    var lname_disp =  jQuery("#lname_display").is(':checked');
    var phone_req =  jQuery("#phone_require").is(':checked');
    var phone_disp =  jQuery("#phone_display").is(':checked');
    var email_req =  jQuery("#email_require").is(':checked');
    var email_disp =  jQuery("#email_display").is(':checked');

    if (fname_disp == 'true') {
        fname_req = 'false';
    }

    var formData = {
        'fname_req'      : fname_req,
        'fname_disp'     : fname_disp,
        'lname_req'      : lname_req,
        'lname_disp'     : lname_disp,
        'phone_req'      : phone_req,
        'phone_disp'     : phone_disp,
        'email_req'      : email_req,
        'email_disp'     : email_disp,
        'action'         : 'save_form_settings',
    };

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: formData,
        success: function(data){
            if (!data.success) {
                console.log('Something wrong!');
            }
            jQuery('body').loading('stop');
            jQuery('.modall').css('z-index', '');
            sweetfb();
            location.reload();
        }
    });

});

jQuery(document).ready(function () {

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "configure_form_car" },
        success: function(data){
            var array = ['fname_require_car', 'fname_display_car', 'lname_require_car', 'lname_display_car', 'phone_require_car', 'phone_display_car', 'email_require_car', 'email_display_car' ]
    
            for ( let i = 0; i < data[0].length; i++ ) {
                if ( data[0][i] == 'true' ) {
                    jQuery('#'+array[i]).prop('checked', 'true');
                }
            }
        }
    });

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "configure_form" },
        success: function(data){

            var array = ['fname_require', 'fname_display', 'lname_require', 'lname_display', 'phone_require', 'phone_display', 'email_require', 'email_display' ]

            for ( let i = 0; i < data[0].length; i++ ) {
                if ( data[0][i] == 'true' ) {
                    jQuery('#'+array[i]).prop('checked', 'true');
                }
            }
        }
    });

    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "set_attr_form" },
        success: function(data){

            var array = ['firstname', 'lastname', 'phone', 'email-address' ];
            var disp_array = [];

 
            for (let j = 1; j < data[0].length; j+=2) {
                disp_array.push(data[0][j]);
            }


            for (let l = 0; l < array.length; l++) {
                if (disp_array[l] === 'true') {
                    jQuery('#continue_payment #'+array[l]).prop('hidden', disp_array[l]);
                    jQuery('#continue_payment .personal-info.'+array[l]).prop('hidden', disp_array[l]);
                } else {
                    jQuery('#'+array[l]).removeAttr('hidden');
                    jQuery('#continue_payment .personal-info.'+array[l]).removeAttr('hidden');
                }
            }
        }
    });

    jQuery(document).on('submit', '#sync_car_settings', function (e) {
        e.preventDefault();
    
        var fname_req_car = jQuery("#fname_require_car").is(':checked');
        var fname_disp_car =  jQuery("#fname_display_car").is(':checked')
        var lname_req_car =  jQuery("#lname_require_car").is(':checked');
        var lname_disp_car =  jQuery("#lname_display_car").is(':checked');
        var phone_req_car =  jQuery("#phone_require_car").is(':checked');
        var phone_disp_car =  jQuery("#phone_display_car").is(':checked');
        var email_req_car =  jQuery("#email_require_car").is(':checked');
        var email_disp_car =  jQuery("#email_display_car").is(':checked');
    
        if (fname_disp_car == 'true') {
            fname_req_car = 'false';
        }
    
        var formData = {
            'fname_req_car'      : fname_req_car,
            'fname_disp_car'     : fname_disp_car,
            'lname_req_car'      : lname_req_car,
            'lname_disp_car'     : lname_disp_car,
            'phone_req_car'      : phone_req_car,
            'phone_disp_car'     : phone_disp_car,
            'email_req_car'      : email_req_car,
            'email_disp_car'     : email_disp_car,
            'action'         : 'save_form_settings_car',
        };
    
        jQuery.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: formData,
            success: function(data){
                if (!data.success) {
                    console.log('Something wrong!');
                }
                jQuery('body').loading('stop');
                jQuery('.modall').css('z-index', '');
                sweetfb();
                location.reload();
            }
        });
    });
    
    jQuery.ajax({
        type: "POST",
        dataType: "json",    
        url: easync_admin_ajax_directory.ajaxurl,
        data: { action: "set_attr_form_car" },
        success: function(data){
            var array = ['firstname', 'lastname', 'phone', 'email-address' ];
            var disp_array = [];

            for (let j = 1; j < data[0].length; j+=2) {
                disp_array.push(data[0][j]);
            }

            for (let l = 0; l < array.length; l++) {
                
                if (disp_array[l] === 'true') {
                    jQuery('#car_continue_payment #'+array[l]).prop('hidden', disp_array[l]);
                    jQuery('#car_continue_payment .personal-info.'+array[l]).prop('hidden', disp_array[l]);
                } else {
                    jQuery('#'+array[l]).removeAttr('hidden');
                    jQuery('#car_continue_payment .personal-info.'+array[l]).removeAttr('hidden');
                }
            }
        }
    });
    
    jQuery(document).on('submit', '#sync_color', function (e) {
        e.preventDefault();
        var color = jQuery('#color_picker').val();
        var color_hover = jQuery('#color_picker_hover').val();

        jQuery.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: {action: 'custom_theme_color', color: color, color_hover: color_hover },
            success: function(data){
                if (data[0] != 'Success') {
                    console.log('Something wrong!');
                }
                jQuery('body').loading('stop');
                jQuery('.modall').css('z-index', '');
                location.reload();
            }
        });
        
    });

    jQuery(document).on('submit', '#sync_form_authorize', function (e) {
        e.preventDefault();
        var authorize_login = jQuery('#authorize_login').val();
        var authorize_key = jQuery('#authorize_key').val();
        var authorize_endpoints = jQuery('#authorize_endpoints option:selected').val();

        jQuery.ajax({
            type: "POST",
            dataType: "json",    
            url: easync_admin_ajax_directory.ajaxurl,
            data: {action: 'save_authorize_key', authorize_login: authorize_login, authorize_key: authorize_key, authorize_endpoints: authorize_endpoints  },
            success: function(data){
                if (data[0] != 'Success') {
                    console.log('Something wrong!');
                }

                jQuery('body').loading('stop');
                jQuery('.modall').css('z-index', '');
                sweetfb();
            }
        });

    });

});

function sweetfb() {
    swal(
      'Done',
      'Success!',
      'success'
    )
}

function sweetfb_reload() {
    swal(
      'Done',
      'Success!',
      'success'
    ).then(function() {
        location.reload();
    });
}