// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} 
                    

mn=['Jan','Feb','March','April','May','June','July','Aug','Sept','Oct','Nov','Dec'];
dim=[31,0,31,30,31,30,31,31,30,31,30,31];

todaydate=new Date()
curmonth=todaydate.getMonth()+1 //get current month (1-12)
curyear=todaydate.getFullYear() //get current year

function resizeScrollable(){

windowHeight = pageHeight() - 460;
// windowHeight = pageHeight() - 493;
if (windowHeight >=170){
// alert(windowHeight);
document.getElementById('middleColumn').style.height=windowHeight + "px";}

else{
document.getElementById('middleColumn').style.height="auto";
document.getElementById('middleColumn').style.overflow="auto";
}

}

function initCalendar(){

resizeScrollable();


var cContainerText = "";
var cContainerText = "";

cContainerText+='<select id="calendarSelect" onchange="javascript: showMonth()">';
var k=0;

for(j=0;j<12;j++){
var l = curmonth-1+j;
if(l>=12){
l=l-12;
k=1;
}
cContainerText += '<option value="'+l+','+(parseInt(curyear)+k)+'">'+mn[l]+' - '+(parseInt(curyear)+k)+'</option>';

}

cContainerText+='</select><div id="calendarContainer"></div>';

document.getElementById('leftNavCalendar').innerHTML = cContainerText;
 var calendarText ="";
 calendarText += buildCal(curmonth ,curyear, "thecalendar", "month", "daysofweek", "calDays", 1);

var nextmonth = parseInt(curmonth)+1;

var nextyear = curyear;
if(nextmonth>=12){
nextyear=nextyear+1;
nextmonth=1;
}
 
 calendarText += buildCal(nextmonth ,nextyear, "thecalendar", "month", "daysofweek", "calDays", 1);
 document.getElementById('calendarContainer').innerHTML = calendarText;
var monthHeaders = document.getElementById('tripsList').getElementsByTagName('h2');
for (i=0;i<monthHeaders.length;i++){
monthHeaders[i].style.background="transparent";
} 
 try{
 document.getElementById('date' + curmonth + curyear).style.background="url(/images/common/indicator.gif) 0px 5px no-repeat";
}catch(e){}

}

function showMonth(){
newDateString=document.getElementById('calendarSelect').value;
newDateArray=newDateString.split(",")
newmonth=newDateArray[0];
newyear=newDateArray[1];
calendarText = "";

calendarText += buildCal(parseInt(newmonth)+1 ,newyear, "thecalendar", "month", "daysofweek", "calDays", 1);
window.location.href="#date"+(parseInt(newmonth)+1)+newyear;

var nextmonth = parseInt(newmonth)+1;
var nextyear = newyear;
if(nextmonth>=12){
nextyear=(parseInt(nextyear)+1);
nextmonth=0;
}

calendarText += buildCal((nextmonth)+1 ,nextyear, "thecalendar", "month", "daysofweek", "calDays", 1);
document.getElementById('calendarContainer').innerHTML = calendarText;
// alert('date' + (parseInt(newmonth)+1) + newyear);

var monthHeaders = document.getElementById('tripsList').getElementsByTagName('h2');
for (i=0;i<monthHeaders.length;i++){
monthHeaders[i].style.background="transparent";
}

try{
document.getElementById('date' + (parseInt(newmonth)+1) + newyear).style.background="url(/images/common/indicator.gif) 0px 5px no-repeat";
}catch(e){}
}



function buildCal(m, y, cM, cH, cDW, cD, brdr){


var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

var t='<div class="'+cM+'">';
t+='<p><strong><a href="#date'+m + y+'">'+mn[m-1]+' - '+y+'</a></strong></p>';
t+='<table class="'+cM+'" cols="7" cellpadding="0" border="'+brdr+'" cellspacing="0"><tr align="center">';
for(s=0;s<7;s++)t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
t+='</tr><tr align="center">';
for(i=1;i<=42;i++){
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '';

if(x==""){
t+='<td class="noday">&nbsp;</td>';

}

else if (x==scanfortoday) {
t+='<td id="today" class="'+cD+' ">'+x+'</td>'; }

else{
t+='<td class="'+cD+'">'+x+'</td>';}
if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
}
return t+='</tr></table></div>';
}

