if(typeof widgets=="undefined") widgets = new Object();
widgets.wct = function(args)
{
	this.id = args.id;
	this.activeClass = "sv_active";
	this.dateActiveClass = "sv_dateActive";
	
	this.transType = null; //null or fade.
	this.tansSpeed = "normal";
	
	this.noConflict = args.noConflict | false;
	
	var me = this;
	var pre = "#" + me.id + " ";
	var jQ;
	
	//Prevent prototype.js conflicts.
	if(this.noConflict)
	{
		jQ = jQuery;
		jQ.noConflict();
	}
	else
		jQ = $;
	
	jQ(document).ready(function()
	{
		
		me.dateBoxes = [{start: jQ(pre + "#sv_flights input.sv_startDate"), end: jQ(pre + "#sv_flights input.sv_endDate")},
					 {start: jQ(pre + "#sv_hotels input.sv_startDate"), end: jQ(pre + "#sv_hotels input.sv_endDate")},
					 {start: jQ(pre + "#sv_packages input.sv_startDate"), end: jQ(pre + "#sv_packages input.sv_endDate")},
					 {start: jQ(pre + "#sv_cars input.sv_startDate"), end: jQ(pre + "#sv_cars input.sv_endDate")},					 
					 {start: jQ(pre + "#sv_activities input.sv_startDate"), end: jQ(pre + "#sv_activities input.sv_endDate")}];
		
		me.children = 
					[
						{drop: jQ(pre + "#hotelChildren"), ages: jQ(pre + "#sv_hotels .sv_minors")},
						{drop: jQ(pre + "#flightsChildren"), ages: jQ(pre + "#sv_flights .sv_minors")},
						{drop: jQ(pre + "#packagesChildren"), ages: jQ(pre + "#sv_packages .sv_minors")}
					];
		
		me.initDates();
		me.initChildren();
		
		jQ(pre + "#sv_flights form").submit(me.flights.validate);
		jQ(pre + "#sv_flights input[name='dateTypeSelect']").click(me.flights.activateDate);
		jQ(pre + "#sv_hotels form").submit(me.hotels.validate);
		jQ(pre + "#sv_packages form").submit(me.packages.validate);
		jQ(pre + "#sv_activities form").submit(me.activities.validate);
		jQ(pre + "#sv_cars form").submit(me.cars.validate);
	});
		
	this.initChildren = function()
	{
		childBoxes = me.children;
		
		for(var childIndex = 0; childIndex < childBoxes.length; childIndex++)
		{
			var childbox = childBoxes[childIndex];
			
			childbox.drop.data("ages", childbox.ages);
			
			//Transition in/out age dropdowns.
			childbox.drop.change(function()
			{
				var numChildren = new Number(jQ(this).val());
				
				var ages = jQ(this).data("ages");
				
				for(var ageIndex = 1; ageIndex <= 4; ageIndex++)
				{
					if(ageIndex <= numChildren){
						ages.find(".sv_child" + ageIndex + ":hidden").fadeIn(me.transSpeed);
					}
					else
						ages.find(".sv_child" + ageIndex + ":visible").fadeOut(me.transSpeed);
				}
				
				var label = ages.find("label");
				
				if(numChildren > 0)
					label.fadeIn(me.transSpeed);
				else
					label.fadeOut(me.transSpeed);
			});
		}
	}
	
	this.initDates = function()
	{
		var dateBoxes = me.dateBoxes, dateDepart, dateReturn;
		
		//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.
		var opt = {
			minDate: new Date(),
			clickInput: true,
			createButton: false,
			numberOfMonths: 1,
			mandatory: true,
			changeMonth: false,
			changeYear: false,
			prevText: 'Previous',
			nextText: 'Next'
		};




		for(var i = 0; i < dateBoxes.length; i++)
		{
			var dates = dateBoxes[i];

			dates.start.datepicker(opt);
			dates.end.datepicker(opt);

			$("#ui-datepicker-div").addClass("eventSearch")
			

			dates.start.val(dateDepart.asString());
			dates.end.val(dateReturn.asString());
			
			//Remove focus from date boxes so the cursor doesn't show through calendar.
			//dates.start.bind('click', function() { this.blur(); });
			//dates.end.bind('click', function() { this.blur(); });
			
			dates.start.siblings(".dp-choose-date").data("box", dates.start);
			dates.end.siblings(".dp-choose-date").data("box", dates.end);
			
			dates.start.siblings(".dp-choose-date").bind("click", function() { jQ(this).data("box").dpDisplay(); return false; });
			dates.end.siblings(".dp-choose-date").bind("click", function() { jQ(this).data("box").dpDisplay(); return false; });
			
			dates.start.data("end", dates.end);

			//Propagate manual date entries to the calendar and prefill end date.
			dates.start.change(function()
			{
				
				//Parse date.
				var newDate = new Date(jQ(this).val().replace(/-/g, "/"));
				
				//Calculate full year.
				var yr = String(newDate.getYear());
				yr = "2" + String(Number(yr.substr(yr.length - 2, 2)).pad(3));
				newDate.setFullYear(yr);
				
				if(newDate.asString().length != jQ(this).val().length)
					jQ(this).val(newDate.asString());
					
				if(!isNaN(newDate))
				{

					var end = jQ(this).data("end");
					dates.end.datepicker({minDate: newDate.addDays(1),defaultDate:newDate.addDays(1)});
				}
			});
			
			//Propagate manual date entries to the calendar.
			dates.end.change(function()
			{
				var newDate = new Date(jQ(this).val());
				if(!isNaN(newDate))
				{
					//dates.end.datepicker("setDate",newDate.asString());
				}
			});
		}
	}
	
	this.activate = function(tab)
	{
	var inEl = jQ("#" + tab);
	    // --- changes tab background color based on tab variable
		//jQ("#tabContainer").removeClass();
		//jQ("#tabContainer").addClass(tab);
		
		if(!inEl.hasClass(me.activeClass))
		{
			var outEl = jQ(pre + ".sv_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();
			
			jQ(pre + " #sv_wctTabs li.sv_active").removeClass("sv_active");
			jQ(pre + " #sv_wctTabs li." + tab).addClass("sv_active");
		}
		return false;
	}
	
	this.hotels = new Object();
	
	this.hotels.activateRooms = function(rooms)
	{
		var rows = jQ(pre + "#sv_hotels .sv_guests tr");
		for(var i = 0; i <= rows.length; i++)
			if(i <= rooms)
				jQ(rows[i]).fadeIn(me.transSpeed);
			else
				jQ(rows[i]).fadeOut(me.transSpeed);
	}
	
	this.hotels.validate = function()
	{
		var errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
		
			if(jQ(city).val().length == 0)
				reqMsg += "\tHotels in\n";
			
			if(jQ(hotelsStart).val().length == 0)
				reqMsg += "\tCheck-in\n";
			
			if(jQ(hotelsEnd).val().length == 0)
				reqMsg += "\tCheck-out\n";
				
			var now = new Date();
			var currdate = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
			var checkInDate = new Date(hotelsStart.value.replace(/-/g, "/"));
			var checkOutDate = new Date(hotelsEnd.value.replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(checkInDate))
					fmtMsg += "\tCheck-in\n";
				
				if(isNaN(checkOutDate))
					fmtMsg += "\tCheck-out\n";
			}
			
			if(checkInDate < currdate)
				fmtMsg += "\tCheck-in cannot be a date before today.\n";
			if(checkOutDate <= currdate)
				fmtMsg += "\tCheck-out must be a date in the future.\n";
			if(checkOutDate < checkInDate)
				fmtMsg += "\tCheck-out must be a date later than Check-in.\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)
				{
					jQ(hotelsStart).val(hotelsStart.value.replace(/\//g, "-"));
					jQ(hotelsEnd).val(hotelsEnd.value.replace(/\//g, "-"));
				}
				else
				{
					jQ(doa_mm).val((checkInDate.getMonth() + 1).pad(2));
					jQ(doa_dd).val(checkInDate.getDate().pad(2));
					jQ(doa_yy).val(checkInDate.getFullYear());
					jQ(dod_mm).val((checkOutDate.getMonth() + 1).pad(2));
					jQ(dod_dd).val(checkOutDate.getDate().pad(2));
					jQ(dod_yy).val(checkOutDate.getFullYear());
				}
			}
			return true;
		}
	}
	
	this.hotels.xml = args.hotelXml === true;
	
	this.flights = new Object();
	
	this.flights.activateDate = function()
	{
		var inClass = jQ(this).val(), outClass;
		
		if(inClass == "plusMinusDates")
			outClass = "exactDates";
		else
			outClass = "plusMinusDates";
			
		var outEl = jQ(pre + "#sv_flights .date ." + outClass);
		var inEl = jQ(pre + "#sv_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(jQ(leavingFrom).val().replace("Departing from", "").length == 0)
				reqMsg += "\tDeparting from\n";
		
			if(jQ(goingTo).val().length == 0)
				reqMsg += "\tGoing to\n";
			
			if(jQ(leavingDate).val().length == 0)
				reqMsg += "\tDepart\n";
			
			if(jQ(returningDate).val().length == 0)
				reqMsg += "\tReturn\n";
				
			var now = new Date();
			var currdate = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
			var dateDepart = new Date(jQ(leavingDate).val().replace(/-/g, "/"));
			var dateReturn = new Date(jQ(returningDate).val().replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(dateDepart))
					fmtMsg += "\tDepart\n";
				
				if(isNaN(dateReturn))
					fmtMsg += "\tReturn\n";
			}
			
			if(dateDepart < currdate)
				fmtMsg += "\tDepart cannot be a date before today.\n";
			if(dateReturn < currdate)
				fmtMsg += "\tReturn cannot be a date before today.\n";
			if(dateReturn < dateDepart)
				fmtMsg += "\tReturn must be a date later than Depart.\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)
			{
				jQ(leavingDate).val(leavingDate.value.replace(/-/g, "/"));
				jQ(returningDate).val(returningDate.value.replace(/-/g, "/"));
			}
			return true;
		}
	}
	
	this.packages = new Object();
	
	this.packages.validate = function()
	{
		var errMsg = "", fmtMsg = "", reqMsg = "";
		with (this)
		{
			if(jQ(leavingFrom).val().replace("Departing from", "").length == 0)
				reqMsg += "\tDeparting from\n";
		
			if(jQ(goingTo).val().length == 0)
				reqMsg += "\tGoing to\n";
			
			if(jQ(packages_leavingDate).val().length == 0)
				reqMsg += "\tDepart\n";
			
			if(jQ(packages_returningDate).val().length == 0)
				reqMsg += "\tReturn\n";
				
			var now = new Date();
			var currdate = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
			var dateDepart = new Date(jQ(packages_leavingDate).val().replace(/-/g, "/"));
			var dateReturn = new Date(jQ(packages_returningDate).val().replace(/-/g, "/"));
			
			if(reqMsg == "")
			{
				if(isNaN(dateDepart))
					fmtMsg += "\tDepart\n";
				
				if(isNaN(dateReturn))
					fmtMsg += "\tReturn\n";
			}
			
			if(dateDepart < currdate)
				fmtMsg += "\tDepart cannot be a date before today.\n";
			if(dateReturn <= currdate)
				fmtMsg += "\tReturn must be a date in the future.\n";
			if(dateReturn < dateDepart)
				fmtMsg += "\tReturn must be a date later than Depart.\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)
			{
				jQ(dateLeavingMonth).val((dateDepart.getMonth() + 1).pad(2));
				jQ(dateLeavingDay).val(dateDepart.getDate().pad(2));
				jQ(dateLeavingYear).val(dateDepart.getFullYear());
				jQ(dateReturningMonth).val((dateReturn.getMonth() + 1).pad(2));
				jQ(dateReturningDay).val(dateReturn.getDate().pad(2));
				jQ(dateReturningYear).val(dateReturn.getFullYear());
			}
			return true;
		}
	}
	
	this.activities = new Object();
	
	this.activities.validate = function()
	{
		var errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)
		{
			if(actStart.value.length == 0)
				reqMsg += "\tStart Date\n";
			if(actEnd.value.length == 0)
				reqMsg += "\tThru\n";
			if(jQ("#sv_activities input:checkbox").filter("input:checked").length == 0)
				reqMsg += "\tActivity\n";
				
			var now = new Date();
			var currdate = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
			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 < currdate)
				fmtMsg += "\tStart date cannot be a date before today.\n";
			if(returningDate < currdate)
				fmtMsg += "\tThru cannot be a date before today.\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;
	}
	/*************************CARS*****************************/
	this.cars = new Object();
	
	this.cars.validate = function(){
		var errMsg = "", reqMsg = "", fmtMsg = "";
		with(this){
	
			if(jQ(carsPickupDate).val().length == 0)
				reqMsg += "\tPick-up Date\n";
			
			if(jQ(carsDropoffDate).val().length == 0)
				reqMsg += "\tDrop-off Date\n";
				
			var now = new Date();
			var currdate = new Date((now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear());
			var pickupDate = new Date(carsPickupDate.value.replace(/-/g, "/"));
			var dropoffDate = new Date(carsDropoffDate.value.replace(/-/g, "/"));
			
			
			if(reqMsg == ""){
				if(isNaN(pickupDate))
					fmtMsg += "\tPick-up Date\n";
				
				if(isNaN(dropoffDate))
					fmtMsg += "\tDrop-off Date\n";
			}
			
			if(pickupDate < currdate)
				fmtMsg += "\tPick-Up cannot be a date before today.\n";
			if(dropoffDate < currdate)
				fmtMsg += "\tDrop-Off cannot be a date before today.\n";
			if(dropoffDate < pickupDate)
				fmtMsg += "\tDrop-Off must be a date later than Pick-up.\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){
				jQ(pu_month).val((pickupDate.getMonth() + 1).pad(2));
				jQ(pu_day).val(pickupDate.getDate().pad(2));

				jQ(do_month).val((dropoffDate.getMonth() + 1).pad(2));
				jQ(do_day).val(dropoffDate.getDate().pad(2));
				
				jQ(puair).val(goingTo.value);
				jQ(doair).val(goingTo.value);
			}
			return true;
		}
	}
}

Number.prototype.pad = function(digits) {
	var n = this.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}