$(function() {		
$("#botonactualizar").css("display", "none");
if(typeof(miurl)!='undefined'){
	$("#botonactualizar").attr("href",miurl+"&orden="+$("#selector")[0].value);
}
$("#tabs").tabs();
var desde;
var hasta;
var galletita=document.cookie.split(';');
var buscar=false;
var hoy=new Date();
hoy.setDate(hoy.getDate()+3);
var humandesde=$.datepicker.formatDate('dd/mm/yy', hoy);
hoy.setDate(hoy.getDate()+1);
var humanhasta=$.datepicker.formatDate('dd/mm/yy', hoy);
var seleccionardesde=false;
var seleccionarhasta=false;
for(key in galletita){
	var temp=galletita[key].replace(/^\s*|\s*$/g,"");
	if (temp=='buscar=true'){ 
		buscar=true;
	}else if (temp=='seleccionardesde=true'){
			seleccionardesde=true;
 	}else if  (temp=='seleccionarhasta=true'){
			seleccionarhasta=true;
	} else {
		var temp=temp.split('=');
		if (temp[0]=='humandesde'){
				humandesde=temp[1];
		}else if (temp[0]=='humanhasta'){
			humanhasta=temp[1];
		}else if (temp[0]=='desde'){
			desde=temp[1];
		}else if (temp[0]=='hasta'){
			hasta=temp[1];
		}
	}
}
errormayor();
	
		$('#spandesde').text(humandesde);
		$('#spanhasta').text(humanhasta);
		var tdatepicker=false;
		$("#desde").datepicker({
				minDate: 3,
				maxDate: '+1y',
				changeMonth: true,
				changeYear: true,
				onSelect:function(textDate){
					document.cookie='seleccionardesde=true';
					seleccionardesde=true;
					tdatepicker=true;
				    $('#spandesde').text($.datepicker.formatDate('dd/mm/yy',new Date(textDate)));
					var fecha=new Date(textDate);
					fecha.setDate(fecha.getDate()+1);
				    $('#hasta').datepicker('option','minDate',fecha);						
				},
				onClose: function(date){
					if (tdatepicker){
						tdatepicker=false;
						desde=$.datepicker.formatDate('yy-mm-dd',new Date(date));
						humandesde=$.datepicker.formatDate('dd/mm/yy',new Date(date));
						document.cookie='desde='+desde;						

                   			document.cookie='humandesde='+humandesde;							
						errormayor();
						$('#botonactualizar').css('display', 'inline');
					}else{
					}
				} 
			}
		);
		$('#spandesde, #caldesde').click(function(){
			$('#desde').datepicker("show");
		});
		$("#hasta").datepicker({	
				minDate: 4,
				maxDate: '+1y',
				changeMonth: true,
				changeYear: true,
				onSelect:function(textDate){
					document.cookie='seleccionarhasta=true';
					seleccionarhasta=true;
					tdatepicker=true;
				    $('#spanhasta').text($.datepicker.formatDate('dd/mm/yy',new Date(textDate)));
				   // $('#desde').datepicker('option','maxDate',new Date(textDate))							
				},
				onClose: function(date){
					if (tdatepicker){
						hasta=$.datepicker.formatDate('yy-mm-dd',new Date(date));
						document.cookie='hasta='+hasta;
						humanhasta=$.datepicker.formatDate('dd/mm/yy',new Date(date));
							document.cookie='humanhasta='+humanhasta;
						tdatepicker=false;
						errormayor();
						$('#botonactualizar').css('display', 'inline');
					}else{
					}
				} 
			}
		);
		$('#spanhasta, #calhasta').click(function(){
			$('#hasta').datepicker("show");
		});
$('#botonbuscar').click(function(){
	document.cookie='buscar=true';
})
function errormayor(){
	if((buscar && seleccionardesde && seleccionarhasta)||!buscar){
		$('#errormayor').text('');
	}else if(buscar && !(seleccionardesde && seleccionarhasta)){
		$('#errormayor').text(' Seleccione sus fechas');
	}
	if(desde>=hasta){
		$('#errormayor').text(' Error: la fecha de entrada no puede ser igual o mayor que la de salida');
	}
}
});
$(function(){
   	$("#accordion").accordion({
	header: ".h3",
	alwaysOpen: false
}).accordion( 'activate' , -1 );
});
$(function(){
   	$("#selector").change(function(){
	$("#botonactualizar").attr("href",miurl+"&orden="+this.value);							   
	$('#botonactualizar').css('display', 'inline');
});
});
$(function(){
function validar(){
	$valido=false;
	$(".scantidad").each(function(){
		if (this.value!='nada'){
			$valido=true;
			return false;
		}
	});
	return $valido;
}
    $("#form1").submit(function() {
      if (validar()){
	  return true;
  }else{
	 $('#errorhb').css('display', 'inline');
 	       return false; 
  }
     });
      
});
