today = new Date();
mons = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

function process_tour_form(whichpage)
{
	var t_mons = fetch_object('depart_month');
	var t_days = fetch_object('depart_day');
	var value = new Date();
	value.setFullYear(today.getFullYear(), t_mons.options[t_mons.selectedIndex].value, t_days.options[t_days.selectedIndex].value);
	
	if (value < today)
	{
		value.setFullYear(value.getFullYear() + 1);
	}
	
	var origin_day_val;
	fetch_object('tour_leave_date').value = origin_day_val = string_date(value);
	var origin = fetch_object('origin');
	var origin_val = origin.options[origin.selectedIndex].value;
	var destin = fetch_object('destin');
	var destin_val = destin.options[destin.selectedIndex].value;
	var weeks = fetch_object('durat');
	var weeks_val = weeks.options[weeks.selectedIndex].value;
	var flex = fetch_object('flex');
	var flex_val = flex.options[flex.selectedIndex].value;
	var hotelchain = fetch_object('hotels');
	var hotelchain_val = hotelchain.options[hotelchain.selectedIndex].value;

	//send it off to be recorded
	var com = new vB_AJAX_Handler(true);
	com.send('http://www.cheapticketscanada.com/forum/form_record.php', 'rec_type=tour&searchform=' + whichpage + '&origin=' + origin_val + '&destin=' + destin_val + '&dateleave=' + origin_day_val + '&weeks=' + weeks_val + '&flex=' + flex_val + '&hotelchain=' + hotelchain_val);

	//create cookies
	var thecookie = [origin_val, destin_val, hotelchain_val, weeks_val, origin_day_val, flex.selectedIndex];

	today.setDate(today.getDate() + 7); //7 days from now
	set_cookie('tour_form', thecookie.join('|'), today);

	return true;
}

function build_tour_dates(pickval)
{
	var t_mons = fetch_object('depart_month');
	var t_days = fetch_object('depart_day');
	t_days.options.length = 0;
	t_days.selectedIndex = 0;
	var count = 0;
	var value;

	for (var i = 0; i < tdc_dateArray.length; i++)
	{
		value = tdc_dateArray[i].split('-');

		if (value[1] - 1 == t_mons.options[t_mons.selectedIndex].value)
		{
			t_days.options[count] = new Option(value[2], value[2]);
			
			if (value[2] == pickval)
			{
				t_days.selectedIndex = count;
			}

			count++;
		}
	}
}

function is_empty(variable)
{
	return (variable == null || variable == false || variable == '' || variable == 0);
}

function fetch_tour_cookie()
{
	return ( fetch_cookie('tour_form') ? fetch_cookie('tour_form').split('|') : [null, null, null, 7, string_date(today), 0] );
}

function tour_fetch(level)
{
	var thecookie = fetch_tour_cookie();
	var toursub = fetch_object('tour_submit');
	var t_mons = fetch_object('depart_month');
	var t_days = fetch_object('depart_day');
	var progress = ['origin', 'destination', 'hotels', 'duration'];
	var dropdowns = ['origin', 'destin', 'hotels', 'durat'];
	var defaults = ['Type', 'Origin', 'Destination', 'Date'];
	var sendurl = 'object=' + progress[level];
	toursub.disabled = true;
	var value;
	var i;

	for (i = 3; i >= 0; i--)
	{
		value = fetch_object(dropdowns[i]);

		if (i >= level)
		{
			value.options.length = 0;
			value.options[0] = new Option('Select ' + defaults[i], '');
			value.selectedIndex = 0;
			value.disabled = true;

			if (i == 2)
			{
				tdc_dateArray = new Array();
				fetch_object('depart_month').disabled = fetch_object('depart_day').disabled = true;				
			}
		}
		else
		{
			if (i == 2)
			{
				value = new Date();
				value.setFullYear(today.getFullYear(), t_mons.options[t_mons.selectedIndex].value, t_days.options[t_days.selectedIndex].value);
				
				if (value < today)
				{
					value.setFullYear(value.getFullYear() + 1);
				}
				
				value = string_date(value);
			}
			else
			{
				value = value.options[value.selectedIndex].value;
			}
			if (!is_empty(value))
			{
				sendurl = sendurl + '&' + ( i == 2 ? 'ddate' : progress[i] ) + '=' + value;
			}
			else
			{
				return;				
			}

			thecookie[i] = value;
		}
	}

	var dropdown = fetch_object(dropdowns[level]);
	var tsgres = new vB_AJAX_Handler(true);
	tsgres.onreadystatechange(function()
	{
		if (tsgres.handler.readyState == 4 && tsgres.handler.status == 200)
		{
			eval(tsgres.handler.responseText);
			dropdown.options.length = 0;
			var advance = false;
			var indexes = [0, 0];
			var nonblank = [0, 0];
			
			if (level == 2)
			{
				dropdown.options[0] = new Option('All Properties', '');
				indexes[0]++;
			}
			for (i = 0; i < tdc_valueArray.length; i++)
			{
				indexes[1] = i + indexes[0];
				dropdown.options[indexes[1]] = new Option(tdc_textArray[i], tdc_valueArray[i]);
				
				if (is_empty(tdc_valueArray[i]))
				{
					dropdown.options[indexes[1]].style.color = 'blue';
				}
				else
				{
					nonblank[0]++;
					nonblank[1] = indexes[1];
				}
				if (tdc_valueArray[i] == thecookie[level] && !is_empty(thecookie[level]))
				{
					dropdown.selectedIndex = indexes[1];
					advance = true;
				}
			}
			if (!advance && nonblank[0] == 1)
			{
				dropdown.selectedIndex = nonblank[1];
				advance = true;
			}
			if (advance)
			{
				thecookie[level] = dropdown.options[dropdown.selectedIndex].value;
				
				if (level < 2)
				{
					tour_fetch(level + 1);
				}
			}

			dropdown.disabled = false;

			if (level == 3)
			{
				toursub.disabled = false;
			}
		}
	});

	dropdown.options[0].text = 'Please Wait';
	tsgres.send('http://www.cheapticketscanada.com/proxy.php?type=tour', sendurl);

	if (level == 2)
	{
		var tsgresb = new vB_AJAX_Handler(true);
		tsgresb.onreadystatechange(function()
		{
			if (tsgresb.handler.readyState == 4 && tsgresb.handler.status == 200)
			{
				var curobj;
				var usedate = null;
				var curdate = new Date();
				var refdate = new Date();
				var thedates = new Array();
				var addmons = new Array();
				refdate.setFullYear(today.getFullYear(), t_mons.options[t_mons.selectedIndex].value, t_days.options[t_days.selectedIndex].value);

				if (refdate < today)
				{
					refdate.setFullYear(refdate.getFullYear() + 1);
				}

				eval(tsgresb.handler.responseText);
				t_mons.options.length = 0;
				t_mons.selectedIndex = 0;
				var count = 0;

				for (i = 0; i < tdc_dateArray.length; i++)
				{
					curobj = tdc_dateArray[i].split('-');
					curobj[1]--;
					curdate.setFullYear(curobj[0], curobj[1], curobj[2]);
					thedates[i] = [curobj, curdate];
					
					if (addmons[curobj[1]] == null)
					{
						t_mons.options[count] = new Option(mons[curobj[1]], curobj[1]);
						addmons[curobj[1]] = count;
						count++;
					}
					if (usedate == null && curdate >= refdate)
					{
						usedate = i - ( (i != 0 && (curdate - refdate) > (refdate - thedates[i - 1][0])) ? 1 : 0 );
					}
				}
				if (usedate == null)
				{
					usedate = tdc_dateArray.length - 1;
				}

				thecookie[4] = tdc_dateArray[usedate];
				t_mons.selectedIndex = addmons[thedates[usedate][0][1]];
				build_tour_dates(thedates[usedate][0][2]);
				t_mons.disabled = t_days.disabled = false;
				tour_fetch(3);
			}
		});

		tsgresb.send('http://www.cheapticketscanada.com/proxy.php?type=tour', 'object=dates' + sendurl.substr(13));
	}
}

function init_tour_form()
{
	var thecookie = fetch_tour_cookie();
	fetch_object('flex').selectedIndex = thecookie[5];
	var temp = thecookie[4].split('-');
	fetch_object('depart_month').options[0] = new Option(mons[temp[1] - 1], temp[1] - 1);
	fetch_object('depart_day').options[0] = new Option(temp[2], temp[2]);

	if (is_empty(document.referrer) || fetch_object('vac_select_tour').checked || thecookie[0])
	{
		vac_enable();
	}
}

function vac_enable()
{
	fetch_object('vac_select_tour').checked = true;
	fetch_object('vac_message').style.display = 'none';
	tour_fetch(0);
}