//START:
$(document).ready(
		
		function() {
			// confirm exit
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			// parent info
			$('#parent_id').on('inputchange',
					function() {
						$(".loading_panel").css("display", "block");
						setTimeout(function() {
							getParentInfo($("#parent_id").val(), 'Accounts');
						}, 1000);
					});
			// categories
			getCategoriesList();
			// its vat free change
			$("#no_tax").change(function() {
				// calculate totals
				var count = $('#' + itemsTable + '_T tr').length - 1; // -1 -
																	// thead row
				for (var index = 0; index != count; index++)
					calculateRow(index);
			});
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :selected").val()));
				if($("#is_correct").val() != true){
					generateNumber($("#stock :selected").val());
				} else {
					generateNumberCorrect($("#stock :selected").val());
				}
			});
			$("#currency_id").change(function() {
				getNBPCurrencyExchange($("#currency_id :selected").val());
			
			});
			$("#ecmpaymentcondition_name").on('inputchange',
					function() {
						$(".loading_panel").css("display", "block");
						setTimeout(function() {
							
							calculatePaymentDate($("#ecmpaymentcondition_id").val());
							$(".loading_panel").css("display", "none");

						},1000);
					});

			$('#paid_val').change(function() {
				calculateTotal();
			});
			$('#prepaid').change(function() {
				calculateTotal();
			});
			// searchProductInput change
			$('#searchProductsInput').keyup(function() {
				searchProducts();
			});
			DrawHeaders();
			
			if (($("#new_number").val() == true)
					&& ($("#duplicate").val() != true) && ($("#is_correct").val() != true) && ($("#is_wz").val() != true) && ($("#is_sale").val() != true)) {
				

			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItems();
			} else if (($("#new_number").val() == true)
					&& ($("#is_correct").val() == true)) {
				generateNumberCorrect();
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItems();
			}else if (($("#new_number").val() == true)
					&& ($("#is_wz").val() == true)) {
				generateNumber($("#stock_id").val());
				
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItemsFromWZ();
			} 	else if (($("#new_number").val() == true)
					&& ($("#is_sale").val() == true)) {
				generateNumber($("#stock_id").val());
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItemsFromSale();
				if($("#ecmpaymentcondition_id").val()!='')calculatePaymentDate($("#ecmpaymentcondition_id").val());
				
			} else {
				
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getItems();
			}
			$(".loading_panel").css("display", "none");
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
				}
			};
			// wyszukiwanie start
			$('#searchProductsInput').keyup(function() {
				// stronicowanie od 1 strony przy zmianie szukanego produktu
				$('#searchStart').val(0);
				searchProducts();
			}); 
			if($('#type').val()=='normal'){
			AddSearchRecord();
			}
		});

// handle save
var check_form_ = check_form;
check_form = function(formname,event) {
	// zapobiega zapisywaniu dokumentu firefox bug, w przypadku nacisniecia enter w polu z autocomplete
	if(event.clientY==0 && event.clientX==0){
			return false;
		}
	window.onbeforeunload = null;
	if (items.length == 0 || items[0].product_id=='') {
		alert("Brak produktów");
		return false;
	}
	$(".loading_panel").css("display", "block");
	// calculate totals
	clearEmpty();
	var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
	for (var index = 0; index != count; index++)
		calculateRow(index);

	$("#total_netto").val($("#t_netto").val());
	$("#total_brutto").val($("#left").text());
	$("#discount").val($("#disc").val());
	$("#position_list").val(JSON.stringifyNoSecurity(items));
	/*
	if(checkProducts()==false){
		$(".loading_panel").css("display", "none");
		alert("Brak wystarczającej ilości magazynowej do wystawienia dokumentu!");
		return false;
	} */
	if (check_form_(formname) == false) {
		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;
		}
	});
}
