<!--

/************************************************************************/
/* browser.js                                                           */
/*----------------------------------------------------------------------*/
/* Functions that interact with the browser and its environment.        */
/*----------------------------------------------------------------------*/
/* File created 30Apr08 acurtis                                         */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// Enum of browser types
var eBT_Unknown  = 0;
var eBT_IE       = 1;
var eBT_Firefox  = 2;
var eBT_Netscape = 3;
var eBT_Opera    = 4;
var eBT_Safari   = 5;

// Enum of browser operating systems
var eOS_Unknown = 0;
var eOS_Windows = 1;
var eOS_Mac = 2;
var eOS_Linux = 3;
var eOS_Unix = 4;
var eOS_Series60 = 5;

// Instantiate the browser object
var gBrowser = new Browser();

// Does this string contain a dot?
function hasDot(input) 
{
    return (input.search(/\./) == -1)
}

// Is input empty?
function isEmpty(input) 
{
    return (input==null || input =="")
}

// Browser object manages interaction with browser environment
function Browser()
{
  this.version = 0;
  this.majorVer = -1;
  this.type = eBT_Unknown;
  this.os = eOS_Unknown;
  this.screenw = screen.width;
  this.screenh = screen.height;

  var user_agent = navigator.userAgent.toLowerCase();

  // Assume Opera is the only browser that mentions Opera in its userAgent string
  if( user_agent.indexOf('opera') !=-1 )
  {
    this.type = eBT_Opera;
    if( user_agent.match(/^opera[\/ ]([0-9\.]+)/) )
    {
      var ver = RegExp.$1;
	if(ver != 9.80)
	{
      		this.version = ver;
	}
	else
	{
		if( user_agent.match(/version[\/ ]([0-9][0-9\.]+)/) )
    		{
			this.version = RegExp.$1;	
		}
	}
    }
  }
  else if( user_agent.indexOf('safari') !=-1 )
  {
    this.type = eBT_Safari;
    if( user_agent.match(/safari\/([0-9\.]+)$/) )
    {
      this.version = RegExp.$1;
    }
  }
  else if( user_agent.indexOf('navigator') !=-1 )
  {
    this.type = eBT_Netscape;
    if( user_agent.match(/navigator\/([0-9\.]+)$/) )
    {
      this.version = RegExp.$1;
    }
  }
  else if( user_agent.indexOf('firefox') !=-1 )
  {
    this.type = eBT_Firefox;
    if( user_agent.match(/firefox\/([0-9\.]+)$/) )
    {
      this.version = RegExp.$1;
    }
  }
  else if( user_agent.indexOf('msie') !=-1 )
  {
    this.type = eBT_IE;
    if( user_agent.match(/msie ([0-9\.]+)/) )
    {
      this.version = RegExp.$1;
    }
  }
  var ver = this.version;
  this.majorVer = (isEmpty(ver) ? -1 : (hasDot(ver) ? ver : ver.match(/(\d*)(\.\d*)*/)[1]));

  var app_version = navigator.appVersion.toLowerCase();
  if( app_version.indexOf("series60") != -1)
  {
    this.os = eOS_Series60;
  }
  else if( app_version.indexOf("linux") != -1)
  {
    this.os = eOS_Linux;
  }
  else if( app_version.indexOf("x11") != -1)
  {
    this.os = eOS_Unix;
  }
  else if( app_version.indexOf("windows") != -1)
  {
    this.os = eOS_Windows;
  }
  else if( app_version.indexOf("mac") != -1)
  {
    this.os = eOS_Mac;
  }

}

// Converts browser type enum to string, useful for debug messages.
Browser.prototype.TypeString = function()
{
  switch(this.type)
  {
    case eBT_Unknown:
      return "Unknown";
      break;
    case eBT_IE:
      return "IE";
      break;
    case eBT_Firefox:
      return "Firefox";
      break;
    case eBT_Netscape:
      return "Netscape";
      break;
    case eBT_Opera:
      return "Opera";
      break;
    case eBT_Safari:
      return "Safari";
      break;
    }
    return "Undefined";
}

// Converts browser os enum to string, useful for debug messages.
Browser.prototype.OsString = function()
{
  switch(this.os)
  {
    case eOS_Unknown:
      return "Unknown";
      break;
    case eOS_Windows:
      return "Windows";
      break;
    case eOS_Mac:
      return "Mac";
      break;
    case eOS_Linux:
      return "Linux";
      break;
    case eOS_Unix:
      return "Unix";
      break;
    case eOS_Series60:
      return "Nokia S60";
      break;
    }
    return "Undefined";
}
// Gets the value of the named cookie, or null if not available.
Browser.prototype.GetCookie = function( name )
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while( i < clen )
  {
    var j = i + alen;
    if( document.cookie.substring( i, j ) == arg )
    {
      var endstr = document.cookie.indexOf (";", j);
      if( endstr == -1 )
      {
        endstr = document.cookie.length;
      }
      return unescape( document.cookie.substring( j, endstr ) );
    }
    i = document.cookie.indexOf( " ", i ) + 1;
    if( i == 0 )
    {
      break;
    }
  }
  return null;
}

// Deletes the named cookie
// Calls GetCookie to check if the cookie exists
Browser.prototype.DeleteCookie = function( name )
{
  if( this.GetCookie( name ) )
  {
    document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Sets the actual value of the cookie, escaped.
Browser.prototype.SetCookie = function( name, value )
{
  var today = new Date();
  var expires = new Date( today.getTime() + 365 * 24 * 60 * 60 * 1000 );

  document.cookie = name + "=" + escape( value ) + "; expires=" + expires.toGMTString();
}

Browser.prototype.NoteClientArea = function()
{
  var canv = null;
  if (!window.opera && document.all && typeof document.body.clientWidth != "undefined")
  {
     var cm = document.compatMode && document.compatMode == "CSS1Compat";
     canv = cm ? document.documentElement : document.body;
  }

  if( canv )
  {
     this.width = canv.clientWidth;
     this.height = canv.clientHeight;
  }
  else
  {
     this.width = window.innerWidth - 18;
     this.height = window.innerHeight - 18;
  }
/*
         getScrollX: function ()
         {
            var canv;
            if (canv = this.getIECanvas())
               this.scrollX = canv.scrollLeft;
            else if (window.pageXOffset)
               this.scrollX = window.pageXOffset;
            else if (window.scrollX)
               this.scrollX = window.scrollX;
            else
               this.scrollX = 0;
         },

         getScrollY: function ()
         {
            var canv;
            if (canv = this.getIECanvas())
               this.scrollY = canv.scrollTop;
            else if (window.pageYOffset)
               this.scrollY = window.pageYOffset;
            else if (window.scrollY)
               this.scrollY = window.scrollY;
            else
               this.scrollY = 0;
         },    */

  var clientarea = this.width + "x" + this.height;
  this.SetCookie('clientarea',clientarea);

  this.serverwidth = SERVER_WIDTH;
  this.serverheight = SERVER_HEIGHT;
}

Browser.prototype.IsTiny = function()
{
  return (this.serverwidth && this.serverwidth < 640);
}

//-->

