$(function() {
	//Global variables
	
	// TRACKING JS START
	if ( DOMAIN_TRACKING ) {
		var trackDomains = DOMAIN_TRACKING.split(',');
		for (var i=trackDomains.length-1;i>=0;i--) {
			$('a[href*="'+trackDomains[i]+'"], form[action*="'+trackDomains[i]+'"]').addClass('track-crossdomain');
		}
	}
	if ( FILETYPE_TRACKING ) { 
		var trackFiles = FILETYPE_TRACKING.split(',');
		for (var i=trackFiles.length-1;i>=0;i--) {
			var a = $('a[href$="'+trackFiles[i]+'"]');
			a.addClass('track').attr('name','download:'+a.attr('href'));
		}
	}
	
	// Tracking email links / set email as the name
	$('a[href^="mailto:"]').each(function(){
		$(this).attr('name',$(this).attr('href')).addClass('track');
	});
	
	$('body').delegate("a.track, a.track-crossdomain", "click", function(e){
		var tag = '';
		if ( this.name ) { tag = '/'+this.name.replace(/:/g,'/'); }
		if (tag) { _gaq.push(['_trackEvent','link','click', tag]); }
		if ( $(this).hasClass('track-crossdomain') ) {
			e.preventDefault();
			var lh = this.href
			if ($(this).attr('target') == '_blank') {
				_gaq.push(function () {
					var pageTracker = _gat._getTrackerByName();
					var l = pageTracker._getLinkerUrl(lh);
					window.open(l, '');
				});
			} else {
				_gaq.push(function () {
					var pageTracker = _gat._getTrackerByName();
					var l = pageTracker._getLinkerUrl(lh);
					window.location = l;
				});
			}
		}
	});
	
	$('body').delegate("form.track, form.track-crossdomain", "submit", function(e){
		var tag = '';
		if ( this.name ) { tag = '/'+this.name.replace(/:/g,'/'); }
		var a = $(this).attr('action');
		if (tag) { _gaq.push(['_trackEvent','form', 'submit', tag]); }
		if ( $(this).hasClass('track-crossdomain') ) {
			$(this).attr({action:a+'?'+$(this).serialize(), method:'post'});
			_gaq.push(['_linkByPost',this]);
		}
	});
	
	// TRACKING JS END
	
	$("a.open-promo").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		$(this).next().show();
	});
	$("a.close-promo").click(function(event){
		event.preventDefault();
		event.stopPropagation();
		$(this).parents("#booking-promo-fields").slideUp("fast");
	});

});

(function($) {		  
	$.fn.initializeForm = function(options) {
		var defaults = {
			calText: 'Click to select a date',
			dateFormat: 'mm/dd/yyyy'
		},
		opts = $.extend(defaults, options),
		focusIsSupported = (function(){
		    // Create an anchor + some styles including ':focus'.
		    // Focus the anchor, test if style was applied,
		    // if it was then we know ':focus' is supported.
		     
		    var ud = 't' + +new Date(),
		    anchor = $('<a id="' + ud + '" href="#"/>').css({top:'-999px',position:'absolute'}).appendTo('body'),
		    style = $('<style>#'+ud+'{font-size:10px;}#'+ud+':focus{font-size:1px !important;}</style>').appendTo('head'),
		    supported = anchor.focus().css('fontSize') !== '10px';
		    anchor.add(style).remove();
		return supported;

		})();

		return this.each(function() {
			var form = $(this);

			if ( !focusIsSupported ) {
				$(".textfield")
					.focus(function() { $(this).css({backgroundColor: "#fdfcfa"}); })
					.blur(function() { $(this).css({backgroundColor: "#ffffff"}); });
				$('.required', form).each(function() {
					$(this).prev().addClass('ie-icon-required');
				}); 
			}
			
			$(".date-picker", form).each(function() {
				$(this).datepicker({ 
					beforeShow: setDatePicker,
					buttonImage: "/images/icon-cal.gif", 
					buttonImageOnly: true ,
					buttonText: opts.calText,
					duration: "fast",
					gotoCurrent: true,
					hideIfNoPrevNext: true,
					numberOfMonths: 2,
					showAnim: "blind",
					showOn: "both"
				});
				
				if($(this).val()==opts.dateFormat) {
					$(this).val('');
				}
				else {
					if($(this).hasClass('date-end')&&$(this).hasClass('update-blocks'))
						getRange(this);
				}
			});
			
			// hide all "other" fields
			$("ul.checkboxgroup input.other, ul.checkboxgroup textarea.other", form).not(':radio, :checkbox').hide();
			$("ul.checkboxgroup input:not(:text)", form).click(function() {
				$('.other', $(this).parents('ul')).siblings().removeClass('required').filter(':text').hide();
				if($(this).is(':checked')) {
					$('.validation-error', $(this).parents('ul')).removeError(form);
					if($(this).hasClass('other'))
						$(this).siblings().addClass('required').show();
				}
			});
			
			$("div.select-other", form).hide();
			// if "Other" is selected, then show it's "other" field
			$("select.select-other", form).change( function () {
				var el = $(this).parent().next()
				if($(this).val() == "--") {
					el.show().children('label, input').addClass("required");
				} else {
					el.hide().children().removeClass("required").find("input").val("");
					$('.validation-error', el).removeError(form);
				}
			});

		});
	};
	
	$.fn.formValidate = function(options) {
		var defaults = {
			errorStart: 'A valid ',
			errorEnd: ' is required.'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			var form = $(this);
			$('.submit', form).click(function() {
				var _errors = '';
				//Removes validation errors from previous submit
				$('.validation-error', form).removeError(form);	
				
				$('.required', form).each(function() {
					var valid = true;
					
					if($(this).is(':input')) {
						var o = $.trim($(this).val());
						
						if($(this).hasClass('email')) {
							// if it's an email address make sure the email is valid using both regular expressions
							valid = /^[a-z0-9_+.-]+\@(?:[a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test(o);
							if($(this).hasClass('confirm-email')&&valid) {
								var prev = $.trim($(this).parents('.field').prev().children('.email').val());
								if(o!=prev)
									valid=!valid;
							}
						} else if (o.replace(/(?:^\s+)|(?:\s+$)/g,'').length < 1) {
							// if not an email address take out funky characters and see if its still blank
							valid = !valid;
						}
					} else if($(this).is('ul')) {
						valid = false;
						
						$(':radio, :checkbox', this).each(function() {
							if($(this).is(':checked'))
								valid = true;
						});
					}
					
					if(!valid) {
						if(form.hasClass('inline')) {
							$(this).parents('.field').children('label').addClass('validation-error');
						} else {
							var warn = $('<img />').attr('src', '/images/icon-warning.gif').addClass('validation-error');
							
							if( $(this).is('ul') )
								$('li:first', this).append(warn);
							else 
								$(this).parent().append(warn);
						}
					
						if(_errors == '')
							_errors = $(this).attr("id");
					}
				});
				if(_errors.length) {
					var lbl = $('#'+_errors).parents('.field').find('label:eq(0)');
					alert(lbl.attr('title').length?lbl.attr('title'):opts.errorStart + lbl.html().replace(/[*:]/g, '') + opts.errorEnd)
					if ($('#'+_errors).is('ul'))
						$('#'+_errors, form).find('label:eq(0)').focus();
					else
						$('#'+_errors, form).focus();
					return false;			
				}
			});
		});
	};
	
	$.fn.expand = function(options) {
		var defaults = {
			openText: 'View Details',
			closeText: 'Hide Details',
			longClass: '.package-long'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			var obj = $(this);
			var tog = $('<div />').addClass('toggle');
			var control = $('<a />').attr('href', '#')
							.addClass('toggler')
							.html(opts.openText)
							.appendTo(tog)
							.toggle(function() { $(opts.longClass, obj).slideDown('slow'); $(this).html(opts.closeText).addClass('open'); },
									function() { $(opts.longClass, obj).slideUp('slow'); $(this).html(opts.openText).removeClass('open'); });
			
			$(opts.longClass, obj).after(tog).hide();
		});
	};
	
	$.fn.removeError = function(form) {
		return this.each(function() {
			if(form.hasClass('inline'))
				$(this).removeClass('validation-error');
			else
				$(this).remove();
		});
	};
	
	$.fn.ajaxCalendar = function(options) {
		var overlay = $('<div />').attr('id','overlay').css({width:$('#calendar-wrapper').width(),height:$('#calendar-wrapper').height(),opacity:0.5});
		overlay.prependTo('#calendar-wrapper');
		$(this).eventPreview();
		$(this).initializeSharing();
		return this.each(function() {
			$('#calendar-wrapper a').live('click',function(event) {
				event.preventDefault();
				overlay.fadeIn('fast');
				url = this.href.split('?');
				$.get('/direct/events-calendar-ajax.php?' + url[1], { op: 'cal' },function(data) { 
					$('#calendar-wrapper').html(data).prepend(overlay);
					overlay.css({height:$('#calendar-wrapper').height()});
				});
				$.get('/direct/events-calendar-ajax.php?' + url[1], { op: 'list' },function(data) { 
					$('#events-wrapper').html(data);
					overlay.fadeOut('fast');
				});
			});
		});
	};

	$.fn.eventPreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Details',
			viewDetails : 'View Details'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('.view-details').live('click',function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				if ($(this).parent().hasClass('active')) {
					var testnode = $(this).find('*').contents().filter(function() { return this.nodeType == 3 });
					testnode.length ? testnode.after(opts.viewDetails).remove() : $(this).html(opts.viewDetails);
					$(this).parent().removeClass('active').parent().find('.event-description').slideUp('slow', function() { 
						$(this).html('');
					});
				}
				else {
					var el = $(this);
					var temp = this.rel.split(':');
					$.get('/direct/events-calendar-ajax.php', { permalink: temp[0], rssfeed: temp[1], lang: LANG, op: 'details' },function(data) { 
					var testnode = el.find('*').contents().filter(function() { return this.nodeType == 3 });
					testnode.length ? testnode.after(opts.hideDetails).remove() : el.html(opts.hideDetails);
					el.parent().addClass('active').parents('.event').find('.event-description').hide().html(data).slideDown('slow');
					if (AJAX_SHARE.indexOf('events')>=0) {
							gapi.plusone.go();
							twttr.widgets.load();
							FB.XFBML.parse();
							shareTrack();
						}	
					});
				}
			});
		});
	};

	$.fn.postPreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Full Post',
			viewDetails : 'View Full Post'
		},
		opts = $.extend(defaults, options);
		return this.each(function( counter, context ) {

			$('.post-icon', this).click(function(e) {
			    e.preventDefault();
			    $('.post-details a', context).trigger('click')
			});

			$('a.posting-details', this).toggle(function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				var el = $(this);
				$.get('/direct/scrapbook-details.php', { event_id: this.id.substr(3), channel: this.rel, lang: LANG },function(data) { 
					var testnode = el.find('*').contents().filter(function() { return this.nodeType == 3 });
					testnode.length ? testnode.after(opts.hideDetails).remove() : el.html(opts.hideDetails);
					el.parent().addClass('active').parents('.post').find('.post-description').hide().html(data).slideDown('slow');
					if (AJAX_SHARE.indexOf('scrapbook')>=0) {
						gapi.plusone.go();
						twttr.widgets.load();
						FB.XFBML.parse();
						shareTrack();
					}
				});
			},function(event) { 
				event.preventDefault();
				$('.share-panel').hide();
				var testnode = $(this).find('*').contents().filter(function() { return this.nodeType == 3 });
				testnode.length ? testnode.after(opts.viewDetails).remove() : $(this).html(opts.viewDetails);
				$(this).parent().removeClass('active').parents('.post').find('.post-description').slideUp('slow', function() { 
					$(this).html('');
				});
			});
		});
	};

	$.fn.packagePreview = function(options) {
		var defaults = {
			hideDetails : 'Hide Details',
			viewDetails : 'View Details'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			$('a.package-details', this).toggle(function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				var el = $(this);
				$.get('/direct/package-details.php', { package_id: this.id.substr(3), channel: this.rel, lang: LANG },function(data) {																								
					var testnode = el.find('*').contents().filter(function() { return this.nodeType == 3 });
					testnode.length ? testnode.after(opts.hideDetails).remove() : el.html(opts.hideDetails);
					el.parent().addClass('active').parents('.package-wrapper').find('.package-long').hide().html(data).slideDown('slow');
					if (AJAX_SHARE.indexOf('packages')>=0) {
						gapi.plusone.go();
						twttr.widgets.load();
						FB.XFBML.parse();
						shareTrack();
					}
				});
			},function(event) { 
				event.preventDefault();
				$('.share-panel').hide();
				var testnode = $(this).find('*').contents().filter(function() { return this.nodeType == 3 });
				testnode.length ? testnode.after(opts.viewDetails).remove() : $(this).html(opts.viewDetails);
				$(this).parent().removeClass('active').parent().find('.package-long').slideUp('slow', function() { 
					$(this).html('');
				});
			});
		});
	};

	$.fn.initializeSharing = function() {
		return this.each(function() {
			$('.share-link').live('click',function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				$(this).parents('.rss-item').find('.share-panel').css('display','inline');
			});
			$('.share-close').live('click',function(event){
				event.preventDefault();
				$(this).parents('.rss-item').find('.share-panel').slideUp('fast');
			});
		});
	};

	$.fn.initializeFollowing = function() {
		return this.each(function() {
			$('.follow-link').click(function(event) {
				event.preventDefault();
				$('.follow-panel').hide();
				$(this).siblings('.follow-panel').css('display','inline');
			});
			$('.follow-close').click(function(event){
				event.preventDefault();
				$(this).parent('.follow-panel').slideUp('fast');
			});
		});
	};
	
	
	
	
	
$.fn.clickableCalendar = function( options ) {

	var context = this,
	    opts = {
		arriveInput:false,
		departInput:false,
		pairs:false,
		callback:false,
		unitPage:false,
		preselect:false,
		generateSubmit:false
	    },
	    arrival = {},
	    departure = {};

	window.arrival = arrival;
	window.departure = departure;
	window.opts = opts;

	if ( options ) {
	    opts = $.extend( opts, options );
	}

	if ( opts.arriveInput.length == 1 && opts.departInput.length == 1 ) {
	    pairs = $(opts.arriveInput).add( opts.departInput )

	    if ( 
		    opts.preselect && 
		    $(opts.arriveInput).val() != '' && 
		    $(opts.departInput).val() != '' &&
		    $(opts.arriveInput).attr('format') &&
		    $(opts.departInput).attr('format')
	    ) {

		var arriveDate = $.datepicker.parseDate( $(opts.arriveInput).attr('format'), $(opts.arriveInput).val() ),
		    arriveFormatted = $.datepicker.formatDate('m/d/yy', arriveDate ),
		    arriveCell = $(context).find('td[date="' + arriveFormatted + '"]'),
		    arriveDayNum = $(arriveCell).length ? $(arriveCell).attr('day') : false,

		    departDate = $.datepicker.parseDate( $(opts.departInput).attr('format'), $(opts.departInput).val() ),
		    departFormatted = $.datepicker.formatDate('m/d/yy', departDate ),
		    departCell = $(context).find('td[date="' + departFormatted + '"]'),
		    departDayNum = $(departCell).length ? $(departCell).attr('day') : false;

		arrival.time = arriveDate.valueOf();
		arrival.lastEvent = 'preset';
		arrival.tableCell = arriveCell;
		arrival.dayNum = arriveDayNum;

		departure.time = departDate.valueOf()
		departure.lastEvent = 'preset';
		departure.tableCell = departCell;
		departure.dayNum = departDayNum;

		initialSelection()
		departureSelection()
		
	    }
	}

	$(this).click(function(e) {
	    var el = e.target;

	    // Make sure its selectable and available
	    if ( !$(el).is('td.available') || !$(el).attr('date') ) {
		return;
	    }

	    var ts = new Date( $(el).attr('date') ).valueOf()

	    if ( departure.lastEvent && departure.lastEvent == 'invalidSelection' ) {
	    
		var dayNum = n($(el).attr('day'));
		if ( dayNum > arrival.dayNum ) {
		    arrival.time = ts;
		    arrival.lastEvent = 'click';
		    arrival.tableCell = el;
		    arrival.dayNum = dayNum;
		    clearSelected()
		    initialSelection()
		    updateArrival()
		    delete departure.lastEvent;
		}

	    } else if ( !arrival.time && !departure.time ) {
		arrival.time = ts;
		arrival.lastEvent = 'click';
		arrival.tableCell = el;
		arrival.dayNum = n($(el).attr('day'));
		
		initialSelection()
		updateArrival()

	    } else if ( arrival.time && !departure.time && arrival.time != ts ) {

		departure.time = ts;
		departure.lastEvent = 'click';
		departure.tableCell = el;
		departure.dayNum = n($(el).attr('day'))

		fixOrdering()

		if ( validDates() ) {
		    departureSelection()
		    updateArrival()
		    updateDeparture()

		    if ( opts.generateSubmit ) {
			generateSubmit()
		    }
		} else {
		    delete departure.time
		    delete departure.tableCell
		    delete departure.dayNum
		    departure.lastEvent = 'invalidSelection'
		}

	    } else if ( arrival.time && !departure.time && arrival.time == ts ) {
	    } else if ( arrival.time && departure.time ) {
		delete departure.time
		delete departure.tableCell
		delete departure.dayNum
		delete departure.lastEvent
		arrival.time = ts;
		arrival.lastEvent = 'click';
		arrival.tableCell = el;
		arrival.dayNum = n($(el).attr('day'));

		clearSelected()
		initialSelection()
		updateArrival()
		clearDeparture()
		hideSubmit()
	    }
	})
	
	
	
	
	function generateSubmit() {
	    var form = $('form.reload-dates');
	    if ( form.length && $(form).find('.submit').length == 0 ) {
		$('<input type="submit" value="Check Rates for Dates Selected" class="check-rates button-image">').appendTo( form )
	    } else {
		$(form).find('.submit').show()
	    }
	}

	function hideSubmit() {
		var form = $('form.reload-dates')
		if ( form.length ) {
			form.find('.submit').hide()
		}
	    //var form = $(context).siblings('form');

	    //if ( form.length ) {
		//$(form).find('.check-rates').hide()
	    //}
	}

	function validDates() {
	    for ( var startCell = n(arrival.dayNum); startCell < n(departure.dayNum); startCell++ ) {
		var td = $('td[day="' + startCell + '"]', context);
		if ( !$(td).is('td.available') && !$(td).is('td.default') ) {
		    return false;
		}
	    }
	    return true;
	}

	function fixOrdering() {
	    var tempArrival = arrival, tempDeparture = departure;
	    if ( n(tempArrival.dayNum) > n(departure.dayNum) ) {
		arrival = tempDeparture;
		departure = tempArrival;
	    }
	}

	function initialSelection() {
	    $(arrival.tableCell).addClass('selected')
	}

	function clearSelected() {
	    $('td.selected', context).removeClass('selected')
	}

	function n( n ) {
	    return parseInt( n, 10 )
	}

	function departureSelection() {

	    for ( var startCell = n(arrival.dayNum); startCell < n(departure.dayNum); startCell++ ) {
		var td = $('td[day="' + startCell + '"]', context);
		if ( $(td).is('.available') ) {
		    $(td).addClass('selected')
		}
	    }
	    $(departure.tableCell).addClass('selected')
	}

	function updateArrival() {
	    if ( $(opts.arriveInput).length ) {
		$(opts.arriveInput).val(
		    $.datepicker.formatDate( $(opts.arriveInput).attr('format'), new Date( arrival.time ))
		)
	    }
	}

	function updateDeparture() {
	    if ( $(opts.departInput).length ) {
		$(opts.departInput).val(
		    $.datepicker.formatDate( $(opts.departInput).attr('format'), new Date( departure.time ))
		)
	    }
	}

	function clearDeparture() {
	    $(opts.departInput).val('')
	}

	function clearArrival() {
	    $(opts.arriveInput).val('')
	}
	
	
	
	
	
	
	
	
};	
	
	
	

	
	$.fn.calendarToggle = function() {

		$(this).each(function() {

			window.k = this;

			var datesSelected = $(this).find('td.selected'),
			p = this,
			firstMonth = currentMonth = datesSelected.length ? $(datesSelected).eq(0).closest('.table')[0] : $('div.table:not(.alt)', this)[0],
			secondMonth = $(firstMonth).next().length ? $(firstMonth).next()[0] : false,
			lastMon = $('div.table', this)[23];

			if ( currentMonth == lastMon ) {
			secondMonth = currentMonth;
			currentMonth = $(currentMonth).prev()[0]
			firstMonth = currentMonth
			}

			$(firstMonth).removeClass('alt').prevAll().addClass('alt')

			if ( secondMonth ) {
			$(secondMonth).removeClass('alt').nextAll().addClass('alt');
			}

			window.datesSelected = datesSelected;
			window.firstMonth = firstMonth;
			window.secondMonth = secondMonth;
			window.lastMon = lastMon;
			window.currentMonth = currentMonth;

			$('a.previous-month, a.next-month', this).click(function(e) {

			e.preventDefault()

			//firstMonth = currentMonth = $('.table:not(.alt)', p)[0];
			secondMonth = $(currentMonth).next().length ? $(currentMonth).next()[0] : false;

			if ( $(this).is('.next-month') ) { // next
				if ( secondMonth != lastMon ) {
				$( secondMonth ).next().removeClass('alt');
				$( currentMonth ).addClass('alt');
				currentMonth = secondMonth;
				secondMonth = $( secondMonth ).next()[0];
				if ( secondMonth == lastMon ) {
					$('.next-month', p).css('visibility', 'hidden');
				}
				}
				$('.previous-month', p).css('visibility', 'visible');
			} else { // previous


				if ( $(currentMonth).prev().length ) {
				$(currentMonth).prev().removeClass('alt');
				$( secondMonth ).addClass('alt');
				secondMonth = currentMonth;
				currentMonth = $(currentMonth).prev()[0];

				if ( $(currentMonth).prev().length == 0 ) {
					$('.previous-month', p).css('visibility', 'hidden');
				}
				}
				$('.next-month', p).css('visibility', 'visible');
			}
			});

			if ( $(currentMonth).prev().length ) {
			$('.previous-month', p).css('visibility', 'visible');
			}

			if ( secondMonth == lastMon ) {
			$('.next-month', p).css('visibility', 'hidden');
			}

		});
    };
	
	//If the URL has a hash (anchor) bit, scroll to that.
	if ( window.location.hash ) {
		$.scrollTo(window.location.hash);
	}

})(jQuery);

function setDatePicker(input) {
	if(($(input).hasClass('date-begin')||$(input).hasClass('date-end'))&&$(input).hasClass('update-blocks')){
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) ),
			onClose: function() { getRange(input); }
		}; 
	} else if(($(input).hasClass('date-begin')||$(input).hasClass('date-end'))&&$(input).hasClass('netbooker')){
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) ),
			onSelect: function() { splitDates(input); }
		}; 
	} else if($(input).hasClass('date-begin')||$(input).hasClass('date-end')) {
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) )
		};				
	} 
} 

function setDates(input, minDate) {
	if( ($(input).hasClass('date-begin') && !$('.date-end', $(input).parent().next()).length) ||
		($(input).hasClass('date-end') && !$('.date-begin', $(input).parent().prev()).length) ) {
		return minDate ? new Date() : null;
	} else if( $(input).hasClass('date-begin') ) {
		return minDate ? new Date() : ($('.date-end', $(input).parent().next()).datepicker("getDate") ? new Date($('.date-end', $(input).parent().next()).datepicker("getDate").getTime() - 1000*60*60*24) : null);
	} else if( $(input).hasClass('date-end') ) {
		return minDate ? ($('.date-begin', $(input).parent().prev()).datepicker("getDate") ? new Date($('.date-begin', $(input).parent().prev()).datepicker("getDate").getTime() + 1000*60*60*24) : '+1d' ) : null;
	}
}

function splitDates(input) {
	var start = $(input).hasClass('date-end')?$('.date-begin', $(input).parent().prev()).datepicker("getDate"):$(input).datepicker("getDate");
	var end = $(input).hasClass('date-end')?$(input).datepicker("getDate"):$('.date-end', $(input).parent().next()).datepicker("getDate");

	if($(input).hasClass('date-begin')) {
		$('#arriveMonth').val( $.datepicker.formatDate('mm', $(input).datepicker("getDate")) );
		$('#arriveDay').val( $.datepicker.formatDate('dd', $(input).datepicker("getDate")) );
		$('#arriveYear').val( $.datepicker.formatDate('yy', $(input).datepicker("getDate")) );
	}
	if($(input).hasClass('date-end')) {
		$('#departMonth').val( $.datepicker.formatDate('mm', $(input).datepicker("getDate")) );
		$('#departDay').val( $.datepicker.formatDate('dd', $(input).datepicker("getDate")) );
		$('#departYear').val( $.datepicker.formatDate('yy', $(input).datepicker("getDate")) );
	}
	
	if(start&&end) {
		var one_day=1000*60*60*24;
		var days = Math.ceil((end.getTime()-start.getTime())/(one_day))	
		$('#numberOfNights').val(days);
	}
}

function getRange(el) {
	var form = $(el).parents('form');
	var start = $(el).hasClass('date-end')?$('.date-begin', $(el).parent().prev()).datepicker("getDate"):$(el).datepicker("getDate");
	var end = $(el).hasClass('date-end')?$(el).datepicker("getDate"):$('.date-end', $(el).parent().next()).datepicker("getDate");

	if(start&&end) {
		var one_day=1000*60*60*24;
		var days = Math.ceil((end.getTime()-start.getTime())/(one_day))	
		$('#room-requirements').show();
		createBlock(start, days, form);
		createMeeting(start, days, form);
	}
}

function createBlock(date, days, form) {
	$("#block-details", form).remove();
	var tbody = $('<tbody />').attr('id', 'block-details');
	
	for(var i=0; i<=days; i++) {
		var newDate = $.datepicker.formatDate('D, m/d', new Date(date.getFullYear(), date.getMonth(), date.getDate()+i) );
		
		tbody.append('<tr>\n\
					<th scope="row">' + newDate + '</th>\n\
					<td><input type="text" class="textfield single" maxlength="5" value="0" id="single_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][single]" /></td>\n\
					<td><input type="text" class="textfield dbl" maxlength="5" value="0" id="dbl_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][dbl]" /></td>\n\
					<td><input type="text" class="textfield suite" maxlength="5" value="0" id="suite_rooms_day_' + (i+1) + '" name="room_block[day_' + (i+1) + '][suite]" /></td>\n\
					<td><input type="text" class="textfield readonly day-total" readonly="readonly" value="0" id="day_' + (i+1) + '_total" name="room_block[day_' + (i+1) + '][total]" /></td>\n\
					</tr>');
	}
	//Add the totals row
	tbody.append('<tr><th scope="row">Room Total</th>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="single_rooms_total" name="room_block[single][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="dbl_rooms_total" name="room_block[dbl][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="suite_rooms_total" name="room_block[suite][total]" /></td>\n\
		<td><input type="text" class="textfield readonly" readonly="readonly" value="0" id="total_rooms" name="total_rooms" /></td>\n\
		</tr>');

	//add tbody to existing table
	$("#block-requirements", form).append(tbody);
	$('td input:not(.readonly)', tbody)
	    .focus(function() { 
		if ( $(this).val().length ) {
		    $(this).data('num', $(this).val() )
		}
		$(this).val('');
	    })
	    .blur(function() { 
		var num = $(this).data('num')
		if( $(this).val()=='') {
		    if ( !num ) {
			$(this).val(0);
		    } else {
			$(this).val( num )
		    }
		}
	    })
	    .change(function() { calcTotal(this); });
}

function createMeeting(date, days, form) {
	$("#meeting-details", form).remove();
	var tbody = $('<tbody />').attr('id', 'meeting-details');
	
	// For Each Number of Days that need information a row is created for each
	for(var i=0; i<=days; i++) {
		var newDate = $.datepicker.formatDate('D, m/d', new Date(date.getFullYear(), date.getMonth(), date.getDate()+i) );
		
		// Create a row with a cell for the day, room type and breakout room info
		tbody.append('<tr>\n\
			<th scope="row">' + newDate + '</th>\n\
			<td><select name="meeting_requirements[day_' + (i+1) + '][room_setup]">\n\
				<option value="Classroom" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Classroom</option>\n\
				<option value="Boardroom" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Boardroom</option>\n\
				<option value="Theater" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Theater</option>\n\
				<option value="U-Shaped" name="meeting_requirements[day_' + (i+1) + '][room_setup]">U-Shaped</option>\n\
				<option value="Hollow Sq." name="meeting_requirements[day_' + (i+1) + '][room_setup]">Hollow Sq.</option>\n\
				<option value="Rounds" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Rounds</option>\n\
				<option value="Reception" name="meeting_requirements[day_' + (i+1) + '][room_setup]">Reception</option>\n\
				</select></td>\n\
			<td><input type="text" class="textfield" maxlength="5" id="breakout_rooms_day_' + (i+1) + '" name="meeting_requirements[day_'+(i+1)+'][breakout_rooms]" /></td>\n\
			<td><input type="text" class="textfield" maxlength="5" id="breakout_people_day_' + (i+1) + '" name="meeting_requirements[day_'+(i+1)+'][breakout_people]" /></td>\n\
			<td><select name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">\n\
				<option value="Classroom" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Classroom</option>\n\
				<option value="Boardroom" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Boardroom</option>\n\
				<option value="Theater" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Theater</option>\n\
				<option value="U-Shaped" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">U-Shaped</option>\n\
				<option value="Hollow Sq." name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Hollow Sq.</option>\n\
				<option value="Rounds" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Rounds</option>\n\
				<option value="Reception" name="meeting_requirements[day_' + (i+1) + '][breakout_setup]">Reception</option>\n\
				</select></td>\n\
			</tr>');
	}
	//add tbody to existing table
	$("#meeting-requirements", form).append(tbody);
}

function calcTotal(field) {

	var row = $(field).parents('tr'),
	    tbody = $(field).parents('tbody'),
	    rTotal=sTotal=dTotal=suite=total=0;
	
	$('input:not(.readonly)', row).each(function() {
		rTotal += parseInt($(this).val(), 10);										 
	});
	$('.day-total', row).val(rTotal);
	
	if($(field).hasClass('single')) {
		$('input.single', tbody).each(function() {
			sTotal += parseInt($(this).val(), 10);										 
		});
		$('#single_rooms_total',tbody).val(sTotal);
	}
	if($(field).hasClass('dbl')) {
		$('input.dbl', tbody).each(function() {
			dTotal += parseInt($(this).val(), 10);										 
		});
		$('#dbl_rooms_total',tbody).val(dTotal);
	}
	if($(field).hasClass('suite')) {
		$('input.suite', tbody).each(function() {
			dTotal += parseInt($(this).val(), 10);										 
		});
		$('#suite_rooms_total',tbody).val(dTotal);
	}
	
	$('.day-total', tbody).each(function() {
		total += parseInt($(this).val(), 10);								 
	});
	$('#total_rooms', tbody).val(total);
}

function openPreview(urlrequest) {
	window.open(urlrequest, 'myWindow', 'width=750,height=520,left=100,top=100,toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscreen=No');
	document.getElementById('flashCallBox').value = urlrequest;
}
function extractParamFromUri(uri, paramName) {
  if (!uri) {
    return;
  }
  var uri = uri.split('#')[0];  // Remove anchor.
  var parts = uri.split('?');  // Check for query params.
  if (parts.length == 1) {
    return;
  }
  var query = decodeURI(parts[1]);

  // Find url param.
  paramName += '=';
  var params = query.split('&');
  for (var i = 0, param; param = params[i]; ++i) {
    if (param.indexOf(paramName) === 0) {
      return unescape(param.split('=')[1]);
    }
  }
}


