function sortNumber(a, b)
{
	return a - b;
}

function fillMonth(dates, firstDropdownValue)
{
	var newDateItems = {}, monthDates = [], monthSelection, weekSelection, weekFirstValue;
	if (dates)
	{
		var replaceString = new RegExp(calendarFacetId + "%3A\\d{8}\-\\d{8}");
		var matchString = new RegExp("(" + calendarFacetId + "%3A\\d{8}\-\\d{8})");
		var match = currentUrl.match(matchString);
		if (match)
		{
			monthSelection = match[1].replace('%3A', ':');
			currentUrl = currentUrl.replace(replaceString, '');
			
			var match = currentUrl.match(matchString);
			if (match) {
				weekSelection = match[1].replace('%3A', ':');
				currentUrl = currentUrl.replace(replaceString, '');
			} else {
				weekSelection = monthSelection;
			}
		}
		if (currentUrl.indexOf('%3A') != -1)
		{
			currentUrl += '%3A';
		}
		currentUrl = currentUrl.replace('%3A%3A', '%3A');
		currentUrl = currentUrl.replace('=%3A', '=');
		for (var item in dates)
		{
			var matches = dates[item].match(/\d+\:(\d\d)(\d\d)(\d\d\d\d)\-(\d\d)(\d\d)(\d\d\d\d)/);
			var date = new Date(matches[3], matches[1] - 1, 1);
			var yearMonth = date.getTime();
			if (!newDateItems[yearMonth])
			{
				monthDates[monthDates.length] = date.getTime(); 
				newDateItems[yearMonth] = '';
			}
			for (var cur = Number(matches[2]); cur <= Number(matches[5]); cur ++)
			{
				if (newDateItems[yearMonth].indexOf(' ' + cur + ',') == -1)
				{			
					newDateItems[yearMonth] += ' ' + matches[3] + cur + matches[1] + ',';
				}	
			}	
		}
		monthDates = monthDates.sort(sortNumber);
		var monthEl = $('#fDBDepartureDateMonth');
		monthEl.append('<option value="' + currentUrl + '">'+ firstDropdownValue + '</option>');
		var today = new Date();
		if (weekSelection)
		{
			var matches = monthSelection.match(/\d+\:(\d\d)(\d\d)(\d\d\d\d)\-\d{8}/);
			var curMonth = new Date(Number(matches[3]), Number(matches[1]) - 1, 1, 0, 0, 0);
			var monthValue = calendarFacetId + '%3A' + formatString(curMonth.getMonth() + 1) + '01' + curMonth.getFullYear() + '-' + formatString(curMonth.getMonth() + 1) + formatString(curMonth.getDaysInMonth()) + curMonth.getFullYear();
			weekFirstValue = currentUrl + monthValue;
			monthEl.append('<option value="' + currentUrl + monthValue + '">'+ Date.monthNames[curMonth.getMonth()] + ' ' + curMonth.getFullYear() + '</option>');
			monthEl.attr('selectedIndex', 1);
		}
		else
		{ 
			if (monthSelection)
			{
				var matches = monthSelection.match(/\d+\:(\d\d)(\d\d)(\d\d\d\d)\-\d{8}/);
				var curMonth = new Date(Number(matches[3]), Number(matches[1]) - 1, 1, 0, 0, 0);
				startDateStamp = formatString(curMonth.getMonth() + 1) + '01' + curMonth.getFullYear();
				endDateStamp = formatString(curMonth.getMonth() + 1) + '' + formatString(curMonth.getDaysInMonth()) + curMonth.getFullYear();
			}	
			var currentMonth = new Date(today.getFullYear(), today.getMonth(), 1); 
			for (var item in monthDates)
			{
				var date = new Date(monthDates[item]);
				if (date >= currentMonth)
				{	
					var value = formatString(date.getMonth() + 1) + '01' + date.getFullYear() + '-' + formatString(date.getMonth() + 1) + formatString(date.getDaysInMonth()) + date.getFullYear();
					selectedMonth = '';
					if (monthSelection && value == (startDateStamp + '-' + endDateStamp))
					{
						selectedMonth = ' selected';
						weekFirstValue = currentUrl + calendarFacetId + '%3A' + value;
					}					
					monthEl.append('<option value="' + currentUrl + calendarFacetId + '%3A' + value + '"' + selectedMonth +'>'+ Date.monthNames[date.getMonth()] + ' ' + date.getFullYear() + '</option>');
				}	
			}
		}
		if (monthSelection || weekSelection)
		{
			var monthItems;
			if (typeof(newDateItems[curMonth.getTime()]) != 'undefined')
			{
				monthItems = newDateItems[curMonth.getTime()];			
			}
			var weekEl = $('#fDBDepartureDateWeek');
			weekEl.append('<option value="' + weekFirstValue + '">'+ firstDropdownValue + '</option>');
			
			var firstDayOffset = Date.firstDayOfWeek - curMonth.getDay() + 1;
			if (firstDayOffset > 1) firstDayOffset -= 7;
			var weeksToDraw = Math.ceil(((-1 * firstDayOffset + 1) + curMonth.getDaysInMonth()) /7);
			
			var w = 0;
			curMonth.addDays(firstDayOffset - 1);
			var startDate, endDate, startDateStr, endDateStr, itemExist, startDateStamp, endDateStamp, selectedWeek, value, curMonday;
			while (w++ < weeksToDraw) {
				if (curMonth < today)
				{
					curMonday = new Date(today.getTime());
					startDate = today.getDate();
					startDateStr = formatString(startDate) + ' ' + Date.abbrMonthNames[today.getMonth()];
					startDateStamp = formatString(today.getMonth() + 1) + '' + formatString(startDate) + today.getFullYear();
				}
				else
				{
					curMonday = new Date(curMonth.getTime());
					startDate = curMonth.getDate();
					startDateStr = formatString(startDate) + ' ' + Date.abbrMonthNames[curMonth.getMonth()];
					startDateStamp = formatString(curMonth.getMonth() + 1) + '' + formatString(startDate) + curMonth.getFullYear();
				}
				curMonth.addDays(6);
				endDate = curMonth.getDate(); 
				endDateStr = formatString(endDate) + ' ' + Date.abbrMonthNames[curMonth.getMonth()] + ' ' + curMonth.getFullYear();
				endDateStamp = formatString(curMonth.getMonth() + 1) + '' + formatString(endDate) + curMonth.getFullYear();
				var hasData = false;
				if (monthItems)
				{
					for (var weekDay = 1; weekDay <= 7; weekDay++)
					{
						if (monthItems.indexOf(' ' + curMonday.getFullYear() + curMonday.getDate() + formatString(curMonday.getMonth() + 1) + ',') != -1) {
							hasData = true;
							break;
						}
						curMonday.addDays(1);						
					}
				}
				if (hasData && ((today.getMonth() == curMonth.getMonth() && today.getWeekOfYear() <= curMonth.getWeekOfYear() && today.getFullYear() == curMonth.getFullYear()) || (today.getMonth() != curMonth.getMonth() || (today.getFullYear() != curMonth.getFullYear()))))
				{
					selectedWeek = '';
					if (weekSelection && weekSelection == calendarFacetId + ':' + startDateStamp + '-' + endDateStamp)
					{
						selectedWeek = ' selected';
					}
					value = currentUrl + calendarFacetId + '%3A' + startDateStamp + '-' + endDateStamp;
					value = value.replace('%3A%3A', '%3A');
					weekEl.append('<option value="' + value + '"' + selectedWeek +'>'+ startDateStr + ' - ' + endDateStr + '</option>');
				}
				curMonth.addDays(1);
			}
			$('#fDBDepartureDateWeekLabel').show();
			weekEl.show();
		}
	}
}

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function formatString(data)
{
	if (Number(data) < 10)
	{
		data = '0' + Number(data);
	}
	return data;	
}
