// Javascript auto events update -- "vanish" past events
// Set events container (DIV, TD, whatever) ID="events"
// Call updateEvents() in BODY ONLOAD

function round2(num) {
 if (num<10) { return '0'+num }
 else { return ''+num };
}

function dateCheck() {
 now = new Date();
 today = "" + now.getFullYear() + round2(now.getMonth()+1) + round2(now.getDate());
 return today
}

function updateEvents()
{
  var thispage = new String(self.location);
//  Ansible site only -- don't wipe an archived page
  if ( thispage.substring(0,19) != 'http://news.ansible' ) {
   return;
  }

  monthstr = 'JanFebMarAprMayJunJulAugSepOctNovDec';
  var eventbox = document.getElementById("events");
  if ( eventbox == null ) {
   // No such ID? Then process all LI elements
   var allItems = document.getElementsByTagName("LI");
  }
  else {
   // Confine search to element whose ID is "events"
   var allItems = eventbox.getElementsByTagName("LI");
  }
  var num = allItems.length;
  var today = dateCheck();

//  alert("There are " + num + " <li> elements in this document.");
  for ( i=0; i<num; i++ ) {
// find the text contents of each element
   var content = allItems[i].innerHTML;
// hide element depending on contents -- updated for 2010s
   where = content.indexOf('201');
   if ( where <0 ) {
    where = content.indexOf('202');
   }
   if ( where >=0 ) {
    var year = content.substring(where,where+4);
    tmp = content.substring(0,where);
// strip any "Until"
    if (tmp.substring(0,6).toLowerCase() == "until ") {
     tmp = tmp.substring(6,20);
    }
// strip any opening boldfaced etc bit, e.g. CANCELLED
    where = tmp.indexOf('</');
    if ( where >=0 ) {
     tmp = tmp.substring(where+5,40);
    }
// strip first date in e.g. "10-12 Nov"
    var hyphen = tmp.indexOf('-');
    if (hyphen >= 0) {
     var tmp = tmp.substring(hyphen+1,20);
     if (tmp.substring(0,1) == ' ') {
      tmp = tmp.substring(1,20);
     }
    }
    if ( tmp.substring(1,2) == ' ') {
     tmp = '0'+tmp;
    }
    month = tmp.substring(3,6); // 3-letter month
    year = year + round2( (monthstr.indexOf(month)/3) +1)  + tmp.substring(0,2);
//    alert(year + ' [' + tmp + '] '+ today);
    if ( year < today ) {
     allItems[i].style.display = 'none';
    }
   }
  }
}


