// Javascript utilities




  // ****************************
  // General Navigation Utilities
  // ****************************
  function goToFrameCtl(target) {
    window.document.location = target;
  }
  
  function openWin(fileName, title) {
     //myFloater = window.open('',title,'width=600,height=600')
     myFloater = window.open('',title)
     myFloater.location.href = fileName;
  }

  function closeMe() {
     window.close();
  }
  
  function displayScrollbars() {
  // Due to the general crappiness of IE have to fool
  // browser into displaying scroll bars by doing this onLoad
     resizeBy(1);
  }

  // ***************
  // Number utilites
  // ***************
  function checkFormat(input) {
    output = parseInt(input);
    if (output == "NaN") {
      output = 1;
    }
    return output;
  }

  function getRandom(num) {
    return Math.floor(Math.random() * num) + 1;
  }

  // *****************
  // Browser utilities
  // *****************
  function getAppVersion() {
   return navigator.appVersion;
  }

  function getAppName() {
   return navigator.appName;
  }

  function getCodeName() {
   return navigator.appCodeName;
  }

  function getLanguage() {
   return navigator.appLanguage;
  }

  function getPlatform() {
   return navigator.platform;
  }

  function getUserAgent() {
   return navigator.userAgent;
  }
  
  function getBrowser() {
    if (getUserAgent().indexOf("Opera") > -1) {
      return "Opera";
    }
    else if (getAppVersion().indexOf("IE") > -1) {
      return "IE";
    } 
    else {
      return getAppVersion();
    }
  }
  
  function showBrowser() {
    alert("Browser is " + getBrowser());
    return true;
  }

  // *********************  
  // Photo Album Utilities
  // *********************
  function displayPhotoAlbum(fileIndex) {
     currentPicture=fileIndex;
     if (getAppVersion().indexOf("IE 5") > -1) {
       myFloater = window.open('album.htm?fileIndex=' + fileIndex,'photoAlbum','width=750,height=550');
     } 
     else if (getAppVersion().indexOf("IE 6") > -1) {
       myFloater = window.open('album.htm?fileIndex=' + fileIndex,'photoAlbum','width=750,height=550');
     }
     else {
       myFloater = window.open('picture.htm','picture' + fileIndex,'width=750,height=500'); 
     }
  }

  function getPictureNumber() {
    return window.name.substring(7,window.name.length);
  }
  
  function getCurrentIndex() {
     var currentIndex = window.location.search;
     currentIndex = currentIndex.substring(11, currentIndex.length);
     if (currentIndex == "") {
       currentIndex = 0;
     }
     return currentIndex;
  }
  
  function previousPicture(currentIndex) {
     var prev = getPrevious(currentIndex);
     window.location ="album.htm?fileIndex=" + prev;
  }

  function nextPicture(currentIndex) {
     var next = getNext(currentIndex);
     window.location ="album.htm?fileIndex=" + next;
  }
    
  function getPrevious(currentIndex) {
     var nextIndex = 0;
     if (currentIndex == 0) {
       nextIndex = pictureArray.length-1;
     }
     else {
       nextIndex = + currentIndex - 1;
     }
     return nextIndex;
  }

  function getNext(currentIndex) {
     var nextIndex = 0;
     if (currentIndex == pictureArray.length-1) {
       nextIndex = 0;
     }
     else {
       nextIndex = + currentIndex + 1;
     }
     return nextIndex;
  }

  function displayRandomThumb(maxNumber) {
        var randomIndex = getRandom(maxNumber);
  	document.write('<A HREF="javascript:displayPhotoAlbum(' + randomIndex + ')" ALT="Click to see picture"');
	document.write(' ONMOUSEOVER="status=\'Click here to see picture\'; return true;"');
	document.write(' ONMOUSEOUT="status=\'\'; return true;">');
  	document.write('<IMG src="images/' + pictureArray[randomIndex].thumbFile + '.jpg"' + ' alt="" width=' + pictureArray[randomIndex].thumbWidth + ' height=' + pictureArray[randomIndex].thumbHeight + '>');
	document.write('</A>');
  }
    
  function displayThumbs() {
    var picsOnLine = 5;
    var picsFilled = 0;
    for (i=0; i<pictureArray.length; i++) {
      if (i%picsOnLine==0) {
        if (i > 0) {
          document.write("</tr>");  
        }
        document.write("<tr align='center'>");
        document.write("<td>");        
        drawCell(i);
        document.write("</td>");        
      }
      else {
        document.write("<td>");        
        drawCell(i);
        document.write("</td>");        
      }
    }
    document.write("</tr>");
  }
  
  function drawCell(i) {
    document.write("<table cellspacing=0 border=0 align='center'>");
    document.write("  <tr align='center'>");
    document.write("    <td>");
    document.write("      <A HREF='javascript:displayPhotoAlbum(" + i + ")'>");
    document.write("        <img BORDER='0' SRC='images/"+ pictureArray[i].thumbFile + ".jpg' width='" + pictureArray[i].thumbWidth + "' height='" + pictureArray[i].thumbHeight + "' ALT='" + pictureArray[i].pictureName +"'>");
    document.write("      </A>");
    document.write("    </td>");    
    document.write("  </tr>");    
    document.write("  <tr align='center'>");
    document.write("    <td>");
    document.write("     <font face='Arial' size='3' color='#3300FF'>");
    document.write(        pictureArray[i].pictureName);
    document.write("     </font>");    
    document.write("    </td>");    
    document.write("  </tr>");    
    document.write("</table>");
  }
  
  function preloadPictures() {
    for (i=0; i<pictureArray.length; i++) {
      document.write("<img BORDER='0' SRC='images/"+ pictureArray[i].thumbFile + ".jpg' width='1' height='1'>");  
      document.write("<img BORDER='0' SRC='images/"+ pictureArray[i].pictureFile + ".jpg' width='1' height='1'>"); 
    }
  }


  // ********************
  // Tickertape utilities
  // ********************

function getTickerTapeMessage() {
  if (overrideMessage.length > 0) {
    return overrideMessage;
  }
  var currentEvent;
  for (i=0; i<eventsArray.length; i++) {
    currentEvent = eventsArray[i];
    if (isActive(currentEvent.eventDate, 3)) {
      if (i==(eventsArray.length-1)) {
        tickerTapeMessage += currentEvent.eventText;
        break;        
      }
      else {
        tickerTapeMessage += currentEvent.eventDate + " " + currentEvent.eventText;
        break;
      }
    }
  }
  return tickerTapeMessage;
}

  // ********************
  // Events utilities
  // ********************

function getEvents() {
  var currentEvent;
  for (i=0; i<(eventsArray.length-1); i++) {
    currentEvent = eventsArray[i];
    document.write('<tr>');
    document.write('<td width="20%" valign="top"><font face="Arial" size="3" color="#3300FF">');
    document.write(currentEvent.eventDate);
    document.write('</font></td>');
    document.write('<td width="80%"><font face="Arial" size="3" color="#3300FF">');
    document.write(currentEvent.eventText);
    document.write('</font></td>');
    document.write('</tr>');
  }

}

//Get system date month
function getCurrentMonth()
{
  var today = new Date();
  return today.getMonth();
}

function isActive(dateString,dateType) 
{
/*
   function isActive 
   parameters: dateString dateType
   returns: boolean
   
   dateString is a date passed as a string in the following
   formats:

   type 1 : 19970529
   type 2 : 970529
   type 3 : 29/05/1997
   type 4 : 29/05/97
   
   dateType is a numeric integer from 1 to 4, representing
   the type of dateString passed, as defined above.

   Returns true if the input date is not in the past 
   Returns false if the input date is in the past
   or if dateType is not 1 to 4.
*/

    var now = new Date();
    if (dateType == 1)
        var date = new Date(dateString.substring(0,4),
                            dateString.substring(4,6)-1,
                            dateString.substring(6,8));
    else if (dateType == 2)
        var date = new Date(dateString.substring(0,2),
                            dateString.substring(2,4)-1,
                            dateString.substring(4,6));
    else if (dateType == 3)
        var date = new Date(dateString.substring(6,10),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else if (dateType == 4)
        var date = new Date(dateString.substring(6,8),
                            dateString.substring(3,5)-1,
                            dateString.substring(0,2));
    else
        return false;

    // Add hours to 11pm
    date.setHours(23);
    
    // Test against now
    if (date.getTime() < now.getTime())
        return false;
    else
        return true;
}

  // ***************
  // Number utilites
  // ***************

  function getRandom(num) {
    return Math.floor(Math.random() * num);
  }

