var jQuery_1_8_2 = jQuery_1_8_2 || $.noConflict();
(function ($, undefined) {
	$(function () {
		"use strict";
		var $pjTsReportForm = $("#pjTsReportForm"),
			validate = ($.fn.validate !== undefined);
		
		if ($pjTsReportForm.length > 0 && validate) {
			$pjTsReportForm.validate({
				rules: {
					login_email: {
						required: true,
						email: true
					},
					login_password: "required"
				},
				errorPlacement: function (error, element) {
					error.insertAfter(element.parent());
				},
				onkeyup: false,
				errorClass: "err",
				wrapper: "em",
				submitHandler: function(){
					generateReport();
					return false;
				}
			});
			generateReport();
		}
		function generateReport()
		{
			$.post("index.php?controller=pjAdminReports&action=pjActionGenerate", $pjTsReportForm.serialize()).done(function (data) {
				$("#reportWrapper").html(data);
			});
		}
		$(document).on("click", ".pj-form-field-icon-date", function (e) {
			var $dp = $(this).parent().siblings("input[type='text']");
			if ($dp.hasClass("hasDatepicker")) {
				$dp.datepicker("show");
			} else {
				$dp.trigger("focusin").datepicker("show");
			}
		}).on("focusin", ".datepick", function (e) {
			var $this = $(this);
			$this.not('.hasDatepicker').datepicker({
				firstDay: $this.attr("rel"),
				dateFormat: $this.attr("rev"),
			});
		});
	});
})(jQuery_1_8_2);