
function getNumNights(date2,date1){

    var miFecha1 = new Date( date1[2], date1[1]-1, date1[0] )
    var miFecha2 = new Date( date2[2], date2[1]-1, date2[0] )

	// check correct dates
	if(miFecha1.getMonth() != (date1[1]-1)){
		alert('Debe elegir una fecha de salida válida');
		return false;
	};
	if(miFecha2.getMonth() != (date2[1]-1)){
		alert('Debe elegir una fecha de llegada válida');
		return false;
	};
	
	var diferencia = miFecha1.getTime() - miFecha2.getTime()
	// check departure date over arrive date 
	if(diferencia < 0){
		alert('Debe elegir una fecha de salida mayor que la actual');
		return false;
	}
    var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
	
	return dias-1;
}

function getArrDate(){
	//data
	sel	=	document.getElementById('arrival_month');
	monthsel	=	sel.options[sel.selectedIndex].value;
	sel	=	document.getElementById('arrival_day');
	day_arr	=	sel.options[sel.selectedIndex].value;
	month_arr	=monthsel.substring(0,2);
	year_arr	=monthsel.substring(3,8);

	data = Array();
	data[0] = day_arr;
	data[1] = month_arr;
	data[2] = year_arr;
	return data;
}


function getDepDate(){
	//data
	sel	=	document.getElementById('departure_month');
	monthsel	=	sel.options[sel.selectedIndex].value;
	sel	=	document.getElementById('departure_day');
	day_dep	=	sel.options[sel.selectedIndex].value;
	month_dep	=monthsel.substring(0,2);
	year_dep	=monthsel.substring(3,8);

	data = Array();
	data[0] = day_dep;
	data[1] = month_dep;
	data[2] = year_dep;
	return data;
}

function checkOverCurrent(year,month,day){
	// currents
	var date=new Date();
	var currentd=date.getDate();
	if(currentd<10) currentd = '0'+currentd;
	var currentm	=	date.getMonth() +1;
	if(currentm <10) currentm = '0'+currentm;
	var currenty	=	date.getFullYear();
	if(date.setFullYear(year,month,day) < date.setFullYear(currenty,currentm,currentd)){
		return false;
	}
	return true;
}

function compareDate(date1,date2){
	date = new Date();
	if(date.setFullYear(date1[2],date1[1],date1[0]) > date.setFullYear(date2[2],date2[1],date2[0])){
		alert('Debe elegir una fecha de salida mayor que la llegada');
		return false;
	}
	if(date.setFullYear(date1[2],date1[1],date1[0]) == date.setFullYear(date2[2],date2[1],date2[0])){
		alert('La fecha de llegada no puede ser igual a la de la salida');
		return false;
	}
	return true;
}

function checkArrive(year,month,day){

	// arrive date over current
	if(!checkOverCurrent(year,month,day)){
		alert('Debe elegir una fecha de llegada mayor que la actual');
		return false;
	}
	return true;
}

function checkDeparture(year,month,day){

	// departure date over current
	if(!checkOverCurrent(year,month,day)){
		alert('Debe elegir una fecha de salida mayor que la actual');
		return false;
	}
	return true;
}


function arrive(){
	date_arr = getArrDate();
	date_dep = getDepDate();

	if(!checkArrive(date_arr[2],date_arr[1],date_arr[0])) return false;

	nights = getNumNights(date_arr,date_dep);
	if(!nights) return false;
	if(nights >99){
		alert('No puede elegir más de 99 noches. Consulte con el hotel');
		return false;
	}
	document.search_form.nights_number.options[nights].selected = true ;
}

function departure(){
	date_arr = getArrDate();
	date_dep = getDepDate();

	if(!checkDeparture(date_arr[2],date_arr[1],date_arr[0])) return false;
	if(!compareDate(date_arr,date_dep))	return false;

	nights = getNumNights(date_arr,date_dep);
	if(!nights) return false;
	if(nights >99){
		alert('No puede elegir más de 99 noches. Consulte con el hotel');
		return false;
	}
	document.search_form.nights_number.options[nights].selected = true ;
}

function changeMonth(){
	date_arr = getArrDate();
	date_dep = getDepDate();

	if(!checkDeparture(date_arr[2],date_arr[1],date_arr[0])) return false;
	if(!checkArrive(date_arr[2],date_arr[1],date_arr[0])) return false;
	if(!compareDate(date_arr,date_dep))	return false;

	nights = getNumNights(date_arr,date_dep);
	if(!nights) return false;
	if(nights >99){
		alert('No puede elegir más de 99 noches. Consulte con el hotel');
		return false;
	}
	document.search_form.nights_number.options[nights].selected = true ;
}

function validateForm(){
	date_arr = getArrDate();
	date_dep = getDepDate();

	if(!checkDeparture(date_arr[2],date_arr[1],date_arr[0])) return false;
	if(!checkArrive(date_arr[2],date_arr[1],date_arr[0])) return false;
	if(!compareDate(date_arr,date_dep))	return false;
	nights = getNumNights(date_arr,date_dep);
	if(!nights) return false;	
	if(nights >99){
		alert('No puede elegir más de 99 noches. Consulte con el hotel');
		return false;
	}
	return true;
}
function modNight(nights){
	date_arr = getArrDate();
    var date1 = new Date( date_arr[2], date_arr[1]-1, date_arr[0] );
	tiempo = parseInt(date1.getTime()) + parseInt((nights * 1000 * 60 * 60 * 24));
	date1.setTime(tiempo);

	date_dep = Array();
	date_dep[0] = date1.getDate();
	date_dep[1] = date1.getMonth()+1;
	date_dep[2] = date1.getFullYear();
	document.search_form.departure_day.options[date_dep[0]-1].selected = true ;
	month = String(date_dep[1]);
	if(month.length == '1') month = '0'+date_dep[1];
	var sel = month +'/'+ date_dep[2];
	for (var idx=0;idx<document.getElementById('departure_month').options.length;idx++) {
            if (sel==document.getElementById('departure_month').options[idx].value) {
                  document.getElementById('departure_month').selectedIndex=idx;
                  document.getElementById('departure_month').options[idx].selected=true;
                  break;
            }
				
	}
}


function monthCalendarChange(){
	// get month and year
	sel	=	document.getElementById('month_calendar');
	monthsel	=	sel.options[sel.selectedIndex].value;
	month	=monthsel.substring(0,2);
	year	=monthsel.substring(3,8);
	window.location.href	= "busquedas.php?fecha=01-"+month+"-"+year;
}

function previousMonth(){
	// go to previous month
	month	=	document.getElementById('current_month').value;
	year	=	document.getElementById('current_year').value;

	month--;
	// previous year
	if(month == '0'){
		month = 12; year--;
	}
	x = String(month);
	if(x.length == '1') x = '0'+ month;

	window.location.href	= "busquedas.php?fecha=01-"+x+"-"+year;
}

function nextMonth(){
	// go to next month
	month	=	document.getElementById('current_month').value;
	year	=	document.getElementById('current_year').value;
	var yearNxt = year;

	month++;
	// next year
	if(month == '13'){
		month = 1;
		yearNxt++;
	}
	x = String(month);
	if(x.length == '1') x = '0'+ month;

	window.location.href	= "busquedas.php?fecha=01-"+x+"-"+yearNxt;
}



