/* set of javascript functions used with WCAM web site
Author: Renaud caron
Created: 27 June 2010
*******************************************************/
function preloadImages() 
// Called by body tag of each HTML file
{
 if (document.images) {preloadFlag = true;};
}

function DisplayBanner()
{
 InitVars();
 var sBanner = '<div class="Banner">';
 sBanner += '<a href="learning.html">';
 sBanner += '<img title="More information on Take Flight!" src="Top-banner-fall2007.gif"';
 sBanner += ' alt="" height="128" width="750" border="0" /></a>';
 sBanner += '</div>';
 document.write(sBanner);
}

function DisplayTopLinks()
{
 var sTopLinks = '<div class="TopLinks">';
 sTopLinks += sContactUs;
 sTopLinks += spaces(14);
 sTopLinks += sVisitUs;
 sTopLinks += spaces(14);
 sTopLinks += sHome;
 sTopLinks += '</div>';
 document.write(sTopLinks);
}

function DisplayLeftFrame()
{
 var sLeftFrame = '<div class="LeftBox">';
 sLeftFrame += '<p>' + sCollection + '</p>';
 sLeftFrame += '<p>' + sAboutUs + '</p>';
 sLeftFrame += '<p>' + sDonations + '</p>';
 sLeftFrame += '<p>' + sEvents + '</p>';
 sLeftFrame += '<p>' + sForKids + '</p>';
 sLeftFrame += '<p>' + sForTeachers + '</p>';
 sLeftFrame += '<p>' + sFacilities + '</p>';
 sLeftFrame += '<p>' + sGiftShop + '</p>';
 sLeftFrame += '<p>' + sLinks + '</p>';
 sLeftFrame += '<p>' + sMembership + '</p>';
 sLeftFrame += '<p>' + sVolunteers + '</p>';
 sLeftFrame += '<hr/>';
 sLeftFrame += '<p><a title="24 pages of tales from the sky!" href="altitude.html">';
 sLeftFrame += 'Free with membership!</a></p>';
 sLeftFrame += '<a href="Altitude-Winter09-Cover.gif">';
 sLeftFrame += '<img id="Altitude" src="Altitude-Spring10-Cover3.gif" alt="Altitude-Spring10-Cover3.gif"';
 sLeftFrame += 'name="Altitude" height="185" width="145" border="0" /></a>';
 sLeftFrame += '</div>';
// alert(sLeftFrame);
 document.write(sLeftFrame);
}

function DisplayFooter()
{
 InitVars();
 var sSpacer = '&nbsp;|&nbsp;';
 var sFooter = '<div class="Footer">';
 sFooter += '<br /><br /><p>' + sSpacer;
 sFooter += sHome + sSpacer;
 sFooter += sCollection + sSpacer;
 sFooter += sEvents + sSpacer;
 sFooter += sForKids + sSpacer;
 sFooter += sForTeachers + sSpacer;
 sFooter += sFacilities + sSpacer + '</p>';
 sFooter += '<p>' + sSpacer + sGiftShop + sSpacer;
 sFooter += sMembership + sSpacer;
 sFooter += sVolunteers + sSpacer;
 sFooter += sAboutUs + sSpacer;
 sFooter += sContactUs + sSpacer;
 sFooter += sVisitUs + sSpacer + '</p><br />';
 sFooter += '<p> Copyright 2010 Western Canada Aviation Museum, Hangar T-2, 958 Ferry Road, Winnipeg, Manitoba, R3H 0Y8</p>';
 sFooter += '<p> P: 204-786-5503, F: 204-775-4761</p>';
 sFooter += '</div>';
 document.write(sFooter);
}
function InitVars()
// Create set of global variables which are used more than one
{
 sContactUs = '<b><a title="E-mail us!" href="mailto:info@wcam.mb.ca">Contact Us</a></b>';
 sVisitUs = '<b><a title="Directions, hours and more" href="visit.html">Visit Us</a></b>';
 sHome = '<b><a title="Back to our home page" href="index.html">Home</a></b>';
 sCollection = '<b><a title="Learn more about our collection" href="collections1.html">Aircraft Collection</a></b>';
 sAboutUs = '<b><a title="Learn more about the Museum" href="aboutus.html">About Us</a></b>';
 sDonations = '<b><a href="donations.html">Donations</a></b>';
 sEvents = '<b><a title="Find out what\'s on the event calendar" href="calendar.html">Events</a></b>';
 sForKids = '<b><a title="Great stuff for kids!" href="facilities.html">For Kids</a></b>';
 sForTeachers = '<b><a title="Special programs for teachers" href="learning.html">For Teachers</a></b>';
 sFacilities = '<b><a title="Host your next event at the Museum!" href="event.html">Facilities Rental</a></b>';
 sGiftShop = '<b><a title="Stuff to buy for aviation buffs!" href="giftshop.html">Gift Shop</a></b>';
 sLinks = '<b><a title="Links to other sites" href="links.html">Interesting Links</a></b>';
 sMembership = '<b><a title="Become a member" href="members.html">Memberships</a></b>';
 sVolunteers = '<b><a title="Volunteer with the Museum" href="volunteers.html">Volunteers</a></b>';
}

function GetParam(name)
 // This function extracts the search parameter i.e. text after '?'
 // Returns the search string after the '?' if it finds one; otherwise returns empty string
 // Called by VRGateway.html
{
 var sLocation = location.search.toLowerCase(); // This statement and the next one make the search variable case insensitive
 name = name.toLowerCase();
 var start=sLocation.indexOf("?"+name+"="); // look for '?qtvr='
 if (start<0) return '';
 start += name.length+2; // start after '=' sign
 var end=location.search.indexOf("&",start)-1; // finish at first '&'
 if (end<0) end=location.search.length;
 var result='';
 for(var i=start;i<=end;i++)
 {
  var c=location.search.charAt(i); // get character at each position
  result=result+ (c=='+'?' ':c); // if char is '+' replace with a space
 }
 return unescape(result);
}
// Set of generic functions
function spaces(NumSpaces)
// this function creates a string of 'NumSpaces' spaces
{
  return (new Array(NumSpaces + 1)).join('&nbsp;');
}

function writeDateModified(time)
// call example: <script type="text/javascript">writeDateModified();</script>
// 'time is optional variable; set to 'true' to display time
{
  var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); // Array to hold day names
  var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); // Array to hold up month names
  var sOutput = "";

  // Assign date variables with document.lastModified 
  var modDate = new Date(Date.parse(document.lastModified));
  
  // If we have a valid date, reformat it.
  if (modDate != 0 && modDate != "Invalid Date")
  {
    // Set up day variable to hold the name of the day
    var day = days[modDate.getDay()] + " - ";
    day = ""; // I don't want the day displayed
    
    // ndate variable holds day of month
    var ndate = modDate.getDate();
    
    // Set up month variable to hold the name of the month
    var month = months[modDate.getMonth()];
    
    // Get the year and if it is less than 1000 add 1900 to it.
    var year = modDate.getYear();
    if (year < 1000) year = year + 1900;
    
    // Load up the time variables if required
    if (time)
    {
      var hour = modDate.getHours().toString();
      if (hour.length == 1) hour = "0" + hour; 
      var minute = modDate.getMinutes().toString();
      if (minute.length == 1) minute = "0" + minute;
      var second = modDate.getSeconds().toString();
      if (second.length == 1) second = "0" + second;
    }
    
    // Display date and time document was last updated.
    sOutput = "<b>Page last modified: " + day + ndate + " " + month + " " + year + "</b>";
    if (time) {sOutput += "<b> @ " + hour + ":" + minute + ":" + second + "</b>";}
  }
  else sOutput = "<i>Page last modified date not available!</i>";
  //document.write(sOutput);
  return sOutput;
}



function isEmpty(obj)
// Tests if an object is empty or not 
// Algorithm: empty string will not execute for loop
// Return True if empty; False otherwise
{
 bReturn = true;
 for(var prop in obj) 
 {
  bReturn = false;
 }
 return bReturn;
}

// Create string trim prototype
  // Usage example:
  //sTest = "  Example ";
  //alert("|" + sTest + "|" + " Converts to |" + sTest.trim() + "|");

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");}
// The ReplaceAll prototype replaces all instances of the given substring. 
// Source: http://www.bennadel.com/blog/142-Ask-Ben-Javascript-String-Replace-Method.htm
// Calling example: strReplaceAll2 = strText.replaceAll( "th", "[X]" )
String.prototype.replaceAll = function(strTarget, strSubString)
{
 // strTarget is The substring you want to replace
 // strSubString is The string you want to replace in.
 var strText = this;
 var intIndexOfMatch = strText.indexOf( strTarget );
 while (intIndexOfMatch != -1)
 {
  strText = strText.replace( strTarget, strSubString )
  intIndexOfMatch = strText.indexOf( strTarget );
 }
 return( strText );
}
