  var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  var years = ["2007", "2008", "2009"];

  var nxt_active = new Image();
  nxt_active.src = "/images/moneyforbreakfast/nxtmonth.gif";
  var nxt_inactive = new Image();
  nxt_inactive.src = "/images/moneyforbreakfast/nxtmonth-inactive.gif";

  function setToday() {
    var now = new Date();
    var day = now.getDate();
    var month = now.getMonth();
    var year = now.getYear();
    
    if (year < 2000)    // Y2K Fix, Isaac Powell
      year = year + 1900; // http://onyx.idbsu.edu/~ipowell
      
    this.focusDay = day;
    setElements(month, year);
    calendar(month, year);
  }
  
  function setElements(month, year) {
    $('#month').html( months[month]);
    $('#year').html( year);
    document.calControl.year.value = year;
    document.calControl.month.value = month;
    
    var now = new Date();

    if ((month + '/' + year) == (now.getMonth() + '/' + now.getFullYear())) {
      document.getElementById('prvmonth_img').src = nxt_inactive.src;
      //document.getElementById('prvmonth_img').src = document.getElementById('prvmonth_img').src.replace(".gif", "-inactive.gif");
    } else {
      if (document.getElementById('prvmonth_img').src.indexOf("-inactive.gif") != -1)
        document.getElementById('prvmonth_img').src = nxt_active.src;
        //document.getElementById('prvmonth_img').src = document.getElementById('prvmonth_img').src.replace("-inactive.gif", ".gif");
    }
  }

  function setPreviousMonth() {
    var year  = document.calControl.year.value;
    var day   = 0;
    var month = document.calControl.month.value;
    if (month == 0) {
      month = 11;
      if (year > 1000) {
        year--;
        document.calControl.year.value = year;
      }
    } else {
      month--;
    }
    
   setElements(month, year); 
   calendar(month, year);
  }

  function setNextMonth() {
    var year  = document.calControl.year.value;
    var day   = 0;
    var month = document.calControl.month.value;

    if (month == 11) {
      month = 0;
      year++;
      document.calControl.year.value = year;
    } else {
      month++;
    }

    setElements(month, year); 
    calendar(month, year);
  }

  function calendar(month, year) {
   
    //current month
    var now = new Date();
    var day = now.getDate();
    var current_month = now.getMonth();

    var current_year = now.getFullYear();
    var current_date = Date.parse(months[current_month]+" "+day+","+current_year);
    //days we are setting
    year = parseInt(year);
    var mydate = Date.parse(months[month]+" "+day+","+year);
    var output = '';
    var extra = '';
    var i = 0;
    var days = getDaysInMonth(month+1,year);
    var firstOfMonth = new Date (year, month, 1);
    var startingPos = firstOfMonth.getDay();
   
    days += startingPos;
    //week
    output += '<ul class="row week">'+
                '<li class="day">Su</li>'+
                '<li class="day">Mo</li>'+
                '<li class="day">Tu</li>'+
                '<li class="day">We</li>'+
                '<li class="day">Th</li>'+
                '<li class="day">Fr</li>'+
                '<li class="day">Sa</li>'+
              '</ul>';
    //days
    output += '<ul class="row">';
    for (i = 0; i < startingPos; i++) {
     // if ( i%7 == 0 ) {
        output += '<li>&nbsp;</li>';
     // }
    }
    var num = '';
    //add 0 to month if is < 10
    month = (parseInt(month))+1;
    if (month < 10) {
      month = '0'+month;
    }
    for (i = startingPos; i < days; i++) {
      if ( i%7 == 0 ) { 
        output += "</ul><ul class='row'>";
      }
      
      num = i-startingPos+1;
        //bold the day if is today or past events
        //for current month
        if (mydate==current_date) {
          if (num < day) {
            extra = 'bold';
          } else if (num == day) {
            extra = 'current';
          } else {
            extra = '';
          }
        //future dates  
        }else if (mydate > current_date) {
          extra = '';
        }else {
          extra = 'bold';
        }
        
      if (num < 10) {
        num = '0'+num;
      }
      if (extra!='') {
        output += '<li class="day '+extra+'"><a href="/money-for-breakfast/transcript/'+year+''+month+''+num+'">'+ num +'</a></li>';
      }else {
        output += '<li class="day">'+ num +'</li>';
      }
    }
    output += '</ul>';        
    //clear
    output +='<ul class="row"><li class="clear"></li></ul>';
    
    $('.calendar-table').html(output);
  }

  function getDaysInMonth(month,year)  {
    var days;
    
    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
      days=31;
    else if (month==4 || month==6 || month==9 || month==11)
      days=30;
    else if (month==2)  {
      if (isLeapYear(year)) {
       days=29;
      } else { days=28;
      }
    }

    return (days);
  }

  function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
    return (true);
    } else {
      return (false);
    }
  }

    
  function isWeekend(month, date, year) {
    var d=new Date();
    d.setMonth(month - 1);
    d.setDate(date);
    d.setFullYear(year);

    //alert(months[d.getMonth()] + '/' + d.getDate() + '/' + d.getFullYear() + ':' + days[d.getDay()] + ' ' + d.getDay());

    if(d.getDay() == 0 || d.getDay() == 6)
      return true;
    else
      return false;
  }
  