// <!--


function getObject(objID) {

    if (document.getElementById) {
        return(document.getElementById(objID)) ;
    } else {
        return(document.all[objID]) ;
    }
}   //   getObject(objID)

// Begin global early code section A
onerror = handleErr ;
var txt = "" ;
var tempfreqsfx ;

// Create and set some GLOBAL VARIABLES
// var img_dir = "/psd/technology/bao/browser/data/images/";
var img_dir = "data/images/" ;  // relative paths work, and are more portable
// var img_str = "eur";  // no longer used
// var img_type = new Array();  // no longer used
// img_type[0] = ".000000.png"; // no longer used
// img_type[1] = ".000000.jpg"; // no longer used
//
// Here's some directory structure for reference:
//
// data/Processed_Images/daily
// BAO_LLL_yyyyddd_V.png
// where LLL = 010, 100, 300 (see below for combined)
// where yyyy = year  ddd = day_of_year   V = TRH, WS, PP
// where LLL = 10100300 (combined TRH), 10300 (combined WS)
//
// data/Processed_Images/monthly
// BAO_LLL_yyyymm_V.png
// where LLL = 010, 100, 300 (see below for combined)
// where yyyy = year    mm = month  V = TRH, WS, PP
// where LLL = 10100300 (combined TRH), 10300 (combined WS)
// 
// field names
//
// "Day" tabset before "Mon" tabset

// The "Combined" tabsets
var bname1 = new Array();
bname1[0] = "TRH";
bname1[1] = "WS";
bname1[2] = "nofileimg";

var bprfx1 = new Array() ;
bprfx1[0] = "BAO_10100300_" ;
bprfx1[1] = "BAO_10300_" ;
bprfx1[2] = "" ;

var bsfx1 = new Array() ;
bsfx1[0] = "_TRH.png" ;
bsfx1[1] = "_WS.png" ;
bsfx1[2] = "" ;

var bname5 = new Array();
bname5[0] = "TRH" ;
bname5[1] = "HT" ;
bname5[2] = "HRH" ;
bname5[3] = "nofileimg" ;

var bprfx5 = new Array() ;
bprfx5[0] = "BAO_10100300_" ;
bprfx5[1] = "BAO_10100300_" ;
bprfx5[2] = "BAO_10100300_" ;
bprfx5[3] = "" ;

var bsfx5 = new Array() ;
bsfx5[0] = "_TRH.png" ;
bsfx5[1] = "_HT.png" ;
bsfx5[2] = "_HRH.png" ;
bsfx5[3] = "" ;

// the 010 tabsets
var bname2 = new Array();
bname2[0] = "TRH";
bname2[1] = "WS";
bname2[2] = "PP";
bname2[3] = "nofileimg";

var bprfx2 = new Array() ;
bprfx2[0] = "BAO_010_" ;
bprfx2[1] = "BAO_010_" ; 
bprfx2[2] = "BAO_010_" ;
bprfx2[3] = "" ;

var bsfx2 = new Array() ;
bsfx2[0] = "_TRH.png" ;
bsfx2[1] = "_WS.png" ;
bsfx2[2] = "_PP.png" ;
bsfx2[3] = "" ;

var bname6 = new Array();
bname6[0] = "TRH";
bname6[1] = "WS";
bname6[2] = "PP";
bname6[3] = "WR" ;
bname6[4] = "nofileimg";

var bprfx6 = new Array() ;
bprfx6[0] = "BAO_010_" ;
bprfx6[1] = "BAO_010_" ; 
bprfx6[2] = "BAO_010_" ;
bprfx6[3] = "BAO_010_" ;
bprfx6[4] = "" ;

var bsfx6 = new Array() ;
bsfx6[0] = "_TRH.png" ;
bsfx6[1] = "_WS.png" ;
bsfx6[2] = "_PP.png" ;
bsfx6[3] = "_WR.png" ;
bsfx2[4] = "" ;

// the 100 tabsets
var bname3 = new Array();
bname3[0] = "TRH";
bname3[1] = "nofileimg";
bname3[2] = "nofileimg";
bname3[3] = "nofileimg";

var bprfx3 = new Array() ;
bprfx3[0] = "BAO_100_" ;
bprfx3[1] = "" ;
bprfx3[2] = "" ;
bprfx3[3] = "" ;

var bsfx3 = new Array() ;
bsfx3[0] = "_TRH.png" ;
bsfx3[1] = "" ;
bsfx3[2] = "" ;

var bname7 = new Array();
bname7[0] = "TRH";
bname7[1] = "nofileimg";
bname7[2] = "nofileimg";
bname7[3] = "nofileimg";

var bprfx7 = new Array() ;
bprfx7[0] = "BAO_100_" ;
bprfx7[1] = "" ;
bprfx7[2] = "" ;
bprfx7[3] = "" ;

var bsfx7 = new Array() ;
bsfx7[0] = "_TRH.png" ;
bsfx7[1] = "" ;
bsfx7[2] = "" ;

// the 300 tabsets
var bname4 = new Array();
bname4[0] = "TRH";
bname4[1] = "WS";
bname4[2] = "nofileimg";

var bprfx4 = new Array() ;
bprfx4[0] = "BAO_300_" ;
bprfx4[1] = "BAO_300_" ;
bprfx4[2] = "" ;

var bsfx4 = new Array() ;
bsfx4[0] = "_TRH.png" ;
bsfx4[1] = "_WS.png" ;
bsfx4[2] = "" ;

var bname8 = new Array();
bname8[0] = "TRH";
bname8[1] = "WS";
bname8[2] = "WR" ;
bname8[3] = "nofileimg";

var bprfx8 = new Array() ;
bprfx8[0] = "BAO_300_" ;
bprfx8[1] = "BAO_300_" ;
bprfx8[2] = "BAO_300_" ;
bprfx8[3] = "" ;

var bsfx8 = new Array() ;
bsfx8[0] = "_TRH.png" ;
bsfx8[1] = "_WS.png" ;
bsfx8[2] = "_WR.png" ;
bsfx8[3] = "" ;

// var dflttxt = new String(img_dir + "daily/" + bname3[5] +  "/" +
//                         "nofilelog.txt") ;
// need default images to cover daily & monthly, and all 4 levels
//
var dfltimg = new String("data/monthly/nofileimage.png" ) ;
var currTabSetName = "tabSetCombinedDay" ;
// or "tabSet010" or "tabSet100" or "tabSet300" suffixed with either Day or Mon
var numTabsDay = new Array() ;
    numTabsDay[0] = 2 ; // Combined
    numTabsDay[1] = 3 ; // 010
    numTabsDay[2] = 1 ; // 100
    numTabsDay[3] = 2 ; // 300
var numTabsMon = new Array() ;
    numTabsMon[0] = 3 ; // Combined
    numTabsMon[1] = 4 ; // 010
    numTabsMon[2] = 1 ; // 100
    numTabsMon[3] = 3 ; // 300

// End global early code section A (creating and setting some global variables)

function Cookie(document, name, hours, path, domain, secure) {
    this.$document = document;
    this.$name = name;
    if (hours)
	this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;

    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
} // end Cookie()

function _Cookie_store() {
    var cookieval = "";
    for (var prop in this) {
	if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
		continue;
	if (cookieval != "") cookieval += '&';
	cookieval += prop + ':' + escape(this[prop]);
    }

    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
	cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';

// store cookie
    this.$document.cookie = cookie;
} // end _Cookie_store()

function _Cookie_load() {
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

// extract the named cookie

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false; //cookie not defined
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start,end);

    var a = cookieval.split('&');
    for(var i=0; i < a.length; i++)
	a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++)
	this[a[i][0]] = unescape(a[i][1]);

    return true;

} // end _Cookie_load()

function _Cookie_remove() {
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this.$document.cookie = cookie;
} // end _Cookie_remove()

// Begin global early code section B
new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

// Now, set the default date to latest available (as of when the Perl script ran)
var curr_yr = Math.round(end_yr) ;
var curr_mon = Math.round(end_mon) ;
var curr_day = Math.round(end_day) ;
var curr_lvl = "Combined" ;
var curr_tf = "daily" ;
var currdate_YYYYMMDD = Math.round((curr_yr * 10000) + (curr_mon * 100) + curr_day) ;
// Get cookie values or set defaults
var dataparam = new Cookie(document, "BAOTower_field", 0);
if (!dataparam.load() || !dataparam.field || !dataparam.date) {

    dataparam.field = 400 ;
    dataparam.tf = curr_tf ;
    dataparam.lvl = curr_lvl ;
//    dataparam.date = 20050804;
    dataparam.date = Math.round(currdate_YYYYMMDD) ;
    dataparam.store();
} else { // have cookie from earlier in session
    curr_day = Math.round(Math.round(dataparam.date) % 100) ;
    curr_month = Math.round((((Math.round(dataparam.date) - curr_day) % 10000))/100) ;
    curr_yr = Math.round(Math.round(dataparam.date - 
                           ((curr_month * 100) + curr_day)) / 10000) ;
    currdate_YYYYMMDD = (curr_yr * 1000) + (curr_month * 100) + curr_day ;
    curr_tf = dataparam.tf ;
//    if (curr_tf == "monthly") {
//        goMonthly() ;
//    }
    curr_lvl = dataparam.lvl ;
//    change_field(1000 + Math.round(dataparam.field)) ;  // document not done loading
} // end if - no cookie; else - cookie exists
// End global early code section B

function reset_tabs() {
    var tabinx = Math.round(Math.round(dataparam.field) % 10) ;
    var tabsetlvl ;

    tempfreqsfx = currTabSetName.substr(-3, 3) ;
// alert("Entering reset_tabs() with currTabSetName="+currTabSetName+" and tabinx="+tabinx+".") ;
    if        (currTabSetName == "tabSetCombinedDay") {
            for (var itab = 0 ;  itab < numTabsDay[0] ;  ++itab) {
	        var tabobjnm = bname1[itab] + "1" + "D" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(2).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
	    } // end for each tab
    } else if (currTabSetName == "tabSetCombinedMon") {
            for (var itab = 0 ;  itab < numTabsMon[0] ;  ++itab) {
	        var tabobjnm = bname5[itab] + "1" + "M" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(1).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
            } // end for each tab
    } else if (currTabSetName == "tabSet010Day") {
            for (var itab = 0 ;  itab < numTabsDay[1] ;  ++itab) {
                var tabobjnm = bname2[itab] + "2" + "D" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(4).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
	    } // end for each tab
    } else if (currTabSetName == "tabSet010Mon") {
            for (var itab = 0 ;  itab < numTabsMon[1] ;  ++itab) {
                var tabobjnm = bname6[itab] + "2" + "M" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(3).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
            } // end for each tab
    } else if (currTabSetName == "tabSet100Day") {
            for (var itab = 0 ;  itab < numTabsDay[2] ;  ++itab) {
                var tabobjnm = bname3[itab] + "3" + "D" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(6).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
	    } // end for each tab
    } else if (currTabSetName == "tabSet100Mon") {
            for (var itab = 0 ;  itab < numTabsMon[2] ;  ++itab) {
                var tabobjnm = bname7[itab] + "3" + "M" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(5).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
            } // end for each tab
    } else if (currTabSetName == "tabSet300Day") {
            for (var itab = 0 ;  itab < numTabsDay[3] ;  ++itab) {
                var tabobjnm = bname4[itab] + "4" + "D" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(8).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
	    } // end for each tab
    } else if (currTabSetName == "tabSet300Mon") {
            for (var itab = 0 ;  itab < numTabsMon[3] ;  ++itab) {
                var tabobjnm = bname8[itab] + "4" + "M" ;
                var tab = getObject(tabobjnm);
                if (tab == null) {
                    alert("tab is null for "+tabobjnm+"(7).") ;
                } else {
                    if (itab == tabinx) { // turn this one on
// alert("Turning tab #"+itab+" on.") ;
                        tab.className = "xbox_set" ;
                    } else {  // turn these off
// alert("Turning tab #"+itab+" off.") ;
                        tab.className = "xbox_unset" ;
                    }
                } // tab is good in else part
            } // end for each tab
    }
//        tab.className="xbox_unset"; // de-select old tab
} // end reset_tabs(tabinx)

function change_field(field) {
// alert("In change_field(), field argument="+field+".") ;
   if (field > 1000) { // this is the initialization call; don't mess with display
// alert("It's an initialization call.") ;
      dataparam.field = Math.round(field) % 1000 ;
      dataparam.store();
      var tabinx = Math.round(Math.round(field) % 10) ;
      var tsinx = Math.round(Math.round(dataparam.field - tabinx) / 10) ;
// alert("tabinx="+tabinx+", tsinx="+tsinx+".") ;
      var tssfx ;
      var tabsetprefix ;
      var oldTabSetName = currTabSetName ;
      
      if (tsinx > 40) { // Monthly
          tempfreqsfx = "Mon" ;
          tsinx -= 50 ;
      } else {
          tempfreqsfx = "Day" ;
      }
      if (tsinx == 40) {
          tabsetprefix = "Combined" ;
      } else if (tsinx == 1) {
          tabsetprefix = "010" ;
      } else {
          tabsetprefix = tsinx + "0" ;  // AKA (10 * tsinx)
      }
      tssfx = tabsetprefix + tempfreqsfx ;
// alert("tssfx="+tssfx+".") ;
      switch (tabsetprefix) { //check correct form button; uncheck others
	    case "Combined" :
	        currTabSetName = "tabSetCombined" + tempfreqsfx ;
		document.form1.LvlSelect[0].checked = true ;
		document.form1.LvlSelect[1].checked = false ;
		document.form1.LvlSelect[2].checked = false ;
		document.form1.LvlSelect[3].checked = false ;
	    break ;
	    case "010" :
	        currTabSetName = "tabSet010" + tempfreqsfx ;
		document.form1.LvlSelect[0].checked = false ;
		document.form1.LvlSelect[1].checked = true ;
		document.form1.LvlSelect[2].checked = false ;
		document.form1.LvlSelect[3].checked = false ;
	    break ;
	    case "100" :
	        currTabSetName = "tabSet100"  + tempfreqsfx ;
		document.form1.LvlSelect[0].checked = false ;
		document.form1.LvlSelect[1].checked = false ;
		document.form1.LvlSelect[2].checked = true ;
		document.form1.LvlSelect[3].checked = false ;
	    break ;
	    case "300" :
	        currTabSetName = "tabSet300" + tempfreqsfx ;
		document.form1.LvlSelect[0].checked = false ;
		document.form1.LvlSelect[1].checked = false ;
		document.form1.LvlSelect[2].checked = false ;
		document.form1.LvlSelect[3].checked = true ;
	    break ;
	    default:
//	        alert("Field value has bad tabset column; field=" + field + ";") ;
	    break ;
      } // end switch(tabsetprefix)
//      var relPart = getTabSet() ;
      if (oldTabSetName != currTabSetName) { // need to switch
        selectTabSet(currTabSetName, 0) ;
      } // if we need to load a different set of tabs
      if (field != 400) { // don't reset tabs if they match the initial loading
          reset_tabs() ;
//        reset_tabs() ; // may do setTimeout("reset_tabs("+tabinx+")", 1000) ;
//        setTimeout("reset_tabs()", 1000) ;
// trying to call reset_tabs() in the TabSet doc's body tag onload attribute
      }
      var cookieyr = Math.round(Math.round(dataparam.date) / 10000) ;
      var cookiemon = Math.round(Math.round(dataparam.date
                                          - (10000 * cookieyr)) / 100) ;
      var cookieday = Math.round(Math.round(Math.round(dataparam.date
                                                     - (10000 * cookieyr))) -
			         (100 * cookiemon)) ;
      var monSelect = getObject("MonthSelect");
      var yrSelect = getObject("YearSelect") ;
      yrSelect.options[Math.round(cookieyr) - Math.round(beg_yr)].selected = true ;
      if (cookieyr == Math.round(beg_yr)) {
          begyr_altMonMenu(monSelect) ;
      } else if (cookieyr == Math.round(end_yr)) {
          endyr_altMonMenu(monSelect) ;
      } else {
          midyr_altMonMenu(monSelect) ;
      } // end if cookie year is beg, end, mid
      monSelect.options[Math.round(cookiemon - 1)].selected = true ;
// Now display the year-month calendar page for the cookie's date
      if (Math.round(cookiemon) < 10) {
        var path = new String("months_BAO_Tower/month_" + Math.round(cookieyr) + 
	                      "0" + Math.round(cookiemon) + ".html") ;
      } else {
        var path = new String("months_BAO_Tower/month_" + Math.round(cookieyr) +
	                      Math.round(cookiemon) + ".html") ;
      }
// alert("In change_field initialization-type call, tempfreqsfx is " + tempfreqsfx + ".") ;
      if (tempfreqsfx == "Day") {
        var ifcal = getObject("currMonth") ;
        try {
            ifcal.src = path ;
        } catch (er) {
            ifcal.src = "months_BAO_Tower/month_" + latest_date.substring(0, 6) +
	            ".html" ;
        } // end try-catch on displaying calendar page for cookie's year-month
      } else { // Monthly, but from cookie, so must change radio buttons
// alert("Searching through form elements to swap radio buttons.") ;
        var ourForm = getObject("form1") ;
        var nelems = ourForm.elements.length ;
	var indxelem ;
	for (indxelem = 0 ;  indxelem < nelems ;  ++indxelem) {
	    var currElement = ourForm.elements[indxelem] ;
	    if (currElement.nodeType == 1) { // IE doesn't have Node.ELEMENT_NODE
// alert("Found an ELEMENT_NODE; nodeName="+currElement.nodeName+".") ;
	        if (currElement.nodeName == "INPUT") {
// alert("Found an input element.") ;
	            if (currElement.id == "TfSet") {
// alert("Found an input element with id='TfSet'.") ;
	                if (currElement.value == "Daily") {
// alert("Setting Daily radio button to unchecked.") ;
	                    currElement.checked = false ;
	                } else if (currElement.value == "Monthly") {
// alert("Setting Monthly radio button to checked.") ;
	                    currElement.checked = true ;
	                }
	            }
	        }
	    }
	} /* end for each element in the form */
      }
      change_date(Math.round(dataparam.date)) ; // Reset to same date but new field
    } else { // here for the calls from the tabs
        var tabinx = Math.round(field) % 10 ;
        var tsinx = Math.round(Math.round(field - tabinx) / 10) ;
// alert("In non-initialization code, tabinx="+tabinx+", tsinx="+tsinx+".") ;
        var tssfx ;
        var tabsetprefix ;

        if (tsinx > 40) { // Monthly
            tempfreqsfx = "Mon" ;
            tsinx -= 50 ;
        } else {
            tempfreqsfx = "Day" ;
        }
	if (tsinx == 40) {
	    tabsetprefix = "Combined" ;
	} else if (tsinx == 1) {
	    tabsetprefix = "010" ;
	} else {
	    tabsetprefix = tsinx + "0" ; // AKA (10 * tsinx)
	}
	tssfx = tabsetprefix + tempfreqsfx ;
// alert("tssfx="+tssfx+".") ;
	switch (tabsetprefix) {
	    case "Combined" :
	        currTabSetName = "tabSetCombined" + tempfreqsfx ;
	    break ;
	    case "010" :
	        currTabSetName = "tabSet010" + tempfreqsfx ;
	    break ;
	    case "100" :
	        currTabSetName = "tabSet100" + tempfreqsfx ;
	    break ;
	    case "300" :
	        currTabSetName = "tabSet300" + tempfreqsfx ;
	    break ;
	    default:
//	        alert("Field value has bad tabset column; field=" + field + ";") ;
	    break ;
	} // end switch (tabsetprefix) 

        dataparam.field = Math.round(field) ;
        dataparam.store();

        change_date(dataparam.date) ; // Reset to same date but new field
        if (oldTabSetName != currTabSetName) { // need to switch
            selectTabSet(currTabSetName, 0) ;
        } // if we need to load a different set of tabs
        reset_tabs() ; // may do setTimeOut("reset_tabs()", 1000) ;
   } // else it's not the initialization call

} // end change_field()

function isLeap(year4dig) {
    if ((year4dig % 4) == 0) {
        if ((year4dig % 100) == 0) {
            if ((year4dig % 400) == 0) {
                return(true) ;
            } else {
                return(false) ;
            }
        } else {
            return(true) ;
        }
    } else {
        return(false) ;
    }
} // end isLeap(year4dig)

function getDoy(yr4dig, mon2dig, day2dig) {
// alert("Entering getDoy("+yr4dig+", "+mon2dig+", "+day2dig+")") ;
    var imonth = 1 ;
    var finmonth = mon2dig * 1 ;
    var accumdoy = 0 ;
    var numDays = day2dig * 1 ;
    var numYear = yr4dig * 1 ;
    var dys_in_mon = [31,28,31,30,31,30,31,31,30,31,30,31] ;
    
    for (imonth = 1 ;  imonth < finmonth ;  ++imonth) {
        accumdoy += dys_in_mon[imonth - 1] ;
	if ((imonth == 2) && (isLeap(numYear))) {
	    ++accumdoy ;
	}
    } // end for each month
    accumdoy += numDays ;
// alert("Leaving getDoy and returning doy="+accumdoy+".") ;
    return(accumdoy) ;
} // end getDoy(yr4dig, mon2dig, day2dig)

function change_date(date) {
// alert("Entering change_date("+date+")") ;
    if (date == 0) {
        var IFname = new String(dfltimg) ;
    } else {
        dataparam.date = Math.round(date);
        dataparam.store();
        curr_day = Math.round(date) %  100 ;  // day part of YYYYMMDD
	curr_mon = Math.round(date /   100) % 100 ; // MM in YYYYMMDD
	curr_yr =  Math.round(date / 10000) ; // year part 
	var cdateTag = getObject("CurrentDate") ;
	if (curr_mon < 10) {
	    var cmon = new String("0" + curr_mon) ;
	} else {
	    var cmon = new String(curr_mon) ;
	}
	if (curr_day < 10) {
	    var cday = new String("0" + curr_day) ;
	} else {
	    var cday = new String(curr_day) ;
	}
	if (dataparam.tf == "daily") {
	    cdateTag.innerHTML = "Current Date: &nbsp;" + curr_yr + "/" + cmon +
	                         "/" + cday ;
	} else {
	    cdateTag.innerHTML = "Current Date: &nbsp;" + curr_yr + "/" + cmon +
	                         "/01" ;
	}
        var tabnum = Math.round(dataparam.field) % 10 ;
// alert("tabnum="+tabnum+", currTabSetName="+currTabSetName+".") ;
//        var IFname = new String(img_dir + img_str + bname[dataparam.field] +
//                           "." + dataparam.date + img_type[dataparam.field]); 
        var dirPart = new String(img_dir + dataparam.tf + "/") ;
// Filename depends on temporal frequency. Also, daily files have doy, not mmdd
        var strDate = new String(dataparam.date) ;

	if (dataparam.tf == "monthly") {
	    var datestr = strDate.substring(0, 6) ;
	    tempfreqsfx = "Mon" ;
	} else {
	    var datestr = strDate.substring(0, 4) ;
// alert("datestr set to year part="+datestr+".") ;
	    var mm = strDate.substring(4, 6) ;
	    var dd = strDate.substring(6, 8) ;
	    var doy = getDoy(datestr, mm, dd) ;
	    if (doy < 10) {
// alert("doy="+doy+", and is < 10.") ;
	        datestr += "00" + doy ;
	    } else if (doy < 100) {
// alert("doy="+doy+", and is < 100.") ;
	        datestr += "0" + doy ;
	    } else {
// alert("doy="+doy+", and is >= 100.") ;
	        datestr += doy ;
	    }
	    tempfreqsfx = "Day" ;
	}
// alert("datestr="+datestr+".") ;
        if (currTabSetName == "tabSetCombinedDay") {
            if (bprfx1[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx1[tabnum]+ datestr +
		                        bsfx1[tabnum]);
	    } // legit prefix for a filename
	} else if (currTabSetName == "tabSetCombinedMon") {
            if (bprfx5[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx5[tabnum]+ datestr +
		                        bsfx5[tabnum]);
// alert("Going to load image path: "+IFname+".");
	    } // legit prefix for a filename
        } else if (currTabSetName == "tabSet010Day") {
            if (bprfx2[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx2[tabnum]+ datestr +
		                        bsfx2[tabnum]);
	    } // legit prefix => legit filename
	} else if (currTabSetName == "tabSet010Mon") {
            if (bprfx6[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx6[tabnum]+ datestr +
		                        bsfx6[tabnum]);
	    } // legit prefix => legit filename
        } else if (currTabSetName == "tabSet100Day") {
            if (bprfx3[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx3[tabnum]+ datestr +
		                        bsfx3[tabnum]);
	    } // good prefix => good filename
	} else if (currTabSetName == "tabSet100Mon") {
            if (bprfx7[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx7[tabnum]+ datestr +
		                        bsfx7[tabnum]);
	    } // good prefix => good filename
        } else if (currTabSetName == "tabSet300Day") {
            if (bprfx4[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx4[tabnum] + datestr +
			                bsfx4[tabnum]);
	    } // good prefix => good filename
	} else if (currTabSetName == "tabSet300Mon") {
            if (bprfx8[tabnum] == "nofileimg") {
                var IFname = new String(dfltimg) ;
            } else {
                var IFname = new String(dirPart + bprfx8[tabnum] + datestr +
			                bsfx8[tabnum]);
	    } // good prefix => good filename
        }
    } // else part with legit date value
// alert("Trying to load image file name="+IFname+".") ;
    var imgdivNode = getObject("imgDiv") ;
    try {
            document.data_image.src = IFname ;
    } catch (er) {
// alert("Catching error in attempt to load image, going to default image.") ;
	    document.data_image.src = dfltimg ;
    } // end catch clause
} // end change_date()

function handleErr(message, url, linenum) {
    txt = "There was an error on this page.\n" ;
    txt += "Error:  " + message + "\n" ;
    txt += "URL:  " + url + "\n" ;
    txt += "Line:  " + linenum + "\n" ;
    txt += "Click OK to continue.\n" ;
    alert(txt) ;
    var tabnum = Math.round(dataparam.field) % 10 ;
    document.data_image.src = dfltimg ;
    return false ;
} // end handleErr(message, url, linenum)

function selectTabSet(setName, fromHTML) {
// alert("In selectTabSet(setName="+setName+", fromHTML="+fromHTML+".") ;
    var tabsADay = getObject("tabSetCombinedDay") ;
    var tabsBDay = getObject("tabSet010Day") ;
    var tabsCDay = getObject("tabSet100Day") ;
    var tabsDDay = getObject("tabSet300Day") ;
    var tabsAMon = getObject("tabSetCombinedMon") ;
    var tabsBMon = getObject("tabSet010Mon") ;
    var tabsCMon = getObject("tabSet100Mon") ;
    var tabsDMon = getObject("tabSet300Mon") ;
/* above are all DIVs */
/* hide all first */
    tabsADay.style.display = "none" ;
    tabsBDay.style.display = "none" ;
    tabsCDay.style.display = "none" ;
    tabsDDay.style.display = "none" ;
    tabsAMon.style.display = "none" ;
    tabsBMon.style.display = "none" ;
    tabsCMon.style.display = "none" ;
    tabsDMon.style.display = "none" ;
    if        (setName == "tabSetCombinedDay") {
        tabsADay.style.display = "block" ;
	if (fromHTML == 1) {
            change_field(400) ;
        }
    } else if (setName == "tabSetCombinedMon") {
        tabsAMon.style.display = "block" ;
	if (fromHTML == 1) {
	    change_field(900) ;
	}
    } else if (setName == "tabSet010Day") {
        tabsBDay.style.display = "block" ;
	if (fromHTML == 1) {
	    change_field(10) ;
	}
    } else if (setName == "tabSet010Mon") {
        tabsBMon.style.display = "block" ;
	if (fromHTML == 1) {
	    change_field(510) ;
	}
    } else if (setName == "tabSet100Day") {
        tabsCDay.style.display = "block" ;
	if (fromHTML == 1) {
            change_field(100) ;
        }
    } else if (setName == "tabSet100Mon") {
        tabsCMon.style.display = "block" ;
	if (fromHTML == 1) {
            change_field(600) ;
        }
    } else if (setName == "tabSet300Day") {
        tabsDDay.style.display = "block" ;
	if (fromHTML == 1) {
            change_field(300) ;
        }
    } else if (setName == "tabSet300Mon") {
        tabsDMon.style.display = "block" ;
	if (fromHTML == 1) {
            change_field(800) ;
        }
    } else {
        alert("Try calling selectTabSet with an arg other than " + setName + ";") ;
    }
} // end selectTabSet(setName, fromHTML)

function getTabSet() { // does not work - all .displays are empty.
    var tabsADay = getObject("tabSetCombinedDay") ;
if (tabsADay == null) alert("tabsADay is null") ;
    var tabsAMon = getObject("tabSetCombinedMon") ;
if (tabsAMon == null) alert("tabsAMon is null") ;
    var tabsBDay = getObject("tabSet010Day") ;
if (tabsBDay == null) alert("tabsBDay is null") ;
    var tabsBMon = getObject("tabSet010Mon") ;
if (tabsBMon == null) alert("tabsBMon is null") ;
    var tabsCDay = getObject("tabSet100Day") ;
if (tabsCDay == null) alert("tabsCDay is null") ;
    var tabsCMon = getObject("tabSet100Mon") ;
if (tabsCMon == null) alert("tabsCMon is null") ;
    var tabsDDay = getObject("tabSet300Day") ;
if (tabsDDay == null) alert("tabsDDay is null") ;
    var tabsDMon = getObject("tabSet300Mon") ;
if (tabsDMon == null) alert("tabsDMon is null") ;
/* above are all DIVs */
    if (tabsADay.style.display == "block") return("CombinedDay") ;
    if (tabsBDay.style.display == "block") return("010Day") ;
    if (tabsCDay.style.display == "block") return("100Day") ;
    if (tabsDDay.style.display == "block") return("300Day") ;
    if (tabsAMon.style.display == "block") return("CombinedMon") ;
    if (tabsBMon.style.display == "block") return("010Mon") ;
    if (tabsCMon.style.display == "block") return("100Mon") ;
    if (tabsDMon.style.display == "block") return("300Mon") ;
    return("none") ;
} // end getTabSet()

function begyr_altMonMenu(monSelect) {
    for (var imon = 0 ;  imon < 12 ;  ++imon) {
        if (imon < (Math.round(beg_mon) - 1)) {
            monSelect.options[imon].disabled = true ; // grey out months b4 start
	} else {
	    monSelect.options[imon].disabled = false ; // live up the good'ns
	}
    }
} // end altMonMenu_begyr(monSelect)

function endyr_altMonMenu(monSelect) {
    for (var imon = 0 ;  imon < 12 ;  ++imon) {
        if (imon >= Math.round(end_mon)) {
            monSelect.options[imon].disabled = true ; // grey out mons past end
        } else {
            monSelect.options[imon].disabled = false ; // reset rest
        }
    }
} // end endyr_altMonMenu(monSelect)

function midyr_altMonMenu(monSelect) {
    for (var imon = 0 ;  imon < 12 ;  ++imon) {
        monSelect.options[imon].disabled = false ; // all valid in other years
    }
} // end midyr_altMonMenu(monSelect)

function changeYear() {
    var ifcal = getObject("currMonth") ;
    var yrSelect = getObject("YearSelect") ;
    var monSelect = getObject("MonthSelect") ;
// alert("Entering changeYear(), selectedIndex="+yrSelect.selectedIndex+".") ;

    curr_yr = Math.round(yrSelect.selectedIndex) + Math.round(beg_yr) ;
    if (curr_yr == Math.round(beg_yr)) {
        begyr_altMonMenu(monSelect) ;
        monSelect.options[Math.round(beg_mon) - 1].selected = true ;
        curr_mon = Math.round(beg_mon) ;
        curr_day = Math.round(beg_day) ;
    } else if (curr_yr == Math.round(end_yr)) {
        endyr_altMonMenu(monSelect) ;
	monSelect.options[Math.round(end_mon) - 1].selected = true ;
        curr_mon = Math.round(end_mon) ;
	curr_day = Math.round(end_day) ;
    } else {
        midyr_altMonMenu(monSelect) ;
        monSelect.options[0].selected = true ;
        curr_mon = 1 ;
	curr_day = 1 ;
    }
    if (Math.round(curr_mon) < 10) {
        var path = new String("months_BAO_Tower/month_" + Math.round(curr_yr) + 
	                      "0" + Math.round(curr_mon) + ".html") ;
    } else {
        var path = new String("months_BAO_Tower/month_" + Math.round(curr_yr) + 
	                       Math.round(curr_mon) + ".html") ;
    }
    if (path.length != 34) {
        alert("Still bollixing year-month paths, alas;  path=" + path + ";") ;
    } else {
// alert("Trying to load "+path+".") ;
        try {
            ifcal.src = path ;
	    currdate_YYYYMMDD = Math.round((curr_yr * 10000) + (curr_mon * 100) +
	                                   curr_day) ;
            change_date(currdate_YYYYMMDD) ;
        } catch (er) {
// alert("Catching error, loading latest month.") ;
            ifcal.src = "months_BAO_Tower/month_" + latest_date.substring(0, 6) + 
	                ".html" ;
        }
    } // 34 char paths are good
}  //changeYear()

function changeMonth() {
    var ifcal = getObject("currMonth") ;
    var monSelect = getObject("MonthSelect") ;
// alert("Entering changeMonth(), selectedIndex="+monSelect.selectedIndex+".") ;

    curr_mon = monSelect.selectedIndex + 1 ;
    if (curr_mon < 10) {
        var path = new String("months_BAO_Tower/month_" + Math.round(curr_yr) + 
	                      "0" + Math.round(curr_mon) + ".html") ;
    } else {
        var path = new String("months_BAO_Tower/month_" + Math.round(curr_yr) +
	                      Math.round(curr_mon) + ".html") ;
    }
    if (path.length != 34) {
        alert("Still bollixing year-month paths:  " + path + ";") ;
    } else {
        try {
// alert("Trying to load "+path+".") ;
            ifcal.src = path ;
            if ((Math.round(curr_yr) == Math.round(beg_yr)) && 
	        (Math.round(curr_mon) == Math.round(beg_mon))) {
	        currdate_YYYYMMDD = (Math.round(beg_yr) * 10000) +
	                            (Math.round(beg_mon) * 100) + 
				    Math.round(beg_day) ;
	    } else if ((Math.round(curr_yr) == Math.round(end_yr)) &&
	               (Math.round(curr_mon) == Math.round(end_mon))) {
                currdate_YYYYMMDD = (Math.round(end_yr) * 10000) +
		                    (Math.round(end_mon) * 100) + 
				    Math.round(end_day) ;
            } else {
                curr_day = 1 ;
                currdate_YYYYMMDD = (Math.round(curr_yr) * 10000) +
		                    (Math.round(curr_mon) * 100) + 1 ;
            }
            change_date(currdate_YYYYMMDD) ;
        } catch (er) {
            alert("Web page is broken.  Please try later.\n") ;
        }
    } // 34 char paths are good
} // changeMonth()

function changeLvl() {
    var lvlSelect = getObject("LvlSelect") ;
    var lvlindx = lvlSelect.selectedIndex ;
// alert("In changeLvl(), lvlindx="+lvlindx+".") ;
    switch (lvlindx) {
        case 0 :
            dataparam.lvl = "Combined" ;
            dataparam.store() ;
	    if (tempfreqsfx == "Day") {
	        change_field(400) ;
	    } else {
	        change_field(900) ;
	    }
	break ;
	case 1 :
            dataparam.lvl = "010" ;
            dataparam.store() ;
	    if (tempfreqsfx == "Day") {
	        change_field(10) ;
	    } else {
	        change_field(510) ;
	    }
	break ;
	case 2 :
            dataparam.lvl = "100" ;
            dataparam.store() ;
	    if (tempfreqsfx == "Day") {
	        change_field(100) ;
	    } else {
	        change_field(600) ;
	    }
	break ;
	case 3 :
            dataparam.lvl = "300" ;
            dataparam.store() ;
	    if (tempfreqsfx == "Day") {
	        change_field(300) ;
	    } else {
	        change_field(800) ;
	    }
	break ;
	default:
// alert("Bad selectedIndex on level select.") ;
    } // end switch
} // end changeLvl()

function goDaily() {
// alert("In goDaily().") ;
    var monthDiv = getObject("DivMonthCal") ;
    if (monthDiv == null) {
        alert("In goDaily, monthDiv is null.") ;
    }
    monthDiv.style.display = "block" ;
    dataparam.tf = "daily" ;
    tempfreqsfx = "Day" ;
    dataparam.store() ;
    selectTabSet(currTabSetName.replace("Mon", "Day")) ;
    change_date(dataparam.date) ;
} // end goDaily()

function goMonthly() {
// alert("In goMonthly().") ;
    var monthDiv = getObject("DivMonthCal") ;
    if (monthDiv == null) {
        alert("In goMonthly, monthDiv is null.") ;
    }
    monthDiv.style.display = "none" ;
    dataparam.tf = "monthly" ;
    tempfreqsfx = "Mon" ;
    dataparam.store() ;
    selectTabSet(currTabSetName.replace("Day","Mon")) ;
    change_date(dataparam.date) ;
} // end goMonthly()
