//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(), $(
									"#parent_type :selected").val());
						}, 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);
			});

			$("#delivery_date").on('inputchange', function() {
				calculateDate($("#delivery_date").val());
			});
			if ($("#type").val() == 'interval_order') {
				addToValidate('EditView', 'sale_date_interval', 'id', 'true',
						'');
			}

			$("#type")
					.change(
							function() {
								if ($("#type").val() == 'interval_order') {
									addToValidate('EditView',
											'sale_date_interval', 'id', 'true',
											'');
								} else {
									removeFromValidate('EditView',
											'sale_date_interval');
								}
							});
			// language channge
			$("#ecmlanguage").change(function() {
				changeLanguage();
			});
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :selected").val()));
			});
			// searchProductInput change
			$('#searchProductsInput').keyup(function(e) {
				if (e.keyCode == 13) {
					searchProducts();
				}
			});
			DrawHeaders();

			if (($("#new_number").val() == true)
					&& ($("#duplicate").val() != true)
					&& ($("#ecmquote").val() != true)) {
				generateNumber();
				// set stock "Magazyn główny
				$("#stock option[value=c7afd71a-4c3a-bde4-138d-4acaee1644e4]")
						.attr('selected', 'selected');
				$("#stock_id").val("c7afd71a-4c3a-bde4-138d-4acaee1644e4");
				// remove empty stock
				$("#stock option[value='']").remove();
				// sale from subpanel??
				if ($("#parent_id").val() != '') {
					$(".loading_panel").css("display", "block");
					setTimeout(function() {
						getParentInfo($("#parent_id").val(), $(
								"#parent_type :selected").val());
					}, 1000);

					if ($("#edi_file").val() != '') {
						getItemsFromEdi();
					}
				}
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				generateNumber();
				getParentInfo($("#parent_id").val(),
						$("#parent_type :selected").val());
				getItems();

			} else if (($("#new_number").val() == true)
					&& ($("#ecmquote").val() == true)) {
				console.log('asdas');
				generateNumber();
				getParentInfo($("#parent_id").val(), 'Accounts');
				getItemsFromQuote();
			} else {
				getParentInfo($("#parent_id").val(),
						$("#parent_type :selected").val());
				getItems();

			}
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
				}
			};

		});
// handle save
var check_form_ = check_form;
check_form = function(formname) {
	if (new Date().getFullYear() != $("#register_date").val().slice(-4)) {
		alert("Data dokumentu musi zawierać aktualny rok.");
	}
	window.onbeforeunload = null;
	if (items.length == 0) {
		alert("Brak produktów");
		return false;
	}


	if (items.length == 0) {
		result = validateDate();
		if (result == true) {
			alert("Data wysyłki nie może być późniejsza niż data dostawy!");
			return false;
		}
	}
	$(".loading_panel").css("display", "block");
	// calculate totals
	var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
	for (var index = 0; index != count; index++) {
		calculateRow(index);
		if (validation.recipient_code == true)
			console.log('Sprawdzić kody trzeba');
	}

	$("#total_netto").val($("#t_netto").val());
	$("#total_brutto").val($("#t_brutto").val());
	$("#discount").val($("#disc").val());

	$("#position_list").val(JSON.stringifyNoSecurity(items));
	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;
		}
	});
}
