// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirebird    = (ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   
   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);
   
   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);
   
   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);

   /**
    * getPageWidth
    *
    * returns the width of the browser window or the
    * width of the content; whichever is greater.
    *
    */
   this.getPageWidth = function() {
      if( this.getScrollWidth() < this.getWindowWidth() ){	
         return this.getWindowWidth();
      } else {
         return this.getScrollWidth();
      }
   }

   /**
    * getScrollWidth
    *
    * returns the width of the content.
    *
    */
   this.getScrollWidth = function() {
      if ( window.innerHeight && window.scrollMaxY )
      {
         return document.body.scrollWidth;
      }
      else if ( document.body.scrollHeight > document.body.offsetHeight )
      {
         return document.body.scrollWidth;
      }
      else {
         return document.body.offsetWidth;
      }
   }

   /**
    * getWindowWidth
    *
    * returns the width of the browser window.
    *
    */
   this.getWindowWidth = function() {
      if ( self.innerHeight )
      {
         return self.innerWidth;
      }
      else if ( document.documentElement && document.documentElement.clientHeight )
      {
         return document.documentElement.clientWidth;
      }
      else if ( document.body )
      {
         return document.body.clientWidth;
      }	
   }

   /**
    * getPageHeight
    *
    * returns the height of the browser window or the
    * height of the content; whichever is greater.
    *
    */
   this.getPageHeight = function() {
      if ( this.getScrollHeight() < this.getWindowHeight() )
      {
         return this.getWindowHeight();
      }
      else {
         return this.getScrollHeight();
      }
   }

   /**
    * getScrollHeight
    *
    * returns the height of the content.
    *
    */
   this.getScrollHeight = function() {
      if ( window.innerHeight && window.scrollMaxY )
      {
         return window.innerHeight + window.scrollMaxY;
      }
      else if ( document.body.scrollHeight > document.body.offsetHeight )
      {
         return document.body.scrollHeight;
      }
      else {
         return document.body.offsetHeight;
      }
   }

   /**
    * getWindowHeight
    *
    * returns the height of the browser window.
    *
    */
   this.getWindowHeight = function() {
      if ( self.innerHeight )
      {
         return self.innerHeight;
      }
      else if ( document.documentElement && document.documentElement.clientHeight )
      {
         return document.documentElement.clientHeight;
      }
      else if ( document.body )
      {
         return document.body.clientHeight;
      }	
   }

   /**
    * changeAttribute
    *
    * browser safe implementation for altering attributes of elements
    * within the html: show or hide layers; alter width or height; change
    * other style attributes, etc.
    *
    */
   this.changeAttribute = function( elementName, attributeName, newValue ) {
      var element = this.findElement( elementName );
      var isAttributeNotFound = false;

      if ( element != null ) {
         if ( element[ attributeName ] != null )
         {
            element[ attributeName ] = newValue;
            return true;
         }
         else if ( element.style )
         {
            if ( element.style[ attributeName ] != null )
            {
               element.style[ attributeName ] = newValue;
               return true;
            }
            else {
               isAttributeNotFound = true;
            }
         }
      }

      if ( isAttributeNotFound ) {
         // alert( "Attribute not found..." );
      }

      return false;
   }

   /**
    * findElement
    *
    * while this may be used to grab a reference to an element, it is
    * primarily used by the changeAttribute function to attain the reference
    * to the element of which it will alter properties.
    *
    */
   this.findElement = function( elementName ) {
      var isItemNotFound = false;
      
      if ( document.getElementById )
      {
         if ( document.getElementById( elementName ) )
         {
            return document.getElementById( elementName );
         }
         else {
            isItemNotFound = true;
         }
      }
      else if ( document.all )
      {
         if ( document.all[ elementName ] )
         {
            return document.all[ elementName ];
         }
         else {
            isItemNotFound = true;
         }
      }
      else if ( document.layers )
      {
         if ( document.layers[ elementName ] )
         {
            return document.layers[ elementName ];
         }
         else {
            isItemNotFound = true;
         }
      }

      if ( isItemNotFound ) {
         // alert( "Item not found..." );
      }
      return null;
   }
   
   
   /**
    * sizeIFrame
    *
    * very handy function for changing the size of an iframe.
    *
    */
   this.sizeIFrame = function( name, width, height ) {
      this.changeAttribute( name, "height", height );
      this.changeAttribute( name, "width", width );
   }
   
   /**
    * getUrl
    *
    * returns the entire url as a string.
    *
    */
   this.getUrl = function() {
      return new String( document.location );
   }

   /**
    * getDomain
    *
    * returns the domain portion of the url:
    * WWW.THECURRENTURL.COM
    *
    * @return 
    *
    */
   this.getDomain = function() {
      var url = this.getUrl();

      var start = url.indexOf( '//' ) + 2;
      var end   = url.indexOf( '/', start );

      return url.substring( start, end );
   }

   /**
    * getCurrentPath
    *
    * returns the current url to the directory:
    * WWW.THECURRENTURL.COM/DIRECTORY/SUBDIRECTORY
    *
    * @return String path to directory
    *
    */
   this.getCurrentPath = function() {
      var url = this.getUrl();

      var end = url.lastIndexOf( '/' );

      return url.substring( 0, end );
   }

   /**
    * getCurrentFile
    *
    * returns the file of the page being displayed:
    * THEFILE.HTML
    *
    * @return String filename
    *
    */
   this.getCurrentFile = function() {
      var url = this.getUrl();

      var start = url.lastIndexOf( '/' ) + 1;
      var end   = url.indexOf( '?' );

      if ( end == -1 ) {
         end = url.length + 1;
      }

      return url.substring( start, end );
   }

   /**
    * getParams
    *
    * returns the query string of the current request:
    * NAME=VALUE&NAME2=VALUE2&NAME3=VALUE3
    *
    * @return String query string part of url
    *
    */
   this.getParams = function() {
      var url = this.getUrl();

      start = url.indexOf( '?' ) + 1;

      if ( start == -1 ) {
         return "";
      }

      return url.substring( start );
   }

   /**
    * getParamArray
    *
    * returns the query string as an array of:
    * NAME=VALUE
    *
    * @return Array query string name=value pairs
    *
    */
   this.getParamArray = function() {
      var params = this.getParams();

      if ( params.indexOf( '&' ) > -1 ) {
         return params.split( '&' );
      }

      return new Array();
   }

   /**
    * getParamObject
    *
    * returns and object containing query string data:
    * "NAME" => "VALUE"
    *
    * @return Object query string object
    *
    */
   this.getParamObject = function() {
      var params = this.getParamArray();
      var queryObj = new Object();
      var tmpArray = new Array();

      for ( var i = 0; i < params.length; i++ ) {
         tmpArray = params[ i ].split( '=' );
         queryObj[ tmpArray[ 0 ] ] = tmpArray[ 1 ];
      }

      return queryObj;
   }

   /**
    * getParameter
    *
    * returns the value of the supplied parameter
    *
    * @argument String paramName name of the parameter to be returned
    * @return   String value
    *
    */
   this.getParameter = function( paramName ) {
      var queryObj = this.getParamObject();

      if ( queryObj[ paramName ] != null ) {
         return queryObj[ paramName ];
      }

      return "";
   }

   this.draw = function( elementName, strHTML ) {
      var layerRef = this.findElement( elementName );

      if ( layerRef != null ) {
         layerRef.innerHTML = strHTML;
      }
   }

   this.show = function( layerId ) {
      this.changeAttribute( layerId, 'visibility', 'visible' );
   }

   this.hide = function( layerId ) {
      this.changeAttribute( layerId, 'visibility', 'hidden' );
   }

   this.changeStyle = function( layerId, newClass ) {
      this.changeAttribute( layerId, 'class', newClass );
      this.changeAttribute( layerId, 'className', newClass );
   }
   
   this.hideSelects = function()
   {
      // Hide select boxes as they will 'peek' through the image in IE
      var selects = new Array();

      selects = document.getElementsByTagName("select");
      
      for (i = 0; i < selects.length; i++) {
         selects[ i ].style.visibility = "hidden";
      }
   }

   this.showSelects = function()
   {
      // make select boxes visible
	  var selects = new Array();
      
      selects = document.getElementsByTagName("select");
      
      for (i = 0; i < selects.length; i++) {
         selects[ i ].style.visibility = "visible";
	  }
   }
}

var browser = new BrowserDetect();

/**
 * TEST
 *
 * /
   alert( "url: " + browser.getUrl()
    + "\ndomain: " + browser.getDomain()
    + "\ncurrent path: " + browser.getCurrentPath()
    + "\ncurrent file: " + browser.getCurrentFile()
    + "\nparams: " + browser.getParams()
    + "\npremium: " + browser.getParameter( 'premium' ) );
*/

function safariCheck()
{
	if (browser.isSafari == true)
	{
		document.getElementById('searchInput').style.display="none";
		document.getElementById('searchInputSafari').style.display="block";
	}
}

/*
 * Function to open popup window
 * @param url
 *     The url to open - string
 * @param win_width
 *     The window width - int
 * @param win_height
 *     The window height - int
*/
function openWin(url, win_width, win_height) {
		
	win_left = (screen.width - win_width) / 2;
	win_top = (screen.height - win_height) / 2;
	
	window.open(url , '', 'width='+win_width+', height='+win_height+', left='+win_left+', top='+win_top+', resizable=1, scrollbars=1');
}

function openDialog(url, win_width, win_height, id) {
  window.open(url, id, 'width='+win_width+', height='+win_height+',toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1');
}
