var kMinimumDays = 1;

//---------------------------------------------------------------
var iWindow = null;

function CreateCal(Destination, iLang){

	var arTranslation = [
		["x"],
		[ "Solo un calendario a la vez."], 
		[ "Only one calender opens at a time."], 
		[ "---"]
	];



	if(iWindow!=null){
		if(iWindow.closed){
			//iWindow=window.open("calendar.php?casilla="+Destination, "", "menubar='no',resize='no',status=yes,width=250,height=215, dependent=yes");
			iWindow=window.open("/reserva3/calendar.php?casilla="+Destination, "", "menubar='no',resize='no',status=no,width=250,height=215, dependent=yes");
		}else{
			window.alert(arTranslation[iLang]);
		}
	}else{
		iWindow=window.open("/reserva3/calendar.php?casilla="+Destination, "", "menubar='no',resize='no',status=no,width=250,height=215, dependent=yes");
	}
}
//---------------------------------------------------------------

function getElement(_strName) {
	return document.getElementById(_strName);
}
function getValue(_strName) {
	//window.alert(_strName);
	return document.getElementById(_strName).value;
}

function submitForm(_strForm, _strAction) {
	oForm = getElement(_strForm);
	if(oForm) {
		if(_strAction) {
			oForm.action=_strAction;
		}
		oForm.submit();
	}
}


function checkReserveValues(iLang) {
	oForm = document.getElementById('reservas');
	oForm.action="list.php";
	return true;
}

function checkReserveValuesHome(iLang) {
	oForm = document.getElementById('reservas');
	oForm.action="reserva3/list.php";
	return true;
}

//----------------------------------------------------------------------

function getMiliseconds(strDate) {
	arrayOfStrings=strDate.split("-");
	if(arrayOfStrings.length!=3){
		return null;
	}
	if(arrayOfStrings[0].length!=4){
		return null;
	}
	strDia = arrayOfStrings[2];
	strMes = arrayOfStrings[1];
	strAnyo = arrayOfStrings[0];
	
	var NewDate=Date.UTC(strAnyo, (strMes-1), strDia, 0, 0, 0);
	//window.alert(strAnyo+","+(strMes)+","+strDia+"\n"+NewDate);
	return NewDate;
}

function getDifference(strDateBegin, strDateEnd) {

	var UTCDate=getMiliseconds(strDateBegin);//Date.UTC(strAnyo, (strMes-1), strDia, 0, 0, 0);
	var UTCDate2=getMiliseconds(strDateEnd);//Date.UTC(strAnyo, (strMes-1), strDia, 0, 0, 0);
	var Result=(UTCDate2-UTCDate)/(24*60*60*1000);
	return Result;
}


function addTime(_strDate, _iNumDays) {
	
	arrayOfStrings=_strDate.split("-");
	if(arrayOfStrings.length!=3){
		return null;
	}
	if(arrayOfStrings[0].length!=4){
		return null;
	}
	strDay = arrayOfStrings[2];
	strMonth = arrayOfStrings[1];
	strYear = arrayOfStrings[0];
	
	var UTCDate=Date.UTC(strYear, (strMonth-1), strDay, 0, 0, 0);
	
	var AddMilisecons=_iNumDays*24*60*60*1000;
	gmtDate = new Date(UTCDate+AddMilisecons);


	strYear=gmtDate.getFullYear();
	if(strYear<1000)
		strYear+=1000;
	strDay=gmtDate.getDate();
	strMonth=gmtDate.getMonth()+1;

	if(strMonth<10){
		strMonth="0"+strMonth;
	}
	if(strDay<10){
		strDay="0"+strDay;
	}
	
	return strYear+"-"+strMonth+"-"+strDay;
}


function getDateValue(_strLabel) {
	//window.alert("::Alerta::");
	iDay = getValue("f_day_"+_strLabel);
	iMonth = getValue("f_month_"+_strLabel);
	iYear = getValue("f_year_"+_strLabel);

	if(iMonth<10){
		iMonth="0"+iMonth;
	}
	if(iDay<10){
		iDay="0"+iDay;
	}
	return iYear+"-"+iMonth+"-"+iDay;
}
function getDateBegin() {
	return getDateValue("begin");
}
function getDateEnd() {
	return getDateValue("end");
}

function updateCombo2(_strName, _arValues, _iSelected) {
	oCombo = getElement(_strName);
	
	iLen = oCombo.options.length;
	for (i = 0; i <= iLen; i++) {
		oCombo.options[i] = null;
	}
	oCombo.options.length = 0;
	
	iLen = _arValues.length;
	for (i = 0; i < iLen; i++) {
		oOption = new Option(_arValues[i], (i), false, false);
		oCombo.options[i] = oOption;
		if (i == _iSelected) oCombo.selectedIndex = i;
	}
	oCombo.options[0] = null;
}


function updateDayField(strName, iYear, iMonth, iDay) {

	var arr_mon = new Array(31,28,31,30,31,30,31,31,30,31,30,31);	
	if( (iYear%4==0 || iYear%100==0 && iYear%400==0) )
		arr_mon[1] = 29;

	arValues = new Array();
	iMax = arr_mon[(iMonth-1)];
	for(iC=1; iC<=iMax; iC++)
		arValues[iC] = iC;

	if(iDay>iMax)
		iDay = iMax;

	updateCombo2(strName, arValues, iDay);
}


function checkToday() {
	var strDateBegin = getDateBegin();

	var gmtDate = new Date();
	var iYear=gmtDate.getFullYear();
	if(iYear<1000)
		iYear+=1000;
	var iMonth=(gmtDate.getMonth()+1);
	var iDay=gmtDate.getDate();

	var strDateToday = iYear+"-"+iMonth+"-"+iDay;


	if( getDifference(strDateToday ,strDateBegin)<0 ){
		//window.alert(strDateToday+" > "+strDateBegin);
		strDateBegin = addTime(strDateToday, 0);

		arrayOfStrings = strDateBegin.split("-");
		if(arrayOfStrings.length!=3){
			return null;
		}
		if(arrayOfStrings[0].length!=4){
			return null;
		}
		strDay = arrayOfStrings[2];
		strMonth = arrayOfStrings[1];
		strYear = arrayOfStrings[0];


		//window.alert(strYear+"\n"+strMonth+"\n"+strDay);
		var oYear = getElement("f_year_begin");
		oYear.selectedIndex=(strYear-iYear);
		
		var oMonth = getElement("f_month_begin");
		oMonth.selectedIndex=(strMonth-1);
		
		updateDayField("f_day_begin", strYear, strMonth, strDay);
	
	}
	//checkDates();
}
function checkDates() {
	var strDateBegin = getDateBegin();
	var strDateEnd = getDateEnd();
	var gmtDate = new Date();
	var iYear=gmtDate.getFullYear();
	if(iYear<1000)
		iYear+=1000;

	if( getDifference(strDateBegin , strDateEnd)<=1 ){
		//window.alert(strDateBegin+" > "+strDateEnd);
		strDateEnd = addTime(strDateBegin, kMinimumDays);

		arrayOfStrings = strDateEnd.split("-");
		if(arrayOfStrings.length!=3){
			return null;
		}
		if(arrayOfStrings[0].length!=4){
			return null;
		}
		strDay = arrayOfStrings[2];
		strMonth = arrayOfStrings[1];
		strYear = arrayOfStrings[0];


		//window.alert(strYear+"\n"+strMonth+"\n"+strDay);
		var oYear = getElement("f_year_end");
		oYear.selectedIndex=(strYear-iYear);
		
		var oMonth = getElement("f_month_end");
		oMonth.selectedIndex=(strMonth-1);
		
		updateDayField("f_day_end", strYear, strMonth, strDay);
	
	}

}

function checkPensions(dateAltaBegin, dateAltaEnd, lang) {

	var year = new Date();
	year = year.getFullYear();

	//dateAltaBegin = '2006-04-15';
	//dateAltaEnd = '2006-04-25';
	
	//var pensions = document.getElementById('f_pension');
	//var padre = pensions.parentNode;

	var dayBegin = document.getElementById('f_day_begin').selectedIndex+1;
	var monthBegin = document.getElementById('f_month_begin').selectedIndex+1;
	var yearBegin = document.getElementById('f_year_begin').selectedIndex+year;	
	if(monthBegin < 10) {
		monthBegin = '0'+monthBegin;
	}
	if(dayBegin < 10) {
		dayBegin = '0'+dayBegin;
	}

	var dateBegin = yearBegin+'-'+monthBegin+'-'+dayBegin;

	var dayEnd = document.getElementById('f_day_end').selectedIndex+1;
	var monthEnd = document.getElementById('f_month_end').selectedIndex+1;
	var yearEnd = document.getElementById('f_year_end').selectedIndex+year;	
	if(monthEnd < 10) {
		monthEnd = '0'+monthEnd;
	}
	if(dayEnd < 10) {
		dayEnd = '0'+dayEnd;
	}

	var dateEnd = yearEnd+'-'+monthEnd+'-'+dayEnd;

	if((dateBegin >= dateAltaBegin && dateBegin <= dateAltaEnd) || (dateEnd >= dateAltaBegin && dateEnd <= dateAltaEnd) || (dateBegin < dateAltaBegin && dateEnd > dateAltaEnd)) {
		if(lang == '_sp') {
			//padre.innerHTML = '<label for="f_pension">R&eacute;gimen:</label><select name="f_pension" id="f_pension" onchange="javascript:checkShowField();" class="calendario"><option value="" >R&eacute;gimen</option><option value="1" >Media Pensi&oacute;n</option><option value="2" >Pensi&oacute;n Completa</option></select>';
		} else {
			//padre.innerHTML = '<label for="f_pension">Type of Board:</label><select name="f_pension" id="f_pension" onchange="javascript:checkShowField();" class="calendario"><option value="" >Choose board</option><option value="1" >Half Board</option><option value="2" >Full Board</option></select>';
			
		}
	} else {
		if(lang == '_sp') {
			//padre.innerHTML = '<label for="f_pension">R&eacute;gimen:</label><select name="f_pension" id="f_pension" onchange="javascript:checkShowField();" class="calendario"><option value="" >R&eacute;gimen</option><option value="1" >Media Pensi&oacute;n</option><option value="2" >Pensi&oacute;n Completa</option><option value="3" >Con desayuno</option><option value="4" >Solo habitaci&oacute;n</option></select>';
		} else {
			//padre.innerHTML = '<label for="f_pension">Type of Board:</label><select name="f_pension" id="f_pension" onchange="javascript:checkShowField();" class="calendario"><option value="" >Choose board</option><option value="1" >Half Board</option><option value="2" >Full Board</option><option value="3" >With Breakfast</option><option value="4" >Room only</option></select>';
		}
	}

//pensions.value = 3;
	//alert(dateBegin);
}

function f_month_begin_modify(dt_begin, dt_end, lang) {
	var oDay = getElement("f_day_begin");
	var iDay = oDay.value;
	var oMonth = getElement("f_month_begin");
	var iMonth = oMonth.value;
	var oYear = getElement("f_year_begin");
	var iYear = oYear.value;

	updateDayField("f_day_begin", iYear, iMonth, iDay);
	//Check end date
	checkToday();
	checkDates();
	checkShowField();
	f_month_end_modify();
	checkPensions(dt_begin, dt_end, lang);
}



function f_month_end_modify(dt_begin, dt_end, lang) {
	var oDay = getElement("f_day_end");
	var iDay = oDay.value;
	var oMonth = getElement("f_month_end");
	var iMonth = oMonth.value;
	var oYear = getElement("f_year_end");
	var iYear = oYear.value;

	updateDayField("f_day_end", iYear, iMonth, iDay);

	//Check end date
	checkDates();
	checkShowField();
	checkPensions(dt_begin, dt_end, lang);
	
}


function f_day_begin_modify(dt_begin, dt_end, lang) {
	checkToday();
	checkDates();
	checkShowField();
	checkPensions(dt_begin, dt_end, lang);
}


function f_year_begin_modify(dt_begin, dt_end, lang) {
	f_month_begin_modify();
	checkToday();
	checkDates();
	checkShowField();
	checkPensions(dt_begin, dt_end, lang);
}


function f_day_end_modify(dt_begin, dt_end, lang) {
	f_month_end_modify();
	checkToday();
	checkDates();
	checkShowField();
	checkPensions(dt_begin, dt_end, lang);
}

function f_year_end_modify(dt_begin, dt_end, lang) {
	checkToday();
	checkDates();
	checkShowField();
	checkPensions(dt_begin, dt_end, lang);
}

function f_numadults_modify() {
	checkShowField();
}
function f_numyoungs_modify() {
	checkShowField();
}
function f_numchilds_modify() {
	checkShowField();
}
function f_numbabes_modify() {
	 checkShowField();
}

function checkIconCalendar() {
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("img"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.className == "openCalendar")  {
		   //var newDestiny = eval(history.back())
			anchor.style.cursor = "pointer";
			anchor.onclick = launchCalendar;
		}		
	} 
}

function launchCalendar() {
	var name = this.id;
	var language = this.lang;
	CreateCal(name, language);
}
window.onload = function() {
	checkIconCalendar();
}	