// Accessible Date Picker Calendar - webSemantics
// http://www.websemantics.co.uk/tutorials/accessible_date_picker_calendar/

var baseURL="thisweekgames2.php"
var origURL="thisweekgames.php"
var baseURLhome="thisweekhomegames2.php"
var origURLhome="thisweekhomegames.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,"/")
if (x.slice(x.length-19,x.length-10)==dbSTR) 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))


//The date of the calendar is read from the URL otherwise todays date is used. Also the replace function is used to ensure compatibility between form submission (JavaScript off) and calendar submission (JavaScript on).

//The small functions
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())
  return (daysofweek[t_date.getDay()]+", "+months[t_date.getMonth()]+" "+t_date.getDate()+", "+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(d)+"/"+addlead0((m==0)?12:m)+"/"+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(d)+"/"+addlead0((m==13)?1:m)+"/"+y)
}

//The "back a month" and "forward a month" functions are a little complex due to a check to insure the date remains valid.



//Initialise the calendar function


function calendarhome(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" >'
  calendar_html+='<thead>'
  calendar_html+='<tr><td><strong><a href="'+origURLhome+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="'+origURL+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>'

  //Display the calendar table head
  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>'}

  // fill leading blank days first
  week_day=first_week_day
  mm=addlead0(next_month.getMonth())
  mm=(mm==0)?12:mm

  // The month of days loop
  for(day_counter=1;day_counter<=days_in_this_month;day_counter++) {
    week_day%=7
    if(week_day==0) calendar_html+='</tr><tr>'

      calendar_html+='<td'
      if(day==day_counter) calendar_html+=' class="current"'
      calendar_html+='><a title="'+full_date(day_counter,mm,year)+'" href="'+baseURLhome+dbSTR+ addlead0(day_counter)+"-"+mm+"-"+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">'+full_date(today_date.getDate(),today_date.getMonth()+1,today_date.getFullYear())+'</td></tr>'
  calendar_html+='</tfoot>'
  calendar_html+='</table>'

  document.write(calendar_html)  //Display the calendar.
}

