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.transSpeed = "normal";
	
	this.noConflict = args.noConflict | false;
	
	this.args = args;
	var self = this;
	
	var $ = function(){
		if(arguments.length == 1 && typeof(arguments[0]) == "string")
			return jQuery("#" + self.id + " " + arguments[0]);
		else
			return jQuery(arguments);
	};
	
	this.init = function()
	{
		self.dateBoxes = [{start: $("#flights input.startDate"), end: $("#flights input.endDate")},
					 {start: $("#hotels input.startDate"), end: $("#hotels input.endDate")},
					 {start: $("#cars input.startDate"), end: $("#cars input.endDate")}];
		
		self.children = [{drop: $("#hotelChildren"), ages: $("#hotels .minors")}];
		
		self.initDates();
		self.initChildren();
		
		$("#flights form").submit(self.flights.validate);
		$("#flights input[name='dateTypeSelect']").click(self.flights.activateDate);
		$("#hotels form").submit(self.hotels.validate);
		$("#cars form").submit(self.cars.validate);
		$("#tabs li").click(function(){return self.activate($(this).attr("class").replace(" active", ""));});
	};
	
	jQuery(this.init);
	
	this.initChildren = function()
	{
		childBoxes = self.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($(this).val());
				
				ages = $(this).data("ages");
				
				if(numChildren > 0)
				{
					ages.fadeIn(self.transSpeed);
					
					for(j = 1; j <= 4; j++)
					{
						if(j <= numChildren)
							ages.find(".child" + j).fadeIn(self.transSpeed);
						else
							ages.find(".child" + j).fadeOut(self.transSpeed);
					}
				}
				else
					ages.fadeOut(self.transSpeed);
			});
		}
	}
	
	this.initDates = function()
	{
		var dateBoxes = self.dateBoxes, dateDepart, dateReturn;
		
		//Default dates
		if(!self.startDate)
		{
			dateDepart = new Date();
			dateDepart.addDays(1);
		}
		else
			dateDepart = self.startDate;
			
		if(!self.endDate)
		{
			dateReturn = new Date();
			dateReturn.addDays(2);
		}
		else
			dateReturn = self.endDate;
		
		//Set calendar to start at tomorrow.
		var opt = {
			minDate: new Date().addDays(1),
			maxDate: new Date().addDays(329),
			showOn: "both",
			buttonImageOnly: true,
			buttonText: "Choose date",
			buttonImage: imgroot + "/includes/images/cal_button.gif",
			//beforeShowDay: $.datepicker.setHolidays,
			changeYear: false,
			changeMonth: false,
			prevText: "Prev",
			nextText: "Next",
			hideIfNoPrevNext: true
		};
		
		for(var i = 0; i < dateBoxes.length; i++)
		{
			var dates = dateBoxes[i];
			dates.start.datepicker(opt);
			dates.end.datepicker(opt);
		}
	}
	
	this.activate = function(tab)
	{
		inEl = $("#" + tab);
		if(!inEl.hasClass(self.activeClass))
		{
			outEl = $(".tab." + self.activeClass);
			outEl.fadeOut(self.transSpeed, function() { outEl.removeClass(self.activeClass); inEl.addClass(self.activeClass); inEl.fadeIn(self.transSpeed); });
			$(" #tabs li.active").removeClass("active");
			$(" #tabs li." + tab).addClass("active");
		}
		return false;
	}
	
	this.hotels = new Object();
	
	this.hotels.activateRooms = function(rooms)
	{
		rows = $("#hotels .guests tr");
		for(i = 0; i <= rows.length; i++)
			if(i <= rooms)
				$(rows[i]).fadeIn(self.transSpeed);
			else
				$(rows[i]).fadeOut(self.transSpeed);
	}
	
	this.hotels.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			
			if($(hotelsStart).val().length == 0)
				reqMsg += "\tCheck-In Date\n";
			
			if($(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
			{
				if(self.hotels.xml)
				{
					$(hotelsStart).val(hotelsStart.value.replace(/\//g, "-"));
					$(hotelsEnd).val(hotelsEnd.value.replace(/\//g, "-"));
				}
				else
				{
					$(doa_mm).val((checkInDate.getMonth() + 1).pad(2));
					$(doa_dd).val(checkInDate.getDate().pad(2));
					$(doa_yy).val(checkInDate.getFullYear());
					$(dod_mm).val((checkOutDate.getMonth() + 1).pad(2));
					$(dod_dd).val(checkOutDate.getDate().pad(2));
					$(dod_yy).val(checkOutDate.getFullYear());
				}
				return true;
			}
		}
	}
	
	this.hotels.xml = args.hotelXml === true;
	
	this.flights = new Object();
	
	this.flights.activateDate = function()
	{
		outClass = ($(this).val() == "plusMinusDates" ? "exactDates" : "plusMinusDates");
			
		outEl = $("#flights .date ." + outClass);
		inEl = $("#flights .date ." + $(this).val());
		outEl.fadeOut(self.transSpeed, function() { inEl.fadeIn(self.transSpeed); });
		return true;
	}
	
	this.flights.validate = function()
	{
		var errMsg = "", fmtMsg = "", reqMsg = "";
		with (this)
		{
			if($(leavingFrom).val().length == 0)
				reqMsg += "\tDepart From\n";
		
			if($(goingTo).val().length == 0)
				reqMsg += "\tGoing To\n";
			
			if($(leavingDate).val().length == 0)
				reqMsg += "\tDepart Date\n";
			
			if($(returningDate).val().length == 0)
				reqMsg += "\tReturn Date\n";
				
			now = new Date();
			dateDepart = new Date($(leavingDate).val().replace(/-/g, "/"));
			dateReturn = new Date($(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 formatted 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
			{
				$(leavingDate).val(leavingDate.value.replace(/-/g, "/"));
				$(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($(puair).val().length == 0)
				reqMsg += "\tPick-up Location\n";
			if($(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
			{
				$(pu_month).val((leavingDate.getMonth() + 1).pad(2));
				$(pu_day).val(leavingDate.getDate().pad(2));
				$(do_month).val((returningDate.getMonth() + 1).pad(2));
				$(do_day).val(returningDate.getDate().pad(2));
				return true;
			}
		}
	}
}

Number.prototype.pad = function(digits) {
	n = this.toString();
	while (n.length < digits) n = '0' + n;
	return n;
}