var jQuery = jQuery || $.noConflict();
(function ($, undefined) {
	$(function () {
		"use strict";

		var $frmReport = $("#frmReport"),
			validate = ($.fn.validate !== undefined);
		
		
		if ($frmReport.length > 0 && validate) {
			$frmReport.validate({
				submitHandler: function(form){
					$.ajax({
	        	        type: "POST",
	        	        url: "index.php?controller=pjAdminReports&action=pjActionCheckDates",
	        	        data: {date_from: $(form).find('input[name="date_from"]').val(), date_to: $(form).find('input[name="date_to"]').val()},
	        	        async:true,
	        	        success: function (data) {
	        	        	if(data.status == 'OK')
	        	        	{
	        	        		var l = Ladda.create( $(form).find(":submit").get(0) );
								l.start();
								form.submit();
	        	        	}else{
	        	        		swal({
	    			    			title: "",
	    							text: data.text,
	    							type: "warning",
	    							confirmButtonColor: "#DD6B55",
	    							confirmButtonText: "OK",
	    							closeOnConfirm: false,
	    							showLoaderOnConfirm: false
	    						}, function () {
	    							swal.close();
	    						});
	        	        	}
	        	        }
	        	    });
					return false;
				}
			});
		}
		if ($('.datepick').length > 0) {
        	if ($('#datePickerOptions').length) {
            	$.fn.datepicker.dates['en'] = {
            		days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
        		    daysMin: $('#datePickerOptions').data('days').split("_"),
        		    daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
        		    months: $('#datePickerOptions').data('months').split("_"),
        		    monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
        		    format: $('#datePickerOptions').data('format'),
                	weekStart: parseInt($('#datePickerOptions').data('wstart'), 10),
                	stepMinute: 5
        		};
            };
        	$('.datepick').datepicker({autoclose: true}).on('changeDate', function (selected) {
        		if($(this).attr('name') == 'date_from')
        		{
        			if($('input[name="date_to"]').length > 0)
        			{
        				var $minDate = new Date(selected.date.valueOf());
        				$('input[name="date_to"]').datepicker('setStartDate', $minDate);
        				var $to = $('input[name="date_to"]'),
        					$date_to_value = $to.datepicker("getUTCDate");
        				if($date_to_value < selected.date)
    					{
        					$to.val($('input[name="date_from"]').val());
    					}
        			}
        		}
        		
        		if($(this).attr('name') == 'date_to')
        		{
        			if($('input[name="date_from"]').length > 0)
        			{
        				var $maxDate = new Date(selected.date.valueOf());
        				$('input[name="date_from"]').datepicker('setEndDate', $maxDate);
        				var $from = $('input[name="date_from"]'),
        					$date_from_value = $from.datepicker("getUTCDate");
        				if($date_from_value > selected.date)
    					{
        					$from.val($('input[name="date_to"]').val());
    					}
        			}
        		}
            });
        }
	});
})(jQuery);