// -----------------------------------------------------------------------------

function Show_Advertisement( Path_to_Ad_Folder) {
// Purpose :  JavaScript code which inserts HTML commands in the current HTML page.
// Input :
//    Path_to_Ad_Folder :  Path to where this file.  Example :  "../Ad"
// Output :
//    <Return value> :  None.
// Notes :
//    1) To use this function to insert some HTML code in a host HTML file...
//          <! Include the Advertisements >
//          <SCRIPT LANGUAGE="JavaScript" SRC="../../../Common/Include/JavaScript/Array_1D.js"></SCRIPT>
//          <SCRIPT LANGUAGE="JavaScript" SRC="../Ad/Main.js"></SCRIPT>
//          <SCRIPT LANGUAGE="JavaScript"> Show_Advertisement( "../Ad") </SCRIPT>
// Author :  Jerome Daoust, 2009/10/14.

   // Set Error string.
   var c_Error = new String("Error in Show_Advertisement() :  ")

   // Local variables.
   a_Ad_URL = new Array()
   a_Ad_Button_Qtt = new Array()
   aa_Ad_Button_RelPath = new Array()
   a_Ad_OpenInNewWindow_YorN = new Array()
   a_Ad_ExpirationDate = new Array()
   Ad_Qtt = 0
   // Fix Path_to_Ad_Folder if empty string supplied.
   if ( "" == Path_to_Ad_Folder ) Path_to_Ad_Folder = "."

   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   // "How to Advertise here"
   a_Ad_URL[Ad_Qtt] = Path_to_Ad_Folder + "/Advertise.htm"
   a_Ad_Button_Qtt[Ad_Qtt] = 3
   aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Info_HowToAdvertiseHere_1.jpg"
   aa_Ad_Button_RelPath[Ad_Qtt][1] = "Link_Graphic/Info_HowToAdvertiseHere_2.jpg"
   aa_Ad_Button_RelPath[Ad_Qtt][2] = "Link_Graphic/Info_HowToAdvertiseHere_3.jpg"
   aa_Ad_Button_RelPath[Ad_Qtt][3] = "Link_Graphic/Info_HowToAdvertiseHere_4.gif"   // Unused
   aa_Ad_Button_RelPath[Ad_Qtt][4] = "Link_Graphic/Info_HowToAdvertiseHere_5.gif"   // Unused
   a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "N"
   a_Ad_ExpirationDate[Ad_Qtt] = "3000/1/1"
   Ad_Qtt += 1

   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   // Bio Air-Technologies
   // a_Ad_URL[Ad_Qtt] = "http://www.bio-air-technologies.com"
   // a_Ad_Button_Qtt[Ad_Qtt] = 2
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Bio_Air_Technologies_1.jpg"
   // aa_Ad_Button_RelPath[Ad_Qtt][1] = "Link_Graphic/Bio_Air_Technologies_2.jpg"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "2009/1/7"
   // Ad_Qtt += 1

   // Nova
   // a_Ad_URL[Ad_Qtt] = "http://www.nova-wings.com"
   // a_Ad_Button_Qtt[Ad_Qtt] = 3
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Nova_1_Shockwave.jpg"
   // aa_Ad_Button_RelPath[Ad_Qtt][1] = "Link_Graphic/Nova_2_Tycoon.jpg"
   // aa_Ad_Button_RelPath[Ad_Qtt][2] = "Link_Graphic/Nova_3_Ra.jpg"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "2009/9/17"
   // Ad_Qtt += 1

   // San Francisco Tandem Paragliding
   // a_Ad_URL[Ad_Qtt] = "http://www.sftandem.com"
   // a_Ad_Button_Qtt[Ad_Qtt] = 1
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/SanFrancisco_Tandem_Paragliding.jpg"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "2005/10/1"
   // Ad_Qtt += 1

   // Super Fly
   // a_Ad_URL[Ad_Qtt] = "http://www.superflyinc.com/home.htm"
   // a_Ad_Button_Qtt[Ad_Qtt] = 1
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Super_Fly.jpg"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "2005/10/1"
   // Ad_Qtt += 1

   // Torrey Pines Gliderport
   // a_Ad_URL[Ad_Qtt] = "http://www.flytorrey.com"
   // a_Ad_Button_Qtt[Ad_Qtt] = 3
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Torrey_Pines_Gliderport_1.jpg"
   // aa_Ad_Button_RelPath[Ad_Qtt][1] = "Link_Graphic/Torrey_Pines_Gliderport_2.jpg"
   // aa_Ad_Button_RelPath[Ad_Qtt][2] = "Link_Graphic/Torrey_Pines_Gliderport_3.jpg"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "2009/1/12"
   // Ad_Qtt += 1

   // "Your Company Logo"
   // a_Ad_URL[Ad_Qtt] = Path_to_Ad_Folder + "/WebPage_Example.htm"
   // a_Ad_Button_Qtt[Ad_Qtt] = 1
   // aa_Ad_Button_RelPath[Ad_Qtt] = new Array( a_Ad_Button_Qtt[Ad_Qtt])
   // aa_Ad_Button_RelPath[Ad_Qtt][0] = "Link_Graphic/Example_YourCompanyLogo.gif"
   // a_Ad_OpenInNewWindow_YorN[Ad_Qtt] = "Y"
   // a_Ad_ExpirationDate[Ad_Qtt] = "3000/1/1"
   // Ad_Qtt += 1

   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

   // Establish a random order to display the ads.
   a_Ad_Index_RandomOrder = new Array(Ad_Qtt)
   var status = ArrInt1d_CreateRandomIndex( Ad_Qtt, a_Ad_Index_RandomOrder)
   if ( 0 != status ) {
      Use_Random_Ad_Index = 0   // Don't use the randomized index.
      alert( c_Error + "Ads will not be in random order.")
   } else {
      Use_Random_Ad_Index = 1
   }

   // Define a buffer variable.
   // Browsers interpret each document.write() command so having a buffer string
   //    reduced the number of document.write() calls.
   var c_Buf = new String("")
   var c_Buf_OneAd = new String("")

   // Spacing between graphic logos.
   var c_space_horizontal = " "

   // Open a DIV tag.  This allows for local properties (alignement, colors...).  More info :
   //    http://www.gunn.palo-alto.ca.us/~pbruegger/projects/dhtml/dennis/page2.html
   c_Buf += '<DIV>'
      // To set a given background color :   <DIV style="background-color:WHITE;">
   // Start a paragraph.
   c_Buf += '<P>'

   // Establish today's date.
   var Today_date = new Date()
   var Today_year = Today_date.getYear()
   if ( Today_year < 2000 ) Today_year += 1900
   var Today_month = Today_date.getMonth() + 1
   var Today_day = Today_date.getDate()
   // DEBUG :  Show today's date.
      // c_Buf += 'Today=' + Today_year + '/' + Today_month + '/' + Today_day + '<BR>'

   // Loop on each Advertisement.
   for ( var i_Ad=0 ; i_Ad<Ad_Qtt ; i_Ad++ ) {
      // Determine which ad index to use.
      var Ad_Index = i_Ad   // Always in the same order (as supplied).
      if ( 1 == Use_Random_Ad_Index ) {
         Ad_Index = a_Ad_Index_RandomOrder[i_Ad]
      }

      // Select a random logo index the this advertiser.
      var Ad_Logo_Index = Math.round( Math.random() * (a_Ad_Button_Qtt[Ad_Index]-1) )
      if ( Ad_Logo_Index < 0 ) {
         Ad_Logo_Index = 0
      }
      if ( (a_Ad_Button_Qtt[Ad_Index]-1) < Ad_Logo_Index ) {
         Ad_Logo_Index = a_Ad_Button_Qtt[Ad_Index]-1
      }

      // Start building the ad's string buffer.
      c_Buf_OneAd = ""

      // Generic syntax :  <A HREF="Some_URL" TARGET=blank><IMG SRC="Path_to_Logo"></A>
      c_Buf_OneAd += '<A HREF="'
                   + a_Ad_URL[Ad_Index]
                   + '"'
      if ( "Y" == a_Ad_OpenInNewWindow_YorN[Ad_Index] ) {
         c_Buf_OneAd += ' TARGET=blank'
      }
      c_Buf_OneAd += '><IMG SRC="'
                   + Path_to_Ad_Folder
                   + '/'
                   + aa_Ad_Button_RelPath[Ad_Index][Ad_Logo_Index]
                   + '"></A>'

      // Get the ad's expiration date.
      c_ExpirationYearMonthDay = a_Ad_ExpirationDate[Ad_Index]
      ac_ExpirationYearMonthDay = c_ExpirationYearMonthDay.split( "/")
      var Expiration_year  = parseInt( ac_ExpirationYearMonthDay[0] )
      var Expiration_month = parseInt( ac_ExpirationYearMonthDay[1] )
      var Expiration_day   = parseInt( ac_ExpirationYearMonthDay[2] )

      // Establish expiration date as an object.
      Expiration_date = new Date( Expiration_year, Expiration_month-1, Expiration_day)
         // Year value is stored as-is.
         // Month is stored in 0-11 range.  0-based.
         // Day value is stored as-is (1-31 range).   1-based.

      // Establish number of days prior to expiration.
      // Dates are stored in milliseconds since January 1, 1970, 00:00:00 .
      var Expiration_FullDaysRemaining = Math.floor( (Expiration_date - Today_date) / 1000 / 60 / 60 / 24 )

      // Show urgency to renew funding for ads which will soon expire
      //    by adding a note after the graphic link.
      if ( Expiration_FullDaysRemaining < 0 ) {
         // Add has expired
         c_Buf_OneAd += '<FONT SIZE=+3 COLOR=RED><B>!</B></FONT>'
      } else {
         if ( Expiration_FullDaysRemaining <= 15 ) {
            // Add will soon expire.
            c_Buf_OneAd += '<FONT SIZE=-2 COLOR=ORANGE><B>'+Expiration_FullDaysRemaining+'</B></FONT>'
         }
      }

      // Horizontal spacing between graphics.
      if ( i_Ad < Ad_Qtt-1 ) {
         c_Buf_OneAd += c_space_horizontal;
      }

      // Add the one-advertisement buffer to the main buffer.
      c_Buf += c_Buf_OneAd
   }

   // Close the paragraph.
   c_Buf += '</P>'

   // Close the DIV tag.
   c_Buf += '</DIV>' 

   // To debug, uncomment the next line...
   //    alert(c_Buf)

   // Write the buffer to the current document.
   document.write( c_Buf)
}
// -----------------------------------------------------------------------------

