// Accessible Date Picker Calendar - webSemantics
// http://www.websemantics.co.uk/tutorials/accessible_date_picker_calendar/

var baseURL="gen_search.asp"
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))


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(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)
}

// function to capture the keyboard activation of the calendar.  Pressing the Enter key will fire the onClick event
function verifyKey(oElement,oEvent){
	if (oEvent.keyCode==13 && oElement.onclick){
		oEvent();
	}
}

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 ='<div id="accessible_nav"><a href="#skipcalendar">Skip the date selection calendar</a></div>'
  calendar_html='<div id="accessible_nav"><a href="#skipcalendar">Skip the date selection calendar</a></div>'
  calendar_html+='<label for="selectedDate" id="calendarLabel"><span class="formLabel"><span class="blockLabel">Posted Date</span></span>'
  calendar_html+='<input type="text" id="selectedDate" name="initial_date" readonly="readonly" />'
  //calendar_html+='<input type="text" id="selectedDate" name="initial_date" readonly="readonly" /></label>'
  //calendar_html+='<label for="selectedDate" id="calendarLabel"><span class="formLabel"><span class="blockLabel"><a href="#skipcalendar">Skip the date selection calendar</a></span></span>'
  calendar_html+='<table id="calendar" align="center" summary="Calendar - Select date to search">'
  calendar_html+='<thead>'
  var currDate = new Date();
//  alert("month : " + month + " this_month : " + currDate.getYear());
//  alert("month : " + month + " this_month : " + this_month.getMonth());
//  alert("year : " + year + " this_year : " + this_month.getYear());
  if (month >= currDate.getMonth() && year >= currDate.getYear()){
	  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" id="monthName">'+months[month]+' '+year+'</th><td class="weekend"><strong><a title="Next month: '+months[(month==11)?0:month+1]+'">&raquo;</a></strong></td></tr>'
  }else{
	  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" id="monthName">'+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>'
  }
  //var monthClass="";
  calendar_html+='<tr class="days"><th scope="col" class="weekend" headers="monthName"><abbr title="Sunday">S</abbr></th><th scope="col" headers="monthName"><abbr title="Monday">M</abbr></th><th scope="col" headers="monthName"><abbr title="Tuesday">T</abbr></th><th scope="col" headers="monthName"><abbr title="Wednesday">W</abbr></th><th scope="col" headers="monthName"><abbr title="Thursday">T</abbr></th><th scope="col" headers="monthName"><abbr title="Friday">F</abbr></th><th scope="col" class="weekend" headers="monthName"><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

//alert("days in this month : " + days_in_this_month);
  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">'+day_counter+'</td>'
    if(day==day_counter)calendar_html+=' class="current"><a title="'+full_date(day_counter,mm,year)+'" href="'+baseURL+dbSTR+ addlead0(day_counter)+"/"+mm+"/"+year+'" onClick="dateSelected('+day_counter+','+mm+','+year+'); return false;">'+day_counter+'</a></td>'
    if(day<day_counter && month>=currDate.getMonth())calendar_html+=' class="weekend">'+day_counter+'</td>'
    if(day<day_counter && month<currDate.getMonth())calendar_html+='><a title="'+full_date(day_counter,mm,year)+'" href="'+baseURL+dbSTR+ addlead0(day_counter)+"/"+mm+"/"+year+'" onClick="dateSelected('+day_counter+','+mm+','+year+'); return false;">'+day_counter+'</a></td>'
    if(day>day_counter)calendar_html+='><a title="'+full_date(day_counter,mm,year)+'" href="'+baseURL+dbSTR+ addlead0(day_counter)+"/"+mm+"/"+year+'" onClick="dateSelected('+day_counter+','+mm+','+year+'); return false;">'+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></label>'
  calendar_html+='<a name="skipcalendar" id="skipcalendar"></a>'

  document.write(calendar_html)  //Display the calendar.
}
