if(typeof widgets=="undefined") widgets = new Object();
widgets.wct = function(id, flightsDateId)
{
	this.id = id;
	this.activeClass = "tabActive";
	this.dateActiveClass = "dateActive";
	this.fadeSpeed = "normal";
	me = this;
	pre = "#" + me.id + " ";
	
	$(document).ready(function(){
		
		me.dateBoxes = [{start: $(pre + "#flights input.startDate"), end: $(pre + "#flights input.endDate")},
					 {start: $(pre + "#hotels input.startDate"), end: $(pre + "#hotels input.endDate")},
					 {start: $(pre + "#packages input.startDate"), end: $(pre + "#packages input.endDate")},
					 {start: $(pre + "#cars input.startDate"), end: $(pre + "#cars input.endDate")},
					 {start: $(pre + "#activities input.startDate"), end: $(pre + "#activities input.endDate")}];
		
		me.children = [{drop: $(pre + "#hotelsChildren"), ages: $(pre + "#hotels .minors")},
					{drop: $(pre + "#packagesChildren"), ages: $(pre + "#packages .minors")},
					{drop: $(pre + "#FO_children"), ages: $(pre + "#flights .minors")}];
		
		me.initDates();
		me.initChildren();
		
		$(pre + "#flights form").submit(me.flights.validate);
		$(pre + "#flights input[name='dateTypeSelect']").click(me.flights.activateDate);
		$(pre + "#hotels form").submit(me.hotels.validate);
		$(pre + "#packages form").submit(me.packages.validate);
		$(pre + "#cars form").submit(me.cars.validate);
		$(pre + "#activities form").submit(me.activities.validate);
	});
		
	this.initChildren = function()
	{
		childBoxes = me.children;
		
		for(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");
				
				for(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
		dateDepart = new Date();
		dateDepart.addDays(1);
		dateReturn = new Date();
		dateReturn.addDays(2);
		
		//Set calendar to start at today.
		opt = {
			minDate: new Date()
		};
		
		for(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($(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 != $(this).val().length)
					$(this).val(newDate.asString());
					
				if(!isNaN(newDate))
				{
					$(this).dpSetSelected(newDate.asString());
					end = $(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($(this).val());
				if(!isNaN(newDate))
				{
					$(this).dpSetSelected(newDate.asString());
				}
			});
		}
	}
	
	this.activate = function(tab)
	{
		inEl = $("#" + tab);
		if(!inEl.hasClass(me.activeClass))
		{
			outEl = $(pre + "." + me.activeClass);
			outEl.fadeOut(me.fadeSpeed, function() { outEl.removeClass(me.activeClass); inEl.addClass(me.activeClass); inEl.fadeIn(me.fadeSpeed); });
			$(pre + " #tabs li.active").removeClass("active");
			$(pre + " #tabs li." + tab).addClass("active");
		}
		return false;
	}
	
	this.hotels = new Object();
	
	this.hotels.activateRooms = function(rooms)
	{
		rows = $(pre + "#hotels .guests tr");
		for(i = 0; i <= rows.length; i++)
			if(i <= rooms)
				$(rows[i]).fadeIn(me.fadeSpeed);
			else
				$(rows[i]).fadeOut(me.fadeSpeed);
	}
	
	this.hotels.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
		
			if($(region).val().length == 0)
				reqMsg += "\tHotels In\n";
			
			if($(checkIn).val().length == 0)
				reqMsg += "\tCheck In\n";
			
			if($(checkOut).val().length == 0)
				reqMsg += "\tCheck Out\n";
				
			now = new Date();
			checkInDate = new Date(checkIn.value.replace(/-/g, "/"));
			checkOutDate = new Date(checkOut.value.replace(/-/g, "/"));
			
			
			if(reqMsg == "")
			{
				if(isNaN(checkInDate))
					fmtMsg += "\tCheck In\n";
				
				if(isNaN(checkOutDate))
					fmtMsg += "\tCheck Out\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)
			{
				$(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());
				
				/*
				rCode = $(region).val();
				
				switch(rCode)
				{
					case "KIN": // Kingston
						$(t1_zip).val("JMAKN04");
						$(src).val("10021999");
						$(t1_city).val("Kingston");
						break;
					case "MON": // Montego Bay
						$(t1_zip).val("JMCJS12");
						$(src).val("10022000");
						$(t1_city).val("Montego Bay");
						break;
					case "NEG": // Negril
						$(t1_zip).val("JMDWD14");
						$(src).val("10022001");
						$(t1_city).val("Negril");
						break;
					case "OCH": // Ocho Rios
						$(t1_zip).val("JMCAN19");
						$(src).val("10022002");
						$(t1_city).val("Ocho Rios");
						break;
					case "POR": // Port Antonio
						$(t1_zip).val("JMBPD15");
						$(src).val("10022003");
						$(t1_city).val("Port Antonio");
						break;
					case "STE": // Saint Elizabeth
						$(t1_zip).val("JMDCN12");
						$(src).val("10022004");
						//$(t1_city).val("Saint Elizabeth");
						$(t1_city).val("Westmoreland|10022004");
						break;
					case "WES": // Westmoreland
						$(t1_zip).val("JMDCN12");
						$(src).val("10022004");
						//$(t1_city).val("Westmoreland");
						$(t1_city).val("Westmoreland|10022004");
						break;
					case "SOU": // South Coast
						$(t1_zip).val("JMDCN12");
						$(src).val("10022004");
						//$(t1_city).val("Westmoreland");
						$(t1_city).val("Westmoreland|10022004");
						break;
					default: // All Resort Areas
						$(t1_zip).val("JMDCN12");
						$(src).val("10021926");
						$(t1_city).val("");
				}
				
				//Switch to city search for South Coast.
				if(rCode == "STE" || rCode == "WES" || rCode == "SOU")
					$(tab).val("tab0");
				else
					$(tab).val("tab1");
				
				$(city).val($(t1_city).val());
				*/
				$(src).val("10024980");
			}
			return true;
		}
	}
	
	this.flights = new Object();
	
	this.flights.activateDate = function()
	{
		inClass = $(this).val();
		
		if(inClass == "plusMinusDates")
			outClass = "exactDates";
		else
			outClass = "plusMinusDates";
			
		outEl = $(pre + "#flights .date ." + outClass);
		inEl = $(pre + "#flights .date ." + inClass);
		outEl.fadeOut(me.fadeSpeed, function() { inEl.fadeIn(me.fadeSpeed); });
		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 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)
			{
				$(leavingDate).val(leavingDate.value.replace(/-/g, "/"));
				$(returningDate).val(returningDate.value.replace(/-/g, "/"));
			}
			return true;
		}
	}
	
	this.packages = new Object();
	
	this.packages.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if($(leavingFrom).val().length == 0)
				reqMsg += "\tDepart From\n";
			if($(goingTo).val().length == 0)
				reqMsg += "\tGoing To\n";
			//if($(polygonId).val().length == 0)
			//	reqMsg += "\tStaying In\n";
			if(leaving.value.length == 0)
				reqMsg += "\tDepart Date\n";
			if(returning.value.length == 0)
				reqMsg += "\tReturn Date\n";
				
			now = new Date();
			leavingDate = new Date(leaving.value.replace(/-/g, "/"));
			returningDate = new Date(returning.value.replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(leavingDate))
					fmtMsg += "\tDepart Date\n";
				
				if(isNaN(returningDate))
					fmtMsg += "\tReturn Date\n";
			}
			
			if(leavingDate < now)
				fmtMsg += "\tDepart Date date must be a date in the future.\n";
			if(returningDate <= now)
				fmtMsg += "\tReturn Date must be a date in the future.\n";
			if(returningDate < leavingDate)
				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)
			{
				$(dateLeavingMonth).val((leavingDate.getMonth() + 1).pad(2));
				$(dateLeavingDay).val(leavingDate.getDate().pad(2));
				$(dateLeavingYear).val(leavingDate.getFullYear());
				$(dateReturningMonth).val((returningDate.getMonth() + 1).pad(2));
				$(dateReturningDay).val(returningDate.getDate().pad(2));
				$(dateReturningYear).val(returningDate.getFullYear());
			}
			return true;
		}
	}
	
	this.cars = new Object();
	
	this.cars.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if(chk_in.value.length == 0)
				reqMsg += "\tPick-up Date\n";
			if(chk_out.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(chk_in.value.replace(/-/g, "/"));
			returningDate = new Date(chk_out.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)
			{
				$(pu_year).val(leavingDate.getFullYear());
				$(pu_month).val((leavingDate.getMonth() + 1));
				$(pu_day).val(leavingDate.getDate());
				
				$(do_year).val(returningDate.getFullYear());
				$(do_month).val((returningDate.getMonth() + 1));
				$(do_day).val(returningDate.getDate());
			}
			return true;
		}
	}
	
	this.activities = new Object();
	
	this.activities.validate = function()
	{
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if(fromdate.value.length == 0)
				reqMsg += "\tStart Date\n";
			if(todate.value.length == 0)
				reqMsg += "\tThru\n";
			if($("#activities input:checkbox").filter("input:checked").length == 0)
				reqMsg += "\tActivity\n";
				
			now = new Date();
			leavingDate = new Date(fromdate.value.replace(/-/g, "/"));
			returningDate = new Date(todate.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;
}


var travelocityWidget = widgets.wct;
var travelocity = new travelocityWidget("travelocity");

