function OCTgeturlparam(param) {
	var urlParams = new URLSearchParams(window.location.search);
	return urlParams.get(param) !== null ? param+'='+urlParams.get(param) : '';
}
var OCT4_GET_LANG = '';
var prdinq_ur1201 = 'index.php?route=module/prdinq/';
var prdinq_url230 = 'index.php?route=extension/module/prdinq/';
var prdinq_url401 = 'index.php?route=extension/prdinq/module/prdinq|';
var prdinq_url402 = 'index.php?route=extension/prdinq/module/prdinq.';

var prdinq = {
	'atgrid': function() {
  		var product_ids = [];
 		$("[onclick*='cart.add'], [onclick*='addToCart'], button[formaction*='checkout/cart.add']").each(function() {
			if($(this).closest('form').find("[name*='product_id']").length) {
				var target = $(this).closest('.product-thumb, .product-wrapper');
				var product_id = $(this).closest('form').find("[name*='product_id']").val(); 
			} else {
				var target = $(this).closest('.product-thumb, .product-wrapper');
				var product_id = $(this).attr('onclick').match(/[0-9]+/).toString(); 
			}
			if(target.length && target.hasClass('prdinq_atgridcol') == false) {
				target.addClass('prdinq_atgridcol').addClass('prdinq_atgridcol'+product_id);
				product_ids.push(product_id);
 			}
		});
		if(product_ids.length) { 
			//console.log(product_ids.length);
 			$.ajax({
				url: prdinq_url230 + 'getcache' + OCT4_GET_LANG,
				type: 'post',
				data: {product_ids:product_ids},
				cache: true,
				complete: function() { 
				},
				success: function(json) {
					if(json) {
						//console.log(json);
						$("[onclick*='cart.add'], [onclick*='addToCart'], button[formaction*='checkout/cart.add']").each(function() {
 							if($(this).closest('form').find("[name*='product_id']").length) {
								var target = $(this).closest('.product-thumb, .product-wrapper');
								var product_id = $(this).closest('form').find("[name*='product_id']").val(); 
							} else {
								var target = $(this).closest('.product-thumb, .product-wrapper');
								var product_id = $(this).attr('onclick').match(/[0-9]+/).toString(); 
							}
							if(json[product_id] && target.length && target.find('.prdinq_grdbtn').length == 0) {
								if(parseInt(json[product_id]['btntype']) == 0) {
									$(this).html(json[product_id]['btntext']).addClass('prdinq_grdbtn').attr('onclick', 'prdinq.add('+product_id+','+json[product_id]['prdinq_id']+')');
								} else {									
									$(this).parent().before('<a class="prdinq_grdbtn btn btn-inverse btn-lg btn-block" onclick="prdinq.add('+product_id+','+json[product_id]['prdinq_id']+');">'+json[product_id]['btntext']+'</a>');
								}
								if(parseInt(json[product_id]['hideprc']) >= 1) {
									target.find('.price').remove();
								}
							}							
						});
					}
				}
			});
		}		
  	},	
	'prodpage': function() {
		var product_ids = [];
		var product_id = false;
		
		if($(".button-group-page input[name='product_id']").length) {
			product_id = $(".button-group-page input[name='product_id']").val();
			product_ids.push(product_id);
		} else if($('#input-product-id').length) {
			product_id = $('#input-product-id').val();
			product_ids.push(product_id);
		} else if($(".product-info input[name='product_id']").length) {
			product_id = $(".product-info input[name='product_id']").val();
			product_ids.push(product_id);
		} else if($("#product input[name='product_id']").length) {
			product_id = $("#product input[name='product_id']").val();
			product_ids.push(product_id);
		} 
			
		if (product_ids.length) {
			$.ajax({
				url: prdinq_url230 + 'getcache' + OCT4_GET_LANG,
				type: 'post',
				data: {product_ids:product_ids},
				cache: true,
				complete: function() { 
				},
				success: function(json) {
					if(json) {
						//console.log(json);
						var target = $('#product');
						var target_J2 = $('.route-product-product .product-info .left .image');
						var target_J3 = $('.product-left .product-image');
						var target_OC4 = $('.magnific-popup');
						
						if(json[product_id]) {							
							if(target.length && target.find('.prdinq_prdbtn').length == 0) {
								if(parseInt(json[product_id]['btntype']) == 0) {
									$('#button-cart').unbind('click').removeAttr('id').attr('type','button').html(json[product_id]['btntext']).addClass('prdinq_prdbtn').attr('onclick', 'prdinq.addprd('+product_id+', '+json[product_id]['prdinq_id']+')');
								} else {									
									$('#button-cart').after('<a class="prdinq_prdbtn btn btn-inverse btn-lg btn-block" onclick="prdinq.addprd('+product_id+', '+json[product_id]['prdinq_id']+');">'+json[product_id]['btntext']+'</a>');
								}
								if(parseInt(json[product_id]['hideprc']) >= 1) {
									$('#product').parent().find('ul.list-unstyled:eq(1)').remove();
									$('#product .price-wrapper, #product ul.price').remove();
								} 							
							}
						}
					}
				}
			});
		}
	},
	'setdatepicker': function() {
		$('.date').datetimepicker({
			pickTime: false
		});
		$('.datetime').datetimepicker({
			pickDate: true,
			pickTime: true
		});
		$('.time').datetimepicker({
			pickDate: false
		});
	},
	'showpop': function() {
		$('#pop-prdinq').modal('show');
		$('#pop-prdinq .modal-body').html('<i class="fa fa-circle-o-notch fa-spin"></i>');		
	},
	'refreshpop': function() {
 		$.ajax({
			url: prdinq_url230 + 'loadenqcart' + OCT4_GET_LANG,
			dataType: 'html',
			cache: false,
			beforeSend: function() { $('#pop-prdinq .modal-body').html('<i class="fa fa-circle-o-notch fa-spin"></i>'); },
			success: function(html) {
				$('#pop-prdinq .modal-dialog').html(html);
				prdinq.setdatepicker();
  			}
		});
	},
	'after_add': function(json) {
		setTimeout(function () { $('.btnenqcart').html(json['total']); }, 100);
		$('#cart > ul').load('index.php?route=common/cart/info ul li');
 		
		if(json['rsdata']['btnact'] == 0 && json['rsdata']['carttype'] == 1) { 
			location = 'index.php?route=checkout/cart';
		} else if(json['rsdata']['btnact'] == 1) { 
			prdinq.showpop();
			prdinq.refreshpop();
		} else if(json['rsdata']['btnact'] == 2) { 			
			$('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
			$('html, body').animate({ scrollTop: 0 }, 'slow');
		} else {
			prdinq.showpop();
			prdinq.refreshpop();
		}
	},
	'add': function(product_id, prdinq_id, quantity) {
		$.ajax({
			url: prdinq_url230 + 'add' + OCT4_GET_LANG,
			type: 'post',
			data: {product_id:product_id, prdinq_id:prdinq_id, quantity:(typeof(quantity) != 'undefined' ? quantity : 1)},
			dataType: 'json',
			beforeSend: function() { $('.btnenqcart').button('loading'); },
			complete: function() { $('.btnenqcart').button('reset'); },
			success: function(json) {
				$('.alert, .text-danger').remove();
				if (json['redirect']) { location = json['redirect']; }
				if (json['success']) { prdinq.after_add(json); }
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	},
	'addprd': function(product_id, prdinq_id) {
		$('#product').append('<input type="hidden" name="prdinq_id" value="'+prdinq_id+'">');
		$.ajax({
			url: prdinq_url230 + 'add' + OCT4_GET_LANG,
			type: 'post',
			data: $('#product input[type=\'text\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea'),
			dataType: 'json',
			beforeSend: function() { $('.btnenqcart').button('loading'); },
			complete: function() { $('.btnenqcart').button('reset'); },
			success: function(json) {
				$('.alert, .text-danger').remove();
				$('.form-group, .mb-3').removeClass('has-error');
	
				if (json['error']) {					
					if (json['error']['option']) {
						for (i in json['error']['option']) {
							var element = $('#input-option' + i.replace('_', '-'));
							if (element.parent().hasClass('input-group')) {
								element.parent().after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
							} else {
								element.after('<div class="text-danger">' + json['error']['option'][i] + '</div>');
							}
						}
					}
					// Highlight any found errors
					$('.text-danger').parent().addClass('has-error');
				}
				if (json['success']) { prdinq.after_add(json); }
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	},
	'edit': function(key, quantity) {
 		$.ajax({
			url: prdinq_url230 + 'edit' + OCT4_GET_LANG,
			type: 'post',
			data: $('#pop-prdinq input[type="text"]'), 
			dataType: 'json',
			cache: false,
			beforeSend: function() { $('.btnenqcart').button('loading'); },
			complete: function() { $('.btnenqcart').button('reset'); },
 			success: function(json) {
				setTimeout(function () { $('.btnenqcart').html(json['total']); }, 100);
				$('#cart > ul').load('index.php?route=common/cart/info ul li');
				prdinq.refreshpop();
			} 
		});
	},
	'remove': function(key) {
		$.ajax({
			url: prdinq_url230 + 'remove' + OCT4_GET_LANG,
			type: 'post',
			data: 'key=' + key,
			dataType: 'json',
			cache: false,
 			beforeSend: function() { $('.btnenqcart').button('loading'); },
			complete: function() { $('.btnenqcart').button('reset'); },
 			success: function(json) {
				setTimeout(function () { $('.btnenqcart').html(json['total']); }, 100);
				$('#cart > ul').load('index.php?route=common/cart/info ul li');
				prdinq.refreshpop();
			}
		});
	},
	'initjson': function() {
		prdinq.atgrid(); 
		$(document).ajaxStop(function() { 
			prdinq.atgrid(); $(".price, #product .price-wrapper, #product ul.price, .col-sm-4 ul.list-unstyled, #product-info ul.list-unstyled, [onclick*='cart.add'], [formaction*='checkout/cart.add'], [onclick*='addToCart'], #button-cart, .btn-1-extra").attr('style','opacity:1;'); 
		});
		prdinq.prodpage();
		
		var pophtml = '<div id="pop-prdinq" class="modal fade" role="dialog"> <div class="modal-dialog modal-lg"></div></div>';
		$('body').append(pophtml);
		
		$.ajax({
			url: prdinq_url230 + 'get_enqcart_count' + OCT4_GET_LANG,
			dataType: 'json',
			cache: false,
			success: function(json) {
				$('#cart').after('<button type="button" class="btn btn-inverse btn-block btn-lg btnenqcart"><i class="fa fa-envelope-o"></i></button>');
				// OC4 $('#header-cart').after('<div class="col-md-12 col-lg-12 mb-2"><button type="button" class="btn btn-inverse btn-block btn-lg btnenqcart float-end"><i class="fa fa-envelope-o"></i></button></div>');
				
				setTimeout(function () { $('.btnenqcart').html(json['total']); }, 100);
 				$('#cart > ul').load('index.php?route=common/cart/info ul li');
			}
		});		
	}	
}
$(document).ready(function() {
prdinq.initjson();
});

$(document).delegate('.btnenqcart', 'click', function() {
	prdinq.showpop();
	prdinq.refreshpop();
});


/*Quote Form Submit*/
$(document).delegate('#button-prdinq_form_submit', 'click', function() {
	$('.alert, .text-danger').remove();
	$('#prdinqform td').removeClass('has-error');
	
	$.ajax({
		url: prdinq_url230 + 'submitform' + OCT4_GET_LANG,
		type: 'post',
		data: $('#prdinqform input[type="text"], #prdinqform input[type="email"], #prdinqform input[type="date"], #prdinqform input[type="datetime"], #prdinqform input[type="time"], #prdinqform input[type="password"], #prdinqform input[type="hidden"], #prdinqform input[type="checkbox"]:checked, #prdinqform input[type="radio"]:checked, #prdinqform textarea, #prdinqform select'),
		dataType: 'json',
		beforeSend: function() { $('#button-prdinq_form_submit').button('loading'); },
		complete: function() { $('#button-prdinq_form_submit').button('reset'); },
		success: function(json) {
			if (json['redirect']) {
				location = json['redirect'];
			} else if (json['error']) {
				for (i in json['error']) {
					var element = $('#prdinqform').find('#input-' + i);
					
					if ($(element).parent().hasClass('input-group')) {
						$(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
					} else {
						$(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
					}
					
					if(i == 'captcha'){
						$('div.prdinqcaptcha').append('<div class="text-danger">' + json['error'][i] + '</div>');
					}
				}
				$('.text-danger').closest('td').addClass('has-error');
			} 
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});


/*AUTO SAVE START*/
$(document).delegate('#prdinqform input[type="text"], #prdinqform textarea, #prdinqform input[type="date"], #prdinqform input[type="datetime"], #prdinqform input[type="time"], #prdinqform input[type="password"], #prdinqform input[type="hidden"]', 'blur', function() {
	$.ajax({
		url: prdinq_url230 + 'autosave' + OCT4_GET_LANG,
		type: 'post',
		data: $('#prdinqform input[type="text"], #prdinqform input[type="email"], #prdinqform input[type="date"], #prdinqform input[type="datetime"], #prdinqform input[type="time"], #prdinqform input[type="password"], #prdinqform input[type="hidden"], #prdinqform input[type="checkbox"]:checked, #prdinqform input[type="radio"]:checked, #prdinqform textarea, #prdinqform select'),
		dataType: 'json',
		beforeSend: function() { },
		complete: function() { },
		success: function(json) { 
			//console.log(json['postdata']);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	}); 
});
$(document).delegate('#prdinqform input[type="checkbox"], #prdinqform input[type="radio"], #prdinqform select', 'change', function() {
	$.ajax({
		url: prdinq_url230 + 'autosave' + OCT4_GET_LANG,
		type: 'post',
		data: $('#prdinqform input[type="text"], #prdinqform input[type="email"], #prdinqform input[type="date"], #prdinqform input[type="datetime"], #prdinqform input[type="time"], #prdinqform input[type="password"], #prdinqform input[type="hidden"], #prdinqform input[type="checkbox"]:checked, #prdinqform input[type="radio"]:checked, #prdinqform textarea, #prdinqform select'),
		dataType: 'json',
		beforeSend: function() { },
		complete: function() { },
		success: function(json) { 
			//console.log(json['postdata']);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	}); 
});
$(document).delegate('#prdinqform button[id*="button-fld"]', 'click', function() {
	var node = this;
	$('#form-upload').remove();
	$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');
	$('#form-upload input[name="file"]').trigger('click');
	if (typeof timer != 'undefined') {
		clearInterval(timer);
	}
	timer = setInterval(function() {
		if ($('#form-upload input[name="file"]').val() != '') {
			clearInterval(timer);
			$.ajax({
				url: 'index.php?route=tool/upload' + OCT4_GET_LANG,
				type: 'post',
				dataType: 'json',
				data: new FormData($('#form-upload')[0]),
				cache: false,
				contentType: false,
				processData: false,
				beforeSend: function() {
					$(node).button('loading');
				},
				complete: function() {
					$(node).button('reset');
				},
				success: function(json) {
					$(node).parent().find('.text-danger').remove();
					if (json['error']) {
						$(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
					}
					if (json['success']) {
						alert(json['success']);
						$(node).parent().find('input').val(json['code']);
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}, 500);
});
