var date  = new Date();
var cur_year  = date.getFullYear();
var cur_month = date.getMonth()+1;
var cur_day = date.getDate();


//This returns the days in a given month
function daysInMonth(month,year) {
  var dd = new Date(year, month, 0);
  return dd.getDate();
}

//This will update the 'days' <select> to have the correct number of days
function updateDays(id) {
  var monthSelect = document.getElementById(id+"_month");
  var month       = monthSelect.options[monthSelect.selectedIndex].value;  
  var yearSelect  = document.getElementById(id+"_year");
  var year        = yearSelect.options[yearSelect.selectedIndex].value;
  var daySelect   = document.getElementById(id+"_day");
  var days        = daysInMonth(month,year);
  
  if(days != daySelect.options.length) {
    if (days < daySelect.options.length) {
      daySelect.options.length = days;
    }
    else {
      for(var i=daySelect.options.length; i < days; i+=1) {
        daySelect.options[i] = new Option(i+1,i+1);
      }
    }
  }
 
  //Do not let the user select a date in the future (past todays date)
  if(year == cur_year) {
    monthSelect.options.length = cur_month;

    if(month >= cur_month) {
    daySelect.options.length = cur_day;
    }
  }
  else {
    //check the number of months here
    if(monthSelect.options.length < 12) {
      for(var i=monthSelect.options.length; i < 12; i+=1) {
        monthSelect.options[i] = new Option(i+1,i+1);
      }
    }
  }
}

//This will set the calender to have today auto-selected
function setCalendar(start,end) {
  var calendar_form   = document.forms["calendarForm"];
  var curr_year_index = calendar_form.beginning_year.options.length-1;

  if(start != "" && end != "") {
    var startSplit = start.split("-");
    var endSplit   = end.split("-");
    calendar_form.beginning_month.options[startSplit[1]-1].selected = true;
    calendar_form.ending_month.options[endSplit[1]-1].selected = true;
    calendar_form.beginning_day.options[startSplit[2]-1].selected = true;
    calendar_form.ending_day.options[endSplit[2]-1].selected = true;
    calendar_form.beginning_year.options[curr_year_index-(cur_year-startSplit[0])].selected = true;
    calendar_form.ending_year.options[curr_year_index-(cur_year-endSplit[0])].selected = true;    
  }
  else {
    calendar_form.beginning_month.options[cur_month-1].selected = true;
    calendar_form.ending_month.options[cur_month-1].selected = true;
    calendar_form.beginning_day.options[cur_day-1].selected = true;
    calendar_form.ending_day.options[cur_day-1].selected = true;
    calendar_form.beginning_year.options[curr_year_index].selected = true;
    calendar_form.ending_year.options[curr_year_index].selected = true;    
  }

  updateDays('beginning');
  updateDays('ending');
}

//This will initially add the options to the calendars <select>
//The years will need to be adjusted according to the desired range
function initCalendarOptions() {
  var max_days   = 31;
  var max_months = 12;
  var start_year = 2007;
  var calendar_form = document.forms["calendarForm"];
  
  for(i = 0; i < max_days; i+=1) {
    calendar_form.beginning_day.options[i] = new Option(i+1,i+1);
    calendar_form.ending_day.options[i]    = new Option(i+1,i+1);
  }
  
  for(i = 0; i < max_months; i+=1) {
    calendar_form.beginning_month.options[i] = new Option(i+1,i+1);
    calendar_form.ending_month.options[i]    = new Option(i+1,i+1);
  }
  
  for(i = start_year; i <= cur_year; i+=1) {
    var idx = i - start_year;
    calendar_form.beginning_year.options[idx] = new Option(i,i);
    calendar_form.ending_year.options[idx]    = new Option(i,i);
  }
}

function buildDate() {
  var start = buildDateHelper("beginning");
  var end   = buildDateHelper("ending");
  
  if(end < start) {
    alert("Beginning Date cannot be after Ending Date.");
    return;
  }

  var calendar_form = document.forms["calendarForm"];
  var selected = "";
  
  for (var i = 0; i < calendar_form.breakdown.length; i++) {
    if (calendar_form.breakdown[i].checked) {
     selected = calendar_form.breakdown[i].value;
    }
  }

  if(selected == 'cumulative') {
    //window.location = "stats.pl?start="+start+"&end="+end;
    window.location = "index.cgi?action=stats&start="+start+"&end="+end;
  }
  else if(selected == 'daily') {
    //window.location = "daily_stats.pl?start="+start+"&end="+end;
    window.location = "index.cgi?action=daily_stats&start="+start+"&end="+end;
  }
}

function buildDateHelper(id) {
  var calendar_form = document.forms["calendarForm"];

  var yearSelect  = document.getElementById(id+"_year");
  var year        = yearSelect.options[yearSelect.selectedIndex].value;  
  var monthSelect = document.getElementById(id+"_month");
  var month       = monthSelect.options[monthSelect.selectedIndex].value;
  if(month < 10) {
    month = "0"+month;
  }
  var daySelect   = document.getElementById(id+"_day");
  var day         =daySelect.options[daySelect.selectedIndex].value;
  if(day < 10) {
    day = "0"+day;
  }
  return year+"-"+month+"-"+day;
}

function downloadUserInfo() {
  var start = buildDateHelper("beginning");
  var end   = buildDateHelper("ending");
  
  if(end < start) {
    alert("Beginning Date cannot be after Ending Date.");
    return;
  }
  //window.location = "download_user_stats.pl?start="+start+"&end="+end;
  window.location = "index.cgi?action=download_user_stats&start="+start+"&end="+end;
}

