

function arp_redirect(unique_id, re_url, is_new_tab, is_new_window, is_script, object, template_id, column_id) {
    var template_obj = jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"]');
    var plan_id = jQuery('#' + column_id).attr('data-order');
    var column_id_new = column_id.replace('main_', '');
    var is_preview = jQuery(object).parents('.arp_template_main_container').attr('data-table-preview');
    var is_enable_alytics = jQuery(object).parents('.arp_template_main_container').attr('data-enable-analytics');
    var ajaxurl_value = jQuery('#arp_ajaxurl').val();
    if (jQuery('#main_column_0').hasClass('maincaptioncolumn')) {
        var caption_column = 1;
    } else {
        var caption_column = 0;
    }

    if (is_preview == 0) {

        var patt1 = /^#*/g; 
        var result = re_url.match(patt1);
        if(result!=''){
            if(re_url!="#"){
                if(jQuery(re_url).length != 0){
                    jQuery('html,body').animate(
                        {scrollTop: jQuery(re_url).offset().top},'slow'
                    );    
                }
                return false;
            }
        }

        if (is_enable_alytics == 0) {
            arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
            return false;
        }

        var is_new_tab_window = false;
        if( is_new_tab == 1 || is_new_window == 1 ){
            is_new_tab_window = true;
            arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
        }


        if(jQuery(template_obj).attr('data-enable-loader') == 'arp_enable_loader'){
            jQuery(template_obj).find('.arp_template_loader_continer').addClass('arp_loader_activate');
            jQuery('html').addClass('arp_no_scroll');
        }

        if( jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).hasClass('arp_clicked_column') ){
            jQuery(template_obj).find('.arp_template_loader_continer').removeClass('arp_loader_activate');
            jQuery('html').removeClass('arp_no_scroll');
            return false;
        } else {
            jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).addClass('arp_clicked_column');
        }

        jQuery.ajax({
            url: ajaxurl_value,
            type: 'POST',
            data: 'action=arp_insert_plan_id&arp_template_id=' + template_id + '&arp_plan_id=' + plan_id + '&caption_column=' + caption_column,
            statusCode: {
                401: function() {
                    if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                        jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                        jQuery('html').removeClass('arp_no_scroll');
                    }
                    if( !is_new_tab_window ){
                        arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                        jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                    }
                },
                403: function() {
                    if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                        jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                        jQuery('html').removeClass('arp_no_scroll');
                    }
                    if( !is_new_tab_window ){
                        arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                        jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                    }
                },
                400: function() {
                    if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                        jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                        jQuery('html').removeClass('arp_no_scroll');
                    }
                    if( !is_new_tab_window ){
                        arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                        jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                    }
                },
                404: function() {
                    if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                        jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                        jQuery('html').removeClass('arp_no_scroll');
                    }
                    if( !is_new_tab_window ){
                        arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                        jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                    }
                },
                500: function() {
                    if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                        jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                        jQuery('html').removeClass('arp_no_scroll');
                    }
                    if( !is_new_tab_window ){
                        arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                        jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                    }
                }
            },
            success: function() {
                if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                    jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                    jQuery('html').removeClass('arp_no_scroll');
                }
                if( !is_new_tab_window ){
                    arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                    jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                }
            },
            error: function() {
                if(jQuery('.arp_template_loader_continer').hasClass('arp_loader_activate')){
                    jQuery('.arp_template_loader_continer').removeClass('arp_loader_activate');
                    jQuery('html').removeClass('arp_no_scroll');
                }
                if( !is_new_tab_window ){
                    arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window);
                    jQuery('.arp_template_main_container[data-arp-uniq-id="'+unique_id+'"] #'+column_id).removeClass('arp_clicked_column');
                }
            },
        });
    } else {
        if (is_script == 1) {
            jQuery(object).parent().find('.arp_paypal_form').find('form').submit();
            jQuery(object).parent().find('.arp_paypal_form').find('button').trigger('click');
            jQuery(object).parent().find('.arp_paypal_form').find('input[type="button"]').trigger('click');
            jQuery(object).parent().find('.arp_paypal_form').find('input[type="submit"]').trigger('click');
            if (jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href') != '#' && jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href') != 'javascript:void(0)' && typeof(jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href')) != 'undefined') {
                var a_href = jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href');
                if (a_href.charAt(0) != '#') {
                    location.href = a_href;
                } else {
                    jQuery(object).parent().find('.arp_paypal_form').find('a').trigger('click');
                }
            } else {
                jQuery(object).parent().find('.arp_paypal_form').find('a').trigger('click');
            }
        } else {
            var is_extra_post = jQuery(object).attr('data-is-post-variables');
            var post_variables = jQuery(object).attr('data-post-variables');
            if(post_variables!=undefined || post_variables!=null)
            {
                post_variables = post_variables.replace(/:/g, '=');
                post_variables = post_variables.replace(/;/g, '&');
                post_variables = post_variables.replace(/&+$/, '');
            }
            var hash_pattern = /(#)/g;
        
            if (re_url.indexOf(post_variables) < 0) {
                if (re_url != "#" && re_url != "" && !hash_pattern.test(re_url)) {
                    if (re_url.indexOf('?') > -1) {
                        if (post_variables != '') re_url = re_url + '&' + post_variables;
                        else re_url = re_url;
                    } else {
                        if (post_variables != '') re_url = re_url + '?' + post_variables;
                        else re_url = re_url;
                    }
                } else if( hash_pattern.test(re_url) || re_url.indexOf('#') > -1 ){
                    var new_re_url = re_url.split('#');
                    var n_re_url = new_re_url[0];
                    var n_hs_url = new_re_url[1];
                    if (re_url.indexOf('?') > -1) {
                        if (post_variables != ''){
                            re_url = n_re_url + '&' + post_variables + '#' + n_hs_url;
                        } else {
                            re_url = n_re_url + '#' + n_hs_url;
                        } 
                    } else {
                        if (post_variables != '' && re_url!='#'){
                            re_url = n_re_url + '?' + post_variables + '#' + n_hs_url;
                        } else {
                            re_url = n_re_url + '#' + n_hs_url;
                        }
                    }
                }
            }

            var hash_link = false;
            var qry_str_pattern = /^\?/;

            if (!/^https?:\/\//i.test(re_url) && re_url != "#" && re_url != "") {
                if(qry_str_pattern.test(re_url.trim())){
                    hash_link = true;
                    re_url = "#";    
                } else{
                    re_url = "http://" + re_url;    
                }
            }
            
            if (is_new_tab == '1' || (is_new_window == '1' && is_new_tab == '1')) {} else if (is_new_window == '1' && is_new_tab != '1') {} else {
                location.href = re_url;
            }
        }
    }
}

function arprice_redirection_handler(is_script, object, re_url, is_new_tab, is_new_window) {

    if (is_script == 1) {     
        var indexButton = jQuery(object).parent().find('.arp_paypal_form').find('button').first().index();
        var indexForm = jQuery(object).parent().find('.arp_paypal_form').find('form').index();
        var indexa = jQuery(object).parent().find('.arp_paypal_form').find('a').first().index();
        var indexInput = jQuery(object).parent().find('.arp_paypal_form').find('input[type="button"]').first().index();
        var link_pattern = /javascript:void\(0\)(|;)/gi;

        if (indexa == -1) {
            if (indexButton < indexForm && indexButton > -1) {
                jQuery(object).parent().find('.arp_paypal_form').find('button').first().trigger('click');
            } else if (indexForm > -1 && indexForm < indexButton) {
                jQuery(object).parent().find('.arp_paypal_form').find('form').submit();
            } else if (indexButton == -1 && indexForm > -1) {
                jQuery(object).parent().find('.arp_paypal_form').find('form').submit();
            } else if (indexButton > -1 && indexForm == -1) {
                jQuery(object).parent().find('.arp_paypal_form').find('button').first().trigger('click');
            } else if( indexForm == -1 && indexInput > -1 ){
                jQuery(object).parent().find('.arp_paypal_form').find('input[type="button"]').first().trigger('click');
            } else {
                let elm = jQuery(object).parent().find('.arp_paypal_form').find('*[onclick]').first();
                if( elm.length > 0 ){
                    elm.trigger('click');
                }
            }
        } else if (typeof(jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href')) != 'undefined' && jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href') != '#' && !link_pattern.test(jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href'))) {
            var a_href = jQuery(object).parent().find('.arp_paypal_form').find('a').attr('href');
            var is_wc_button = /(add\-to\-cart\=(\d+))/gi.test(a_href);
            var isAjaxProduct = false;
            if( jQuery(object).parent().find('.arp_paypal_form').find('a').hasClass('ajax_add_to_cart') ){
                isAjaxProduct = true;
            }
            if (is_wc_button) {

                if( isAjaxProduct ){
                    jQuery(object).parent().find('.arp_paypal_form').find('a').trigger('click');
                } else {
                    location.href = a_href;
                }
            } else {
                if (a_href.charAt(0) != '#') {
                    location.href = a_href;
                } else {
                    jQuery(object).parent().find('.arp_paypal_form').find('a').trigger('click');
                }
            }
        } else {
            jQuery(object).parent().find('.arp_paypal_form').find('a').trigger('click');
        }
    } else {
        var is_extra_post = jQuery(object).attr('data-is-post-variables');
        var post_variables = jQuery(object).attr('data-post-variables');
        if(post_variables!=undefined || post_variables!=null){
            post_variables = post_variables.replace(/:/g, '=');
            post_variables = post_variables.replace(/;/g, '&');
            post_variables = post_variables.replace(/&+$/, '');
        }

        var hash_pattern = /(#)/g;

        if (re_url.indexOf(post_variables) < 0) {
            
            if (re_url != "#" && re_url != "" && !hash_pattern.test(re_url)) {
                if (re_url.indexOf('?') > -1) {
                    if (post_variables != '') re_url = re_url + '&' + post_variables;
                    else re_url = re_url;
                } else {
                    if (post_variables != '') re_url = re_url + '?' + post_variables;
                    else re_url = re_url;
                }
            } else if( hash_pattern.test(re_url) || re_url.indexOf('#') > -1 ){
                
                var new_re_url = re_url.split('#');
                var n_re_url = new_re_url[0];
                var n_hs_url = new_re_url[1];
                if (re_url.indexOf('?') > -1) {
                    if (post_variables != ''){
                        re_url = n_re_url + '&' + post_variables + '#' + n_hs_url;
                        
                    } else {
                        re_url = n_re_url + '#' + n_hs_url;
                        
                    } 
                } else {
                    var btn_link = re_url;
                    
                    if (post_variables != ''){
                        if(btn_link=='#'){
                            re_url = btn_link;
                        }else{
                            re_url = n_re_url + '?' + post_variables + '#' + n_hs_url;        
                        }
                    } 
                    else {
                        re_url = n_re_url + '#' + n_hs_url;
                    }
                }
            }
        }
        if (!/^https?:\/\//i.test(re_url) && re_url != "#" && re_url != "") {
            re_url = "http://" + re_url;
        }
        if (is_new_tab == '1' || (is_new_window == '1' && is_new_tab == '1')) {
            var win = window.open(re_url, '_blank');
            win.focus();
        } else if (is_new_window == '1' && is_new_tab != '1') {
            var win = window.open(re_url, '', 'width = ' + jQuery(window).width() + ', height = ' + jQuery(window).height() + ', scrollbars = yes');
            win.focus();
        } else {
            location.href = re_url;
        }
    }
}