// JavaScript Document

// date conversions

// returns a month and year as a string, as it is used by the 
// monthYear dropdowns.
//
// m: the numeric month
// y: the year
// returns string
function dateToString(m, y) {
	var monthValue = m + "-" + y;
	
	// if date is like '3-2006', add a 0 to the front to make '03-2006'
	if (m < 10)
		monthValue = '0' + monthValue;
		
	return monthValue;
}

// parses an mm-yyyy string into an object
// with integer fields month and year
function stringToDate(mmyyyy) {
	var arr = mmyyyy.split('-', 2);
							
	return { month: parseInt(arr[0], 10), year: parseInt(arr[1], 10) };
}

// calendar functions

// given a month and a year, returns the number of days in that month
function numDaysInMonth(month, year) {
	var daysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	
	if (month != 2)  {
		return daysInMonth[month];
	}
	else {
		// leap year calculation
		if (((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0))
			return 29;
		else
			return 28;
	}
}

// callback used after calendar popup date selection
// sets the coressponding date drop down to the value selected
// by the user
function setDateDropDowns(label) {
	return function(y, m, d) {
		var dayDropDown = eval("document.forms['EventsCalendarForm']." + label + "Day");
		var monthDropDown = eval("document.forms['EventsCalendarForm']." + label + "MonthYear");
		
		var monthValue = dateToString(m, y);
				
		var dayOpt = findOptionWithValue(d, dayDropDown);
		dayOpt.selected = true;
		
		var monthOpt = findOptionWithValue(monthValue, monthDropDown);
		monthOpt.selected = true;
		if(label == 'start')
		{
			changeToDate();
		}
	}
}

// onChange callback for the month dropdowns. updates the corresponding day dropdown
// with the correct number of days in that particular month.
// param is either 'start' or 'end'
function monthOnChange(label) {
	var dayDropDown = eval("document.forms['EventsCalendarForm']." + label + "Day");
	var monthDropDown = eval("document.forms['EventsCalendarForm']." + label + "MonthYear");
	
	var selected = monthDropDown.selectedIndex;
	
	var dateString = monthDropDown.options[selected].value;
	
	var date = stringToDate(dateString);
	
	var days = numDaysInMonth(date.month, date.year);

	
	// picked a month with different amount of days. need to refresh drop down.
	if (days != dayDropDown.options.length) {
	
		var oldSelectedIndex = dayDropDown.selectedIndex;
		dayDropDown.options.length = 0;
		
		// create a drop down entry for each day in the newly selected month
		for (var i = 0; i < days; i++) {
			var value = i+1;
			var name = i+1;
			
			if (value < 10)
				value = '0' + value;
				
			dayDropDown.options[i] = new Option(name, value);
		}
		
		// restore selection
		if (oldSelectedIndex >= dayDropDown.options.length) {
			oldSelectedIndex = dayDropDown.options.length-1;
		}

		dayDropDown.selectedIndex = oldSelectedIndex;
	}	
}
// returns the option object in the given select box that has the given value.
// value: the value to look for in the drop down
// select: a select object
function findOptionWithValue(value, select) {
	for (var i = 0; i < select.options.length; i++) {
		if (escapeEntities(select.options[i].value) == value)
			return select.options[i];
	}
	
	return undefined;
}

// initializes the calendar callbacks and date ranges
// cal: a CalendarPopup object
// label: strings 'start' or 'end'
function initCalendar(cal, label) {
	cal.addDisabledDates(null, "12-31-2006");
	cal.addDisabledDates("01-01-2009", null);
	cal.setReturnFunction( "setDateDropDowns('" + label + "')" );
}

// init both calendars
var startDateCalendar = new CalendarPopup();
var endDateCalendar = new CalendarPopup();

// category to subcategory mappings
var categoryMapping = new Object();

categoryMapping['Concerts &amp; Shows'] = [
	
		'Comedy',
	
		'Concerts',
	
		'Production Shows',
	
	
		'STOP'
];

categoryMapping['Tournaments'] = [
	
		'Blackjack',
	
		'Poker',
	
		'Slot',
	
		'Video Poker',
	
	
		'STOP'
];

categoryMapping['Special Events'] = [
	
		'Holiday Events',
	
		'Local Events',
	
		'Sporting Events',
	
	
		'STOP'
];

categoryMapping['Drawings &amp; Giveaways'] = [
	
	
		'STOP'
];

categoryMapping['Nightlife'] = [
	
		'After Hours',
	
		'Club Events',
	
		'Racetrack',
	
	
		'STOP'
];


// replaces special characters with html entities
function escapeEntities(text) {
	text = text.replace(/&/g,'&amp;');
	return text;
}

// on change for category drop downs 
// updates the options in the subcategory drop down
function categoryOnChange() {

	var catDropDown = document.forms.EventsCalendarForm.categoryName;
	var subcatDropDown = document.forms.EventsCalendarForm.subcategoryName;
	
	var selectedCat = catDropDown.options[catDropDown.selectedIndex];
	
	if (selectedCat.value == 'ALL_CATEGORIES') {
		subcatDropDown.options.length = 0;
		subcatDropDown.options[0] = new Option('ALL', 'ALL_SUBCATEGORIES');
		subcatDropDown.selectedIndex = 0;
	}
	else {
		var oldSelectedSubcatVal = subcatDropDown.options[subcatDropDown.selectedIndex].value;
	
		var key = escapeEntities(selectedCat.value);
	
		var subcategories = categoryMapping[key];
		subcatDropDown.options.length = 0;
		
		subcatDropDown.options[0] = new Option('ALL', 'ALL_SUBCATEGORIES');
		
		// use length-1 to skip the STOP sign		
		var limit = subcategories.length - 1;
		
		for (var i = 0; i < limit; i++) {
			subcatDropDown.options[i+1] = new Option(subcategories[i], subcategories[i]);
			
			if (subcategories[i] == oldSelectedSubcatVal) {
				subcatDropDown.selectedIndex = i+1;
			}
		}
		
		var opt = findOptionWithValue(oldSelectedSubcatVal, subcatDropDown);
		if (opt != undefined)
			opt.selected = true;
	}
}

function initializeEventsSearchBox() {
	// init day drop downs
	monthOnChange('start');
	monthOnChange('end');
	// init subcategories
	categoryOnChange();
	// init calendars	
	initCalendar(startDateCalendar, "start");
	initCalendar(endDateCalendar, "end");
}

if (arrOnLoad) {
	// add init callback to onload array
	arrOnLoad.push("initializeEventsSearchBox()");
}


function changeToDate()
{  
   
    var fromMonthYear = document.forms['EventsCalendarForm'].startMonthYear.options[document.forms['EventsCalendarForm'].startMonthYear.selectedIndex].value;
var date = stringToDate(fromMonthYear);
var fromMonth=date.month;
    var fromDay = document.forms['EventsCalendarForm'].startDay.options[document.forms['EventsCalendarForm'].startDay.selectedIndex].value;

   var fromYear=date.year;
   
    var fromDate = new Date();
    fromDate.setYear(fromYear);
    fromDate.setDate(fromDay);
    fromDate.setMonth(fromMonth-1);
    
	var fromUTC = fromDate.getTime();
	var toUTC = fromUTC + (6*24*60*60*1000);
	fromDate = new Date();
	fromDate.setTime(fromUTC);
	var toDate = new Date();
	toDate.setTime(toUTC);
	
	var toMonthYear = dateToString(toDate.getMonth()+1,toDate.getYear());
	
	for (var i=0; i<document.forms['EventsCalendarForm'].endDay.options.length; i++) 
	{
    	if (document.forms['EventsCalendarForm'].endDay.options[i].value==toDate.getDate())
    	{
        	document.forms['EventsCalendarForm'].endDay.selectedIndex=i;
    	}
    }
	for (var i=0; i<document.forms['EventsCalendarForm'].endMonthYear.options.length; i++) 
	{
    	if (document.forms['EventsCalendarForm'].endMonthYear.options[i].value==toMonthYear)
    	{
        	document.forms['EventsCalendarForm'].endMonthYear.selectedIndex=i;
    	}
    }
	
}