// accordian.js JavaScropt document

  /* 
   * Name: accordian.js
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 4-May-2009
   * Abstract: A collect of JavaScript functions to use on an accordian section.
   * Description: 
   */

var accordianDisplayExpandSection = 'block';
var accordianDisplayCollapseSection = 'none';

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 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;
  }

/*
 * --------------------------------------------------------------------------------
 * 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();
        }
    }
}

/*
 * =============================================================================
 * =============================================================================
 */

function zeroFill(p_number, p_length) {
  /* 
   * ++
   * Name: zeroFill
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 23-Nov-2008
   * Abstract: To return a string with the specified number zero filled.
   * Description: 
   * Parameters: p_number - number to be returned as a string.
   *             p_length - length of output string.
   *                        Default value: 2
   * --
   */
  // [ zeroFill Constants ]
  var outputLength = ( !p_length )
                       ? 2
                       : p_length;

  // [ zeroFill Variables ]
  var outputString = String( p_number )

  // [ zeroFill Main Line ]
  while ( outputString.length < outputLength )
    outputString = ( '0' + outputString );
  return outputString;
  } // end zeroFill function
  
/*
 * =============================================================================
 * =============================================================================
 */
 
function removeWhiteSpace(p_string) {
  /* 
  * ++
  * Name: removeWhiteSpace
  * Author: Alvin Orzechowski, MyFirstWebPage.net
  * Creation Date: 7-Jan-2009
  * Abstract: To remove white space from the specified string.
  * Description: 
  * Parameters: p_string - string that is to have the white space removed. 
                           (required)
  * History:
  * - Created
  * --
  */

  // [ removeWhiteSpace Main Line ]

  return p_string.replace(/^\s*|\s*$/g,'');
  } // end removeWhiteSpace function

/*
 * =============================================================================
 * =============================================================================
 */
 
function accordianHTML() {
  /* 
   * ++
   * Name: accordianHTML
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 23-Nov-2008
   * Abstract: To add attributes to all accordian class tags
   * Description: 
   * Parameters: 
   * History:
   * --
   */
  // [ accordianHTML Constants ]
  var allAccordianSections = getElementsByClass('accordianSection');
  var allAccordianHeaders = getElementsByClass('accordianHeader');

  // [ accordianHTML Variables ]
  var doToggle = '';
  var idSection = '';
  var idCnt = '';

  // [ accordianHTML Main Line ]
  // alert('accordianHTML function');
  if ( allAccordianSections.length != allAccordianHeaders.length )
    alert('Error: The number of accordian headers and sections do not match');
    else {
      for ( i=0; i<allAccordianHeaders.length; i++ ) {
	    idCnt = zeroFill(i+1);
	    idSection = ( 'accordianSection' + idCnt );
	    allAccordianSections[i].id = idSection;
	    allAccordianHeaders[i].id = ( 'accordianHeader' + idCnt );
	    // allAccordianHeaders[1].border = "0";
	    // allAccordianHeaders[i].href = ( '#' + idHeader );
	    allAccordianHeaders[i].href = '#';
	    doToggle = ( "accordianToggle('" + idSection + "')" );
	    allAccordianHeaders[i].onclick = new Function(doToggle);
        }
      }

  return;
  } // end accordianHTML function

/*
 * =============================================================================
 * =============================================================================
 */

function accordianToggle(p_sectionId) {
  /* 
   * ++
   * Name: accordianToggle
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 23-Nov-2008
   * Abstract: To expand and collape the specified area
   * Description: Put this anchor tag in the area where the header is:
   *                <a href="#" onclick="accordianToggle('content', this)">Collapse</a>
   * Parameters: 
   * History:  Based on casablanca tutorial (http://www.ozzu.com/)
   *           - Created
   * --
   */

  var accordianSection = document.getElementById(p_sectionId);
  if (accordianSection.style.display != accordianDisplayCollapseSection) {
    accordianSection.style.display = accordianDisplayCollapseSection;
    } else {
      accordianSection.style.display = accordianDisplayExpandSection;
      }
  return;
  }


/*
 * =============================================================================
 * =============================================================================
 */

function initializeAllAccordianSections(p_action) {
  /* 
   * ++
   * Name: initializeAllAccordianSections
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 
   * Abstract: To toggle all areas with the accordianClass class.
   * Parameters: p_action - Which way to toggle.  Either one of two values:
   *                          collapse - close all areas (default)
   *                          expand - open all areas
   * History:
   *           - Created
   * --
   */
  // [ initializeAllAccordianSections Constants ]
  var allAccordianHeaders = getElementsByClass('accordianHeader');
  var allAccordianSections = getElementsByClass('accordianSection');
  var takeAction = ( initializeAllAccordianSections.arguments.length == 0 )
                   ? 'collapse'
                   : ( p_action != 'expand' )
                       ? 'collapse'
                       : 'expand';

  // [ initializeAllAccordianSections Variables ]
  innerHTML = ''

  // [ initializeAllAccordianSections Main Line ]
  // alert('initializeAllAccordianSections function');
  if ( allAccordianHeaders.length == allAccordianSections.length )
    for ( i=0; i<allAccordianSections.length; i++ ) {
  	  if ( takeAction == 'expand' )
          allAccordianSections[i].style.display = accordianDisplayExpandSection
          else
            allAccordianSections[i].style.display = accordianDisplayCollapseSection;
      }
  return;
  } // end initializeAllAccordianSections function

/*
 * =============================================================================
 * =============================================================================
 */

function expandAllAccordianSections() {
  initializeAllAccordianSections('expand');
  return;
  } // end expandAllAccordianSections function

/*
 * =============================================================================
 * =============================================================================
 */

function collapseAllAccordianSections() {
  initializeAllAccordianSections('collapse');
  return;
  } // end collapseAllAccordianSesctions function