var calmeta = new Array();
var dateobj = new Date;
var monthnames = new Array('','January','February','March','April','May','June','July','August','September','October','November','December');
//var monthnames = new Array('','Jan.','Feb.','Mar.','Apr.','May','June','July','Aug.','Sept.','Oct.','Nov.','Dec.');

function pickdate( choice, formeltid, eltid ) {
 var res = choice.split(/-/);
 document.getElementById(formeltid).value=monthnames[parseInt(res[1])] + ' ' + parseInt(res[2]) + ', ' + parseInt(res[0]);
 if( eltid ) { document.getElementById(eltid).style.display='none'; }
}

function initcalendar( eltid, year, month, link, startdate, enddate ) {
 if( year == 0 ) {
  month = dateobj.getMonth() + 1; year = dateobj.getFullYear(); 
 }
 calmeta[eltid] = new Array( year, month, link, startdate, enddate );
 changecalendar( eltid, 0, 0 ); 
}

function changecalendar( eltid, dir, render ) {
 presentmonth = dateobj.getMonth() + 1; presentyear = dateobj.getFullYear(); presentday = dateobj.getDate();
 thisyear = calmeta[eltid][0]; thismonth = calmeta[eltid][1] + dir; link = calmeta[eltid][2];
 startdate = calmeta[eltid][3]; enddate = calmeta[eltid][4];
 
 if( thismonth > 12 ) { thisyear++; thismonth = 1; } else if( thismonth < 1 ) { thisyear--; thismonth = 12; }
 calmeta[eltid][0] = thisyear; calmeta[eltid][1] = thismonth;
 prevyear = thisyear; prevmonth = thismonth - 1;
 if( prevmonth < 1 ) { prevyear--; prevmonth = 12; }
 prevdate = prevyear + '-' + (prevmonth < 10 ? '0' : '') + prevmonth + '-31';

 // Show month-to-month controls
 if( startdate <= prevdate ) {
  document.getElementById(eltid + 'prev').innerHTML = '<a href="javascript:changecalendar(\'' + eltid + '\',-1,1);">&lt;&lt;</a>'; 
 } else {
  document.getElementById(eltid + 'prev').innerHTML = '';
 }
 nextyear = thisyear; nextmonth = thismonth + 1;
 if( prevmonth > 12 ) { prevyear++; prevmonth = 1; }
 nextdate = nextyear + '-' + (nextmonth < 10 ? '0' : '') + nextmonth + '-01';
 
 if( enddate >= nextdate ) {
  document.getElementById(eltid + 'next').innerHTML = '<a href="javascript:changecalendar(\'' + eltid + '\',1,1);">&gt;&gt;</a>';
 } else {
  document.getElementById(eltid + 'next').innerHTML = '';
 }

 // Build actual calendar
 if( render ) {
  document.getElementById(eltid + 'title').innerHTML = monthnames[thismonth] + ' ' + thisyear;
  out = '';
  daysinmonth = 32 - new Date(thisyear, thismonth-1, 32).getDate();
  firstday = new Date(thisyear, thismonth-1, 1).getDay();
  for(i=0; i<firstday; i++) {
   out = out + '<div></div>';
  }
  for(i=1; i<=daysinmonth; i++) {
   thisdate = thisyear + '-' + (thismonth < 10 ? '0' : '') + thismonth + '-' + (i < 10 ? '0' : '') + i;
   if( thisdate >= startdate && thisdate <= enddate ) { 
    if( link.match(/=$/) ) {
     thislink = link + thisdate;
    } else {
     thislink = link.replace( /\(date\)/, thisdate );
    }
    out = out + '<div><a href="' + thislink + '">' + i + '</a></div>';
   } else {
    out = out + '<div>' + i + '</div>';
   }
  }
  document.getElementById(eltid + 'days').innerHTML = out;
 }
}

