// File:     common.js
// Purpose:  Contains commonly-used JavaScript routines for MyFSTA.com
// security: COPYRIGHT (c) Copyright 2010 C&G Web Enterprises
// owner:    Gary Richtmeyer, gary@CandGWeb.com
// Created:  Monday, 1 March 2010
// Updated:
//   2010-03-01 - Original version
//   2010-05-17 - Update for Galleria picture gallery javascript code

var author_name     = 'Gary Richtmeyer'
var author_email    = 'gary@CandGWeb.com'
var browser_name    = navigator.appVersion
var browser_release = browser_name.substring(0,browser_name.indexOf(' '))
var browser_version = parseInt(navigator.appVersion)
var browser_type    = '?'
var image_list      = new Array();

if (navigator.appName == "Netscape")            { browser_type = 'NS'    }
if (navigator.userAgent.indexOf("Opera") != -1) { browser_type = 'OPERA' }
if (navigator.appVersion.indexOf("MSIE") != -1) { browser_type = 'MSIE'  }

if (browser_version < 4) { size='M' }
  else { w=window.screen.width; size='L'
    if (w <= 800) { size='M' }
    if (w <= 640) { size='S' }
    }


//*******************************************************************
// Various functions
//*******************************************************************


//-------------------------------------------------------------------
// setGlobalVars: set global variables
//   Usage: setGlobalVars(x)   x = option number of how many dirs
//                                 "up" from here is the root dir.
//   Sets the following global variables (referred to as
//   "top.xxxx" within other files):
//      baseURL    - The URL of the "root" directory
//      ImagePath  - The URL where the images are stored
//-------------------------------------------------------------------
function setGlobalVars(updirs) {
  temp = document.location.pathname    // change "\" in path
  j = temp.indexOf('\\')               // (e.g. on a PC and using MSIE)
  while ( j >= 0 ) {                   // to "/"
    temp = temp.substr(0,j) + '/' + temp.substr(j+1)
    j = temp.indexOf('\\',j)
    }
  if ( arguments.length == 0 )
         { updirs = 0 }
    else { updirs = arguments[0] }
  if ( updirs == null ) updirs = 0
  while ( updirs > 0 ) {
    temp = temp.substring(0,temp.lastIndexOf('/'))
    updirs = updirs - 1
    }
  baseURL   = document.location.protocol + '//' +
              document.location.host     +
              temp.substring(0,temp.lastIndexOf('/'))
  ImagePath = baseURL + '/images/'
  }

//-------------------------------------------------------------------
// ScriptFN: Returns only the filename of a URL
// Use: ScriptFN([url])
//   If not URL is provided, the current URL is used.
//-------------------------------------------------------------------
function ScriptFN(surl) {
  if ( surl == undefined ) surl = document.location.pathname
  pathend = surl.lastIndexOf('/') + 1
  fnext   = surl.substr(pathend)
  return fnext.substr(0,fnext.indexOf('.'))
  }

//-------------------------------------------------------------------
// DefineGalleryPictures: Define the image names & optional text for the picture gallery.
// Picture info is stored in the "image_names" array.
//    image_names[0] = url of the first picture 
//    image_names[1] = info describing the first picture
//    image_names[2] = url of of the second picture
//    image_names[3] = info describing the second picture
//       (etc.)
//-------------------------------------------------------------------
function DefineGalleryPictures(dgp_title) {
  if (document.images) {
    document.writeln('<span class=gallerypagetitle>' + dgp_title + '</span>' +
                     '<br><i>(If you don\'t see any thumbnail-sized pictures, please click your browser\'s refresh button)</i>')
  	document.writeln('<div id="galleria">')
    for (var idx=0; idx<image_names.length; idx = idx + 2) {
    	idx1 = idx + 1
    	temp = '<img src="' + image_path + image_names[idx] + '" title="' + image_names[idx] + '" '
    	if (image_names[idx1] == '' )
             { temp += '>' }
        else { temp += '><p>' + image_names[idx1] + '</p>' }
      document.writeln(temp)
      }
    document.writeln('</div>')
    temp = "<span class=gallery_using>" +
           "About the picture gallery:<ul>" +
           "<li>Click a thumbnail to display it fullsize" +
           "<li>In the thumbnail area, click < or > to scroll the thumbnails" +
           "<li>For simple scrolling, move the mouse into the large picture and click < or >" +
           "<li>Click the '<i>i</i>' in the upper-left of the large picture to see the picture's name</ul>" +
           "</span>"
    document.writeln(temp)
    }
  }



//-------------------------------------------------------------------
// Routines for creating the beginning/end of the screen, the
// top-of-page and the bottom-of-page
//-------------------------------------------------------------------

//-------------------------------------------------------------------
// StartScreen: Setup initial screen layout, header section and navigation.
// Use: StartScreen('sectionId','nodeId',dirlevel)
//   sectionId - ID of the active major section
//   nodeId    - ID of the active minor section
//   dirlevel  - Directory level (0=root, 1=one "down", 2=two "down"...)
//-------------------------------------------------------------------
function StartScreen(sectionId,nodeId,dirlevel) {
  setGlobalVars(dirlevel)
  nodeId = nodeId.toUpperCase()
  sectionId = sectionId.toUpperCase()

//----- start top-of-page header block
  var temp  = ""
  temp += '<body><br>' +
          '<table width=852 align=center cellpadding=0 cellspacing=0 border=0><tr>' +
             '<td><img src="images/logo-small.jpg"></td>' +
             '<td align=center><span class=headercompname><nobr>Florida State Taxidermists Association</span>' +
                 '<br><span class=headeraddr>43653 CR 54E, Kathleen, FL&nbsp; 33849 &nbsp; (352) 567-1521</span>' +
                 '</td>' +
          '</tr>'
          
    document.write(temp)
//----- start navigation portion
  navtemp = '<tr class=topheader><td colspan=2 align=center valign=middle height=30>' + // start navigation icons
   	        '<hr><ul id="nav">'

  //----- HOME ------
  navtemp += NavButton(sectionId, 'HOME',        baseURL + '/index.html',       'Home')
  //----- ABOUT US ------
  navtemp += NavButton(sectionId, 'ABOUTUS',     baseURL + '/aboutus.html',     'About Us')
  //----- CONTACT US ------
  navtemp += NavButton(sectionId, 'CONTACTUS',   baseURL + '/contactus.html',   'Contact Us')
  //----- CONVENTIONS ------
  navtemp += NavButton(sectionId, 'CONVENTIONS', baseURL + '/conventions.html', 'Conventions')
  //----- NEWSLETTERS ------
  navtemp += NavButton(sectionId, 'NEWSLETTERS', baseURL + '/newsletters.html', 'Newsletters')
  //----- LINKS ------
  navtemp += NavButton(sectionId, 'LINKS',       baseURL + '/links.html',       'Links')
    
  //----- finish the nav icons -----
  navtemp += '</ul><hr></td></tr></table>'
  document.write(navtemp)

  //----- end top-of-page header block
  var temp  = "<table width=852 align=center border=0>"
  document.write(temp)
  return
  }

//-------------------------------------------------------------------
// EndScreen: Closes table begun by "StartScreen()"
// Use: EndScreen()
//-------------------------------------------------------------------
function EndScreen() {
  document.write('</td></tr></table>')
  return
  }



//-------------------------------------------------------------------
// PageTop: Create top-of-page HTML text
//    PageTop(title [, parm1, parm2, ..., parmx ] )
//      title - The title of this page to be displayed to the user
//      parm  - any one of the following parameters:
//         { PRINT | NOPRINT }
//              Should the print-this-page icon be shown?
//              If omitted, PRINT is assumed.
//         CLASS=class
//              The CSS class to be used for the title.  If omitted,
//              the class "titlepage" is used.
//              e.g.  'CLASS=h2'
//         PREV=url | [ description ] | [ frame ]
//              A "previous-page" link to the specified url should be
//              generated.  If a description is provided, it is shown.
//              If a frame name is specified, a target is generated.
//              e.g.  'PREV=overview.html'
//              e.g.  'PREV=overview.html|Overview description'
//              e.g.  'PREV=overview.html|Overview description|dataframe'
//         NEXT=url | [ description ] | [ frame ]
//              A "next-page" link to the specified url should be
//              generated.  If a description is provided, it is shown.
//              If a frame name is specified, a target is generated.
//              e.g.  'NEXT=detail.html'
//              e.g.  'NEXT=detail.html|Detail Information'
//              e.g.  'NEXT=detail.html|Detail Information|dataframe'
//         { RETURN | NORETURN ]
//              Should a "return-to-calling-page" link be generated?
//              If omitted, NORETURN is assumed.
//
//-------------------------------------------------------------------
function PageTop(pagetitle) {
  var tclass='titlepage', _print=true, _return=false
  var pu='', pt='', pf='', nu='', nt='', nf=''
  if ( pagetitle == null ) pagetitle = ''
  //----- scan & evaluate the passed parameters
  for ( var ix=1; ix < arguments.length; ix++ ) {
    thisParm = arguments[ix]
    if ( thisParm == '' ) continue      // ignore null parms
    tempray = thisParm.split('=',2)
    keyword = tempray[0].toUpperCase()
    data    = tempray[1]; if ( data == undefined ) data = ''
    switch (keyword) {
      case 'PRINT'   : _print  = true;  break;
      case 'NOPRINT' : _print  = false; break;
      case 'RETURN'  : _return = true;  break;
      case 'NORETURN': _return = false; break;
      case 'CLASS'   : tclass  = data;  break;
      case 'NEXT'    :
        tempray = data.split('|',3);
        nu = tempray[0];
        nt = tempray[1]; if ( nt == undefined ) nt = '';
        nf = tempray[2]; if ( nf == undefined ) nf = '';
        break;
      case 'PREV'    :
        tempray = data.split('|',3);
        pu = tempray[0];
        pt = tempray[1]; if ( pt == undefined ) pt = '';
        pf = tempray[2]; if ( pf == undefined ) pf = '';
        break;
      default: alert(keyword + ' is an unknown keyword to PageTop() in ' +
                     document.URL)
      }
    }
  //----- generate the start-of-main page -----
  document.writeln('<tr><td colspan=1 valign=top class="mainbody"><a name="PageTop"></a>')
  
  //----- generate any other needed html -----
  if ( pu != '' || nu != '' || _return ) {    // show links if any given
    PrevNextPage(pu,pf,pt,nu,nf,nt,_return ? 'RETURN' : '')
    document.writeln('<br>')
    }
  if ( _print && browser_version>=4 ) {  // show print icon if requested
       prticon = '<div style="float: right;">' +
                 '<a href="Javascript:window.print()">' +
                 '<img src="' + top.ImagePath + 'printer.gif" ' +
                      'width=32 height=42 border=0 alt="Print this page"></a>' + 
                 '</div>'
       document.writeln(prticon);
       }
  if ( pagetitle != '') {  // show page title if a title provided

    if ( ( browser_type == 'NS' ) && ( browser_version <= 4 ) )
           { document.writeln('<h1><i>' + pagetitle + '</i></h1>')
             }
      else { document.writeln('<br><span class="' + tclass + '">' +
                              pagetitle + '</span>')
             }
    }
  return
  }


//-------------------------------------------------------------------
// PageBottom: Create bottom-of-page HTML text
//    PageBottom( [ parm1 [, parm2, parm3, ... , parmx ] ] )
//      parm - any one of the following parameters:
//         { DATE | NODATE }
//              Should the date of the calling HTML file be shown?
//              If omitted, DATE is assumed.
//         { FEED | NOFEED }
//              Should a Feedback-type link be included?
//              If omitted, FEED is assumed.
//         PREV=url | [ description ] | [ frame ]
//              A "previous-page" link to the specified url should be
//              generated.  If a description is provided, it is shown.
//              If a frame name is specified, a target is generated.
//              e.g.  'PREV=overview.html'
//              e.g.  'PREV=overview.html|Overview description'
//              e.g.  'PREV=overview.html|Overview description|dataframe'
//         { PROP | NOPROP }
//              Should the "Copyright ..." text be shown?
//              If omitted, PROP is assumed.
//         NEXT=url | [ description ] | [ frame ]
//              A "next-page" link to the specified url should be
//              generated.  If a description is provided, it is shown.
//              If a frame name is specified, a target is generated.
//              e.g.  'NEXT=detail.html'
//              e.g.  'NEXT=detail.html|Detail Information'
//              e.g.  'NEXT=detail.html|Detail Information|dataframe'
//         { RETURN | NORETURN ]
//              Should a "return-to-calling-page" link be generated?
//              If omitted, NORETURN is assumed.
//         { HITBOX | NOHITBOX ]
//              Should the "hitbox" be generated for tracking hits?
//              If omitted, HITBOX is assumed.
//-------------------------------------------------------------------
function PageBottom() {
  var _date=true, _feed=true, _prop=true, _return=false, _hitbox=false, _todd=false
  var pu='', pt='', pf='', nu='', nt='', nf=''
  //----- scan & evaluate the passed parameters
  for ( var ix=0; ix < arguments.length; ix++ ) {
    thisParm = arguments[ix]
    if ( thisParm == null ) continue      // ignore null parms
    if ( thisParm == ''   ) continue
    tempray = thisParm.split('=',2)
    keyword = tempray[0].toUpperCase()
    data    = tempray[1]; if ( data == undefined ) data = ''
    switch (keyword) {
      case 'DATE'    : _date   = true;  break;
      case 'NODATE'  : _date   = false; break;
      case 'FEED'    : _feed   = true;  break;
      case 'NOFEED'  : _feed   = false; break;
      case 'PROP'    : _prop   = true;  break;
      case 'NOPROP'  : _prop   = false; break;
      case 'RETURN'  : _return = true;  break;
      case 'NORETURN': _return = false; break;
      case 'HITBOX'  : _hitbox = true;  break;
      case 'NOHITBOX': _hitbox = false; break;
      case 'TODD'    : _todd   = true;  break;
      case 'NEXT'    :
        tempray = data.split('|',3);
        nu = tempray[0];
        nt = tempray[1]; if ( nt == undefined ) nt = '';
        nf = tempray[2]; if ( nf == undefined ) nf = '';
        break;
      case 'PREV'    :
        tempray = data.split('|',3);
        pu = tempray[0];
        pt = tempray[1]; if ( pt == undefined ) pt = '';
        pf = tempray[2]; if ( pf == undefined ) pf = '';
        break;
      default: alert(keyword + ' is an unknown keyword to PageBottom() in ' +
                     document.URL)
      }
    }
  document.write('<br>&nbsp;</td></tr>')  // finish the web page
  
  //----- generate the page footer
  footertemp  = ''
  footertemp += '<tr><td colspan=2 valign=top class="footer"><a name="PageBottom"></a><hr>' 

  if ( _prop ) 
         { footertemp += '<span class=footcopyr><nobr>'  +
                         'Copyright &copy; 2010 - FSTA' +
                         '<br>Web site by ' +
                         '<a href="http://www.CandGWeb.com" target="_new">' +
                         'C&G Web Enterprises</a>' +
                         '</nobr></span>' }
    else { footertemp += '&nbsp;' }
    if ( _date ) {
    footertemp += '<span class=footpageupd><nobr>Page last updated: ' +
                  DateStr(document.lastModified) + '</nobr></span><br>'
    }
  if ( _feed ) {
    footertemp += '<span class=footfeedbk><nobr>' +
                  'Questions about this web site or this page? ' +
                  '<a href="mailto:' + author_email +
                  '?subject=MyFSTA%20Website%20Feedback%20(' +
                  document.location.href + ')">' +
                  'Tell us about it!</a></nobr></span>'
    }
     
  // ----- generate "hitbox" if requested
  if ( _hitbox ) {
    p  = document.location.pathname.lastIndexOf('/') + 1
    fn = document.location.pathname.substr(p)
    footertemp = '<!-- BEGIN WEBSIDESTORY CODE v5 -->\n' +
           '<!-- COPYRIGHT 1998-1999 WEBSIDESTORY, INC. ALL RIGHTS RESERVED.  \n' +
           'U.S. PATENT PENDING. -->'
    footertemp += '<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br><p align="right">'
    footertemp += '<a href="http://rd1.hitbox.com/rd?acct=WQ591007BBSE87EN0&amp;p=s">'
    footertemp += '<img SRC="http://hg1.hitbox.com/HG?hc=wf122&amp;cd=1&amp;hv=6' +
            '&amp;ce=u&amp;hb=WQ591007BBSE87EN0&amp;n=' + fn + '"'
    footertemp += ' height="62" width="88" ALT="Click Here!" border="0"></a>\n' +
            '<!-- END WEBSIDESTORY CODE  -->'
    }	         
	         
  //----- finish up the footer (if any) and the main web page	         
  footertemp += '</td></tr>'
  document.write(footertemp)
    
  //----- finish up the main web page -----
  temp  = '</table><td height=1 width=1 class="vertborder"><img src="images/border.gif" width=1 height=1></td>' // right border
  temp += '</tr>'
  temp += '</table>'
  document.write(temp)
  return
  
  }


//-------------------------------------------------------------------
// NavButton: Generate HTML for a major section in the nav bar
// Use: NavButton(callerid,thisitem,thisurl,navtitle)
//   callerid  - Section ID of requester
//   thisitem  - Section ID associated with the following items
//   thisurl   - URL of target file
//   navtitle  - description for the "bubble" text
//-------------------------------------------------------------------
function NavButton(callerid,thisitem,thisurl,navtitle) {
  if (thisurl == undefined) thisurl = ''
  if (thisurl.substr(0,1) == '!')
         { thisurl = thisurl.substr(1); targ = 'target="_new" ' }
    else { thisurl = thisurl;           targ = ''               }
  if (callerid == thisitem)
         { tmpnav = ' class="selected"' }
    else { tmpnav = '' }
  result = '<li><a href="' + thisurl + '"' + targ + tmpnav + '>&nbsp; <span>' + navtitle + '</span></a>&nbsp; </li>'
  return result
  }




//-------------------------------------------------------------------
// Routines to manipulate/show dates
//-------------------------------------------------------------------

// MakeArray:  create an array of the designated size
  function MakeArray(n) {
    this.length = n
    for (var i = 0; i <= n; i++) { this[i] = 0 }
    return this
    }

// DateStr: Return a date string formatted from the passed date value.
// e.g. abc = DateStr()  might return: Sunday, August 29 at 3:00:00 p.m.
  function DateStr(dsparm) {
    dow=new MakeArray(6); moy=new MakeArray(11)
    dow[0]='Sunday';    dow[1]='Monday';   dow[2]='Tuesday';   dow[3]='Wednesday'
    dow[4]='Thursday';  dow[5]='Friday';   dow[6]='Saturday'
    moy[0]='January';   moy[1]='February'; moy[2]='March';     moy[3]='April'
    moy[4]='May';       moy[5]='June';     moy[6]='July';      moy[7]='August'
    moy[8]='September'; moy[9]='October';  moy[10]='November'; moy[11]='December'

    if ( (dsparm=='') || (dsparm==null) ) // any passed value?
           { dsdate  = new Date() }       //   no, get today's date
      else { dsdate  = new Date(dsparm) } //   yes, get the date
    year  = dsdate.getYear()  ; hour    = dsdate.getHours()
    month = dsdate.getMonth() ; minutes = dsdate.getMinutes()
    dom   = dsdate.getDate()  ; seconds = dsdate.getSeconds()
    day   = dsdate.getDay()
    if        ( year < 100  ) { yyyy = year + 2000 }
      else if ( year < 1000 ) { yyyy = year + 1900 }
      else                    { yyyy = year }
    if        ( hour <= 11 ) { ampm='a.m.' }
      else if ( hour == 12 ) { ampm='p.m.' }
      else                   { ampm='p.m.'; hour=hour-12 }
    if ( hour == 0 ) showhour="00"; else showhour=hour
    if ( minutes < 10 ) minutes = "0" + minutes
    if ( seconds < 10 ) seconds = "0" + seconds
    return dow[day] + ", " + moy[month] + " " + dom + ", " + yyyy + " at " +
           showhour + ":" + minutes + ":" + seconds + " " + ampm
    }







function loremipsum() {
  // Lorem Ipsum Generator
  // Version 2.0
  // Copyright 2003 - 2005 Marcus Campbell
  // Open-source code under the GNU GPL:
  // http://www.gnu.org/licenses/gpl.txt

  this.source = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

  this.getLetters = function getLetters(value) {
          var output = new String();
          if (isNaN(value))
                  value = this.source.length;
          var tempSource = this.growSource(value / this.source.length + 1);
          for (var i = 0; i < value; i++)
                  output += tempSource.charAt(i);
          return output;
  }

  this.getWords = function getWords(value) {
          var output = new String();
          var pattern = /[\w\!\.\?\;\,\:]+/g;
          var matches = this.source.match(pattern);
          if (isNaN(value))
                  value = matches.length;
          var tempSource = this.growSource(value / matches.length);
          var tempMatches = tempSource.match(pattern);
          for (var i = 0; i < value; i++) {
                  if (i > 0)
                          output += " ";
                  output += tempMatches[i];
          }
          return output;
  }

  this.growSource = function growSource(value) {
          var output = new String();
          for (var i = 0; i < value; i++) {
                  if (i > 0)
                          output += " ";
                  output += this.source;
          }
          return output;
  }
}
var lipsum = new loremipsum();
