if(typeof widgets=="undefined") widgets = new Object();
widgets.wct = function(args)
{
	this.id = args.id;
	this.activeClass = "active";
	this.dateActiveClass = "dateActive";
	
	this.transType = null; //null or fade.
	this.tansSpeed = "normal";
	
	var me, pre;
	
	me = this;
	pre = "#" + me.id + " ";
	
	jQuery(document).ready(function()
	{
		
		me.dateBoxes = [{start: jQuery(pre + "#flights input.startDate"), end: jQuery(pre + "#flights input.endDate")},
					 {start: jQuery(pre + "#hotels input.startDate"), end: jQuery(pre + "#hotels input.endDate")},
					 {start: jQuery(pre + "#cars input.startDate"), end: jQuery(pre + "#cars input.endDate")},
					 {start: jQuery(pre + "#activities input.startDate"), end: jQuery(pre + "#activities input.endDate")}];
		
		me.children = [{drop: jQuery(pre + "#hotelChildren"), ages: jQuery(pre + "#hotels .minors")}];
		
		me.initDates();
		me.initChildren();
		
		jQuery(pre + "#flights form").submit(me.flights.validate);
		jQuery(pre + "#flights input[name='dateTypeSelect']").click(me.flights.activateDate);
		jQuery(pre + "#hotels form").submit(me.hotels.validate);
		jQuery(pre + "#cars form").submit(me.cars.validate);
		jQuery(pre + "#activities form").submit(me.activities.validate);
	});
		
	this.initChildren = function()
	{
		childBoxes = me.children;
		
		for(var i = 0; i < childBoxes.length; i++)
		{
			childbox = childBoxes[i];
			
			childbox.drop.data("ages", childbox.ages);
			
			//Transition in/out age dropdowns.
			childbox.drop.change(function()
			{
				numChildren = new Number(jQuery(this).val());
				
				ages = jQuery(this).data("ages");
				
				for(var j = 1; j <= 4; j++)
				{
					if(j <= numChildren)
						ages.find(".child" + j + ":hidden").slideToggle(me.fadeSpeed);
					else
						ages.find(".child" + j + ":visible").slideToggle(me.fadeSpeed);
				}
			});
		}
	}
	
	this.initDates = function()
	{
		dateBoxes = me.dateBoxes;
		
		//Default dates
		if(!me.startDate)
		{
			dateDepart = new Date();
			dateDepart.addDays(1);
		}
		else
			dateDepart = me.startDate;
			
		if(!me.endDate)
		{
			dateReturn = new Date();
			dateReturn.addDays(2);
		}
		else
			dateReturn = me.endDate;
		
		//Set calendar to start at today.
		opt = {
			minDate: new Date()
		};
		
		for(var i = 0; i < dateBoxes.length; i++)
		{
			dates = dateBoxes[i];
			dates.start.datePicker(opt);
			dates.end.datePicker(opt);
			
			dates.start.val(dateDepart.asString());
			dates.end.val(dateReturn.asString());
			
			dates.start.data("end", dates.end);
			
			//Propagate manual date entries to the calendar and prefill end date.
			dates.start.change(function()
			{
				//Parse date.
				newDate = new Date(jQuery(this).val().replace(/-/g, "/"));
				
				//Calculate full year.
				yr = String(newDate.getYear());
				yr = "2" + String(Number(yr.substr(yr.length - 2, 2)).pad(3));
				newDate.setFullYear(yr);
				
				if(newDate.asString().length != jQuery(this).val().length)
					jQuery(this).val(newDate.asString());
					
				if(!isNaN(newDate))
				{
					jQuery(this).dpSetSelected(newDate.asString());
					end = jQuery(this).data("end");
					end.dpSetStartDate(newDate.addDays(1).asString());
					end.dpSetSelected(newDate.addDays(2).asString());
					end.val(newDate.asString());
				}
			});
			
			//Propagate manual date entries to the calendar.
			dates.end.change(function()
			{
				newDate = new Date(jQuery(this).val());
				if(!isNaN(newDate))
				{
					jQuery(this).dpSetSelected(newDate.asString());
				}
			});
		}
	}
	
	this.activate = function(tab)
	{
		inEl = jQuery("#" + tab);
		if(!inEl.hasClass(me.activeClass))
		{
			outEl = jQuery(pre + ".wctTab." + me.activeClass);
			
			if(me.transType && me.transType == "fade")
				outEl.fadeOut(me.transSpeed, function() { outEl.removeClass(me.activeClass); inEl.addClass(me.activeClass); inEl.fadeIn(me.transSpeed); });
			else
				outEl.hide(); outEl.removeClass(me.activeClass); inEl.addClass(me.activeClass); inEl.show();
			
			jQuery(pre + " #wctTabs li.active").removeClass("active");
			jQuery(pre + " #wctTabs li." + tab).addClass("active");
		}
		return false;
	}
	
	this.hotels = new Object();
	
	this.hotels.activateRooms = function(rooms)
	{
		rows = jQuery(pre + "#hotels .guests tr");
		for(i = 0; i <= rows.length; i++)
			if(i <= rooms)
				jQuery(rows[i]).fadeIn(me.transSpeed);
			else
				jQuery(rows[i]).fadeOut(me.transSpeed);
	}
	
	this.hotels.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			
			if(jQuery(hotelsStart).val().length == 0)
				reqMsg += "\tCheck-In Date\n";
			
			if(jQuery(hotelsEnd).val().length == 0)
				reqMsg += "\tCheck-Out Date\n";
				
			now = new Date();
			checkInDate = new Date(hotelsStart.value.replace(/-/g, "/"));
			checkOutDate = new Date(hotelsEnd.value.replace(/-/g, "/"));
			
			
			if(reqMsg == "")
			{
				if(isNaN(checkInDate))
					fmtMsg += "\tCheck-In Date\n";
				
				if(isNaN(checkOutDate))
					fmtMsg += "\tCheck-Out Date\n";
			}
			
			if(checkInDate < now)
				fmtMsg += "\tCheck-In Date must be a date in the future.\n";
			if(checkOutDate <= now)
				fmtMsg += "\tCheck-Out Date must be a date in the future.\n";
			if(checkOutDate < checkInDate)
				fmtMsg += "\tCheck-Out Date must be a date later than Check-In Date.\n";
		}
		if(reqMsg.length > 0)
			errMsg = "The form could not be submitted because the following required fields are blank:\n" + reqMsg;
		if(fmtMsg.length > 0 && reqMsg.length > 0)
			errMsg += "\nAdditionally, the following fields are formated invalid:\n" + fmtMsg;
		else if(fmtMsg.length > 0)
			errMsg = "The form could not be submitted because the following fields are formatted invalid:\n" + fmtMsg;
		if(errMsg.length > 0)
		{
			alert(errMsg);
			return false;
		}
		else
		{
			with(this)
			{
				if(me.hotels.xml)
				{
					jQuery(hotelsStart).val(hotelsStart.value.replace(/\//g, "-"));
					jQuery(hotelsEnd).val(hotelsEnd.value.replace(/\//g, "-"));
				}
				else
				{
					jQuery(doa_mm).val((checkInDate.getMonth() + 1).pad(2));
					jQuery(doa_dd).val(checkInDate.getDate().pad(2));
					jQuery(doa_yy).val(checkInDate.getFullYear());
					jQuery(dod_mm).val((checkOutDate.getMonth() + 1).pad(2));
					jQuery(dod_dd).val(checkOutDate.getDate().pad(2));
					jQuery(dod_yy).val(checkOutDate.getFullYear());
				}
			}
			return true;
		}
	}
	
	this.hotels.xml = args.hotelXml === true;
	
	this.flights = new Object();
	
	this.flights.activateDate = function()
	{
		inClass = jQuery(this).val();
		
		if(inClass == "plusMinusDates")
			outClass = "exactDates";
		else
			outClass = "plusMinusDates";
			
		outEl = jQuery(pre + "#flights .date ." + outClass);
		inEl = jQuery(pre + "#flights .date ." + inClass);
		outEl.fadeOut(me.transSpeed, function() { inEl.fadeIn(me.transSpeed); });
		return true;
	}
	
	this.flights.validate = function()
	{
		var errMsg = "", fmtMsg = "", reqMsg = "";
		with (this)
		{
			if(jQuery(leavingFrom).val().length == 0)
				reqMsg += "\tDepart From\n";
		
			if(jQuery(goingTo).val().length == 0)
				reqMsg += "\tGoing To\n";
			
			if(jQuery(leavingDate).val().length == 0)
				reqMsg += "\tDepart Date\n";
			
			if(jQuery(returningDate).val().length == 0)
				reqMsg += "\tReturn Date\n";
				
			now = new Date();
			dateDepart = new Date(jQuery(leavingDate).val().replace(/-/g, "/"));
			dateReturn = new Date(jQuery(returningDate).val().replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(dateDepart))
					fmtMsg += "\tDepart Date\n";
				
				if(isNaN(dateReturn))
					fmtMsg += "\tReturn Date\n";
			}
			
			if(dateDepart < now)
				fmtMsg += "\tDepart Date must be a date in the future.\n";
			if(dateReturn <= now)
				fmtMsg += "\tReturn Date must be a date in the future.\n";
			if(dateReturn < dateDepart)
				fmtMsg += "\tReturn Date must be a date later than Depart Date.\n";
		}
		
		if(reqMsg.length > 0)
			errMsg = "The form could not be submitted because the following required fields are blank:\n" + reqMsg;
		if(fmtMsg.length > 0 && reqMsg.length > 0)
			errMsg += "\nAdditionally, the following fields are formated invalid:\n" + fmtMsg;
		else if(fmtMsg.length > 0)
			errMsg = "The form could not be submitted because the following fields are formatted invalid:\n" + fmtMsg;
			
		if(errMsg.length > 0)
		{
			alert(errMsg);
			return false;
		}
		else
		{
			with(this)
			{
				jQuery(leavingDate).val(leavingDate.value.replace(/-/g, "/"));
				jQuery(returningDate).val(returningDate.value.replace(/-/g, "/"));
			}
			return true;
		}
	}
	
	this.cars = new Object();
	
	this.cars.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if(pu_date.value.length == 0)
				reqMsg += "\tPick-up Date\n";
			if(do_date.value.length == 0)
				reqMsg += "\tDrop-off Date\n";
			if(jQuery(puair).val().length == 0)
				reqMsg += "\tPick-up Location\n";
			if(jQuery(doair).val().length == 0)
				reqMsg += "\tDrop-off Location\n";
				
			now = new Date();
			leavingDate = new Date(pu_date.value.replace(/-/g, "/"));
			returningDate = new Date(do_date.value.replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(leavingDate))
					fmtMsg += "\tPick-up Date\n";
				
				if(isNaN(returningDate))
					fmtMsg += "\tDrop-off Date\n";
			}
			
			if(leavingDate < now)
				fmtMsg += "\tPick-up date date must be a date in the future.\n";
			if(returningDate <= now)
				fmtMsg += "\tDrop-off date must be a date in the future.\n";
			if(returningDate < leavingDate)
				fmtMsg += "\tPick-up date must be a date later than Pick-up date.\n";
		}
		if(reqMsg.length > 0)
			errMsg = "The form could not be submitted because the following required fields are blank:\n" + reqMsg;
		if(fmtMsg.length > 0 && reqMsg.length > 0)
			errMsg += "\nAdditionally, the following fields are formated invalid:\n" + fmtMsg;
		else if(fmtMsg.length > 0)
			errMsg = "The form could not be submitted because the following fields are formatted invalid:\n" + fmtMsg;
		if(errMsg.length > 0)
		{
			alert(errMsg);
			return false;
		}
		else
		{
			with(this)
			{
				jQuery(pu_month).val((leavingDate.getMonth() + 1).pad(2));
				jQuery(pu_day).val(leavingDate.getDate().pad(2));
				jQuery(do_month).val((returningDate.getMonth() + 1).pad(2));
				jQuery(do_day).val(returningDate.getDate().pad(2));
			}
			return true;
		}
	}
	
	this.activities = new Object();
	
	this.activities.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if(actStart.value.length == 0)
				reqMsg += "\tStart Date\n";
			if(actEnd.value.length == 0)
				reqMsg += "\tThru\n";
			if(jQuery("#activities input:checkbox").filter("input:checked").length == 0)
				reqMsg += "\tActivity\n";
				
			var now = new Date();
			var leavingDate = new Date(actStart.value.replace(/-/g, "/"));
			var returningDate = new Date(actEnd.value.replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(leavingDate))
					fmtMsg += "\tStart Date\n";
				
				if(isNaN(returningDate))
					fmtMsg += "\tThru Date\n";
			}
			
			if(leavingDate < now)
				fmtMsg += "\tStart date must be a date in the future.\n";
			if(returningDate <= now)
				fmtMsg += "\tThru must be a date in the future.\n";
			if(returningDate < leavingDate)
				fmtMsg += "\tThru must be a date later than Start date.\n";
		}
		
		if(reqMsg.length > 0)
			errMsg = "The form could not be submitted because the following required fields are blank:\n" + reqMsg;
		if(fmtMsg.length > 0 && reqMsg.length > 0)
			errMsg += "\nAdditionally, the following fields are formated invalid:\n" + fmtMsg;
		else if(fmtMsg.length > 0)
			errMsg = "The form could not be submitted because the following fields are formatted invalid:\n" + fmtMsg;
			
		if(errMsg.length > 0)
		{
			alert(errMsg);
			return false;
		}
		else
			return true;
	}
}

Number.prototype.pad = function(digits) {
	n = this.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}