// source --> https://grupocmm.com/wp-content/plugins/dvin-wcql/js/dvin_wcql.js?ver=1.0.0 
jQuery( function( $ ) {

	//when variation_id changes, take appropriate action on quotelist button (show/hide)
	$('[name|="variation_id"]').change(function() {

		if($('[name|="variation_id"]').val()!='') {

			//find whether it is already exists in quotelist
			var data = {action: 'find_prod_in_qlist'};
			data['prod_id'] = $('[name|="product_id"]').val();
			data['variation_id'] = $('[name|="variation_id"]').val();

			$.post( dvin_wcql_ajax_url, data, function( response ) {

				if(response == "true") {

					$('.quotelistadd_prodpage').css('display','none');
					$('.quotelistexistsbrowse_prodpage').css('display','block');
					$('.quotelistaddedbrowse_prodpage').css('display','none');

				} else {

					$('.quotelistadd_prodpage').css('display','block');
					$('.quotelistexistsbrowse_prodpage').css('display','none');
					$('.quotelistaddedbrowse_prodpage').css('display','none');

				}

			});

			$('.quotelistaddresponse').html('');

			if(dvin_wcql_disable_insteadof_hide_button == "true")
				$('.addquotelistbutton_prodpage').removeClass( 'disabled' ).addClass( 'enabled' );
		} else {
			$('.quotelistadd_prodpage').css('display','block');
			$('.quotelistexistsbrowse_prodpage').css('display','none');
			$('.quotelistaddedbrowse_prodpage').css('display','none');

			if(dvin_wcql_disable_insteadof_hide_button == "true")
				$('.addquotelistbutton_prodpage').removeClass( 'enabled' ).addClass( 'disabled' );
			else
				$('.quotelistadd_prodpage').css('display','none');
				$('.quotelistexistsbrowse_prodpage').fadeOut();
		}

	});
	//Ajax remove from qlist
	$( document ).on( 'click', '.removeproductqlist', function(e) {
		var $thisbutton = $( this );
		if ( ! $thisbutton.attr( 'data-product_id' ) )
			return true;
        var data = {action: 'remove_from_qlist'};
        $.each( $thisbutton.data(), function( key, value ) {
            data[key] = value;
        });
        // Trigger event
        $( 'body' ).trigger( 'remove_from_qlist', [ $thisbutton, data ] );
        // Ajax action
        $.post( dvin_wcql_ajax_url, data, function( response ) {
            if ( ! response )
                return;
            $( this ).closest('tr').remove();
            location.reload(true);

	});
});
	// Ajax add to qlist from shop
	$( document ).on( 'click', '.addquotelistbutton_prodpage', function(e) {

			var $this = $( this );

			if ( $this.is('.disabled') ) {
				event.preventDefault();
				return false;
			}

        var data = {action: 'add_to_qlist_from_prodpage'};

        // AJAX add to cart request
		var $thisbutton = $( this );

         //check for the data attribute
        if ( ! $thisbutton.attr( 'data-product_id' ) )
         return true;

        //get the data into the array
        $.each( $thisbutton.data(), function( key, value ) {
        data[key] = value;
        });

        if($('form.cart').length){
            if($thisbutton.closest('form.cart').serialize()!='') {
                data['form_serialize_data']=$thisbutton.closest('form.cart').serialize();
            } else if($thisbutton.closest('.addquotelistlink').prev("form.cart").serialize()!='') {
    data['form_serialize_data']=$thisbutton.closest('.addquotelistlink').prev("form.cart").serialize();
            } else {
                data['form_serialize_data']=$('form.cart').serialize();
            }
        }

		//check if element(widget DIV) exists and no redirect...need refresh
		if(dvin_quotelist_refresh_afteradd_url !='') {
			data['widget_refresh'] = "false";
			data['count_refresh'] = "false";
		} else {
			if($('div#quotelist-widget').length >0)
				data['widget_refresh'] = "true";
			else
				data['widget_refresh'] = "false";
			if($('div#dvin-quotelist-count').length >0)
				data['count_refresh'] = "true";
			else
				data['count_refresh'] = "false";
		}
 		// Trigger event
		$( 'body' ).trigger( 'adding_to_qlist', [ $thisbutton, data ] );
		$thisbutton.parent().parent().parent().find('.ajax-loading-img').fadeIn();
		// Ajax action
		$.post( dvin_wcql_ajax_url, data, function( response ) {
			if ( !response )
				return;
			var loading = $('.ajax-loading-img');
			loading.fadeOut();
            fragments = response.fragments;
            // Replace fragments
            if ( fragments ) {
               $.each( fragments, function( key, value ) {
                        $( key ).replaceWith( value );
              });
            }

			//if refresh, no need to hide or display message
			if(dvin_quotelist_refresh_afteradd_url !='') {
				window.location = dvin_quotelist_refresh_afteradd_url;
			} else {

                $thisbutton.parent().parent().parent().children('.removefromprodpage').css('display','none');                //
                $thisbutton.parent().parent().parent().children('.quotelistaddedbrowse_prodpage').css('display','block');

                if(dvin_wcql_addons == '1') {
                    $('.quotelistaddedbrowse_prodpage').fadeOut(4000);
                } else {
                        $thisbutton.parent().parent().parent().children('.quotelistadd_prodpage').css('display','none');
                        $('a.removefromprodpage').attr("data-product_id",response.product_id);
                }
				return true;
			}
		});
		return false;
	});
	// Ajax add to qlist from shop
	$( document ).on( 'click', '.addquotelistbutton', function(e) {
		// AJAX add to cart request
		var $thisbutton = $( this );
		if ( $thisbutton.is( '.product_type_simple' ) || $thisbutton.is( '.product_type_external' )) {
            if ( ! $thisbutton.attr( 'data-product_id' ) )
				return true;
			var data = {action: 'add_to_qlist'};
			$.each( $thisbutton.data(), function( key, value ) {
				data[key] = value;
        	});
			//check if element(widget DIV) exists and no redirect...need refresh
			if(dvin_quotelist_refresh_afteradd_url !='') {
				data['widget_refresh'] = "false";
				data['count_refresh'] = "false";
			} else {
				if($('div#quotelist-widget').length >0)
					data['widget_refresh'] = "true";
				if($('div#dvin-quotelist-count').length >0)
					data['count_refresh'] = "true";
			}
            // Trigger event
			$( 'body' ).trigger( 'adding_to_qlist', [ $thisbutton, data ] );
			$('#'+$thisbutton.attr( 'data-product_id')).find('.ajax-loading-img').fadeIn();
			// Ajax action
			$.post( dvin_wcql_ajax_url, data, function( response ) {
				if ( ! response )
					return;

				var loading = $('#'+$thisbutton.attr( 'data-product_id')).children('.ajax-loading-img');
					loading.fadeOut();

				//if refresh, no need to hide or display message
				if(dvin_quotelist_refresh_afteradd_url =='') {

                    fragments = response.fragments;
                    // Replace fragments
                    if ( fragments ) {
                        $.each( fragments, function( key, value ) {
                            $( key ).replaceWith( value );
                        });
                    }

					$('#'+data['product_id']).children('.quotelistadd').css('display','none');
					$('#'+data['product_id']).children('.quotelistaddedbrowse').css('display','block');
				} else {
					window.location = dvin_quotelist_refresh_afteradd_url;
					return;
				}
			});
			return false;
		}
		return true;
	});
	// Ajax remove product from product page
	$( document ).on( 'click', '.removefromprodpage', function(e) {
		var data = {action: 'remove_from_page'};

        // AJAX add to cart request
		var $thisbutton = $( this );

        //check for the data attribute
        if ( ! $thisbutton.attr( 'data-product_id' ) )
         return true;

				 //get variation data
				 if($('[name|="variation_id"]').length && $('[name|="variation_id"]').val() >0) {
				 	var_id = $('[name|="variation_id"]').val();
				 	$thisbutton.attr( 'data-product_id', var_id);
				 }

        //get the data into the array
        $.each( $thisbutton.data(), function( key, value ) {
        data[key] = value;
        });



        if($('div#quotelist-widget').length >0)
            data['widget_refresh'] = "true";
        else
            data['widget_refresh'] = "false";
        if($('div#dvin-quotelist-count').length >0)
            data['count_refresh'] = "true";
        else
            data['count_refresh'] = "false";

 		// Trigger event
		$( 'body' ).trigger( 'remove_from_qlist', [ $thisbutton, data ] );
		// Ajax action
		$.post( dvin_wcql_ajax_url, data, function( response ) {
			if ( ! response )
				return;
                fragments = response.fragments;
                // Replace fragments
                if ( fragments ) {
                    $.each( fragments, function( key, value ) {
                        $( key ).replaceWith( value );
                    });
            }

			$thisbutton.parent().parent().children('.quotelistadd_prodpage').css('display','block');
			$thisbutton.parent().parent().children('.quotelistaddedbrowse_prodpage').css('display','none');
			$thisbutton.parent().parent().children('.quotelistexistsbrowse_prodpage').css('display','none');

		});
		return false;
	});

    //if list has some products
	if($('#dvin-quotelist-count').length>0) {
		//if DIV for count exists, update it
		$('#dvin-quotelist-count').html(dvin_quotelist_count);
	}
	if($('div#quotelist-widget').length>0) {
		refresh_quotelist_widget();
	}

});

function ajax_req_update_quote() {
    serialized_data = jQuery( "input[name^='cart']" ).serialize()+'&action=update_list';
    jQuery.ajax({
     type: 'POST',
      url: dvin_wcql_ajax_url,
      data: serialized_data,
      success: function( response ) {
        location.reload(true);
        }
    });
}
//handles ajax call post form
function call_ajax_submitform_to_admin(url) {

    var data = {action: 'send_request'};
    data['form_serialize_data']=jQuery('.qlist').serialize();

    if(data['form_serialize_data']=='')
        return;
    else
       jQuery('.ajax-loading-img').fadeIn();

        if(jQuery('div#quotelist-widget').length >0)
            data['widget_refresh'] = "true";
        else
            data['widget_refresh'] = "false";
        if(jQuery('div#dvin-quotelist-count').length >0)
            data['count_refresh'] = "true";
        else
            data['count_refresh'] = "false";
    // Ajax action
    jQuery.post( dvin_wcql_ajax_url, data, function( response ) {
        if ( ! response )
            return;
        var msg = jQuery('#dvin-message-popup');
        var loading = jQuery('.ajax-loading-img');
        loading.fadeOut();
        if(response.status == "success") {
            fragments = response.fragments;
            // Replace fragments
            if ( fragments ) {
                jQuery.each( fragments, function( key, value ) {
                    jQuery( key ).replaceWith( value );
                });
            }
            jQuery('.shop_table').remove();
            jQuery('#formtable').remove();
            jQuery('#dvin_wcql_details').css('display','none');
            jQuery('#dvin_wcql_success_msg').css('display','block');
            jQuery('#dvin_messages').html('&nbsp;');
            jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
            jQuery('#dvin_message').html('');
        } else if(response.status == "redirect"){
            window.location = response.redirect_url;
            return;
        } else if(response.status == "error") {
            jQuery('#dvin_messages').html(response.msg);
        }
        msg.fadeIn();
        window.setTimeout(function(){
           msg.fadeOut();
        }, 2000);
    });
}
function refresh_quotelist_widget() {

    var data = {action: 'wcql_widget_refresh'};
    data['widget_refresh'] = "true";
    data['count_refresh'] = "true";
    // Ajax action
    jQuery.post( dvin_wcql_ajax_url, data, function( response ) {
        if ( ! response )
            return;
            if(response.status == "success") {
                fragments = response.fragments;
                // Replace fragments
                if ( fragments ) {
                    jQuery.each( fragments, function( key, value ) {
                        jQuery( key ).replaceWith( value );
                    });
                }
            }
    });
};
// source --> https://grupocmm.com/wp-content/plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js?ver=2.7.0-wc.6.2.1 
/*!
 * jQuery blockUI plugin
 * Version 2.70.0-2014.11.23
 * Requires jQuery v1.7 or later
 *
 * Examples at: http://malsup.com/jquery/block/
 * Copyright (c) 2007-2013 M. Alsup
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Thanks to Amir-Hossein Sobhi for some excellent contributions!
 */
!function(){"use strict";function e(p){p.fn._fadeIn=p.fn.fadeIn;var b=p.noop||function(){},h=/MSIE/.test(navigator.userAgent),k=/MSIE 6.0/.test(navigator.userAgent)&&!/MSIE 8.0/.test(navigator.userAgent),y=(document.documentMode,"function"==typeof document.createElement("div").style.setExpression&&document.createElement("div").style.setExpression);p.blockUI=function(e){o(window,e)},p.unblockUI=function(e){v(window,e)},p.growlUI=function(e,t,o,n){var i=p('<div class="growlUI"></div>');e&&i.append("<h1>"+e+"</h1>"),t&&i.append("<h2>"+t+"</h2>"),o===undefined&&(o=3e3);var s=function(e){p.blockUI({message:i,fadeIn:"undefined"!=typeof(e=e||{}).fadeIn?e.fadeIn:700,fadeOut:"undefined"!=typeof e.fadeOut?e.fadeOut:1e3,timeout:"undefined"!=typeof e.timeout?e.timeout:o,centerY:!1,showOverlay:!1,onUnblock:n,css:p.blockUI.defaults.growlCSS})};s();i.css("opacity");i.on("mouseover",function(){s({fadeIn:0,timeout:3e4});var e=p(".blockMsg");e.stop(),e.fadeTo(300,1)}).on("mouseout",function(){p(".blockMsg").fadeOut(1e3)})},p.fn.block=function(e){if(this[0]===window)return p.blockUI(e),this;var t=p.extend({},p.blockUI.defaults,e||{});return this.each(function(){var e=p(this);t.ignoreIfBlocked&&e.data("blockUI.isBlocked")||e.unblock({fadeOut:0})}),this.each(function(){"static"==p.css(this,"position")&&(this.style.position="relative",p(this).data("blockUI.static",!0)),this.style.zoom=1,o(this,e)})},p.fn.unblock=function(e){return this[0]===window?(p.unblockUI(e),this):this.each(function(){v(this,e)})},p.blockUI.version=2.7,p.blockUI.defaults={message:"<h1>Please wait...</h1>",title:null,draggable:!0,theme:!1,css:{padding:0,margin:0,width:"30%",top:"40%",left:"35%",textAlign:"center",color:"#000",border:"3px solid #aaa",backgroundColor:"#fff",cursor:"wait"},themedCSS:{width:"30%",top:"40%",left:"35%"},overlayCSS:{backgroundColor:"#000",opacity:.6,cursor:"wait"},cursorReset:"default",growlCSS:{width:"350px",top:"10px",left:"",right:"10px",border:"none",padding:"5px",opacity:.6,cursor:"default",color:"#fff",backgroundColor:"#000","-webkit-border-radius":"10px","-moz-border-radius":"10px","border-radius":"10px"},iframeSrc:/^https/i.test(window.location.href||"")?"javascript:false":"about:blank",forceIframe:!1,baseZ:1e3,centerX:!0,centerY:!0,allowBodyStretch:!0,bindEvents:!0,constrainTabKey:!0,fadeIn:200,fadeOut:400,timeout:0,showOverlay:!0,focusInput:!0,focusableElements:":input:enabled:visible",onBlock:null,onUnblock:null,onOverlayClick:null,quirksmodeOffsetHack:4,blockMsgClass:"blockMsg",ignoreIfBlocked:!1};var m=null,g=[];function o(e,o){var t,n,i,s,l,d,a,c,r,u=e==window,f=o&&o.message!==undefined?o.message:undefined;(o=p.extend({},p.blockUI.defaults,o||{})).ignoreIfBlocked&&p(e).data("blockUI.isBlocked")||(o.overlayCSS=p.extend({},p.blockUI.defaults.overlayCSS,o.overlayCSS||{}),i=p.extend({},p.blockUI.defaults.css,o.css||{}),o.onOverlayClick&&(o.overlayCSS.cursor="pointer"),s=p.extend({},p.blockUI.defaults.themedCSS,o.themedCSS||{}),f=f===undefined?o.message:f,u&&m&&v(window,{fadeOut:0}),f&&"string"!=typeof f&&(f.parentNode||f.jquery)&&(a=f.jquery?f[0]:f,t={},p(e).data("blockUI.history",t),t.el=a,t.parent=a.parentNode,t.display=a.style.display,t.position=a.style.position,t.parent&&t.parent.removeChild(a)),p(e).data("blockUI.onUnblock",o.onUnblock),r=o.baseZ,a=h||o.forceIframe?p('<iframe class="blockUI" style="z-index:'+r+++';display:none;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="'+o.iframeSrc+'"></iframe>'):p('<div class="blockUI" style="display:none"></div>'),t=o.theme?p('<div class="blockUI blockOverlay ui-widget-overlay" style="z-index:'+r+++';display:none"></div>'):p('<div class="blockUI blockOverlay" style="z-index:'+r+++';display:none;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>'),o.theme&&u?(c='<div class="blockUI '+o.blockMsgClass+' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+(r+10)+';display:none;position:fixed">',o.title&&(c+='<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(o.title||"&nbsp;")+"</div>"),c+='<div class="ui-widget-content ui-dialog-content"></div>',c+="</div>"):o.theme?(c='<div class="blockUI '+o.blockMsgClass+' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+(r+10)+';display:none;position:absolute">',o.title&&(c+='<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(o.title||"&nbsp;")+"</div>"),c+='<div class="ui-widget-content ui-dialog-content"></div>',c+="</div>"):c=u?'<div class="blockUI '+o.blockMsgClass+' blockPage" style="z-index:'+(r+10)+';display:none;position:fixed"></div>':'<div class="blockUI '+o.blockMsgClass+' blockElement" style="z-index:'+(r+10)+';display:none;position:absolute"></div>',r=p(c),f&&(o.theme?(r.css(s),r.addClass("ui-widget-content")):r.css(i)),o.theme||t.css(o.overlayCSS),t.css("position",u?"fixed":"absolute"),(h||o.forceIframe)&&a.css("opacity",0),c=[a,t,r],n=p(u?"body":e),p.each(c,function(){this.appendTo(n)}),o.theme&&o.draggable&&p.fn.draggable&&r.draggable({handle:".ui-dialog-titlebar",cancel:"li"}),s=y&&(!p.support.boxModel||0<p("object,embed",u?null:e).length),(k||s)&&(u&&o.allowBodyStretch&&p.support.boxModel&&p("html,body").css("height","100%"),!k&&p.support.boxModel||u||(i=U(e,"borderTopWidth"),s=U(e,"borderLeftWidth"),l=i?"(0 - "+i+")":0,d=s?"(0 - "+s+")":0),p.each(c,function(e,t){t=t[0].style;t.position="absolute",e<2?(u?t.setExpression("height","Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.support.boxModel?0:"+o.quirksmodeOffsetHack+') + "px"'):t.setExpression("height",'this.parentNode.offsetHeight + "px"'),u?t.setExpression("width",'jQuery.support.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"'):t.setExpression("width",'this.parentNode.offsetWidth + "px"'),d&&t.setExpression("left",d),l&&t.setExpression("top",l)):o.centerY?(u&&t.setExpression("top",'(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"'),t.marginTop=0):!o.centerY&&u&&(e=o.css&&o.css.top?parseInt(o.css.top,10):0,t.setExpression("top","((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "+e+') + "px"'))})),f&&((o.theme?r.find(".ui-widget-content"):r).append(f),(f.jquery||f.nodeType)&&p(f).show()),(h||o.forceIframe)&&o.showOverlay&&a.show(),o.fadeIn?(c=o.onBlock||b,a=o.showOverlay&&!f?c:b,c=f?c:b,o.showOverlay&&t._fadeIn(o.fadeIn,a),f&&r._fadeIn(o.fadeIn,c)):(o.showOverlay&&t.show(),f&&r.show(),o.onBlock&&o.onBlock.bind(r)()),I(1,e,o),u?(m=r[0],g=p(o.focusableElements,m),o.focusInput&&setTimeout(w,20)):function(e,t,o){var n=e.parentNode,i=e.style,s=(n.offsetWidth-e.offsetWidth)/2-U(n,"borderLeftWidth"),n=(n.offsetHeight-e.offsetHeight)/2-U(n,"borderTopWidth");t&&(i.left=0<s?s+"px":"0");o&&(i.top=0<n?n+"px":"0")}(r[0],o.centerX,o.centerY),o.timeout&&(r=setTimeout(function(){u?p.unblockUI(o):p(e).unblock(o)},o.timeout),p(e).data("blockUI.timeout",r)))}function v(e,t){var o,n,i=e==window,s=p(e),l=s.data("blockUI.history"),d=s.data("blockUI.timeout");d&&(clearTimeout(d),s.removeData("blockUI.timeout")),t=p.extend({},p.blockUI.defaults,t||{}),I(0,e,t),null===t.onUnblock&&(t.onUnblock=s.data("blockUI.onUnblock"),s.removeData("blockUI.onUnblock")),n=i?p(document.body).children().filter(".blockUI").add("body > .blockUI"):s.find(">.blockUI"),t.cursorReset&&(1<n.length&&(n[1].style.cursor=t.cursorReset),2<n.length&&(n[2].style.cursor=t.cursorReset)),i&&(m=g=null),t.fadeOut?(o=n.length,n.stop().fadeOut(t.fadeOut,function(){0==--o&&a(n,l,t,e)})):a(n,l,t,e)}function a(e,t,o,n){var i=p(n);i.data("blockUI.isBlocked")||(e.each(function(e,t){this.parentNode&&this.parentNode.removeChild(this)}),t&&t.el&&(t.el.style.display=t.display,t.el.style.position=t.position,t.el.style.cursor="default",t.parent&&t.parent.appendChild(t.el),i.removeData("blockUI.history")),i.data("blockUI.static")&&i.css("position","static"),"function"==typeof o.onUnblock&&o.onUnblock(n,o),n=(i=p(document.body)).width(),o=i[0].style.width,i.width(n-1).width(n),i[0].style.width=o)}function I(e,t,o){var n=t==window,t=p(t);!e&&(n&&!m||!n&&!t.data("blockUI.isBlocked"))||(t.data("blockUI.isBlocked",e),n&&o.bindEvents&&(!e||o.showOverlay)&&(n="mousedown mouseup keydown keypress keyup touchstart touchend touchmove",e?p(document).on(n,o,i):p(document).off(n,i)))}function i(e){if("keydown"===e.type&&e.keyCode&&9==e.keyCode&&m&&e.data.constrainTabKey){var t=g,o=!e.shiftKey&&e.target===t[t.length-1],n=e.shiftKey&&e.target===t[0];if(o||n)return setTimeout(function(){w(n)},10),!1}t=e.data,o=p(e.target);return o.hasClass("blockOverlay")&&t.onOverlayClick&&t.onOverlayClick(e),0<o.parents("div."+t.blockMsgClass).length||0===o.parents().children().filter("div.blockUI").length}function w(e){!g||(e=g[!0===e?g.length-1:0])&&e.trigger("focus")}function U(e,t){return parseInt(p.css(e,t),10)||0}}"function"==typeof define&&define.amd&&define.amd.jQuery?define(["jquery"],e):e(jQuery)}();