//START:
$(document).ready(
		function() {
			// confirm exit
			var checkgen;
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			// categories
			getCategoriesList();
			// searchProductInput change
			$('#searchProductsInput').keyup(function() {
				searchProducts();
			});
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :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();			
			$(".loading_panel").css("display", "none");
			
			if (($("#new_number").val() == true)
					&& ($("#duplicate").val() != true) && ($("#ecmsale").val() != true)) {
				checkgen=1;
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				checkgen=1;
				getItems();
			} else if (($("#new_number").val() == true)
					&& ($("#ecmsale").val() == true)) {
				checkgen=1;

		
				getItemsFromEcmSale();
				
	
		}   else {
			checkgen=0;
		}
		
			if(checkgen==1){
				generateNumber();
			}
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
					checkProducts();
					//disable stock change
					$("#stock").prop("disabled", true);
				}
			};

		});
// 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;
		}
	});
}
