// include SPECIFIC PAGE LEVEL SCRIPTS 
document.write('<script src="assets/plugins/plupload/js/plupload.full.min.js" type="text/javascript"></script>');
document.write('<script src="assets/plugins/fancybox/source/jquery.fancybox.pack.js" type="text/javascript"></script>');
document.write('<script src="assets/scripts/custom/case-form.js" type="text/javascript"></script>');
document.write('<script src="assets/plugins/jquery-validation/dist/jquery.validate.min.js" type="text/javascript"></script>');

/////////////////////////////////////
jQuery(document).ready(function() {
	App.init();
	caseForm.init();

	$('a[data-toggle="tab"]').on('shown', function(e) {
		e.target // activated tab
		e.relatedTarget // previous tab
	});
	
	$('.remove-case-file').on('click', function(e) {	
		var row = $(this).closest('div.case-file');
		var case_file_name = $(this).attr("rel");
		//alert(case_file_name);
		$.ajax({
			url: "case-file-delete.php",
			data: {"case_file_name": case_file_name},
			type: "POST",
			dataType: "json"
		}).done(function(result) {
			if (result.result == 1)
			{
				row.remove();
				alert("eee");
			}
			else
			alert(case_file_name);
		});
	});
	
	$('#case-form').validate({
		ignore: "",
		errorElement: 'label', //default input error message container
//            errorClass: 'help-block', // default input error message class
		focusInvalid: false, // do not focus the last invalid input
		rules: {
			case_type: "required",
			case_hdd_brand: "required",
			case_hdd_model: "required",
			case_hdd_interface: "required",
			case_hdd_format: "required",
			case_hdd_capacity: "required",
			case_hdd_sn: "required",
			case_hdd_parameters: "required",
			case_status: "required"
		},
		messages: {
			case_type: "Veuillez selectionner un type.",
			case_hdd_brand: "Veuillez selectionner la marque du disque.",
			case_hdd_model: "Veuillez selectionner le modèle du disque.",
			case_hdd_interface: "Veuillez selectionner l'interface du disque.",
			case_hdd_format: "Veuillez selectionner le format du disque.",
			case_hdd_capacity: "Veuillez entrer la capacité du disque en Go.",
			case_hdd_sn: "Veuillez entrer le SN du disque.",
			case_hdd_parameters: "Veuillez entrer les paramètres du disque."
		},
		invalidHandler: function(event, validator) { //display error alert on form submit   
			// $('.alert-danger', $('#case-form')).show();
		},
		highlight: function(element) { // hightlight error inputs
			$(element).closest('.form-group').addClass('has-error'); // set error class to the control group

			if ($(".tab-content").find("div.tab-pane.active:has(div.has-error)").length == 0)
			{
				$(".tab-content").find("div.tab-pane:hidden:has(div.has-error)").each(function() {
					var id = $(this).attr("id");
					console.log(id);
					$('.nav-tabs a[href="#' + id + '"]').tab('show');
				});
			}
		},
		success: function(label) {
			var a = label.closest('.form-group');
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement: function(error, element) {
			//error.insertAfter(element.closest('.input-icon'));
			error.appendTo(element.parent("div"));
		}
	});
});