//START:
$(document).ready(
		function() {
			// confirm exit
			window.onbeforeunload = confirmExit;
			// prevent submit by enter press
			lockEnter();
			//// categories
	
			$('#parent_id').on('inputchange',
					function() {
						$(".loading_panel").css("display", "block");
						setTimeout(function() {
							getParentInfo($("#parent_id").val(), 'Accounts');
						}, 1000);
					});
			$('#contact_id').on('inputchange',
					function() {
						$(".loading_panel").css("display", "block");
						setTimeout(function() {
							getContactInfo($("#contact_id").val());
						}, 1000); 
					});
			$("#stock").change(function() {
				$("#stock_id").val(($("#stock :selected").val()));
			
			});
			getCategoriesList();
			$("#stock_pw").change(function() {
				$("#pw_stock_id").val(($("#stock_pw :selected").val()));
			});
			$("#stock_rw").change(function() {
				$("#rw_stock_id").val(($("#stock_rw :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)) {
			
				generateNumber($('#register_date').val());
				getItemsDetail();
			} else if (($("#new_number").val() == true)
					&& ($("#duplicate").val() == true)) {
				generateNumber();
				getParentInfo($("#parent_id").val(),
						'Accounts');
				getContactInfo($("#contact_id").val());
				getItems();
			
				
			} else {
			
				//getParentInfo($("#parent_id").val(),
				//		'Accounts');
			
				getItems();
			
			}
			// handle setItems
			setITEMS = function() {
				var formname = 'EditView';
				if (check_form_(formname) == true) {
					SetTab('ITEMS');
					checkProducts();
					calculateTotal();
					//disable stock change
					$("#stock_pw").prop("disabled", true);
					$("#stock_rw").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) {
	// 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;
	}
	clearEmpty();
	$(".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;
		}
	});
}
