//--------------------------------------------------------
// Name    : base.js
// &copy;  : Copyright 2005 SouthForkPrairie.com
// Language: JavaScript 1.1
// Summary : Common utility functions. Current functions are
//         : as follows (see comments below for details):
//         : buttonHandler  - Handles dynamic button images.
//         : _Date_format   - Date prototype to format a date.
//         : getSearchValue - Extracts a specified value from the URL search parms.
//         : menu           - Creates the site menu object.
//         : msg            - Sets window.status message box.
//         : noFrame        - Prohibits a page from being loaded within a frame.
//         : replChar       - String prototype to replaces a character.
//         : setFocus       - Forces current window to have focus.
//         : trimRight      - Removes trailing characters from a string.
//         : wPrint_ie4     - Automatically provides equivalent functionality
//         :                  as "window.print()" for MSIE 4.
//         : writeFoot      - Writes the common footer.
// History : Author--------- Description------------------
// 01/01/05: David Winn      Created.
//--------------------------------------------------------

defaultStatus=document.title;

// ----- Define an object to handle mouseOver, mouseOuts, and mouseClicks for buttons.
// The default path for images, if used, must contain forward
// slashes (/), not backslashes (\). Image filenames without a slash
// are prefixed with the default path. If needed, use "./filename"
// to overide the default for images in the current directory.
function buttonHandler(imgPath,isImgClickUsed)
{
 this.imgPath = "" + imgPath; // Default path for all images.
 this.isImgClickUsed = (arguments.length < 2) ? false : isImgClickUsed; // Is "onClick" image used?
 this.selected = null; // Currently selected image.
 this.items = new Array(); // items names
 this.imgNorm = new Array(); // normal images
 this.imgOver = new Array(); // mouseOver (highlight) images
 this.imgClick= new Array(); // mouseClick (Selected) images
 this.imgMsg  = new Array(); // mouseOver window status message.
 this.add = _buttonHandler_Add;
 this.over = _buttonHandler_Over;
 this.out = _buttonHandler_Out;
 this.click = _buttonHandler_Click;
 return this;
}

// ----- buttonHandler method to add (and pre-load images for) a new button to be handled.
// If not supplied, the default image file names are derived from the name parameter.
function _buttonHandler_Add(name, msg, normSrc, overSrc, clickSrc)
{
 this.items[this.items.length] = name;
 this.imgMsg[name] = msg;
 if   (document.images)
 {
      this.imgNorm[name] = new Image();
      this.imgOver[name] = new Image();
      this.imgClick[name] = new Image();
      if   (normSrc  == null) 
           normSrc  = name + "_n.gif";
      if   (overSrc  == null)
           overSrc  = name + "_m.gif";
      if   (clickSrc == null)
           clickSrc = (this.isImgClickUsed) ? name + "_c.gif" : overSrc;
      this.imgNorm[name].src  = (normSrc.indexOf("/")  >= 0) ? normSrc  : this.imgPath + normSrc;
      this.imgOver[name].src  = (overSrc.indexOf("/")  >= 0) ? overSrc  : this.imgPath + overSrc;
      this.imgClick[name].src = (clickSrc.indexOf("/") >= 0) ? clickSrc : this.imgPath + clickSrc;
 }
}

// ----- buttonHandler method to handle OnMouseOver events.
function _buttonHandler_Over(name)
{
 if   (document.images && name != this.selected)
      document.images[name].src = this.imgOver[name].src;
 window.status = this.imgMsg[name];
 return true;
}

// ----- buttonHandler method to handle OnMouseOut events.
function _buttonHandler_Out(name)
{
 if   (document.images && name != this.selected)
      document.images[name].src = this.imgNorm[name].src;
 window.status = "";
 return true;
}

// ----- buttonHandler method to handle OnMouseClick events.
function _buttonHandler_Click(name)
{
 var oldSelected = this.selected;
 this.selected = name;
 if   (document.images)
 {
      if   (oldSelected != null)
           this.out(oldSelected);
      document.images[name].src = this.imgClick[name].src;
 }
 return true;
}

// ----- Date method to format a date.
Date.prototype.format = _Date_format;
function _Date_format(format)
{
 var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
 var weekDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
 var weekDay = weekDays[this.getDay()];
 var dd = this.getDate();
 var mm = this.getMonth() + 1
 var month = months[this.getMonth()];
 var yyyy = this.getYear();
 if  (yyyy < 2000)
     yyyy = yyyy + 1900;
 switch (format)
 {
  case 'mm/dd/yyyy': return mm + "/" + dd + "/" + yyyy;
  case 'mm/dd/yy':   return mm + "/" + dd + "/" + yyyy.toString().substr(2,2);
  case 'yyyy':       return yyyy;  
  default:           return month + " " + dd + ", " + yyyy;
 }
}

// --- Extracts a search value from URL
function getSearchValue(name)
{
 var r = "";  // return string
 var s = document.location.search.substr(1);
 var a = s.split('&');
 for (var i=0; i < a.length; i++)
 {
     var p = a[i].split('=');
     if   (p[0] == name)
     {
          r = unescape(p[1]);
          r = r.replChar("+"," ");
          r = r.replChar("<>","");
        break;
     }
 }
 return trimRight(r);
}

// ----- Define the menu object and its methods.
function Menu(objName, homePath)
{
 this.item = new Array(); // Array to store the menu items.
 this.homePath = homePath;
 this.objName = objName;
 this.add = _menu_Add; // These are the methods for the menu object.
 this.insert = _menu_Insert;
 this.write = _menu_write;
 this.mOver = _menu_mOver;
 this.mOut = _menu_mOut;
 return this;
}

// ----- Menu method to add a new menu item.
function _menu_Add(id, url, msg)
{
 this.item[this.item.length] = new _menu_Item(id,  url, msg, this.homePath);
}

// ----- Menu method to add a new menu item by inserting if afer an existing menu item.
function _menu_Insert(afterID, id, url, msg)
{
 for (i = 0; this.item[i].id != afterID; i++);
 this.item.splice(i+1,0,new _menu_Item(id,  url, msg, this.homePath));
}
// ----- Menu constructor method for a single menu item.
function _menu_Item(id, url, msg, homePath)
{
 this.id = id;
 this.url = (url.substr(0,5) == "http:") ? url : homePath + url;
 this.msg = msg;
 return this;
}

// ----- Menu method to write the menu.
function _menu_write(id,more)
{
 var s = '<DIV id="nav"><UL>';
 for  (var i=0; i < this.item.length; i++) 
 {
  if    (this.item[i].id == id) // Is this the currently displayed item?
        s += '<LI class="navCur"><A>' + this.item[i].id + '</A></LI>';
  else
        s += '<LI><A href="' + this.item[i].url + '" onMouseOver="return msg(' + "'" + this.item[i].msg + "'" + ')" onMouseOut="return msg()" title="' + this.item[i].msg + '">' + this.item[i].id + '</A></LI>';
 }
 s += '<LI><BR>&nbsp;</LI></UL>';
 if    (arguments.length < 2 || more != "Y")
       s += '</DIV>';
 document.writeln(s);
 document.close();
}

// ----- Menu method to handle OnMouseOver events.
function _menu_mOver(i)
{
 window.status = this.item[i].desc;
 return true;
}

// ----- buttonHandler method to handle OnMouseOut events.
function _menu_mOut(i)
{
 window.status = document.title;
 return true;
}

// ----- Changes the Window status message, at the bottom of the browser window.
function msg(msg)
{
 window.status = (msg == null) ? document.title : msg;
 return true;
}

// ----- Prohibits a page from being loaded within a frameset.
function noFrame()
{
 if   (window.top != window.self)
      window.top.location = window.location;
 return;
}

// ----- String method to replace a specified character in a string.
String.prototype.replChar = _String_replChar;
function _String_replChar(fromChars,toChars)
{
 var s = this.toString();
 var r = "";
 for (var j=0; j < s.length; j++)
 {
     var found = false;
     for (var i=0; i < fromChars.length  && !found; i++) found = (s.charAt(j) == fromChars.charAt(i));
     r += (found) ? toChars : s.charAt(j);
 }
 return r;
}

// ----- Draws the browser's focus.
function setFocus()
{
 if   (navigator.userAgent.indexOf("MSIE") == -1 || parseFloat(navigator.appVersion) >= 4) this.focus();
}

// ----- Remove trailing character(s) from a string.
function trimRight(inStr, trimChars)
{
 if   (arguments.length < 2) 
      trimChars = ' ';
 var i = inStr.length - 1;
 for (; i >= 0 && (trimChars.indexOf(inStr.charAt(i)) >= 0); i--);
 return inStr.substr(0,i + 1);
}

// ----- Write the common page footer.
function writeFoot(d)
{
 if   (arguments.length == 0)
      d = new Date(Date.parse(document.lastModified));
 var s = '<DIV id="foot">&copy; Copyright ' + d.format('yyyy') + ', SouthForkPrairie.com&nbsp;&nbsp;All rights reserved ';
 s += '<BR><ADDRESS>Contact: <A href="mailto:webmaster@SouthForkPrairie.com" onMouseOver="return msg(\'Click here to send us an e-mail message\')" title="Click here to send us an e-mail message" onMouseOut="return msg()">webmaster@SouthForkPrairie.com</A></ADDRESS>';
 s += 'Last updated: ' + d.format() + '</DIV>';
 document.writeln(s);
 document.close();
}
