/**************************************************************************
* Popup Calendar - By Nathan DeJong for AccessDubuqe.com/THonline.com
*    The basic drawing of calendar based off of Basic Calendar
* 
* Include - calendar.js in the head of your page
* Include - calendar.css in the head of your page (or copy it into 
*           your site's css file)
* Include - <div id="popup_calendar"></div> in the body of your page
* Include - onfocus="openCal(this);" in each text field you want 
*           the calendar to pop up for
*
* Basic Calendar - By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
*    Script featured on Dynamic Drive (http://www.dynamicdrive.com)
*    This notice must stay intact for use
*    Visit http://www.dynamicdrive.com/ for full source code
**************************************************************************/

//****** Global Variables
var popup_calendar_inputbox; // Variable to store active text box to post date to
var popup_calendar_todaydate=new Date(); // Variable for current day
var popup_calendar_curmonth=popup_calendar_todaydate.getMonth()+1;  //get current month (1-12)
var popup_calendar_curyear=popup_calendar_todaydate.getFullYear(); //get current year

//****** Functions for opening and closing calendar
// Positions and then shows calendar
function openCal(textbox){
  popup_calendar_inputbox=textbox;
  if (popup_calendar_inputbox.size==0)
    xOffset=(7*6)+25;
  else if (popup_calendar_inputbox.size==99)
    xOffset=-190;
  else
    xOffset=(popup_calendar_inputbox.size*6)+25;
  document.getElementById("popup_calendar").style.left=(findPosX(popup_calendar_inputbox)+xOffset)+"px";
  document.getElementById("popup_calendar").style.top=findPosY(popup_calendar_inputbox)+"px";
  if (popup_calendar_inputbox.value != ""){
    var startingdate=new Date(popup_calendar_inputbox.value);
    if(startingdate.getMonth()+1>0){
      popup_calendar_curmonth=startingdate.getMonth()+1;
      popup_calendar_curyear=startingdate.getFullYear();
    }
  }
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
  document.getElementById("popup_calendar").style.display="block";
}

// Hides Calendar from view
function popup_closeCal(){
  document.getElementById("popup_calendar").style.display="none";
}

// Function to be overwitten on page if user wants additional code to execute on completion
// function must be declared before the link to this file
// function must not be declared in this file (reason for it being commented out here)
/*function popup_additional(){
}*/

// Passes choosen date to text field, hides calendar, resets it to current month
function popup_fillInDate(day){
  popup_closeCal();
  popup_calendar_inputbox.value=popup_calendar_curmonth+"/"+day+"/"+popup_calendar_curyear;
  popup_resetCal();
  try {popup_additional();} finally{}
}

// Passes today's date to text field, hides calendar, resets it to current month
function popup_today(){
  popup_closeCal();
  popup_calendar_inputbox.value=popup_calendar_todaydate.getMonth()+1+"/"+popup_calendar_todaydate.getDate()+"/"+popup_calendar_todaydate.getFullYear();
  popup_resetCal();
  try {popup_additional();} finally{}
}

// Resets calendar to current month
function popup_resetCal(){
  popup_calendar_curmonth=popup_calendar_todaydate.getMonth()+1;  // reset month
  popup_calendar_curyear=popup_calendar_todaydate.getFullYear(); // reset year
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
}

//****** Functions for changing months and years
// Flips calendar to Previous Month
function popup_prevMonth(){
  if(popup_calendar_curmonth==1){popup_calendar_curmonth=12;popup_calendar_curyear-=1;}
  else popup_calendar_curmonth-=1;
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
}

// Flips calendar to Next Month
function popup_nextMonth(){
  if(popup_calendar_curmonth==12){popup_calendar_curmonth=1;popup_calendar_curyear+=1;}
  else popup_calendar_curmonth+=1;
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
}

// Flips calendar to Previous Year
function popup_prevYear(){
  popup_calendar_curyear-=1;
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
}

// Flips calendar to Next Year
function popup_nextYear(){
  popup_calendar_curyear+=1;
  document.getElementById("popup_calendar").innerHTML=popup_buildCal();
}

//****** Function for drawing calender
function popup_buildCal(){
  var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
  var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

  var oD = new Date(popup_calendar_curyear, popup_calendar_curmonth-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 popup_calendar_todaydate=new Date(); //DD added
  var scanfortoday=(popup_calendar_curyear==popup_calendar_todaydate.getFullYear() && popup_calendar_curmonth==popup_calendar_todaydate.getMonth()+1)? popup_calendar_todaydate.getDate() : 0 //DD added
    dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
  
  var t='<table class="main" cellpadding="0" cellspacing="0"><tr>';
  t+='<td align="left" width="14" class="move"><a href="javascript:popup_prevYear();" title="Previous Year">&lt;&lt;<\/a><\/td>';
  t+='<td align="left" width="8" class="move"><a href="javascript:popup_prevMonth();" title="Previous Month">&lt;<\/a><\/td>';
  t+='<td align="center" class="month">'+mn[popup_calendar_curmonth-1]+' - '+popup_calendar_curyear+'<\/td>';
  t+='<td align="right" width="8" class="move" on><a href="javascript:popup_nextMonth();" title="Next Month">&gt;<\/a><\/td>';
  t+='<td align="right" width="14" class="move"><a href="javascript:popup_nextYear();" title="Next Year">&gt;&gt;<\/a><\/td>';
  t+='<\/tr><tr>';
  t+='<td colspan="5"><table class="inner" border="0" cols="7" cellpadding="0" cellspacing="0">';
  for(s=0;s<7;s++)
    t+='<td class="daysofweek">'+"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[popup_calendar_curmonth-1]))? i-oD.od+1 : '|';
    if (x!='|'){
      if(x==scanfortoday)
        x='<a href="javascript:popup_fillInDate('+x+');" id="today">'+x+'<\/a>';
      else
        x='<a href="javascript:popup_fillInDate('+x+');">'+x+'<\/a>';
      t+='<td class="days">'+x+'<\/td>';
    } else 
      t+='<td class="blank">'+x+'<\/td>';
    if(((i)%7==0)&&(i<36))t+='<\/tr><tr align="center">';
  }
  t+='<\/tr><tr><td colspan="4" align="left" class="daysofweek"><a href="javascript:popup_today();">today<\/a><\/td>';
  t+='<td colspan="3" align="right" class="daysofweek"><a href="javascript:popup_closeCal();popup_resetCal();">close<\/a><\/td><\/tr>';
  t+='<\/table><\/td>';
  return t+='<\/tr><\/table>';
}

//****** Supporting functions
// Finds the top left x coord of obj passed to it
function findPosX(obj){
  var curleft=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curleft+=obj.offsetLeft;
      obj=obj.offsetParent;
    }
  } else if(obj.x)
    curleft+=obj.x;
  return curleft;
}

// Finds the top left y coord of obj passed to it
function findPosY(obj){
  var curtop=0;
  if(obj.offsetParent){
    while(obj.offsetParent){
      curtop+=obj.offsetTop;
      obj=obj.offsetParent;
    }
  } else if(obj.y)
    curtop+=obj.y;
  return curtop;
}
