//START:
$(document).ready(
		function() {
			// confirm exit
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			//// categories
			getCategoriesList();
			$("#parent_name").change(
					function() {
						$(".loading_panel").css("display", "block");
						setTimeout(function() {
							getParentInfo($("#parent_id").val(), 'Accounts');
						}, 1000); 
					});
			// searchProductInput change
			$('#searchProductsInput').keyup(function() {
				searchProducts();
			});
			$("#stock_pw").change(function() {
				$("#pw_stock_id").val(($("#stock_pw :selected").val()));
			});
			$("#stock_rw").change(function() {
				$("#rw_stock_id").val(($("#stock_rw :selected").val()));
			});
			DrawHeaders();
			
			$("#register_date").prop("disabled", true);
			$("#assigned_user_name").prop("disabled", true);
			//$("#register_date_trigger").hide();			
			$("#btn_assigned_user_name").hide();			
			$("#btn_clr_assigned_user_name").hide();			
			
			if (($("#new_number").val() == true)
					&& ($("#duplicate").val() != true)) {
				generateNumber();
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				generateNumber();
				getParentInfo($("#parent_id").val(),
						$("#parent_type :selected").val());
				getItems();
			} else {
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItems();
				$("#rw_stock_id").val(($("#stock_rw :selected").val()));
				$("#pw_stock_id").val(($("#stock_pw :selected").val()));
			}
			// handle setItems
			setITEMS = function() {
				SetTab('ITEMS');
				//var formname = 'EditView';
				//if (check_form_(formname) == true) {
				//	SetTab('ITEMS');
				//	//checkProducts();
				//	//disable stock change
				//	//$("#stock_pw").prop("disabled", true);
				//	$("#stock_rw").prop("disabled", true);
				//}
			};
			// add mz@bim-it.pl 11.11.2023
			if ($("#position_list").val().length) {
				const data = JSON.parse($("#position_list").val().replaceAll("'", '"'));
				items = data;
				FillTable(data, false);
			}
			$("#stock_rw").change(function() {
				for (var i = 0; i < items.length; i++) {
					if (items[i].product_component === 1) {
						refreshStock(i, 1);
					}
				}
			});
			$("#stock_pw").change(function() {
				for (var i = 0; i < items.length; i++) {
					if (items[i].product_component === 0) {
						refreshStock(i, 0);
					}
				}
			});
			// enc mz
		});
// handle save
var check_form_ = check_form;
check_form = function(formname) {
	window.onbeforeunload = null;
	if (items.length == 0) {
		alert("Brak produktów");
		return false;
	}
	$(".loading_panel").css("display", "block");
	$("#register_date").prop("disabled", false);
	$("#assigned_user_name").prop("disabled", false);
	
	$("#position_list").val(JSON.stringifyNoSecurity(items));
	
	var check = checkProducts();

	$(".loading_panel").css("display", "none");
	if (check_form_(formname) == false || check == false) {
		$("#register_date").prop("disabled", true);
		$("#assigned_user_name").prop("disabled", true);
		window.onbeforeunload = confirmExit;
		$(".loading_panel").css("display", "none");
		return false;
	} else {
		return true;
	}

};
function confirmExit() {
	return "";
}
function lockEnter() {
	// prevent default
	$(window).keydown(function(event) {
		if (event.keyCode == 13 && $(":focus").prop('tagName')!='TEXTAREA') {
			event.preventDefault();
			return false;
		}
	});
}
