/************************************************************************************
   Filename: functions.js
   Author:   Pat Heard (2006/10/18)
   Purpose:  Generic functions for all pages
 ************************************************************************************/

/**
 * Browser detect object
 */
var browser = {
 
  isIe : false,
  isNS : false,
  isFF : false,
  isOP : false,
  
  // Set the browser variables
  init : function(){
    agt = navigator.userAgent.toLowerCase();
    this.isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.isNS = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('firefox')==-1) && (agt.indexOf('hotjava')==-1));
    this.isFF = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('firefox')!=-1) && (agt.indexOf('hotjava')==-1));
    this.isOP = window.opera;
    
    // Stop background image flicker in IE
    if(this.isIE){
      try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {}  
    }
  } 
};
browser.init();  


/**
 * Hover object to handle mouseover/out events
 */
var hover = {

  // holds the preloaded imgs
  preloadedImgs : new Array(),
  
  // Hovers a table row
  tableRow : function(row) {
    // Only IE needs this
    if(!browser.isIE){ return; }
    if(row) { row.className = row.className === "" ? "hover" : ""; }       
  },
  
  // Rolls over an image - names must be: image_name, image_name_hover
  rollover : function(img){
    if(img && img.src){      
      img.src = img.src.replace(/((-e|-f)?\.[a-z0-9]+)$/i,'_hover$1');
    }
  },
  
  // Rolls out an image - names must be: image_name, image_name_hover
  rollout : function(img){
    if(img && img.src){
      img.src = img.src.replace(/_hover((-e|-f)?\.[a-z0-9]+)$/i,'$1');
    }
  },
  
  // Hovers a child image contained in the passed in parent reference
  childImg : function(parent){
    var img = parent.getElementsByTagName('img')[0];
    if(img.src.indexOf("_hover") == -1) { hover.rollover(img); }  
    else { hover.rollout(img); }
    
  },
  
  // Preloads images for rollovers
  preload : function(imgs){
    for(var i = 0; i < imgs.length; i++) {
      this.preloadedImgs[i] = new Image();
      this.preloadedImgs[i].src = imgs[i];
    }  
  } 
};



/**
 * Page object to handle onload, onsubmit and form submissions
 */
var page = {

  // Fired onload of a page
  onLoad : function(top){
    // Gets the gliding page actions ready 
    initPagenav();                   
    if(top) window.scrollTo(0, top);   
  },
  
  // Fired onsubmit of a page
  onSubmit : function(form){
    // only true if the page has submitHook() defined
    if(window.submitHook) submitHook(form);
  },
  
  // Submits a form with the passsed in parameters.  Used by language swap
  // and toggle funtions so that previously entered data isn't lost.
  formSubmit : function(action){
    var form = document.forms[0];
      
    if(form && form.submit && form.action && form.action.trim().length > 0){     
      // Append the passed in action and prevent the link's href from firing
      form.action += action;
      form.submit();    
      return false;
    }
    
    // default to the href attribute
    return true;
  } 
};


// Popup window
var newwindow;
function popupwin(url)
{
  // Set popup height/width depending on if viewing secure or unsecure content
  var width = 635;
  var height = 400;
  if(url.indexOf('unsecure')){
    width = 820;
    height = 550;
  } 

  newwindow=window.open(url,'epbsName','height='+height+',width='+width+',top=200,left=150,toolbar=1,resizable=1,menubar=1,scrollbars=1,status=1,location=1');
  if (window.focus) {
    newwindow.focus();
  }
}


// Extends javascript String obj to include trim function
String.prototype.trim = function (){
  return this.replace( /\s+$/g, "" );
};