//START:
$(document).ready(function () {
    $("#parent_name").attr("size", "70");
    $("#parent_name_copy").attr("size", "70");
    //change parent select button
    $("#btn_clr_parent_name").children().attr("src", "themes/default/images/id-ff-add.png");
    $("#btn_clr_parent_name").attr("title", "Dodaj");
    $("#btn_clr_parent_name").click(createAccount);
    
    // parent info
    var previousVal;
    var pollInterval = setInterval(function () {
        var val = $('#parent_id').val();
        if (val !== previousVal) {
            if ($('#parent_id').val() == '')
                return;
            $(".loading_panel").css("display", "block");
            setTimeout(function () {
                getParentInfo($("#parent_id").val(), 'Accounts');
            }, 1000);
        }
        previousVal = val;
    }, 500);
    
    //newProduct 
    var previousVal2;
    var prodInterval = setInterval(function () {
        var val = $('#newProductId').val();
        if (val !== previousVal2) {
            if ($('#newProductId').val() == '')
                return;
            $(".loading_panel").css("display", "block");
            setTimeout(function () {
                AddProduct(items.length - 1, val);
            }, 500);
        }
        previousVal2 = val;
    }, 500);
    var checkgen;
    
    // confirm exit
    window.onbeforeunload = confirmExit;
    // prevent submit by enter press
    lockEnter();
    // categories
    getCategoriesList();
    $('#parent_id').on('inputchange',
            function () {
                $(".loading_panel").css("display", "block");
                setTimeout(function () {
                    getParentInfo($("#parent_id").val(), 'Accounts');
                }, 1000);
            });
    $("#kind").change(function () {
        if ($("#kind").val() == 'import') {
            $("#import_table").show();
            $("#import_title").show();
        } else {
            $("#import_table").hide();
            $("#import_title").hide();
        }
    });
    
    DrawHeaders();

    //$("#register_date").prop("disabled", true);
    $("#assigned_user_name").prop("disabled", true);
    //$("#register_date_trigger").hide();			
    $("#import_table").hide();
    $("#import_title").hide();
    $("#btn_assigned_user_name").hide();
    $("#btn_clr_assigned_user_name").hide();
    if ($("#kind").val() == 'import') {
        $("#import_table").show();
        $("#import_title").show();
    } else {
        $("#import_table").hide();
        $("#import_title").hide();
    }
    
    $(".loading_panel").css("display", "none");
    if (($("#new_number").val() == true)
            && ($("#duplicate").val() != true)) {
        EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocIns');

    } else if (($("#new_number").val() == false)
            && ($("#duplicate").val() == false)) {
        EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocIns');
        getItems();
    }
    $("#stock").change(function () {
        $("#stock_id").val(($("#stock :selected").val()));
        EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocIns');
    });
    if ($("#stock_id").val() != '') {
        $("#stock_id").val(($("#stock :selected").val()));
        EcmDocumentNumberGenerator_getNumberTemplate('document_no', 'EcmStockDocIns');
    }
    // make pz from other doc 
    if ($("#out_id").val()
            && $("#out_module").val()) {
        getItems2();
    }
    
    // handle setItems
    setITEMS = function () {
        var formname = 'EditView';
        if (check_form_(formname) == true) {
            SetTab('ITEMS');
            checkProducts();
            calculateTotal();
            //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();
    //getFirstpartNumber();
    
});
// 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
	console.log(1);
    if (event.clientY == 0 && event.clientX == 0) {
        return false;
    }
	console.log(2);
    var consigns = checkConsignmentsValues();
    if (consigns != true) {
        setITEMS('ITEMS');
        alert('Brak wymaganych informacji o partiach produktu.');
        return false;
    }
	console.log(3);
    window.onbeforeunload = null;
    if (items.length == 0 || items[0].product_id == '') {
        alert("Brak produktów");
        return false;
    }
	console.log(4);
    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();

    //if (confirm('Czy potwierdzić Dokument PZ?')) {
    $("#status").val("accepted");
    //} else {
    //	$("#status").val("registered");
    //}
    $(".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");
		console.log(5);
        return false;
    } else {
		console.log(6);
        return true;

    }
	console.log(7);
};
function confirmExit() {
    return "";
}

function lockEnter() {
    // prevent default
    $(window).keydown(function (event) {
        if (event.keyCode == 13 && $(":focus").prop('tagName') != 'TEXTAREA') {
            event.preventDefault();
            return false;
        }
    });
}
