  /* 
   * Filename: banner-ad.js
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 30-Nov-2008
   * Abstract: A collect of JavaScript functions that defines the banner-ad 
   *           framework.
   * Description: See the banner-ad-readme.txt file.
   *
   */

function getPassedValue(p_id, p_source) {
  /* 
   * +++
   *
   * 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.
   *                p_source - Look for passed value in parent window.  
   *                               Only checked when calledByFrame variable is
   *                               true.  One of two values is expected:
   *                                  "parent" - Look for passed value in parent 
   *                                             window
   *                                  "current" - Look for passed value in 
   *                                              current window (default)
   *                               This is useful for a frame where a parameter 
   *                               might be passed from the parent window.
   *                               
   *
   *       History: 20-Jan-2010 - p_source added
   * 
   * ---
   */

  // [ getPassedValue Constants ]
  var fromParent = ( typeof p_source != 'undefined' )
                   ? ( calledByFrame )
                     ? ( p_source == "parent" )
                     : new Boolean(false)
                   : new Boolean(false);
  var passedValue = ( fromParent )
                    ? unescape(f_element( 1, "?", window.parent.location.href ))
                    : unescape(location.search.substring(1));

  // [ getPassedValue Variables ]
  var allSubValues = ( fromParent )
                     ? f_element( 1, "?", window.parent.location.href ).split('&')
                     : location.search.substring(1,location.search.length).split('&');
  var idValue = ( passedValue == '?' )
                ? ""
                : passedValue;
  var subValue = "";

  // [ getPassedValue Main Line ]
  if ( p_id != null ) {
    idValue = "";
    for (var 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++)
  } // if ( p_id != null )

  // [ getPassedValue End of Job ]
  return idValue;

} // end getPassedValue function

/*
 * =============================================================================
 * =============================================================================
 */

function fn_isInteger(strString) {
   // check for valid numeric strings
   // variation of function found at http://www.pbdr.com/vbtips/asp/JavaNumberValid.htm
   var strValidChars = "0123456789";
   
   var strChar = '';
   var blnResult = ( strString.length >= 1 );

   //  test strString consists of valid characters listed above
   for (i = 0; i<strString.length && blnResult == true; i++) {
	 strChar = strString.charAt(i);
     if (strValidChars.indexOf(strChar) == -1 )
       blnResult = false;
     }
       
   return blnResult;
   }

/*
 * --------------------------------------------------------------------------------
 * 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 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 Functions ]
  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;
    }

  // [ 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

/*
 * =============================================================================
 * =============================================================================
 */
 
var bannerAdDisplayed = 'waiting';

function currentAd() {
  /* 
   * ++
   * Name: current-ad
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 28-Nov-2008
   * Abstract: To display a li tag with in id date less than or equal to today.
   * Description: 
   * Parameters: None.
   * History:
   * --
   */
  // [ current-ad Constants ]
  var allLItags = document.getElementsByTagName("li");
  var todaysDate = today_yyyymmdd();
  var selectAd = getPassedValue('selectad');

  // [ current-ad Variables ]
  var adSearchResults = top.document.getElementById('adSearchResults');
  var tagArray = new Array();
  var tagFound = -1;
  var validSelectAd = ( ( selectAd.length == 8 ) &&  fn_isInteger(selectAd) )

  // [ current-ad Main Line ]
  // alert('Running currentAd');
  for ( i=0; i<allLItags.length; i++ ) 
	tagArray[i] = [ allLItags[i].id, i ];

  tagArray.sort(function(a,b){return (a[0]>b[0])?1:(a[0]<b[0])?-1:0});
  for ( i=0; i<allLItags.length && tagFound == -1 ; i++ )
    if ( validSelectAd ) {
      if ( tagArray[i][0] == selectAd )
        tagFound = tagArray[i][1];
      } else if ( tagArray[i][0] >= todaysDate ) {
        tagFound = tagArray[i][1];
        }
        
  if ( tagFound >= 0 ) {
    allLItags[tagFound].style.display = 'inline';
    bannerAdDisplayed = allLItags[tagFound].id;
    } else {
      bannerAdDisplayed = 'none';
      if ( validSelectAd ) 
        alert('Id '+selectAd+' not found.');
      }

  return;
  } // end current-ad function

/*
 * =============================================================================
 * =============================================================================
 */

function showAdDisplayed() {
  /* 
   * ++
   * Name: showAdDisplayed
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 2-Dec-2008
   * Abstract: To return the value of the bannerAdDisplayed global variable.
   * Description: 
   * Parameters: 
   * History:
   * --
   */

  // [ showAdDisplayed Main Line ]

  return bannerAdDisplayed;
  } // end showAdDisplayed function
    
function getAdDisplayed() {
  /* 
   * ++
   * Name: getAdDisplayed
   * Author: Alvin Orzechowski, MyFirstWebPage.net
   * Creation Date: 3-Dec-2008
   * Abstract: To set the div display style where the ad is displayed to 'none'
   *           if the showAdDisplayed function returns a none.
   * Description: 
   * Parameters: 
   * History:
   *           - Created
   * --
   */
  // [ getAdDisplayed Constants ]
  
  var adFrameWindow = document.getElementById('adFrame').contentWindow;
  var bannerAdSpaceDiv = document.getElementById('bannerAdSpace');

  // [ getAdDisplayed Variables ]
  
  var adDisplayed = adFrameWindow.showAdDisplayed();

  // [ getAdDisplayed Main Line ]
  
  if ( adDisplayed == 'none' )
    bannerAdSpaceDiv.style.display = 'none';

  return;
  } // end getAdDisplayed function
