jQuery.fn.exists = function () { return this.length > 0; }
var hasChange = false;

function getFilledValues(){
                return true; //STU
		var is_filled = true;
		
		if ( checkInputValues == 1 ) {
			$("input.text-tbldata").each(function() {
   				if($(this).val() === "") {
					var pos = $("#tbldata-cell-fact").position();
					$("#message-error").css({
						top: ( pos.top - $("#message-error").outerHeight() - 6 )+ "px",
						left: (pos.left) + "px"
					}).show();
					
					is_filled = false;
				}
			});
		}	
		return is_filled;
}

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}

function checkNumberFields(n) {
    return !!(/^\d+$/.test(n));
}

function inputApp(){
    var strSize = 0;
    $('td.dateBeforeColumn').each(function(){
        strSize += $(this).html().trim().length;
    });

    if (strSize == 0) {
        $('th.dateBeforeColumnHeader').hide();
        $('td.dateBeforeColumn').hide();
    }

    $('input').change(function () {
        hasChange = true;
        return true;
    });

    $('input.text-tbldata').bind('keyup keypress change', function (event) {
        checkNumberFields(this, event);
    }).click(function () {
        this.select();
    });

    $("input.text-tbldata").keydown(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 9 && e.shiftKey) {
            var tabnumber = parseInt($(this).attr('tabnumber')) - 1;
            var next = $('input.text-tbldata[tabnumber="' + tabnumber + '"]');
            if ($(next).exists()) {
                $(next).focus();
            } else {
                var imputscnt = $('input.text-tbldata[tabnumber]').length;
                console.log(imputscnt);
                next = $('input.text-tbldata[tabnumber="' + imputscnt + '"]');
                $(next).focus();
            }
            e.preventDefault();
        } else if (code == 9) {
            var tabnumber = parseInt($(this).attr('tabnumber')) + 1;
            var next = $('input.text-tbldata[tabnumber="' + tabnumber + '"]');
            if ($(next).exists()) {
                $(next).focus();
            } else {
                next = $('input.text-tbldata[tabnumber="1"]');
                $(next).focus();
            }
            e.preventDefault();
        } else if (code == 40) {
            var tabnumber = parseInt($(this).attr('tabnumber')) + 1;
            var next = $('input.text-tbldata[tabnumber="' + tabnumber + '"]');
            if ($(next).exists()) {
                $(next).focus();
            }
        } else if (code == 38) {
            var tabnumber = parseInt($(this).attr('tabnumber')) - 1;
            var next = $('input.text-tbldata[tabnumber="' + tabnumber + '"]');
            if ($(next).exists()) {
                $(next).focus();
            }
        }
    });

    $(".text-tbldata").focus(function ()
    {
		$(this).parent("td").addClass("tbldata-cell-active");
		$(this).parent("td").parent("tr").addClass("tbldata-row-active");
	});
	
	$( ".text-tbldata" ).blur(function() 
    {
		$(this).parent("td").removeClass("tbldata-cell-active");
		$(this).parent("td").parent("tr").removeClass("tbldata-row-active");
	});
	
	$( "#form-factors" ).submit(function() 
    {   
        hasChange = false;
		return getFilledValues();
	});
	
	$( ".btn-cancel" ).click(function() 
	{
        $( "#message-error" ).hide();
        hasChange = false;
		$( location ).attr( 'href', $( "#form-factors").attr( 'action' ) );
	});

	var zD = new zDater();
	zD.init({lng: Glang});
	zD.add('datepicker', 'datepicker', {
		onSelect: function(){
		    var date = $('#datepicker').val();
            var date_value = formatDate($('#datepicker').val());
            $('#datepicker').val(loadingtextForDatePicker);
		    var date_value = Date.parseString(date, $(" #js_format").val()).format($(" #js_format_internal").val());
			var queryParameters = {}, queryString = location.search.substring(1), re = /([^&=]+)=([^&]*)/g, m;
			while (m = re.exec(queryString)) {
				queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
			}
            queryParameters['date_value'] = date_value;
			location.search = $.param(queryParameters);
		},
		
		start: parseInt( $( "#js_start" ).val() ),
		exit: parseInt( $( "#js_exit" ).val() ),
		format: $( "#js_format" ).val(),
		day: parseInt(  $( "#js_day" ).val() ),
		month: parseInt(  $( "#js_month" ).val() ),
		year: parseInt(  $( "#js_year" ).val() )
    });
    // 57766
    loadingtextForDatePicker = $('#datepicker').val();
}

$(window).bind('beforeunload',function(){
    ShowModalWindow(true);
});