// Popup Window
function popup(src, width, height) {
  width = typeof(width) != 'undefined' ? width : 600;
  height = typeof(height) != 'undefined' ? height : 600;
  var features = 'location=0,scrollbars=1,statusbar=0,menubar=0,width='+width+',height='+height+',resizable=1';
  var theWindow = window.open(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
  theWindow.focus();
  return theWindow;
}

// Toggle Div Display
function toggleDiv(id) {
  var ele = document.getElementById(id);
  if (ele.style.display == 'none') {
    ele.style.display = 'block';
  } else {
    ele.style.display = 'none';
  }
}

// Toggle Div Display by Checkbox Trigger
function toggleDivByCheckbox(checkboxId, divId) {
  var trigger = document.getElementById(checkboxId);
  var target = document.getElementById(divId);
  if (trigger.checked) {
    target.style.display = 'block';
  } else {
    target.style.display = 'none';
  }
}

// Day Selector
function changeLastDayOfMonth(var_head) {

  var month_days = new Array();
  month_days[1]  = 31;
  month_days[2]  = 28;
  month_days[3]  = 31;
  month_days[4]  = 30;
  month_days[5]  = 31;
  month_days[6]  = 30;
  month_days[7]  = 31;
  month_days[8]  = 31;
  month_days[9]  = 30;
  month_days[10] = 31;
  month_days[11] = 30;
  month_days[12] = 31;

  var year_object  = document.getElementById(var_head+'_yy');
  var month_object = document.getElementById(var_head+'_mm');
  var date_object  = document.getElementById(var_head+'_dd');

  var this_year  = year_object[year_object.selectedIndex].value;
  var this_month = month_object[month_object.selectedIndex].value;
  var this_date  = date_object[date_object.selectedIndex].value;

  var date_selected_index = date_object.selectedIndex;

  if (this_year > 0 && this_month > 0) {
    /* FEBRUARY: CHECK LEAP YEAR */
    if (this_month == 2) {
      Last2Digits = this_year % 100
      if (Last2Digits == 0) {
        flag = this_year % 400;
      } else {
        flag = this_year % 4;
      }
      if (flag == 0) {
        /* LEAP YEAR */
        last_day = 29;
      } else {
        last_day = 28;
      }
    } else {
      last_day = month_days[this_month];
    }

    if (date_selected_index > last_day) {
      /* THIS MAKES SENSE BECAUSE 0 ELEMENT IS '-> DATE' */
      date_selected_index = last_day;
      date_object.options[date_selected_index].selected = true;
    }

    var option_length = date_object.options.length;
    for (var i=option_length-1; i>=29; i--) {
      date_object.options[i] = null;
    }
    for (var i=29; i<=last_day; i++) {
      date_object.options[i] = new Option(i,i);
      if (date_selected_index == i) {
        date_object.options[i].selected = true;
      }
    }

  }
}

