
var now         = new Date();
var month        = now.getMonth();
month++; //accounts for Month array starting at 0
var monthday    = now.getDate();
var year        = now.getYear();
if(year < 2000) { year = year + 1900; }

function getTableDate(variation){ 
/*
This function calculates the date(s) to be displayed in <td> cells on the page. Each cell can display a unique date based on 
paramaters passed in source html. Displayed date options = current date, past date (by a specified # of days), future date (by a specified # of days)
- <td> elements on the page that will use this function must be uniquely identified according to the following convention:
  1st cell - <td id="dateSpot0">
  2nd cell - <td id="dateSpot1">
  3rd cell - <td id="dateSpot2">...
- TO DISPLAY CURRENT DATE IN CELL (actually handled by popDate() function), leave the uniqely-identified cell empty. 
  For example: <td id="dateSpot7"></td>
  Page onLoad fills the cell with the current date
- TO DISPLAY FUTURE DATE IN CELL, provide # of days as cell content. 
  For example: <td id="dateSpot7">9</td>
  If today were 6/26/2004, page onLoad would replace the '9' with '7/5/2004'
- TO DISPLAY PAST DATE IN CELL, provide -# of days as cell content. 
  For example: <td id="dateSpot7">-11</td>
  If today were 8/7/2004, page onLoad would replace the '-11' with '7/27/2004'
*/ 

  var calcdMonth;
  var calcdMonthday;
  var caldYear;
  
  if (variation){
    if (parseInt(variation)<0) {  //NEGATIVE VARIATIONS - if so  - whether neg amount indicates previous month
      if (-parseInt(variation)>=monthday){ // for cases in which neg amount = previous month
        switch (month){
          case 5:
          case 7:
          case 10:
          case 12: //if current month may, july, aug, oct, dec - subtract dif. b/w variation and current date from 30 and set month to month-1
            calcdMonth=month-1;
            calcdMonthday=30-(-parseInt(variation)-monthday);
            calcdYear=year;
            break;
          case 2:
          case 4:
          case 6:
          case 8:
          case 9:
          case 11: //if current month feb, april, june, sept, nov - subtract dif. b/w variation and current date from 31 and set month to month-1
            calcdMonth=month-1;
            calcdMonthday=31-(-parseInt(variation)-monthday);
            calcdYear=year;
            break;
          case 3: //if current month march - subtract dif. b/w variation and current date from 28 and set month to month-1
            calcdMonth=month-1;
            calcdMonthday=28-(-parseInt(variation)-monthday);
            calcdYear=year;
            break;
          case 1: //if current month jan - subtract dif. b/w variation and current date from 31, set month to 12, set year to year-1;
            calcdMonth='12';
            calcdMonthday=31-(-parseInt(variation)-monthday);
            calcdYear=year-1;
            break;
        }
      }
      else { // for cases in which neg amount <> previous month
        calcdMonth=month;
        calcdMonthday=monthday+parseInt(variation);
        calcdYear=year;
      }
    }
    else { //POSITIVE VARIATIONS
      switch (month){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10: //tests if current month jan, mar, may, july, august, oct - if so, whether variation indicates next month
          if ((parseInt(variation)+monthday)>31){ //next month
            calcdMonth=month+1;
            calcdMonthday=(parseInt(variation)+monthday)-31;
            calcdYear=year;
          }
          else { //current month
            calcdMonth=month;
            calcdMonthday=parseInt(variation)+monthday;
            calcdYear=year;
          }
          break;
        case 4:
        case 6:
        case 9:
        case 11: //tests if current month april, june, sept, nov - if so, whether variation indicates next month 
          if ((parseInt(variation)+monthday)>30){ //next month
            calcdMonth=month+1;
            calcdMonthday=(parseInt(variation)+monthday)-30;
            calcdYear=year;
          }
          else { //current month
            calcdMonth=month;
            calcdMonthday=parseInt(variation)+monthday;
            calcdYear=year;
          }
          break;
        case 2: //special case for Feb
          if ((parseInt(variation)+monthday)>28){ //next month
            calcdMonth=month+1;
            calcdMonthday=(parseInt(variation)+monthday)-28;
            calcdYear=year;
          }
          else { //current month
            calcdMonth=month;
            calcdMonthday=parseInt(variation)+monthday;
            calcdYear=year;
          }
          break;
        case 12: //special case for December
          if ((parseInt(variation)+monthday)>31){ //next month
            calcdMonth=1;
            calcdMonthday=(parseInt(variation)+monthday)-31;
            calcdYear=year+1;
          }
          else { //current month
            calcdMonth=month;
            calcdMonthday=parseInt(variation)+monthday;
            calcdYear=year;
          }
          break;
      }
    }
  }
  else {
    calcdMonth=month;
    calcdMonthday=monthday;
    calcdYear=year;
  }
  if (calcdMonth<10){
    calcdMonth='0'+calcdMonth;
  }
  if (calcdMonthday<10){
    calcdMonthday='0'+calcdMonthday;
  }
  dateString=calcdMonth+'/'+calcdMonthday+'/'+calcdYear;
  return dateString;
} 

function popDate(){ //Executed on page onLoad, this function in turn executes getTableDate() for each correctly id'd <td> element on the page
  i=0;
  do {
    var dateSpot=document.getElementById('dateSpot'+i);
    if (dateSpot.innerText){
      var variation=dateSpot.innerText;
      dateSpot.innerText=getTableDate(variation);
    }
    else {
      dateSpot.innerText=getTableDate();
    }
    i++;
  } while (document.getElementById('dateSpot'+i));
}


