function OCTgeturlparam(param) {
	var urlParams = new URLSearchParams(window.location.search);
	return urlParams.get(param) !== null ? param+'='+urlParams.get(param) : '';
}
var OCT4_GET_LANG = '';
var opttxtlmt_ur1201 = 'index.php?route=module/opttxtlmt/';
var opttxtlmt_url230 = 'index.php?route=extension/module/opttxtlmt/';
var opttxtlmt_url401 = 'index.php?route=extension/opttxtlmt/module/opttxtlmt|';
var opttxtlmt_url402 = 'index.php?route=extension/opttxtlmt/module/opttxtlmt.';

var opttxtlmt = {	
	'runjs': function(json) {
		$("input[type=text][name*='option'], textarea[name*='option']").each(function() {
			if($(this).attr('data-date-format') || $(this).hasClass('date')) {
				/*DO nothing for date and time*/
			} else {
				var poid = $(this).attr('name').match(/[0-9]+/);
				if(json['optdata'][poid]) { 
					var minlimit = parseInt(json['optdata'][poid]['opttxtlmtmin']);
					var maxlimit = parseInt(json['optdata'][poid]['opttxtlmtmax']);
					var thisval = parseInt($(this).val().length);			
					
					if(maxlimit > 0) { 
						$(this).after('<b class="opttxtlmt opttxtlmtpoid'+poid+'"> ' + json['lang']['text_charleft'].replace("%s", (maxlimit - thisval)) + '</b>');
						$(this).prop('maxlength',maxlimit);
						
						$(this).keyup(function() { 
							thisval = parseInt($(this).val().length);
							if (thisval > maxlimit) {
								$(this).val($(this).val().substr(0, maxlimit));
							}
							$('.opttxtlmtpoid'+poid).html(json['lang']['text_charleft'].replace("%s", (maxlimit - thisval)));
						});	
					}
				}
			}
		});	
  	},
	'initjson': function() {
		var product_id = false;
		
		if($(".button-group-page input[name='product_id']").length) {
			product_id = $(".button-group-page input[name='product_id']").val();
		} else if($('#input-product-id').length) {
			product_id = $('#input-product-id').val();
		} else if($(".product-info input[name='product_id']").length) {
			product_id = $(".product-info input[name='product_id']").val();
		} else if($("#product input[name='product_id']").length) {
			product_id = $("#product input[name='product_id']").val();
		} 
			
		if (product_id) {
			$.ajax({
				url: opttxtlmt_url230 + 'getcache' + OCT4_GET_LANG,
				type: 'post',
				data: {product_id:product_id},
				cache: true,
				success: function(json) {
					if(json) {
 						opttxtlmt.runjs(json);
					}
				}
			});
 		}
	}
}
$(document).ready(function() {
setTimeout(function(){ opttxtlmt.initjson(); }, 500);
});