
var maxdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var today = build_date();
var current = today;

var curset = null;
var matset = null;
var offset = 0;
var daterange = 0;
var separater = null;
var selmatch = false;
var seltoday = false;
var doublejump = false;

function open_cal(cset, mset, oset, drange, srater, smatch, stoday, djump)
{
	curset = cset;
	matset = mset;
	offset = oset;
	daterange = drange;
	separater = srater;
	selmatch = smatch;
	seltoday = stoday;
	doublejump = djump;

	current = build_date(curset);

	var cal = fetch_object('calendar');
	var icon = fetch_object(curset + '_icon');
	var place = position(icon);
	cal.style.left = place[0] + icon.offsetWidth + 2 + 'px';
	cal.style.top = place[1] +'px';

	build_cal();
	cal.style.display = ( (cal.style.display == 'block' || (typeof daterange == 'object' && !daterange.length)) ? 'none' : 'block' );

	return false;
}

function build_cal()
{
	var tempdate;
	var daycounter;
	var text;
	var classdef;
	var valid;

	for (var x = 0; x < 2; x++)
	{
		current.setMonth(current.getMonth() + x);

		current.setMonth(current.getMonth() - 1);
		fetch_object('calcell_m0' + x).title = months[current.getMonth()] + ' ' + current.getFullYear();
		current.setMonth(current.getMonth() + 1);
		fetch_object('calcell_m1' + x).innerHTML = months[current.getMonth()] + ' ' + current.getFullYear();
		current.setMonth(current.getMonth() + 1);
		fetch_object('calcell_m2' + x).title = months[current.getMonth()] + ' ' + current.getFullYear();

		current.setMonth(current.getMonth() - 1);

		daycounter = 1;

		for (var y = 0; y < 6; y++)
		{
			for (var z = 0; z < 7; z++)
			{
				classdef = ''; valid = true;
				tempdate = new Date(current.getFullYear(), current.getMonth(), daycounter);
				maxdays[1] = 28 + (Math.floor(current.getFullYear() / 4) == (current.getFullYear() / 4));

				if ((daycounter == 1 && tempdate.getDay() != z) || daycounter > maxdays[current.getMonth()])
				{
					text = '&nbsp;';
				}
				else
				{
					if (typeof daterange == 'object')
					{
						valid = (PHP.in_array(tempdate.getFullYear() + '-' + ( (tempdate.getMonth() + 1) < 10 ? '0' : '' ) + (tempdate.getMonth() + 1) + '-' + ( tempdate.getDate() < 10 ? '0' : '' ) + tempdate.getDate(), daterange) != -1);
					}
					else
					{
						if (daterange)
						{
							valid = (tempdate.getTime() >= today.getTime() && tempdate.getTime() <= new Date(today.getFullYear(), today.getMonth(), today.getDate() + daterange))
						}
						if (matset && !offset)
						{
							valid = valid && (tempdate.getTime() >= build_date(matset).getTime());
						}
					}
					if (tempdate.getTime() == today.getTime())
					{
						classdef += ' today';
						valid = valid && seltoday;
					}
					if (matset && tempdate.getTime() == build_date(matset).getTime())
					{
						classdef += ' matching';
						valid = valid && selmatch;
					}
					if (tempdate.getTime() == build_date(curset).getTime())
					{
						classdef += ' selected';
					}

					text = ( valid ? '<a href="#" onclick="return close_cal(' + tempdate.getMonth() + ', ' + tempdate.getDate() + ', ' + tempdate.getFullYear() + ')"' + ( classdef != '' ? ' class="' + classdef + '"' : '' ) + '>' + ( (new String(daycounter)).length == 1 ? '0' : '' ) + daycounter + '</a>' : '<span class="' + classdef + ' disabled">' + ( (new String(daycounter)).length == 1 ? '0' : '' ) + daycounter + '</a>' );
					daycounter++;
				}

				fetch_object('calcell_' + x + y + z).innerHTML = text;
			}
		}
	}
	if (!doublejump)
	{
		current.setMonth(current.getMonth() - 1);
	}
}

function close_cal(month, date, year)
{
	if (separater)
	{
		fetch_object(curset + '_text').value = ( (month + 1) < 10 ? '0' : '' ) + (month + 1) + separater + ( date < 10 ? '0' : '' ) + date + separater + year;
	}
	else if (typeof daterange == 'object')
	{
		var t_mons = fetch_object('depart_month');
		var tour = true;

		if (!t_mons)
		{
			t_mons = fetch_object(curset + '_month');
			tour = false;
		}
		for (var i = 0; i < t_mons.options.length; i++)
		{
			if (t_mons.options[i].value == month)
			{
				t_mons.selectedIndex = i
				break;
			}
		}
		if (tour)
		{
			var thecookie = fetch_tour_cookie();
			thecookie[4] = year + '-' + ( (month + 1) < 10 ? '0' : '' ) + (month + 1) + '-' + ( date < 10 ? '0' : '' ) + date;
			build_tour_dates(date);
			tour_fetch(3);
		}
		else
		{
			if (curset == 'air_leave')
			{
				build_charter_dates(2, date);
				charter_fetch(3);
			}
			else
			{
				build_charter_dates(3, date);
			}
		}
	}
	else
	{
		yearobj = fetch_object(curset + '_year');
		fetch_object(curset + '_month').selectedIndex = month;
		build_drop_days(curset);//curset.substring(( yearobj ? 7 : 4 ))
		fetch_object(curset + '_day').selectedIndex = date - 1;
		
		if (yearobj)
		{
			yearobj.selectedIndex = year - 2008;
		}
	}
	if (matset && offset)
	{
		var firdate = new Date(year, month, date);
		var matdate = new Date(year, month, date);
		matdate.setDate(matdate.getDate() + offset);
		month = matdate.getMonth();
		date = matdate.getDate();

		if (separater)
		{
			fetch_object(matset + '_text').value = ( (month + 1) < 10 ? '0' : '' ) + (month + 1) + separater + ( date < 10 ? '0' : '' ) + date + separater + matdate.getFullYear();
		}
		else
		{
			fetch_object(matset + '_month').selectedIndex = month;
			build_drop_days(matset);
			fetch_object(matset + '_day').selectedIndex = date - 1;
		}
	}

	curset = null;
	matset = null;
	offset = 0;
	daterange = 0;
	separater = null;
	seltoday = false;
	selmatch = false;
	doublejump = false;

	fetch_object('calendar').style.display = 'none';

	return false;
}

function change_month(amount)
{
	current.setMonth(current.getMonth() + amount);
	build_cal();
	return false;
}

function position(obj)
{
	var curleft = curtop = 0;

	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;

		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}

	return [curleft, curtop];
}

function build_date(read)
{
	var stamp = new Date();

	switch (typeof read)
	{
		case 'string':
			if (separater)
			{
				read = fetch_object(read + '_text').value;
				var bits = read.split(separater);
				return new Date(bits[2], bits[0] - 1, bits[1]);
			}
			else
			{
				var yearobj = fetch_object(read + '_year');
				var month = fetch_object(read + '_month');
				month = month.options[month.selectedIndex].value;
				var day = fetch_object(read + '_day');
				day = day.options[day.selectedIndex].value;
				return new Date(stamp.getFullYear() + ( yearobj ? yearobj.selectedIndex : ( (month < stamp.getMonth()) ? 1 : 0 ) ), month, day);
			}
		case 'number':
			stamp.setTime(read * 1000);
			return stamp;
		default:
			return new Date(stamp.getFullYear(), stamp.getMonth(), stamp.getDate());
	}
}

function build_drop_days(which)
{
	switch (fetch_object(which + '_month').selectedIndex)
	{
		case 1:
			var datetime = new Date();
			datetime.setTime(build_timestamp(2, 1, 0) * 1000);
			days = 28 + ( (Math.floor(datetime.getFullYear() / 4) == (datetime.getFullYear() / 4)) ? 1 : 0 );
			break;
		case 0:
		case 2:
		case 4:
		case 6:
		case 7:
		case 9:
		case 11:
			days = 31;
			break;
		default:
			days = 30
	}

	var day = fetch_object(which + '_day');
	var oldday = day.selectedIndex + 1;
	day.options.length = 0;

	for (var x = 1; x <= days; x++)
	{
		day.options[x - 1] = new Option(( x < 10 ? '0' : '' ) + x, x);
	}

	day.selectedIndex = ( oldday > days ? days : oldday ) - 1;
}

function build_timestamp(month, day, add)
{
	var datetime = new Date();
	datetime.setFullYear(datetime.getFullYear(), month, day);

	if (datetime < new Date())
	{
		datetime.setFullYear(datetime.getFullYear() + 1, month, day);
	}

	return Math.floor(datetime.getTime() / 1000 + add * 86400);
}

function string_date(stamp)
{
	return stamp.getFullYear() + '-' + ( (stamp.getMonth() + 1) < 10 ? '0' : '' ) + (stamp.getMonth() + 1) + '-' + ( stamp.getDate() < 10 ? '0' : '' ) + stamp.getDate();
}