//START:
$(document).ready(
		function() {
			var checkgen;
			// confirm exit
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			// categories
			getCategoriesList();
			// searchProductInput change
			$("#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)) {
				EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocInsideIns');
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocInsideIns');
				getItems();
			}  else {
			checkgen=0;
		}
			
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :selected").val()));
				EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocInsideIns');
			});
			if($("#stock_id").val()!='') {
				$("#stock_id").val(($("#stock :selected").val()));
				EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocInsideIns');
			}
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
					checkProducts();
					//disable stock change
					$("#stock").prop("disabled", true);
				}
			};
			// wyszukiwanie start
			$('#searchProductsInput').keyup(function() {
				// stronicowanie od 1 strony przy zmianie szukanego produktu
				$('#searchStart').val(0);
				searchProducts();
			}); 
			AddSearchRecord();
		});
// 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 consigns = checkConsignmentsValues();
    if (consigns != true) {
        setITEMS('ITEMS');
        alert('Brak wymaganych informacji o partiach produktu.');
        return false;
    }
	$(".loading_panel").css("display", "block");
	$("#register_date").prop("disabled", false);
	$("#assigned_user_name").prop("disabled", false);
	
	

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