// a simple calendar
// 2007 ryan yoosefi
//
// requires a div with id "calendar" and an input field with id "date"
// call up the initial calendar without arguments: placeCalendar()

// retrieve an array of the current set date in the date field
// returns Array(month,day,year)
var FCurrentDate = "";

function calGetCurrentDate(){
 return FCurrentDate.split("-");
}

function calSetCurrentDate(month,day,year)
{
   FCurrentDate = month+"-"+day+"-"+year;
}

// places the date in the "date" input field when calendar is clicked
// and highlights that day on the calendar
function s(month,day,year)
{
 currentDate = calGetCurrentDate();
 try
     {document.getElementById(currentDate[1]).className='day';}
 catch(e){}//incase new month has more days than last

 if (self.DisplayPlanForDate)
 {
     DisplayPlanForDate(year, month, day);
 }

 FCurrentDate = month+"-"+day+"-"+year;
 document.getElementById(day).className='highlighted';
}

// navigate the calendar left/right (backward/forward)
function calendarNav(direction)
{
 switch (direction){
  case "left":{
   if(d.getMonth()=="0")
     {newMonth=11;newYear=d.getFullYear()-1;}
   else
     {newMonth=d.getMonth()-1;newYear=d.getFullYear()}
   break;
  }
  case "right":{
   if(d.getMonth()==11)
     {newMonth=0;newYear=d.getFullYear()+1;}
   else
     {newMonth=d.getMonth()+1;newYear=d.getFullYear();}
   break;
  }
  default:{
   // do nothing. basically, if you manually set the date field
   // you can use calendarNav() with no args to refresh what day
   // is highlighted.
   // this however is only useful if you are in the same month.
   // to display a custom calendar you should really use
   // placeCalendar(month,year,day);
   break;
  }
 }

 // refresh the calendar
 placeCalendar(newMonth,newYear);

 // preserve date highlighting
 currentDate = calGetCurrentDate();
 if(typeof currentDate[1]!="undefined"){
  if ((currentDate[0]==newMonth+1)&&(currentDate[2]==newYear)){
   s(newMonth+1,currentDate[1],newYear);
  }
 }
}



// write/refresh calendar to the calendar div
// all args optnl:
//  month - sets month of calendar; 0=january
//  year - sets year
//  day - highlight a given day

function placeCalendar(month,year,day){
 // main date object
 d=new Date();

 // load the date object with provided vars if any
 if(typeof month!="undefined")d.setMonth(month);
 if(typeof year!="undefined")d.setFullYear(year);
 if(typeof day!="undefined"){d.setDate(day);highlightDay=day;}

 // use this copy of the date object for local manipulation
 dx=d;

 // bring date vars to local ones
 month=d.getMonth();
 year=d.getFullYear();
 day=d.getDate();

 // configure months and weeks
 days=new Array('Su','M','Tu','W','Th','F','Sa');
 months=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
 mlen=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 if(d.getFullYear()%4==0){mlen[1]=29;} //leapyear

 // calendar start html
 cal="<table width=\"170\" class=\"calendar\" cellpadding=\"2\" cellspacing=\"0\"><tr>";
 cal+="<td class=\"nav\"><a href=\"javascript:{}\" onclick=\"calendarNav(\'left\')\">&lt;&lt;</a></td>";
 cal+="<td colspan=\"5\" class=\"month\">"+months[month]+" "+year+"</td>";
 cal+="<td class=\"nav\"><a href=\"javascript:{}\" onclick=\"calendarNav(\'right\')\">&gt;&gt;</a></td></tr><tr>";
 for(i=0;i<=6;i++){cal+="<td class=\"weekday\" id=\""+days[i]+"\">"+days[i]+"</td>";}
 cal+="</tr><tr>";

 // iterate the days and insert them
 for(i=1;i<=mlen[dx.getMonth()];i++){
  dx.setDate(i);
  iday=dx.getDay();

  // starting at appropriate day of the week
  if(i==1){
   for(j=0;j<iday;j++){
    cal+="<td>&nbsp;</td>";
   }
  }
  if((iday==0)&&(i!=1))cal+="<tr>";
  cal+="<td id=\""+i+"\" class=\"day\"><a href=\"javascript:{}\" onclick=\"s("+(month+1)+","+i+","+year+");\">"+i+"</a></td>";
  if((iday==6)||(i==days[month]))cal+="</tr>";
 }

 // close the calendar table
 cal+="</table>";

  // write out the calendar to the div
 document.getElementById('plancalendar').innerHTML=cal;

 // if a day was specified as an argument, highlight it
 if (typeof highlightDay!="undefined")s(month+1,highlightDay,year);

 // and by default, if there currently is no selected date at all
 // then select the current day
 currentDate = calGetCurrentDate();
 if (typeof currentDate[1]=="undefined")
     s(month+1,day,year);
}
