// JavaScript Document

function f_element( p_index, p_deliminator, p_string ) {
  /* 
   * JavaScript version of OpenVMS DCL F$ELEMENT lexical
   */
  var currIndex = p_string.indexOf(p_deliminator);
  var i = 0;
  var outChar = '';
  var outPoint = 0;
  var outString = p_string;
  var trackIndex = 0;

  if ( currIndex >= 0 ) {
      while ( ( trackIndex != p_index ) && ( currIndex != -1 ) ) {
        outPoint = ++currIndex;
        currIndex = p_string.indexOf(p_deliminator,outPoint);
        trackIndex++;
      } // end while ( ( trackIndex != p_index ) && ( currIndex != -1 ) )
      if ( trackIndex != p_index ) {
        outString = p_deliminator;
      } else {
        outString = '';
        for ( i=outPoint; i<p_string.length; i++ ) { 
          outChar = p_string.charAt(i);
          if ( outChar == p_deliminator ) {
            break;
          } else {
            outString = ( outString + outChar );
          } // end if ( outChar == p_deliminator )
        } // for ( i=outPoint; i<p_string.length; i++ )
      } // end if ( trackIndex != p_index )
    } else if ( p_index >= 1 )
      outString = p_deliminator;
    // end if ( currIndex >= 0 )
  
  return outString;
  } // end f_element function
  
/*
 * --------------------------------------------------------------------------------
 */

function getPassedValue(p_id) {
  /* 
   * +++
   *
   * Function Name: getPassedValue
   *        Author: Alvin Orzechowski MyFirstWebPage.net
   * Creation Date: 15-Aug-2004
   *      Abstract: To return the passed value or the specified portion of it
   *   Description: 
   *    Parameters: p_id - Value found to the left of an equal sign.  If this is
   *                    found, this function will return the value found to the
   *                    right of the equal sign.  If this parameter is not 
   *                    provided, this function will return the whole value 
   *                    passed.
   *       History: 
   * 
   * ---
   */

  // [ getPassedValue Constants ]
  var passedValue = unescape(location.search.substring(1));

  // [ getPassedValue Variables ]
  var allSubValues = location.search.substring(1,location.search.length).split('&');
  var i;
  var idValue = passedValue;
  var subValue;

  // [ getPassedValue Main Line ]
  for (i=0; i<allSubValues.length; i++) {
    subValue = allSubValues[i].split('=');
    if ( subValue[0] == p_id ) {
      idValue = unescape(subValue[1]);
      i = allSubValues.length;
    } // end if ( subValue[0] == p_id )  
  } // end for (i=0; i<allSubValues.length; i++)

  // [ getPassedValue End of Job ]
  return idValue;

} // end getPassedValue function

/*
 * --------------------------------------------------------------------------------
 * addLoadEvent will execute a JavaScript command/function when a page is
 * brought up in a browser.  Ordinarily only one such command/function can be
 * performed at that time, but addLoadEvent overrides that limitation.
 * --------------------------------------------------------------------------------
 */

function addLoadEvent(func) {
// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/*
 * --------------------------------------------------------------------------------
 */

/*
 * --------------------------------------------------------------------------------
 * The following functions are used to change a class when a page loads
 * --------------------------------------------------------------------------------
 */

function getElementsByClass(searchClass,tag) {
  // Source: http://www.dustindiaz.com/getelementsbyclass/
	var classElements = new Array();
	if ( tag == null )
		tag = '*';
	var els = document.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
  }

/*
 * --------------------------------------------------------------------------------
 */

function takeYear(theDate) {
  // Source: http://www.quirksmode.org/js/introdate.html
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
  }

/*
 * --------------------------------------------------------------------------------
 */

function today_yyyymmdd() {
  /* 
   * ++
   * Name: today_yyyymmdd
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 25-Jul-2008
   * Abstract: To return today's date in YYYYMMDD form.
   * Description: 
   * Parameters: None
   * History:
   * --
   */
  // [ today_yyyymmdd Constants ]
  var today = new Date();

  // [ today_yyyymmdd Variables ]
  var dd = ( today.getDate() );
  var mm = ( today.getMonth() + 1 );
  var yyyy = ( takeYear(today) + '' );
  var yyyymmdd = '';

  // [ today_yyyymmdd Main Line ]
  if ( mm <= 9 )
    mm = ( '0' +  mm.toString() );

  if ( dd <= 9 )
    dd = ( '0' +  dd.toString() );

  yyyymmdd = ( yyyy + mm + dd );

  return yyyymmdd;
  } // end today_yyyymmdd function

/*
 * --------------------------------------------------------------------------------
 */

function modifyEventClassClass() {
  /* 
   * ++
   * Name: modifyEventClassClass
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 25-Jul-2008
   * Abstract: To change the eventClass class from any HTML div tag whose id
   *           attribute is less than today's yyyymmdd value.
   * Description: 
   * Parameters: None
   * History:
   *           - 
   * --
   */
  // [ modifyEventClassClass Constants ]
  var allDates = getElementsByClass('eventClass', 'div');
  var currentDate = today_yyyymmdd();
  var emptySchedule = document.getElementById('emptySchedule');
  var explainEventMark = document.getElementById('explainEventMark');
  var goTopSide = document.getElementById('goTopSide');
  var textArea = document.getElementById('textArea');
  var displayAllEvents = ( getPassedValue('alldates') == 'yes' );
  var displayNoEvents = ( getPassedValue('alldates') == 'no' );

  // [ modifyEventClassClass Variables ]
  var datesDisplayed = 0;

  // [ modifyEventClassClass Main Line ]
  for (i=0; i<allDates.length; i++) {
    if ( ( !displayAllEvents ) && ( allDates[i].id < currentDate ) ) {
      allDates[i].style.display = 'none';
      } else if ( displayNoEvents ) {
	    allDates[i].style.display = 'none';
        } else datesDisplayed++;
    } // end for (i=0; i<allDates.length; i++)
    
  // alert('datesDisplayed: '+datesDisplayed);
  
  if ( datesDisplayed == 0 ) {
    emptySchedule.style.display = 'block';
    // textArea.className = 'defaultHeight';
    // explainEventMark.style.display = 'none';
    } 
      
   if ( datesDisplayed >= 5 )
     goTopSide.style.display = 'block';
    
  return;
  } // end modifyEventClassClass function
  
function printerFriendly() {
  /* 
   * ++
   * Name: printerFriendly
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 12-Nov-2008
   * Abstract: To display the page like it will be printed.
   * Description: 
   * Parameters: 
   * History:
   *           - Created
   * --
   */
  // [ printerFriendly Constants ]
  var displayAsPrinterFriendly = ( getPassedValue('printerfriendly') == 'yes' );

  // [ printerFriendly Variables ]

  // [ printerFriendly Main Line ]
  if ( displayAsPrinterFriendly ) {
	document.getElementById('headerSection').style.display = 'block';
	document.getElementById('footerSection').style.display = 'block';
    }
    
  return;
  } // end printerFriendly function
