var startDate=0;
var endDate=0;
function calendar(cal, timestamp, cur_day)
{
	months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	now = new Date();
	date = new Date(now.getFullYear(), now.getMonth(), now.getDate());

	if (timestamp)
		date.setTime(timestamp*1000);
	
	if (cal == "calendarstart" && cur_day) {
		startDate = timestamp;
		if (startDate == 0) startDate = parseInt(date.getTime()/1000);
	}
	if (cal == "calendarend" && cur_day) endDate = timestamp;
	
	if (startDate && cal=="calendarstart") {
		var selDate = new Date(startDate*1000);
	}
	else if (endDate)
		var selDate = new Date(endDate*1000);

	day = date.getDate();
	month = date.getMonth();
	year = date.getFullYear();

	this_month = new Date(year, month, 1);
	nextMonth = new Date(year, month + 1, 1);

	//Find out when this month starts and ends.         
	first_week_day = this_month.getDay()-1;
	if (first_week_day <= -1) first_week_day=6;
	
	days_in_this_month = parseInt((nextMonth.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));

	if (cal=="calendarstart") calendar_html = '<h3>Start Date</h3>';
	else calendar_html = '<h3>End Date</h3>';
	calendar_html += '<table class="calendar">';

	calendar_html += '<tr><th class="left"><a href="" onclick="last_month(\''+cal+'\','+timestamp+'); return false;">&#171;</a></th><th colspan="5" class="center">' + 
	months[month] + ' ' + year + '</th><th class="right"><a href="" onclick="next_month(\''+cal+'\','+timestamp+'); return false;">&#187;</a></th></tr>';
	calendar_html += '<tr class="wdays"><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td><td>S</td></tr>';

	//Fill the first week of the month with the appropriate number of blanks.     
	calendar_html += '<tr>';
	for(week_day = 0; week_day < first_week_day; week_day++)
		calendar_html += '<td> </td>';   

	week_day = first_week_day;
	row_count = 0;
	for(day_counter = 1; day_counter <= days_in_this_month; day_counter++) {
		
		if(week_day == 7) {
			calendar_html += '</tr><tr>';
			row_count++;
		}
		week_day %= 7;
		if(selDate && day_counter == day && month == selDate.getMonth() && year == selDate.getFullYear())   
			calendar_html += '<td class="curday">' + day_counter + '</td>';
		else {
			if (cal=="calendarstart") calendar_html += '<td><a href="" onclick="update_start('+year+','+month+','+day_counter+'); return false;">' + 	day_counter + '</a></td>';
			if (cal=="calendarend") calendar_html += '<td><a href="" onclick="update_end('+year+','+month+','+day_counter+'); return false;">' + 	day_counter + '</a></td>';
		}
		
		week_day++;
	}

	calendar_html += '</tr>';
	if (row_count<=4)
		calendar_html += '<tr><td colspan="7">&nbsp;</td></tr>';
	if (row_count<=3)
		calendar_html += '<tr><td colspan="7">&nbsp;</td></tr>';
	calendar_html += '</table>';

	document.getElementById(cal).innerHTML = calendar_html;
}

function last_month(cal, timestamp) {
	now = new Date();
	date = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	if (timestamp) date.setTime(timestamp*1000);
	date.setMonth(date.getMonth()-1);
	calendar(cal, date.getTime()/1000, 0);
}

function next_month(cal, timestamp) {
	now = new Date();
	date = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	if (timestamp) date.setTime(timestamp*1000);
	date.setMonth(date.getMonth()+1);
	calendar(cal, date.getTime()/1000, 0);
}

function update_start(year, month, day) {
	temp = new Date(year, month, day, 0, 0, 0);
	
	if (endDate && parseInt(temp.getTime()/1000) > endDate) {
		//alert('Start date cannot be after end date!');
		//return false;
		endDate = 0;
	}
	startDate = parseInt(temp.getTime()/1000);
	var newLoc = 'gigs?startdate='+startDate+'&enddate='+endDate;
	var venue = document.getElementById('selvenue');
	var band = document.getElementById('selband');
	var genre = document.getElementById('selgenre');
	var loca = document.getElementById('sellocation');
	var nl = document.getElementById('fnonlocal');
	newLoc += '&venue='+venue.options[venue.selectedIndex].value;
	newLoc += '&band='+band.options[band.selectedIndex].value;
	newLoc += '&genre='+genre.options[genre.selectedIndex].value;
	newLoc += '&location='+loca.options[loca.selectedIndex].value;
	if (nl.checked)
		newLoc += '&nl=1';	

	window.location= newLoc;
}
function update_end(year, month, day) {
	temp = new Date(year, month, day, 23, 59, 59);
	
	if (parseInt(temp.getTime()/1000) < startDate) {
		alert('End date cannot be before start date!');
		return false;
	}
	endDate = parseInt(temp.getTime()/1000);
	var newLoc = 'gigs?startdate='+startDate+'&enddate='+endDate;
	var venue = document.getElementById('selvenue');
	var band = document.getElementById('selband');
	var genre = document.getElementById('selgenre');
	var loca = document.getElementById('sellocation');
	var nl = document.getElementById('fnonlocal');
	newLoc += '&venue='+venue.options[venue.selectedIndex].value;
	newLoc += '&band='+band.options[band.selectedIndex].value;
	newLoc += '&genre='+genre.options[genre.selectedIndex].value;
	newLoc += '&location='+loca.options[loca.selectedIndex].value;
	if (nl.checked)
		newLoc += '&nl=1';	
	window.location= newLoc;
}