///Initialize date range picker
$(document).ready(function () {
	//Cargamos el plugin para desplegable de opciones de fechas
	$('.date-range-picker').daterangepicker($.daterangepicker);
	
	//Seleccionamos el texto en el campo de la expresión de búsqueda con los eventos blur y focus
	$('#expression').focus(function(){if (this.value == this.defaultValue)this.value = '';}).blur(function(){if (this.value == '') this.value = this.defaultValue;});	
});

function allpress_check(f) {
	var value;
	if (f == undefined) {
		f = '#allpress_form';
	} 
	//Aquí sería con que no esté vacía expression
	value = $('#expression').val();
	//alert("value es "+value+" y length: "+ value.length);
	if(value !== "") {
		var fromDateTimestamp	= new Date($('#allPressSelectYear_fromDate').val(), $('#allPressSelectMonth_fromDate').val() - 1, $('#allPressSelectDay_fromDate').val()).getTime();
		var untilDateTimestamp	= new Date($('#allPressSelectYear_untilDate').val(), $('#allPressSelectMonth_untilDate').val() - 1, $('#allPressSelectDay_untilDate').val()).getTime();
		
		var datediff;
		datediff= untilDateTimestamp-fromDateTimestamp;
		 
		//alert(fromDateTimestamp+' :: '+untilDateTimestamp + ' === ' + datediff);
		if (fromDateTimestamp > untilDateTimestamp) {
			alert('Fecha inicial posterior a fecha final');
			//$('allPressIncorrectDateError').show();
			return false;
		} else {
			var permittedDate = new Date();
			//permittedDate.setDate(new Date().getDate() - 90);
			//Le ponemos como fecha permitida la fecha until menos 6 meses (180 dias)
			permittedDate = untilDateTimestamp - (180 * 86400000);
			//permittedDate.setHours(0, 0, 0, 0);
			//alert('permittedDate ' + permittedDate);
			if (fromDateTimestamp < permittedDate) {
			    alert('Fecha inicial no puede pasar de 3 meses');
				//$('allPressDateRangeError').show();
			    return false;
			} else {
				return true;			
			}
			
			
		}
	} else {
		alert("Expresión incorrecta.");
		return false;
	}
}
