﻿var months = ['Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαίου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου'];
var days = ['Δε','Τρ','Τε','Πε','Πα','Σα','Κυ'];
var days3 = ['Δευ','Τρ','Τετ','Πεμ','Παρ','Σαβ','Κυρ'];

function prevMonth(y,m) {
    if (new Date(y,m-1,1) < td) return;
    if (m > 1) {m--} else {m = 12; y--};
    buildCal(y,m);
}

//does this finction need to check for max month/year?
function nextMonth(y,m) {
    if (m<12){m++;} else {m=1;y++;}
    buildCal(y,m);
}

function initDaySelect() {
	var forms = document.getElementsByTagName('form');
	for(var i=0; i<forms.length; i++)
		if(forms[i]['checkin_monthday'])
			updateDaySelect(forms[i]);
}

// XXX: Hack
// TODO: Create generic event-adding
// XXX: Duplicate, see base.js
//window.onload = initDaySelect;

function updateDaySelect( me ) {

	// 1-2 testing
	if(!days3) return;
	
	// IE5/Mac not supported
	if(gClientIsIE5 && gClientIsMac)
		return;
	
	var frm = DOM.getParentOrSelf(me, 'form');
	
  // Check if we have all fields. If not, we are in the first stage
  // of the book process and should not auto-update selects since there
  // is only the check-in select and the amount of nights.
	if(!frm['checkin_monthday'] || !frm['checkout_monthday'] || !frm['checkin_year_month'] || !frm['checkout_year_month'])
    return;
  
 var ci_d = frm['checkin_monthday'];
	var co_d = frm['checkout_monthday'];
	var ci_my = frm['checkin_year_month'].value.split("-");
	var co_my = frm['checkout_year_month'].value.split("-");
	
	var ci_sel = ci_d.selectedIndex;
	var co_sel = co_d.selectedIndex;

	var monthDays = [], opt;
	
	// Checkin month
	monthDays = buildDaysForMonth(ci_my[0], ci_my[1]);
	ci_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = (i+1);
		ci_d.appendChild(opt);
	}
	ci_d.options[ci_sel].defaultSelected = ci_d.options[ci_sel].selected = true;

	// Checkout month
	monthDays = buildDaysForMonth(co_my[0], co_my[1]);
	co_d.innerHTML = '';
	for(var i = 0; i < monthDays.length; i++) {
		opt = document.createElement('option');
		opt.innerHTML = (monthDays[i] + ' ' + (i+1));
		opt.value = (i+1);
		co_d.appendChild(opt);
	}
	co_d.options[co_sel].defaultSelected = co_d.options[co_sel].selected = true;
}

function buildDaysForMonth( year, month ) {
	// Month index starts on 0(-11) in Date()-object
	var monthDate = new Date(year, month-1);
	var orgMonth = monthDate.getMonth();
	var dayArray = [], weekDay;
	while(monthDate.getMonth() == orgMonth) {
		// Week starts on Sunday in Date()-object
		weekDay = (monthDate.getDay() == 0) ? 6 : (monthDate.getDay()-1);
		dayArray.push(days3[weekDay]);
		monthDate.setDate(monthDate.getDate()+1);
	}
	return dayArray;
}

function checkDateOrder(me, ci_day, ci_month_year, co_day, co_month_year) {
	if (document.getElementById) {
		//var frm = document.getElementById(frm);
		// Do findup to get form instead of fixed id
		var frm = DOM.getParentOrSelf(me, 'form');

		// create date object from checkin values
		// set date to 12:00 to avoid problems with one
		// date being wintertime and the other summertime
		var my = frm[ci_month_year].value.split("-");
	 var ci = new Date (my[0], my[1]-1, frm[ci_day].value, 12, 0, 0, 0);
	 frm[ci_day].value =  ci.getDate();
  frm[ci_month_year].value = ci.getFullYear() + "-" + (ci.getMonth()+1);

  // create date object from checkout values
	 my = frm[co_month_year].value.split("-");
	 var co = new Date (my[0], my[1]-1, frm[co_day].value, 12, 0, 0, 0);

		// if checkin date is at or after checkout date,
		// add a day full of milliseconds, and set the
		// selectbox values for checkout date to new value
	    if (ci >= co){
    	    co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
	        frm[co_day].value =  co.getDate();
    	    var com = co.getMonth()+1;
	        frm[co_month_year].value = co.getFullYear() + "-" + com;
    	}
	}
}