// version 1; last updated: 19 May 2005 by Bill Bellon

availableFilesNew = unique(availableFiles);
availableFiles = availableFilesNew;

// ===================================================
// FUNCTIONS
// ===================================================

//---------------------------------------------------
function getAvailable(requestedPart, searchParam) {
  /* Example:
     availableYears = getAvailable('year', 'MEM');
     availableMonths = getAvailable('month', 'MEM2005');
     availableDays = getAvailable('day', 'MEM200505');
  */
  available = new Array();
  index = 0;
  var x;
  strParts = new Array();
  
  if (requestedPart == "year") {
    for (i = 0 ; i < availableFiles.length; i++) {
      strParts["year"] = availableFiles[i].substr(0,4);
//      strParts["month"] = availableFiles[i].substr(4,2);	
//      strParts["day"] = availableFiles[i].substr(6,2);
      strParts["location"] = availableFiles[i].substr(9,3);
      if ( strParts["location"] == searchParam) {
	    available[index] = strParts["year"];
  	  index = index + 1;
	  }
    }
  } else if (requestedPart == "month") {
    for (i = 0 ; i < availableFiles.length; i++) {
      strParts["year"] = availableFiles[i].substr(0,4);
      strParts["month"] = availableFiles[i].substr(4,2);	
//      strParts["day"] = availableFiles[i].substr(6,2);
      strParts["location"] = availableFiles[i].substr(9,3);
      if ( strParts["location"] + strParts["year"] == searchParam) {
	    available[index] = strParts["month"];
  	  index = index + 1;
	  }
    }  
  } else if (requestedPart == "day") {
    for (i = 0 ; i < availableFiles.length; i++) {
      strParts["year"] = availableFiles[i].substr(0,4);
      strParts["month"] = availableFiles[i].substr(4,2);	
      strParts["day"] = availableFiles[i].substr(6,2);
      strParts["location"] = availableFiles[i].substr(9,3);
      if ( strParts["location"] + strParts["year"] + strParts["month"] == searchParam) {
	    available[index] = strParts["day"];
  	  index = index + 1;
	  }
    }  
  }
  
 if (available.length > 0) {
   available = unique(available);
 }
 
 return available; 
}

//---------------------------------------------------
function unique(inputArray) {
  inputArray.sort();
  index = 0;
  newArray = new Array();
  newArray[0] = inputArray[0];
  for (i=1; i < inputArray.length; i++) {
    if (newArray[index] != inputArray[i]) {
      index = index + 1;
	  newArray[index] = inputArray[i];
    }
  }

  return newArray;
}

//---------------------------------------------------
function removeAllChildNodes(node) {
  if (node && node.hasChildNodes && node.removeChild) {
    while (node.hasChildNodes()) {
      node.removeChild(node.firstChild);
    }
  }
}

//---------------------------------------------------
function format00(number) {
  var numberString = new String(number);
  if (0 <= number && number <= 9) {
    formattedNumber = '0' + numberString;
  } else {
    formattedNumber = numberString;  
  }
  
  return formattedNumber;
}

//---------------------------------------------------
function getSelectedIndex(obj) {
  var index = -1;
  var selectField = obj;
  for (i = 0; i < selectField.length; i++) {
    if (obj.options[i].selected) {
	  index = i;
	  break;
	}
  }
  
  return index;
}  

//---------------------------------------------------
function updateMenus() {
  
  // determine currently selected location, year, month and day
  dayIndex = getSelectedIndex(document.menuForm.day);
  monthIndex = getSelectedIndex(document.menuForm.month);
  yearIndex = getSelectedIndex(document.menuForm.year);
  locationIndex = getSelectedIndex(document.menuForm.location);    

 // determine what days are available for selected location, year, and month 
 str1 = document.menuForm.location.options[locationIndex].value;
 str2 = str1 + document.menuForm.year.options[yearIndex].value;	     
 str3 = str2 + document.menuForm.month.options[monthIndex].value;	   

 availableYears = getAvailable('year', str1);
 availableMonths = getAvailable('month', str2);
 availableDays = getAvailable('day', str3);
 
  // create array of days and their css classes for drop down menu
  days = new Array();
  dayClasses = new Array();
  for (i = 1; i <= 31; i++) {
    var a = format00(i);
    days[i-1] = a;
    dayClasses[i-1] = 'disabled';
  }
  
  // create array of months and their css classes for drop down menu
  months = new Array();
  monthClasses = new Array();
  for (i = 1; i <= 12; i++) {
    months[i-1] = i;
    monthClasses[i-1] = 'disabled';
  }
  
  // create array of years and their css classes for drop down menu
  years = allYears;
  yearClasses = new Array();
  for (var i in years) {
    yearClasses[i] = 'disabled';
  }
 
  // enable days that exist for given location, year and month
  for (i = 0; i < availableDays.length; i++) {
    // Firefox hack to fix parseInt bug
	x = parseInt(availableDays[i]);
	if (availableDays[i] == '08') {
      x = 8;
	} else if (availableDays[i] == '09') {
      x = 9;
	}
	
    dayClasses[x - 1] = 'enabled';
  }
  
  // enable months that exist for given location and year
  for (i = 0; i < availableMonths.length; i++) {
    monthClasses[parseInt(availableMonths[i]) - 1] = 'enabled';
  }
  
  // enable years that exist for given location
  for (i = 0; i < availableYears.length; i++) {
    yearClasses[availableYears[i]] = 'enabled';
  }

  // replace existing day drop down menu with new one
  removeAllChildNodes(document.getElementById("day"));
  selectField = document.getElementById("day");
  for (i = 0; i < days.length; i++) {
    opt = document.createElement("option");
    opt.setAttribute("id", days[i]);
    opt.setAttribute("name", days[i]);      
    opt.setAttribute("class", dayClasses[i]);  
    opt.setAttribute("value", days[i]);	  	
    if (dayClasses[i] == "enabled") {
      optLabel = document.createTextNode(days[i]);
	} else {
      optLabel= document.createTextNode("*" + days[i] + "*");	
	}
    opt.appendChild(optLabel);
    selectField.appendChild(opt);  
  }
  
  // reselect selected index
  document.menuForm.day.selectedIndex = dayIndex;
  
  // replace existing month drop down menu with new one
  monthLabels = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  removeAllChildNodes(document.getElementById("month"));
  selectField = document.getElementById("month");
  for (i = 0; i < months.length; i++) {
    var m00 = format00(months[i]);		
    opt = document.createElement("option");
    opt.setAttribute("id", m00);
    opt.setAttribute("name", m00);      
    opt.setAttribute("class", monthClasses[i]);  
    opt.setAttribute("value", m00);	
    if (monthClasses[i] == "enabled") {
      optLabel = document.createTextNode(monthLabels[i]);
	} else {
      optLabel= document.createTextNode("*" + monthLabels[i] + "*");	
	}
    opt.appendChild(optLabel);
    selectField.appendChild(opt);  
  }
  
  // reselect selected index
  document.menuForm.month.selectedIndex = monthIndex;
  
  // replace existing year drop down menu with new one
  removeAllChildNodes(document.getElementById("year"));
  selectField = document.getElementById("year");
  for (var y in years) {
    opt = document.createElement("option");
    opt.setAttribute("id", years[y]);
    opt.setAttribute("name", years[y]);      
    opt.setAttribute("class", yearClasses[y]);  
    opt.setAttribute("value", years[y]);	
    if (yearClasses[y] == "enabled") {
      optLabel = document.createTextNode(years[y]);
	} else {
      optLabel= document.createTextNode("*" + years[y] + "*");	
	}
    opt.appendChild(optLabel);
    selectField.appendChild(opt);  
  }
  
  // reselect selected index
  document.menuForm.year.selectedIndex = yearIndex;

}
//============================================================
// END OF FUNCTIONS
//============================================================
