/* javascript goes here: all javascript has to be imported through this file
 * Javascript code should be broken out into seperate files and imported here
 */
import $ from 'jquery';
import 'slick-carousel';
import 'jquery-mousewheel';
import 'malihu-custom-scrollbar-plugin';
import 'bootstrap';

// html integration 
$(function () {
    initTabs();
    initMobileNav();

    // From HEAD of main layout-- TODO: Categorize all of this
    $( `#wppb-edit-user` ).on( `submit`, function() {      
        if ($(`#passw1`).val() == ``) {
            $(`#passw1`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p class="alert" id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#passw1`).focus();
            return false;
        }
        else {
            $(`#passw1`).css(`border-color`, ``);
        }   

        if ($(`#passw2`).val() == ``) {
            $(`#passw2`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#passw2`).focus();
            return false;
        }
        else {
            $(`#passw2`).css(`border-color`, ``);
        }
        
        var password1 = $(`#passw1`).val();
        var password2 = $(`#passw2`).val();
        if(password1 == password2) {                
           $(`#custom_error`).html(`<p class="alert" id="wppb_form_success_message">Your profile has been successfully updated!</p>`);    
           setTimeout(function() {}, 2000);
        }
        else {
            $(`#custom_error`).html(`<p id="wppb_general_top_error_message">The passwords do not match!</p>`);  
            return false;
        }
    });
    
    $(`#wppb-register-user`).on(`submit`, function() {      
        if ($(`#email`).val() == ``) {
            $(`#email`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p class="alert" id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#email`).focus();
            return false;
        }
        else {
            $(`#email`).css(`border-color`, ``);
        }   

        if ($(`#username`).val() == ``) {
            $(`#username`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#username`).focus();
            return false;
        }
        else {
            $(`#username`).css(`border-color`, ``);
        }
        if ($(`#passw1`).val() == ``) {
            $(`#passw1`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p class="alert" id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#passw1"`).focus();
            return false;
        }
        else {
            $(`#passw1`).css(`border-color`, ``);
        }   

        if ($(`#passw2`).val() == ``) {
            $(`#passw2`).css(`border-color`, `red`);
            $(`#custom_error`).html(`<p id="wppb_general_top_error_message">Fill required field!</p>`);
            $(`#passw2`).focus();
            return false;
        }
        else {
            $(`#passw2`).css(`border-color`, ``);
        }
        var password1 = $(`#passw1`).val();
        var password2 = $(`#passw2`).val();
        if(password1 == password2) {                
           $(`#custom_error`).html(`<p class="alert" id="wppb_form_success_message">Your profile has been successfully updated!</p>`);    
           setTimeout(function() {}, 2000);
        }
        else {
            $(`#custom_error`).html(`<p id="wppb_general_top_error_message">The passwords do not match!</p>`);  
            return false;
        }
    });
    
    $(`#wppb-recover-password`).on(`submit`, function() { 
        if ($(`#username_email`).val() == ``) {
            $(`#username_email`).css(`border-color`, `red`);
            $(`#username_email`).focus();
            return false;
        }
        else {
            $(`#username_email`).css(`border-color`, ``);
        }           
    });

    // FROM FOOTER
    $(window).on(`load`, function(){
        $(`#visual`).mCustomScrollbar({
            axis: `x`,
            advanced:{autoExpandHorizontalScroll:true}
        });
    });

});

$(document).on(`click`, `.search-area__submit`, function() {
  $(`.search-slide`).removeClass(`open_search_form`);
});

$(`.default_field_email `).removeClass(`default_field_email`);
$(`.default_field_username `).removeClass(`default_field_username`);
$(`.default_field_firstname `).removeClass(`default_field_firstname`);
$(`.default_field_lastname `).removeClass(`default_field_lastname`);
$(`.default_field_nickname `).removeClass(`default_field_nickname`);

// mobile menu init
function initMobileNav() {
    $(`body`).mobileNav({
        menuActiveClass: `search-active`,
        menuOpener: `.search-opener`,
        hideOnClickOutside: true,
        menuDrop: `.search-wrap`
    });
}

// content tabs init
function initTabs() {
    $(`.tabset`).tabset({
        tabLinks: `.tab-opener`,
        addToParent: true,
        defaultTab: true
    });
}

/*
 * Simple Mobile Navigation
 */
(function () {
    function MobileNav(options) {
        this.options = $.extend({
            container: null,
            hideOnClickOutside: false,
            menuActiveClass: `nav-active`,
            menuOpener: `.nav-opener`,
            menuDrop: `.nav-drop`,
            toggleEvent: `click`,
            outsideClickEvent: `click touchstart pointerdown MSPointerDown`
        }, options);
        this.initStructure();
        this.attachEvents();
    }

    MobileNav.prototype = {
        initStructure: function () {
            this.page = $(`html`);
            this.container = $(this.options.container);
            this.opener = this.container.find(this.options.menuOpener);
            this.drop = this.container.find(this.options.menuDrop);
        },
        attachEvents: function () {
            var self = this;

            if (activateResizeHandler) {
                activateResizeHandler();
                activateResizeHandler = null;
            }

            this.outsideClickHandler = function (e) {
                if (self.isOpened()) {
                    var target = $(e.target);
                    if (!target.closest(self.opener).length && !target.closest(self.drop).length) {
                        self.hide();
                    }
                }
            };

            this.openerClickHandler = function (e) {
                e.preventDefault();
                self.toggle();
            };

            this.opener.on(this.options.toggleEvent, this.openerClickHandler);
        },
        isOpened: function () {
            return this.container.hasClass(this.options.menuActiveClass);
        },
        show: function () {
            this.container.addClass(this.options.menuActiveClass);
            if (this.options.hideOnClickOutside) {
                this.page.on(this.options.outsideClickEvent, this.outsideClickHandler);
            }
        },
        hide: function () {
            this.container.removeClass(this.options.menuActiveClass);
            if (this.options.hideOnClickOutside) {
                this.page.off(this.options.outsideClickEvent, this.outsideClickHandler);
            }
        },
        toggle: function () {
            if (this.isOpened()) {
                this.hide();
            } else {
                this.show();
            }
        },
        destroy: function () {
            this.container.removeClass(this.options.menuActiveClass);
            this.opener.off(this.options.toggleEvent, this.clickHandler);
            this.page.off(this.options.outsideClickEvent, this.outsideClickHandler);
        }
    };

    var activateResizeHandler = function () {
        var win = $(window),
            doc = $(`html`),
            resizeClass = `resize-active`,
            flag, timer;
        var removeClassHandler = function () {
            flag = false;
            doc.removeClass(resizeClass);
        };
        var resizeHandler = function () {
            if (!flag) {
                flag = true;
                doc.addClass(resizeClass);
            }
            clearTimeout(timer);
            timer = setTimeout(removeClassHandler, 500);
        };
        win.on(`resize orientationchange`, resizeHandler);
    };

    $.fn.mobileNav = function (opt) {
        var args = Array.prototype.slice.call(arguments);
        var method = args[0];

        return this.each(function () {
            var $container = $(this);
            var instance = $container.data(`MobileNav`);

            if (typeof opt === `object` || typeof opt === `undefined`) {
                $container.data(`MobileNav`, new MobileNav($.extend({
                    container: this
                }, opt)));
            } else if (typeof method === `string` && instance) {
                if (typeof instance[method] === `function`) {
                    args.shift();
                    instance[method].apply(instance, args);
                }
            }
        });
    };
}($));

/*
 * $ Tabs plugin
 */

(function () {

    `use strict`;

    function Tabset($holder, options) {
        this.$holder = $holder;
        this.options = options;

        this.init();
    }

    Tabset.prototype = {
        init: function () {
            this.$tabLinks = this.$holder.find(this.options.tabLinks);

            this.setStartActiveIndex();
            this.setActiveTab();

            if (this.options.autoHeight) {
                this.$tabHolder = $(this.$tabLinks.eq(0).attr(this.options.attrib)).parent();
            }

            this.makeCallback(`onInit`, this);
        },

        setStartActiveIndex: function () {
            var $classTargets = this.getClassTarget(this.$tabLinks);
            var $activeLink = $classTargets.filter(`.` + this.options.activeClass);
            var $hashLink = this.$tabLinks.filter(`[` + this.options.attrib + `="` + location.hash + `"]`);
            var activeIndex;

            if (this.options.checkHash && $hashLink.length) {
                $activeLink = $hashLink;
            }

            activeIndex = $classTargets.index($activeLink);

            this.activeTabIndex = this.prevTabIndex = (activeIndex === -1 ? (this.options.defaultTab ? 0 : null) : activeIndex);
        },

        setActiveTab: function () {
            var self = this;

            this.$tabLinks.each(function (i, link) {
                var $link = $(link);
                var $classTarget = self.getClassTarget($link);
                var $tab = $($link.attr(self.options.attrib));

                if (i !== self.activeTabIndex) {
                    $classTarget.removeClass(self.options.activeClass);
                    $tab.addClass(self.options.tabHiddenClass).removeClass(self.options.activeClass);
                } else {
                    $classTarget.addClass(self.options.activeClass);
                    $tab.removeClass(self.options.tabHiddenClass).addClass(self.options.activeClass);
                }

                self.attachTabLink($link, i);
            });
        },

        attachTabLink: function ($link, i) {
            var self = this;

            $link.on(this.options.event + `.tabset`, function (e) {
                e.preventDefault();

                if (self.activeTabIndex === self.prevTabIndex && self.activeTabIndex !== i) {
                    self.activeTabIndex = i;
                    self.switchTabs();
                }
            });
        },

        resizeHolder: function (height) {
            var self = this;

            if (height) {
                this.$tabHolder.height(height);
                setTimeout(function () {
                    self.$tabHolder.addClass(`transition`);
                }, 10);
            } else {
                self.$tabHolder.removeClass(`transition`).height(``);
            }
        },

        switchTabs: function () {
            var self = this;

            var $prevLink = this.$tabLinks.eq(this.prevTabIndex);
            var $nextLink = this.$tabLinks.eq(this.activeTabIndex);

            var $prevTab = this.getTab($prevLink);
            var $nextTab = this.getTab($nextLink);

            $prevTab.removeClass(this.options.activeClass);

            if (self.haveTabHolder()) {
                this.resizeHolder($prevTab.outerHeight());
            }

            setTimeout(function () {
                self.getClassTarget($prevLink).removeClass(self.options.activeClass);

                $prevTab.addClass(self.options.tabHiddenClass);
                $nextTab.removeClass(self.options.tabHiddenClass).addClass(self.options.activeClass);

                self.getClassTarget($nextLink).addClass(self.options.activeClass);

                if (self.haveTabHolder()) {
                    self.resizeHolder($nextTab.outerHeight());

                    setTimeout(function () {
                        self.resizeHolder();
                        self.prevTabIndex = self.activeTabIndex;
                        self.makeCallback(`onChange`, self);
                    }, self.options.animSpeed);
                } else {
                    self.prevTabIndex = self.activeTabIndex;
                }
            }, this.options.autoHeight ? this.options.animSpeed : 1);
        },

        getClassTarget: function ($link) {
            return this.options.addToParent ? $link.parent() : $link;
        },

        getActiveTab: function () {
            return this.getTab(this.$tabLinks.eq(this.activeTabIndex));
        },

        getTab: function ($link) {
            return $($link.attr(this.options.attrib));
        },

        haveTabHolder: function () {
            return this.$tabHolder && this.$tabHolder.length;
        },

        destroy: function () {
            var self = this;

            this.$tabLinks.off(`.tabset`).each(function () {
                var $link = $(this);

                self.getClassTarget($link).removeClass(self.options.activeClass);
                $($link.attr(self.options.attrib)).removeClass(self.options.activeClass + ` ` + self.options.tabHiddenClass);
            });

            this.$holder.removeData(`Tabset`);
        },

        makeCallback: function (name) {
            if (typeof this.options[name] === `function`) {
                var args = Array.prototype.slice.call(arguments);
                args.shift();
                this.options[name].apply(this, args);
            }
        }
    };

    $.fn.tabset = function (opt) {
        var args = Array.prototype.slice.call(arguments);
        var method = args[0];

        var options = $.extend({
            activeClass: `active`,
            addToParent: false,
            autoHeight: false,
            checkHash: false,
            defaultTab: true,
            animSpeed: 500,
            tabLinks: `a`,
            attrib: `href`,
            event: `click`,
            tabHiddenClass: `js-tab-hidden`
        }, opt);
        options.autoHeight = options.autoHeight && $.support.opacity;

        return this.each(function () {
            var $holder = $(this);
            var instance = $holder.data(`Tabset`);

            if (typeof opt === `object` || typeof opt === `undefined`) {
                $holder.data(`Tabset`, new Tabset($holder, options));
            } else if (typeof method === `string` && instance) {
                if (typeof instance[method] === `function`) {
                    args.shift();
                    instance[method].apply(instance, args);
                }
            }
        });
    };
}($, $(window)));


$(function ($) {
    $(`#filter`).submit(function () {
        var filter = $(`#filter`);
        var button = $(`input[type=submit][clicked=true]`).val();
        $.ajax({
            url: filter.attr(`action`),
            data: filter.serialize() + `&onClick=` + button, // form data
            type: filter.attr(`method`), // POST
            dataType: `json`,
            beforeSend: function () {
                $(`#message`).text(`Processing...`); // changing the button label
            },
            success: function (data) {
                if (data.clicked == `filter`) {
                    $(`#message`).text(``); // changing the button label back
                    $(`#response`).html(data.data.part_number);
					$(`#dataSheet`).attr(`href`, data.data.permalink);
                    if (data.data.image) {
                        $(`.image_area img`).attr(`src`, data.data.image);
                        $(`.image_area img`).attr(`srcset`, data.data.image);
                    }
					if (data.data.three_dlink) {
                        $(`#three-dfile`).attr(`href`, data.data.three_dlink);
                    }else {
                        $(`#three-dfile`).hide();
                    }
                }
                else if (data.status && data.clicked == `data_sheet`) {
                    $(`#message`).text(``);
                    if (data.url != ``) {
                        window.open(data.url, `_blank`);
                    }
                    else {
                        $(`#response`).html(`There is no data sheet available.`);
                    }
                } else if (data.status && data.clicked == `req_quote`) {
                    if (data.data == ``) {
                        alert(`No result Found for Quote.`)
                    } else {
                        var output = $.parseHTML(data.data);
                        $(`#input_2_15`).val(output[`0`][`data`]);
                        $(`#myModal`).modal(`show`);
                    }
                    $(`#message`).text(``);
                } else if (data.clicked == `save`) {
                    if (data.data == ``) {
                        $(`#mylogin`).modal(`show`);

                    }
                    $(`#message`).text(``); // changing the button label back
                    if ( typeof(data.url) != `undefined` && data.url !=``) {
                     $(location).attr(`href`, data.url+`?sucess=1`);
                    }
                    $(`#response`).html(data.data);
                } else {
                    $(`#message`).text(``); // changing the button label back
                    $(`#response`).html(data.data); // insert data
                }
                $(`.action_buttons`).removeClass(`d-none`);
				$(`.image_area`).removeClass(`d-none`);
            }
        });
        return false;
    });

    $(`form input[type=submit]`).click(function () {
        $(`input[type=submit]`, $(this).parents(`form`)).removeAttr(`clicked`);
        $(this).attr(`clicked`, `true`);
    });
});

function detectIE() {
  var ua = window.navigator.userAgent;
  var msie = ua.indexOf(`MSIE `);
  if (msie > 0) {
    // IE 10 or older => return version number
    return parseInt(ua.substring(msie + 5, ua.indexOf(`.`, msie)), 10);
  }

  var trident = ua.indexOf(`Trident/`);
  if (trident > 0) {
    // IE 11 => return version number
    var rv = ua.indexOf(`rv:`);
    return parseInt(ua.substring(rv + 3, ua.indexOf(`.`, rv)), 10);
  }

  var edge = ua.indexOf(`Edge/`);
  if (edge > 0) {
    // Edge (IE 12+) => return version number
    return parseInt(ua.substring(edge + 5, ua.indexOf(`.`, edge)), 10);
  }

  // other browser
  return false;
}

function generate_ui() {
    var countSelects = 3;
    $(`.cpv_box_filter`).each(function () {
        var uniquevalue = $(this).parent().attr(`class`);
        var uniquevalueset = `set_` + $(this).parent().attr(`class`);
        var uniquename = `name_` + $(this).parent().attr(`class`);
        var class_name = `choose_options list-unstyled dropdown-data d-flex flex-row flex-wrap generated`;
        var dataparent = `#accordion`;
        var collapse = `#collapse`;
        var datatoggle = `collapse`;
        $(`.` + uniquevalue + ` select`).parent().append(`<ul class="` + class_name + `" id="` + uniquevalueset + `" name="` + uniquename + `"></ul>`);

        $(`.` + uniquevalue + ` option`).each(function () {
            $(`#` + uniquevalueset).append(`<a data-toggle="` + datatoggle + `" data-parent="` + dataparent + `" href="` + collapse + countSelects + `"><div class="img-holder"><li value="` + $(this).val() + `"><div class="value-radio"><input type='radio' name='tool'/></div><span><img class="dropdown-data__img" src="` + $(this).attr(`image`) + `" alt="image-description"></span></li></div><span class="dropdown-data__title"><li value="` + $(this).val() + `"><span><span class="dropdown-data__stitle">` + $(this).text() + `</span><span class="dropdown-data__description">` + $(this).attr(`description`) + `</span></span></span></li></a>`);
        });

        countSelects++;
    });

    $(`.cpv_filter`).each(function () {
        var uniquevalue = $(this).parent().attr(`class`);
        var uniquevalueset = `set_` + $(this).parent().attr(`class`);
        var uniquename = `name_` + $(this).parent().attr(`class`);
        var class_name = `choose_options list-unstyled products-info generated`;
        var dataparent = `#accordion`;
        var collapse = `#collapse`;
        var datatoggle = `collapse`;
        $(`.` + uniquevalue + ` select`).parent().append(`<ul class="` + class_name + `" id="` + uniquevalueset + `" name="` + uniquename + `"></ul>`);

        $(`.` + uniquevalue + ` option`).each(function () {
            if(($(this).attr(`image`)==``||$(this).attr(`image`)==undefined) && ($(this).attr(`description`)==``||$(this).attr(`description`)==undefined)){
                $(`#` + uniquevalueset).append(`<a data-toggle=` + datatoggle + ` data-parent=` + dataparent + ` href=` + collapse + countSelects + `><li value='` + $(this).val() + `'><span class="dropdown-data__stitle">` + $(this).text() + `</span></li></a>`);
            }
            if($(this).attr(`image`)!==`` && $(this).attr(`description`)!==``){
                if($(this).attr(`image`)!==undefined && $(this).attr(`description`)!==undefined){
                    $(`#` + uniquevalueset).append(`<a data-toggle=` + datatoggle + ` data-parent=` + dataparent + ` href=` + collapse + countSelects + `><li value='` + $(this).val() + `'><div class="value-radio"><input type='radio' name='tool'/></div><img class="dropdown-data__img1" src="` + $(this).attr(`image`) + `" alt="image-description"><span class="dropdown-data__stitle">` + $(this).text() + `</span><span class="dropdown-data__description">` + $(this).attr(`description`) + `</span></li></a>`);
                }
            }
            if($(this).attr(`image`)==`` && $(this).attr(`description`)!==``){
                $(`#` + uniquevalueset).append(`<a data-toggle=` + datatoggle + ` data-parent=` + dataparent + ` href=` + collapse + countSelects + `><li value='` + $(this).val() + `'><div class="value-radio"><input type='radio' name='tool'/></div><span class="dropdown-data__stitle">` + $(this).text() + `</span><span class="dropdown-data__description">` + $(this).attr(`description`) + `</span></li></a>`);
            }
            if($(this).attr(`image`)!==`` && $(this).attr(`description`)==``){
                $(`#` + uniquevalueset).append(`<a data-toggle=` + datatoggle + ` data-parent=` + dataparent + ` href=` + collapse + countSelects + `><li value='` + $(this).val() + `'><div class="value-radio"><input type='radio' name='tool'/></div><img class="dropdown-data__img1" src="` + $(this).attr(`image`) + `" alt="image-description"><span class="dropdown-data__stitle">` + $(this).text() + `</span></li></a>`);
            }
            
        });

        countSelects++;
    });

    $(`select`).hide();
}

$(document).ready(function () {
    generate_ui();
    $(`body`).on(`click`, `#filter ul li`, function () {
        var value = ``;
		var selectedTitle = ``;
        if ($(this).closest(`.valvetype`).hasClass(`valvetype`)) {
            value = $(this).attr(`value`);
        } else if ($(this).closest(`.fittingtype`).hasClass(`fittingtype`)) {
            value = $(this).attr(`value`);
        }
        else {
            value = $(this).attr(`value`);
			selectedTitle = $(this).children(`.dropdown-data__stitle`).text();
        }
        var selected_value = value;
		if (selected_value == `` || (selectedTitle != `` && selectedTitle != selected_value))
		{
			selected_value = selectedTitle;
		}
        var container = $(this).parent().parent().parent().attr(`class`);
        var st = container.split(` `);
        var firstClass = st[0];
        if (firstClass == `choose_options`) {
            container = $(this).parent().parent().parent().parent().attr(`class`);
        }
        $(`.` + container + ` select`).val(selected_value).trigger(`change`);
    });
});

$(document).on(`change`, `.last select`, function () {
    $(`.filter_search`).click();
	$(`.repeater_closeit.last .products-info__list`).trigger(`click`);
});

$(document).on(`click`, `#btn_ok`, function () {
	location. reload(true);
});
if(detectIE() == false) {
$(document).on(`click`, `.repeater_closeit`, function () {
	$(`.choose_options`).css(`opacity`, `0`);
	setTimeout(function(){
	$(`.choose_options`).css(`opacity`, `1`);
	}, 1000);
	
});
}
$(document).on(`change`, `#filter select`, function () {
	if(!$(`.image_area`).find(`.action_buttons`).hasClass(`d-none`)){		
		$(`.image_area`).addClass(`d-none`);
	}
	if(detectIE() == false) {
		$(`.choose_options`).css(`opacity`, `0`);
	}
	$(`#start_over`).removeClass(`start_over_btn_hide`);
	$(`#start_over`).addClass(`start_over_btn`);
	$(this).nextAll().attr(`disabled` , true);
	var get_closet= $(this).closest(`.repeater_closeit`);
	var current_closet = get_closet.attr(`class`);
	var ste = current_closet.split(` `);
	var diabledcontainer=``;
	diabledcontainer = ste[0];
	
	var selectedId  = $(this).attr(`id`);
	var selectedText  =$(`#`+selectedId+` option:selected`).text();
	
	$(`.` + diabledcontainer).find(`.selected_value`).next(`span`).text( `: ` + selectedText);
	
	$(`.`+diabledcontainer).nextAll().find(`select`).attr(`disabled`, true);
	var next_container = $(this).closest(`.repeater_closeit`).next().attr(`class`);
    if (typeof next_container !== `undefined`) {
        var st = next_container.split(` `);
		var firstClass = st[0];
        $(`.` + firstClass).removeClass(`d-none`);
        var filter = $(`#filter`);
        var opteration_on = $(`.` + firstClass + ` select`).attr(`name`);
        var action = `get_terms_for_cpt`;
        var post_type = `products`;
        var data = {
            action: action,
            post_type: post_type,
            opteration_on: opteration_on,
            current_selected: $(`#filter :input[value!='']`).serialize()
		};
		var counter_reoccure=``;
        var recursive = ``;        
		counter_reoccure=1;
		(function recursiveRequest(dataarray,containerclass){
			counter_reoccure++;
			var i_counter;
			var get_close=``;
			
			for(i_counter=1;i_counter<counter_reoccure;i_counter++)
			{	
				if(get_close!=``){
					get_close = get_close.next();
				}else{
					get_close = get_closet.next();
				}
			}
			var nextsecond_container = get_close.attr(`class`);
			
			$(`.` + containerclass).removeClass(`d-none`);
			$.ajax({
				url: filter.attr(`action`),
				data: dataarray,
				type: `POST`,
				success: function success(output) {
				if(detectIE() == false) {
					$(`.choose_options`).css(`opacity`, `1`);
				}
					if(output==``){
						$(`.` + containerclass).addClass(`d-none`);
						$(`.` + containerclass + ` select`).prop(`disabled`, true);
						if (typeof nextsecond_container !== `undefined`) {
							var st = nextsecond_container.split(` `);
							var secondClass = st[0];
							var opteration_on = $(`.` + secondClass + ` select`).attr(`name`);
							var action = `get_terms_for_cpt`;
							var post_type = `products`;
							var data_newarray = {
								action: action,
								post_type: post_type,
								opteration_on: opteration_on,
								current_selected: $(`#filter :input[value!='']`).serialize()
							};
							recursive = 1;
							recursiveRequest(data_newarray,secondClass);
							
						}
					}else{
					if(recursive==1){
						$(`.`+containerclass+` .collapsed`).trigger(`click`);
					}
					
					$(`.` + containerclass + ` select`).prop(`disabled`, false);
					var replace_container = $(`.` + containerclass + ` select`).parent().attr(`class`);
					$(`.` + replace_container).html(output);
					
					
					
					$(`.generated`).remove();
					generate_ui();
					}
				}
			});
		})(data,firstClass);

        
    }
});

$(document).ready(function () {
	if($(`.image_area`).find(`.action_buttons`).hasClass(`d-none`)){
		$(`.image_area`).addClass(`d-none`);
	}
    var urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has(`success`)) {
        var filter = $(`#filter`);
        $.ajax({
            url: filter.attr(`action`),
            data: `action=filterProducts&onClick=Save`, // form data
            type: `post`, // POST
            dataType: `json`,
            success: function (data) {
                if (data.data == ``) {
                    $(`.login_form`).click();
                }
                $(`#response`).html(data.data);
            }
        });
    }
});


$(`.login_modal`).on(`click`,function(){
    $(`#mylogin`).modal(`show`);
});

$(`.search-wrap`).on(`click`,function(e){
    $(`.search-slide`).toggleClass(`open_search_form`);
    setTimeout(function() { $(`.search-area__form-control`).focus() }, 1000);
    e.stopPropagation();
});
$(document).on(`click`, function(e) {
    if ($(e.target).is(`.search-slide,.search-area,.search-area__serach-field,.search-area__form-control,.search-area__submit`) === false) {
        $(`.search-slide`).removeClass(`open_search_form`);
    }
});
$(`.forget_password_btn`).on(`click`, function(e) {
    e.stopPropagation();
    $(`.login_form`).addClass(`d-none`);
    $(`.recover_password`).removeClass(`d-none`);
});
$(`.register_btn`).on(`click`, function(e) {
    e.stopPropagation();
    $(`.login_form`).addClass(`d-none`);
    $(`.register`).removeClass(`d-none`);
});
$(`.login_btn`).on(`click`, function(e) {
    e.stopPropagation();
    $(`.login_form`).removeClass(`d-none`);
    $(`.register`).addClass(`d-none`);
    $(`.recover_password`).addClass(`d-none`);
});
$(`#mylogin`).on(`hidden.bs.modal`, function () {
    $(`.login_form`).removeClass(`d-none`);
    $(`.register`).addClass(`d-none`);
    $(`.recover_password`).addClass(`d-none`);
})
$(`.filter_search`).on(`click`, function(e) {
    e.stopPropagation();
    var pid = $(this).data(`id`);
	submit_form(`#form_`+pid, $(this).val());
});

function submit_form(frm, btn)
{
	//$(frm).submit();
        var filter = $(frm);
        var button = btn
        $.ajax({
            url: filter.attr(`action`),
            data: filter.serialize() + `&onClick=` + button, // form data
            type: filter.attr(`method`), // POST
            dataType: `json`,
            beforeSend: function () {
                $(`#message`).text(`Processing...`); // changing the button label
            },
            success: function (data) {
                if (data.clicked == `filter`) {
                    $(`#message`).text(``); // changing the button label back
                    $(`#response`).html(data.data.part_number);
					$(`#dataSheet`).attr(`href`, data.data.permalink);
                    if (data.data.image) {
                        $(`.image_area img`).attr(`src`, data.data.image);
                        $(`.image_area img`).attr(`srcset`, data.data.image);
                    }
					if (data.data.three_dlink) {
                        $(`#three-dfile`).attr(`href`, data.data.three_dlink);
                    }else {
                        $(`#three-dfile`).hide();
                    }
                }
                else if (data.status && data.clicked == `data_sheet`) {
                    $(`#message`).text(``);
                    if (data.url != ``) {
                        window.open(data.url, `_blank`);
                    }
                    else {
                        $(`#response`).html(`There is no data sheet available.`);
                    }
                } else if (data.status && data.clicked == `req_quote`) {
                    if (data.data == ``) {
                        alert(`No result Found for Quote.`)
                    } else {
                        var output = $.parseHTML(data.data);
                        $(`#input_2_15`).val(output[`0`][`data`]);
                        $(`#myModal`).modal(`show`);
                    }
                    $(`#message`).text(``);
                } else if (data.clicked == `save`) {
                    if (data.data == ``) {
                        $(`#mylogin`).modal(`show`);

                    }
                    $(`#message`).text(``); // changing the button label back
                    if ( typeof(data.url) != `undefined` && data.url !=``) {
                     $(location).attr(`href`, data.url+`?sucess=1`);
                    }
                    $(`#response`).html(data.data);
                } else {
                    $(`#message`).text(``); // changing the button label back
                    $(`#response`).html(data.data); // insert data
                }
                $(`.action_buttons`).removeClass(`d-none`);
				$(`.image_area`).removeClass(`d-none`);
            }
        });
        return false;
}
