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;
	
	var me = this;
	var pre = "#" + me.id + " ";
	var jQ;
	var expandtime;
	
	//Prevent prototype.js conflicts.
	if(this.noConflict) {
		jQ = jQuery;
		jQ.noConflict();
	} else
		jQ = $;
	
	this.init = function() {
		me.dateBoxes = $("input[class*=Date]", "#travelocity");
		me.initDates();
		
		$('#hotelChildren').change(function(){
			var child_selected = $('#hotelChildren option:selected').val();
			var child_dd = $("select[name=childage]:lt(" + child_selected + ")");
		
		    (child_dd.length == 0) ? $(".minors").fadeOut(me.transSpeed) : $(".minors").fadeIn(me.transSpeed);
		
			child_dd.each(function(){ $(this).fadeIn(me.transSpeed); });
		
		    var child_not_dd = $("select[name=childage]:gt(" + (child_selected-1) + ")");
		    if(child_not_dd.length > 0){
			  child_not_dd.each(function(){ $(this).fadeOut(me.transSpeed); });
		    }
		});
		
		jQ(pre + "#flights form").submit(me.flights.validate);
		jQ(pre + "#flights input[name='dateTypeSelect']").click(me.flights.activateDate);
		jQ(pre + "#hotels form").submit(me.hotels.validate);
		jQ(pre + "#cars form").submit(me.cars.validate);
		jQ(pre + "#packages form").submit(me.packages.validate);
		jQ(pre + "#packages input[name='dateTypeSelect']").click(me.packages.activateDate);
		jQ(pre + "#lastMin form").submit(me.lastMin.validate);
		
		jQ(pre + ".close").click(me.hide);
		
		$("#travelocity").tabs({ fx: { opacity: 'toggle',speed: 'fast' } });
	};
	this.hide = function()
	{
                jQ("#tabContainer").slideUp(me.transSpeed, function(){
		  jQ(".wctCont").slideUp("fast");
                });
		return false;
	}
		
	this.initDates = function(){
		var dateBoxes = me.dateBoxes, dateDepart, dateReturn;

		if(!me.startDate) {	
			dateDepart = new Date();
			dateDepart.setDate(dateDepart.getDate() + 1);
		}else
			dateDepart = me.startDate;
			
		if(!me.endDate){
			dateReturn = new Date();
			dateReturn.setDate(dateDepart.getDate() + 2);
		}else
			dateReturn = me.endDate;
	
		var opt = { showOn: "both", buttonImageOnly: true, buttonText: "Choose date", buttonImage: "/includes/images/wct/calendar.gif"};		
		dateBoxes.each(function() {
			$(this).datepicker(opt)
		});
	}
	this.hotels = new Object();
	this.hotels.validate = function(){
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this)		{
			
			if(jQ(hotelsStart).val().length == 0)
				reqMsg += "\tCheck-In Date\n";
			if(jQ(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 || fmtMsg.length > 0){
		    errormessage(reqMsg, fmtMsg);
			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()	{
		(jQ(this).val() == "plusMinusDates") ? $(".exactDates", "#flights").fadeOut(me.transSpeed, function() { $(".plusMinusDates", "#flights").fadeIn(me.transSpeed); }) : $(".plusMinusDates", "#flights").fadeOut(me.transSpeed, function() { $(".exactDates", "#flights").fadeIn(me.transSpeed); });
		return true;
	}
	
	this.flights.validate = function(){
		var errMsg = "", fmtMsg = "", reqMsg = "";
		with (this){
			if(jQ(leavingFrom).val().length == 0)
				reqMsg += "\tDepart From\n";
			if(jQ(goingTo).val().length == 0)
				reqMsg += "\tGoing To\n";
			if(jQ(leavingDate).val().length == 0)
				reqMsg += "\tDepart Date\n";
			if(jQ(returningDate).val().length == 0)
				reqMsg += "\tReturn Date\n";
				
			now = new Date();
			dateDepart = new Date(jQ(leavingDate).val().replace(/-/g, "/"));
			dateReturn = new Date(jQ(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 || fmtMsg.length > 0){
		    errormessage(reqMsg, fmtMsg);
			return false;
		}else{
			with(this){
				jQ(leavingDate).val(leavingDate.value.replace(/-/g, "/"));
				jQ(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(jQ(puair).val().length == 0)
				reqMsg += "\tPick-up Location\n";
			if(jQ(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 || fmtMsg.length > 0){
		    errormessage(reqMsg, fmtMsg);
			return false;
		}else{
			with(this){
				jQ(pu_month).val((leavingDate.getMonth() + 1).pad(2));
				jQ(pu_day).val(leavingDate.getDate().pad(2));
				jQ(do_month).val((returningDate.getMonth() + 1).pad(2));
				jQ(do_day).val(returningDate.getDate().pad(2));
			}
			return true;
		}
	}
	
	this.packages = new Object();
	
	this.packages.activateDate = function()	{
		(jQ(this).val() == "plusMinusDates") ? $(".exactDates", "#packages").fadeOut(me.transSpeed, function() { $(".plusMinusDates", "#packages").fadeIn(me.transSpeed); }) : $(".plusMinusDates", "#packages").fadeOut(me.transSpeed, function() { $(".exactDates", "#packages").fadeIn(me.transSpeed); });
		return true;
	}
	
	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(leavingDate).val().length == 0)
				reqMsg += "\tDepart\n";
			if(jQ(returningDate).val().length == 0)
				reqMsg += "\tReturn\n";
				
			var now = new Date();
			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 < now)
				fmtMsg += "\tDepart must be a date in the future.\n";
			if(dateReturn <= now)
				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 || fmtMsg.length > 0){
		    errormessage(reqMsg, fmtMsg);
			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.lastMin = new Object();
	
	this.lastMin.validate = function(){
		errMsg = "", reqMsg = "", fmtMsg = "";
		with(this){
			if(jQ(lastMinStart).val().length == 0)
				reqMsg += "\tDepart Date\n";
			if(jQ(lastMinEnd).val().length == 0)
				reqMsg += "\tReturn Date\n";
				
			now = new Date();
			leavingDate = new Date(jQ(lastMinStart).val().replace(/-/g, "/"));
			returningDate = new Date(jQ(lastMinEnd).val().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 || fmtMsg.length > 0){
		    errormessage(reqMsg, fmtMsg);
			return false;
		}else{
			with(this){
				jQ(in_start_date).val(Math.round(leavingDate.getTime() / 1000));
				jQ(in_end_date).val(Math.round(returningDate.getTime() / 1000));
			}
			return true;
		}
	}
	
	function errormessage(reqMsg, fmtMsg){
		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);
		}
	}
	jQ(this.init);
}

Number.prototype.pad = function(digits) {
	n = this.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}

