  /* 
   * Name: faq.js
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 23-Nov-2008
   * Abstract: A collect of JavaScript functions to use on an faq page.
   * Description: 
   */
   
var faqExpandAnswer = '<img src="images/faq-plus.jpg" alt="[+]" title="Show answer" style="border: 0pt none ;" border="0">';
var faqCollapseAnswer = '<img src="images/faq-minus.jpg" alt="[+]" title="Hide answer" style="border: 0pt none ;" border="0">';
var faqDisplayExpandAnswer = '';
var faqDisplayCollapseAnswer = '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 removePlusMinus(p_question) {
  /* 
   * ++
   * Name: removePlusMinusx
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 29-Nov-2008
   * Abstract: To remove the plus or minus sign string from the specified text.
   * Description: 
   * Parameters: p_question - the question contained in the dt tag
   * History:
   * --
   */
  // [ removePlusMinus Constants ]
  var strMinusSign = '[-]';
  var strPlusSign = '[+]';
  var gtSign = '>';
  var questionIn = removeWhiteSpace(p_question);

  // [ removePlusMinus Variables ]
  var questionOut = p_question;

  // [ removePlusMinus Main Line ]
  if ( questionIn.indexOf(strMinusSign) == 0 ) {
    questionOut = questionIn.slice(strMinusSign.length);
    } else if ( questionIn.indexOf(strPlusSign) == 0 ) {
      questionOut = questionIn.slice(strPlusSign.length);
      } else if ( f_element(1,gtSign,questionIn) != gtSign )
        questionOut = f_element(1,gtSign,questionIn);
        
  while ( questionOut.indexOf(' ') == 0 )
    questionOut = questionOut.slice(1);
    
  return questionOut;
  } // end removePlusMinus function

/*
 * =============================================================================
 * =============================================================================
 */

function faqHTML() {
  /* 
   * ++
   * Name: faqHTML
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 23-Nov-2008
   * Abstract: To add attributes to all faq class tags
   * Description: 
   * Parameters: 
   * History:
   * --
   */
  // [ faqHTML Constants ]
  var allFAQanswers = getElementsByClass('faqAnswer');
  var allFAQquestions = getElementsByClass('faqQuestion');
  var faqShowAll = document.getElementById('faqShowAll');
  var showAll = 'expandAllFAQ()';

  // [ faqHTML Variables ]
  var doToggle = '';
  var idAnswer = '';
  var idCnt = '';

  // [ faqHTML Main Line ]
  // alert('faqHTML function');
  if ( allFAQanswers.length != allFAQquestions.length )
    alert('Error: The number of questions and answers do not match');
    else {
      if ( faqShowAll )
        faqShowAll.onclick = new Function(showAll);
      for ( i=0; i<allFAQquestions.length; i++ ) {
	    idCnt = zeroFill(i+1);
	    idAnswer = ( 'answer' + idCnt );
	    allFAQanswers[i].id = idAnswer;
	    allFAQquestions[i].id = ( 'question' + idCnt );
        allFAQquestions[i].innerHTML = removePlusMinus(allFAQquestions[i].innerHTML);
	    // allFAQquestions[1].border = "0";
	    // allFAQquestions[i].href = ( '#' + idQuestion );
	    allFAQquestions[i].href = '#';
	    doToggle = ( "faqToggle('" + idAnswer + "', this)" );
	    allFAQquestions[i].onclick = new Function(doToggle);
        }
      }

  return;
  } // end faqHTML function

/*
 * =============================================================================
 * =============================================================================
 */

function faqToggle(p_answerId, p_link) {
  /* 
   * ++
   * Name: faqToggle
   * 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 question is:
   *                <a href="#" onclick="faqToggle('content', this)">Collapse</a>
   * Parameters: 
   * History:  Based on casablanca tutorial (http://www.ozzu.com/)
   *           - Created
   * --
   */

  var faqAnswer = document.getElementById(p_answerId);
  var innerHTML = ( ' ' + removePlusMinus(p_link.innerHTML) );
 
  if (faqAnswer.style.display != faqDisplayCollapseAnswer) {
    faqAnswer.style.display = faqDisplayCollapseAnswer;
    p_link.innerHTML = ( faqExpandAnswer + innerHTML );
    } else {
      faqAnswer.style.display = faqDisplayExpandAnswer;
      p_link.innerHTML = ( faqCollapseAnswer + innerHTML );
      }
  return;
  }


/*
 * =============================================================================
 * =============================================================================
 */

function toggleAllFAQ(p_action) {
  /* 
   * ++
   * Name: toggleAllFAQ
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 
   * Abstract: To toggle all areas with the faqClass class.
   * Parameters: p_action - Which way to toggle.  Either one of two values:
   *                          collapse - close all areas (default)
   *                          expand - open all areas
   * History:
   *           - Created
   * --
   */
  // [ toggleAllFAQ Constants ]
  var allFAQanswers = getElementsByClass('faqAnswer');
  var allFAQquestions = getElementsByClass('faqQuestion');
  var takeAction = ( toggleAllFAQ.arguments.length == 0 )
                   ? 'collapse'
                   : ( p_action != 'expand' )
                       ? 'collapse'
                       : 'expand';

  // [ toggleAllFAQ Variables ]
  innerHTML = ''

  // [ toggleAllFAQ Main Line ]
  // alert('toggleAllFAQ function');
  if ( allFAQquestions.length == allFAQanswers.length )
    for ( i=0; i<allFAQquestions.length; i++ ) {
      innerHTML = ( ' ' + removePlusMinus(allFAQquestions[i].innerHTML) ); 
	  if ( takeAction == 'expand' ) {
        allFAQanswers[i].style.display = faqDisplayExpandAnswer;
        allFAQquestions[i].innerHTML = ( faqCollapseAnswer + innerHTML );
        } else {
          allFAQanswers[i].style.display = faqDisplayCollapseAnswer;
          allFAQquestions[i].innerHTML = ( faqExpandAnswer + innerHTML );
          }
      }
  return;
  } // end toggleAllFAQ function

/*
 * =============================================================================
 * =============================================================================
 */

function expandAllFAQ() {
  toggleAllFAQ('expand');
  return;
  } // end expandAllFAQ function

/*
 * =============================================================================
 * =============================================================================
 */

function collapseAllFAQ() {
  toggleAllFAQ('collapse');
  return;
  } // end collapseAllFAQ function
