// Accessible Date Picker Calendar - webSemantics
// http://www.websemantics.co.uk/tutorials/accessible_date_picker_calendar/

var baseURL="ttracker.php"
var dbSTR="?strDate="
var today_date=new Date()
var months     = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
var daysofweek = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')

// get date from the url otherwise use the current date
var date=new Date()
x=window.location.href
x=x.replace(/%2F/g,"/")

/*
alert(x.slice(x.length-19,x.length-10));


alert(x.slice(x.length-4,x.length));
alert(x.slice(x.length-7,x.length-5)-1);
alert(x.slice(x.length-10,x.length-8));
*/

if (x.slice(x.length-19,x.length-10)==dbSTR) 
{
	date.setYear(x.slice(x.length-4,x.length))
	date.setMonth(x.slice(x.length-10,x.length-8)-1)
	date.setDate(x.slice(x.length-7,x.length-5))
//	date=new Date(x.slice(x.length-4,x.length),x.slice(x.length-7,x.length-5)-1,x.slice(x.length-10,x.length-8))
//alert(date);
}
function full_date(d,m,y){  // m = 1-12 not 0-11
  t_date=new Date(y,m-1,d)
  return (daysofweek[t_date.getDay()]+", "+t_date.getDate()+" "+months[t_date.getMonth()]+" "+t_date.getFullYear())
}


function addlead0(x){return ((x<10)?"0"+x:x)}


function backamonth(d,m,y){  // m = 1-12 not 0-11
  if ((m==3)&&(d>28)){
    d=29
    t_date=new Date(y,1,d)
    if (d!=t_date.getDate()) d--
  }
  if (((m==12)||(m==10)||(m==7)||(m==5))&&(d==31)) d--
  if (m==1) y--
  m--
  return (addlead0((m==0)?12:m)+"/"+addlead0(d)+"/"+y)
}


function forwardamonth(d,m,y){  // m = 1-12 not 0-11
  if ((m==1)&&(d>28)){
    d=29
    t_date= new Date(y,1,d)
    if (d!=t_date.getDate()) d--
  }
  if (((m==3)||(m==5)||(m==8)||(m==10))&&(d==31)) d--
  if (m==12) y++
  m++
  return (addlead0((m==13)?1:m)+"/"+addlead0(d)+"/"+y)
}


function calendar(date) {
  // modified version of calendar.js from http://scripts.franciscocharrua.com/calendar.php

  day   = date.getDate()
  month = date.getMonth()
  year  = date.getFullYear()

  this_month = new Date(year, month, 1)
  next_month = new Date(year, month + 1, 1)

  //Find out when this month starts and ends.
  first_week_day = this_month.getDay()
  days_in_this_month = Math.floor((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24))
  if (month==2) days_in_this_month=31

  calendar_html ='<table id="calendar" align="center" summary="Room booking calendar - Select date to book">'
  calendar_html+='<thead>'
  calendar_html+='<tr><td><strong><a href="'+baseURL+dbSTR+backamonth(day,month+1,year)+'" title="Previous month: '+months[(month==0)?11:month-1]+'">&laquo;</a></strong></td><th colspan="5" class="heading">'+months[month]+' '+year+'</th><td><strong><a href="'+baseURL+dbSTR+forwardamonth(day,month+1,year)+'" title="Next month: '+months[(month==11)?0:month+1]+'">&raquo;</a></strong></td></tr>'
  calendar_html+='<tr class="days"><th scope="col" class="weekend"><abbr title="Sunday">S</abbr></th><th scope="col"><abbr title="Monday">M</abbr></th><th scope="col"><abbr title="Tuesday">T</abbr></th><th scope="col"><abbr title="Wednesday">W</abbr></th><th scope="col"><abbr title="Thursday">T</abbr></th><th scope="col"><abbr title="Friday">F</abbr></th><th scope="col" class="weekend"><abbr title="Saturday">S</abbr></th></tr>'
  calendar_html+='</thead>'
  calendar_html+='<tbody>'
  calendar_html+='<tr>'

  for(week_day=0;week_day<first_week_day;week_day++){ calendar_html+=((week_day==0)||(week_day==6))?'<td class="weekend">&nbsp;</td>':'<td>&nbsp;</td>'}

  week_day=first_week_day
  mm=addlead0(next_month.getMonth())
  mm=(mm==0)?12:mm

  for(day_counter=1;day_counter<=days_in_this_month;day_counter++) {
    week_day%=7
    if(week_day==0) calendar_html+='</tr><tr>'

   // if ((week_day==0)||(week_day==6))
      //calendar_html+='<td class="weekend">'+day_counter+'</td>'
   // else {
      calendar_html+='<td'
      if(day==day_counter) calendar_html+=' class="current"'
      calendar_html+='><a title="'+full_date(day_counter,mm,year)+'" href="'+baseURL+dbSTR+ mm+"/"+addlead0(day_counter)+"/"+year+'">'+day_counter+'</a></td>'
   // }
    week_day++
  }

  for(week_day=week_day;week_day<7;week_day++){calendar_html+=((week_day==0)||(week_day==6))?'<td class="weekend">&nbsp;</td>':'<td>&nbsp;</td>'}

  calendar_html+='</tr>'
  calendar_html+='</tbody>'
  calendar_html+='<tfoot>'
  calendar_html+='<tr><td colspan="7" class="foot"><a title="Today" href="'+baseURL+'">'+full_date(today_date.getDate(),today_date.getMonth()+1,today_date.getFullYear())+'</a></td></tr>'
  calendar_html+='</tfoot>'
  calendar_html+='</table>'

  document.write(calendar_html)  //Display the calendar.
}
