wkdays = ["S","M","T","W","T","F","S"];
months = ["January","February","March","April","May","June",
	 "July","August","September","October","November","December"];
monthdays = [31,28,31,30,31,30,31,31,30,31,30,31];

// not to be called from user code
function _cellout(year,month,day,thisyear,thismonth,thisday,nextdays,query) {
    document.write('<td');
    if (year == thisyear && month == thismonth && day == thisday)
	document.write(' class="calendar-day-today"');
    document.write('><span class="calendar-day">');

    nextmonth = (month+1)%12;
    nextyear = year;
    if (nextmonth < month)
	nextyear++;

    if (query.length == 0 || query.indexOf('?') == -1)
        query = '/concerts/search?';
    if (query.substr(0,query.indexOf('?')) != 'search')
        query = '/concerts/search' + query.substr(query.indexOf('?'));
    document.write('<a href="' + query + '&amp;from_day=' + day +
		   '&amp;from_month=' + (month+1) +
                   '&amp;from_year=' + year +
		   '&amp;to_day=' + Math.min(day,nextdays) +
		   '&amp;to_month=' + (nextmonth+1) +
		   '&amp;to_year=' + nextyear + '">');
    document.write(day+'</a></span></td>');
    document.writeln();
    return;

}

function displayCalendar(cyear,cmonth,cday,query) {
    // make month zero-offset to simplify calculations and correspond with
    // the way Date().getMonth() works.  when displaying <a> tags this means
    // we have to add one to the month arguments
    cmonth--;

    today = new Date();
    year = today.getFullYear();
    month = today.getMonth();

    // month previous to the calendar being displayed (cmonth/cyear)
    lastyear = cyear;
    lastmonth = (cmonth-1)%12;
    if (lastmonth > cmonth) {
        lastyear--;
    }

    // month after the calendar being displayed (cmonth/cyear)
    nextyear = cyear;
    nextmonth = (cmonth+1)%12;
    if (nextmonth < cmonth)
        nextyear++;

    // the month after that
    nextnextyear = nextyear;
    nextnextmonth = (nextmonth+1)%12;
    if (nextnextmonth < nextmonth)
        nextnextyear++;

    document.write('<table border="0">');
    document.writeln();

    document.write("<tr>");
    document.writeln();

    // only display link to last month if it's >= the actual month
    document.write("<td>");
    document.writeln();
    if (lastyear > year || lastyear == year && lastmonth >= month) {
        if (query.length == 0 || query.indexOf('?') == -1)
            query = '/concerts/search?';
        if (query.substr(0,query.indexOf('?')) != 'search')
            query = '/concerts/search' + query.substr(query.indexOf('?'));
	if (query.indexOf('&start=') != -1)
	    if (query.indexOf('&',query.indexOf('&start=')+1) == -1) // &start is last parameter
	        query = query.substring(0,query.indexOf('&start='))
	    else
	        query = query.substring(0,query.indexOf('&start=')) + query.substring(query.indexOf('&',query.indexOf('&start=')+1))
        document.write('<a href="' + query +
		       '&amp;from_day=1' +
		       '&amp;from_month=' + (lastmonth+1) +
		       '&amp;from_year=' + lastyear +
		       '&amp;to_day=1' +
		       '&amp;to_month=' + (cmonth+1) +
		       '&amp;to_year=' + cyear +
		       '">')
        document.write("&laquo;");
	document.write("</a>");
    } else
        document.write("&nbsp;");
    document.write("</td>");
    document.writeln();

    // display month and year for this calendar
    document.write('<td colspan="5" align="center">' +
		   months[cmonth] + " " + cyear + "</td>");
    document.writeln();

    // only display link to next month if it's >= the actual month
    document.write("<td>");
    if (nextyear > year || nextyear == year && nextmonth >= month) {
        if (query.length == 0 || query.indexOf('?') == -1)
            query = '/concerts/search?';
        if (query.substr(0,query.indexOf('?')) != 'search')
            query = '/concerts/search' + query.substr(query.indexOf('?'));
	if (query.indexOf('&start=') != -1)
	    if (query.indexOf('&',query.indexOf('&start=')+1) == -1) // &start is last parameter
	        query = query.substring(0,query.indexOf('&start='))
	    else
	        query = query.substring(0,query.indexOf('&start=')) + query.substring(query.indexOf('&',query.indexOf('&start=')+1))
	document.write('<a href="' + query +
		       '&amp;from_day=1' +
		       '&amp;from_month=' + (nextmonth+1) +
		       '&amp;from_year=' + nextyear +
		       '&amp;to_day=1' +
		       '&amp;to_month=' + (nextnextmonth+1) +
		       '&amp;to_year=' + nextnextyear +
		       '">')
        document.write("&raquo;");
	document.write("</a>");
    } else
	document.write("&nbsp;");
    document.write("</td>");
    document.writeln();
    
    document.write("</tr>");
    document.writeln();

    // write days of week
    document.write('<tr class="calendar-weekdays">');
    for(i=0; i<7; i++)
	document.write('<td align="center" class="calendar-weekday">' +
		       wkdays[i] + "</td>");
    document.write("</tr>");
    document.writeln();

    // outputs first line of calendar
    document.write('<tr class="calendar-week">');
    firstofmonth = new Date(cyear,cmonth,1);
    wkday = firstofmonth.getDay();
    for (i=0; i<wkday; i++)
	document.write("<td>&nbsp;</td>");
    
    days = monthdays[cmonth];
    if (cmonth == 1 && cyear % 4 == 0 && cyear % 400 != 0) days = 29;
    nextdays = monthdays[nextmonth];
    if (nextmonth == 1 && nextyear % 4 == 0 && nextyear % 400 != 0)
	nextdays = 29;
    // display body of calendar
    for (i=1; i<=days; i++) {
        _cellout(cyear,cmonth,i,today.getFullYear(),
		today.getMonth(),today.getDate(),nextdays,query);
	wkday = (wkday+1) % 7;
	if (!wkday) {
	    document.write("</tr>");
	    document.writeln();
	    document.write("<tr>");
	}
    }

    document.write("</tr>");
    document.writeln();
    document.write("</table>");
    document.writeln();
}
