//START:
$(document).ready(
		function() {
			var checkgen;
			// confirm exit
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			// categories
			getCategoriesList();

			$("#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();			
			
			if (($("#new_number").val() == true)
					&& ($("#duplicate").val() != true)) {
					EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocCorrects');
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
					EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocCorrects');
				getItems();
			} 
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :selected").val()));
				EcmDocumentNumberGenerator_getNumber('tmp_number', 'EcmStockDocCorrects', $("#stock :selected").val());
			});
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
					checkProducts();
					//disable stock change
					$("#stock").prop("disabled", true);
					$("#type").prop("disabled", true);
				}
			};
			// wyszukiwanie start
			$('#searchProductsInput').keyup(function() {
				// stronicowanie od 1 strony przy zmianie szukanego produktu
				$('#searchStart').val(0);
				searchProducts();
			}); 
			AddSearchRecord();
			
			$(".loading_panel").css("display", "none");
		});
// handle save
var check_form_ = check_form;
check_form = function(formname,event) {
	window.onbeforeunload = null;
	// zapobiega zapisywaniu dokumentu firefox bug, w przypadku nacisniecia enter w polu z autocomplete
	if(event.clientY==0 && event.clientX==0){
			return false;
		}
		
		
	if (items.length == 0 || items[0].product_id=='') {
		alert("Brak produktów");
		return false;
	}
	
	var part_error=true;
	    $.each(items, function(i, v) {
	        if (!v.product_id || v.product_id=="") return;
	        if (v.product_is_consignment!='1') return;
	        if($('#type').val()=='8a6b804b-fac7-5cc2-de19-54c209116b19'){
	        	  if (!v.product_consignment_part || v.product_consignment_part=="") {
		  	            alert("Nie wprowadzono partii");
		  	            part_error=false;
		  	        }
	        } else {
	        	  if (!v.product_consignment_id || v.product_consignment_id=="") {
	  	            alert("Nie wybrano partii");
	  	            part_error=false;
	  	        }
	        }
	      
	    })
	
	$(".loading_panel").css("display", "block");
	$("#register_date").prop("disabled", false);
	$("#assigned_user_name").prop("disabled", false);
	$("#type").prop("disabled", false);
	
	
	var check = checkProducts();

	$(".loading_panel").css("display", "none");
	if (check_form_(formname) == false || check == false || part_error==false) {
		$("#register_date").prop("disabled", true);
		$("#assigned_user_name").prop("disabled", true);
		window.onbeforeunload = confirmExit;
		$(".loading_panel").css("display", "none");
		return false;
	} else {
		clearEmpty();
		$("#position_list").val(JSON.stringifyNoSecurity(items));
		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;
		}
	});
}
