// Retorna la cantidad de noches entre ambos calendarios
	function cantidad_de_noches()	{
	
		return Math.round(($("#fecha_final").datepicker("getDate" )  - $("#fecha_inicial").datepicker("getDate" )) / 86400000);
	}
	
Date.prototype.goto = function(n) { this.setTime(this.getTime()+n*86400000); return this;};
	
function appendOptionLast(num) //Agrega dias a la lista de noches
{
	var elSel = document.getElementById('cantidad_de_noches');
	  for(i = document.getElementById('cantidad_de_noches').options.length ; i <=num ; i++ )
	  {
       var elOptNew = document.createElement('option');
       elOptNew.value = elOptNew.text = i;
       try {elSel.add(elOptNew, null);} // standards compliant; doesn't work in IE
       catch(ex){elSel.add(elOptNew);} // IE only
	  }
}

	// Actualiza el <select> de cantidad de noches
	function actualizar_noches(){
	 var cantidad =cantidad_de_noches();
	  if(cantidad> document.getElementById('cantidad_de_noches').options.length)
	    appendOptionLast(cantidad);
		$("#cantidad_de_noches").val(cantidad)
	}
	
	// Actualiza la fecha final a partir de las noches seleccionadas
	function actualizar_calendario(){
		var fecha = $("#fecha_inicial").datepicker("getDate");
		$("#fecha_final").datepicker("setDate", fecha.goto( $("#cantidad_de_noches").val()));
		$("#fecha_inicial").datepicker('option',{maxDate: $("#fecha_final").datepicker("getDate" )});
		cambiofecha();
	}
   
arregloFecha = function()
{
		var fecha = $("#fecha_inicial").datepicker("getDate");
		$("#fecha_final").datepicker("setDate", fecha.goto( $("#cantidad_de_noches").val()));
		$("#fecha_inicial").datepicker('option',{maxDate: $("#fecha_final").datepicker("getDate" )});
		cambiofecha();
}
  
   //Funcion de pone border 0 a las img
   applychange=function(image){	
	$.each(image, function(){// Ciclo que recorre el array uno por uno
	
                 var img = $(this);//this representa la img
                 var id = img.attr('border');//Optine el border de l img
                 if(id.length == 0)
                  {  //Pone border 0 a las imagenes
                     $(this).attr("border", "0");
                  }
                                 });
                                }

	$(document).ready( function(){
			
           var image = $("images");//Busca todas las img de la pagina, crea un array
		     applychange(image);//En via el array a la funcion
		$("#fecha_inicial").datepicker({
			onSelect: function(){
				// Llena los campos escondidos necesarios para GlobeKey
				if(cantidad_de_noches() < 1)
				    arregloFecha();
				else
				    cambiofecha();					
		        actualizar_noches();
				$("#fecha_final").datepicker('option',{minDate: $("#fecha_inicial").datepicker("getDate" )});
				
			},
			minDate: 0,
			defaultDate: 0,
			showOn: 'both',
			buttonImage: 'images/calendario.gif',
			buttonImageOnly: true
			//dateFormat: 'DD, d MM, yy'

		}).datepicker("setDate", new Date());
		
		$("#fecha_final").datepicker({
			onSelect: function(){
				// Llena los campos escondidos necesarios para GlobeKey
			    cambiofecha();	
		 actualizar_noches();	
				$("#fecha_inicial").datepicker('option',{maxDate: $("#fecha_final").datepicker("getDate" )});
			},
			minDate: +1,
			defaultDate: +2,
			showOn: 'both',
			buttonImage: 'images/calendario.gif',
			buttonImageOnly: true
		});
		
		// Actualiza el calendario segun las noches elegidas
		actualizar_calendario();
		$("#fecha_inicial").change(cambiofecha());
		$("#fecha_final").change(cambiofecha());
		$("#cantidad_de_noches").change(function(){
			actualizar_calendario();
		});
		$("#fecha_inicial").datepicker('option',{maxDate: null });	
		cambiofecha();
	});
	
 cambiofecha= function()
  {
	    
		 var fecha_desglozada = document.getElementById("fecha_inicial").value.split('/');
		 if(fecha_desglozada[0]<10)
		   fecha_desglozada[0]=fecha_desglozada[0].replace("0","")
		 if(fecha_desglozada[1]<10)
		   fecha_desglozada[1]= fecha_desglozada[1].replace("0","")
		   
	    fecha_desglozada[2]= fecha_desglozada[2] - document.getElementById("fy").options[0].text;
        document.getElementById("fm").selectedIndex=fecha_desglozada[0]-1;
		document.getElementById("fd").selectedIndex=fecha_desglozada[1]-1;
		document.getElementById("fy").selectedIndex=fecha_desglozada[2];
		
		 var fecha_desglozada  =document.getElementById("fecha_final").value.split('/');
		  if(fecha_desglozada[0]<10)
		   fecha_desglozada[0]=fecha_desglozada[0].replace("0","")
		 if(fecha_desglozada[1]<10)
		   fecha_desglozada[1]= fecha_desglozada[1].replace("0","")
		   
	    fecha_desglozada[2]= fecha_desglozada[2] - document.getElementById("ty").options[0].text;
		
		 document.getElementById("tm").selectedIndex=fecha_desglozada[0]-1;
		 document.getElementById("td").selectedIndex=fecha_desglozada[1]-1;
		 document.getElementById("ty").selectedIndex=fecha_desglozada[2];
	  
	  }