///***************************************************///
/// General browser functions
///***************************************************///

//******************************************************************************************
//The follwing code snipet (between the astrixes was taken from:
//http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html

// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();


// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);


var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie7  = (is_ie && (agt.indexOf("msie 7.0") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);

//******************************************************************************************


///
/// Purpose: returns the browser version
///
function browserVersion()
{
    var versionString = "";
    if (window.navigator.userAgent.indexOf("MSIE ") > 0)
    {
        versionString = "MSIE";
    }
    else if (window.navigator.userAgent.toLowerCase().indexOf("gecko") > 0) 
    {
        versionString = "gecko"
    }
    else
    {
        versionString = "other";
    }
    return versionString;
}

///
/// Purpose: get the height of the client area of the browser
///
function getWindowHeight() 
{
  var windowHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) 
  //Non-IE...
  {
    windowHeight = window.innerHeight;
  } 
  else if( document.documentElement && document.documentElement.clientHeight) 
  //IE 6+ in 'standards compliant mode'
  {
    windowHeight = document.documentElement.clientHeight;
  } 
  else if( document.body && document.body.clientHeight ) 
  //IE 4 compatible
  {
    windowHeight = document.body.clientHeight;
  }
  return windowHeight;
}

///
/// Purpose: get the width of the client area of the browser
///
function getWindowWidth() 
{
  var windowWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  //Non-IE...
  {
    windowWidth = window.innerWidth;
  } 
  else if( document.documentElement && document.documentElement.clientWidth) 
  //IE 6+ in 'standards compliant mode'
  {
    windowWidth = document.documentElement.clientWidth;
  } 
  else if( document.body && document.body.clientWidth ) 
  //IE 4 compatible
  {
    windowWidth = document.body.clientWidth;
  }
  return windowWidth;
}

///
/// Purpose: returns a CSS rule that corresponds to the selector text used to declare/define the rule in a loaded stylesheet
/// Note: selectorString would take on the form of "TR.MyClassName1 TD.MyClassName2" 
///
function getCSSRule(selectorString)
{
    var currentStyleSheet;
    var cssRule;

    if (!document.styleSheets) return;

    for (styleSheetsCounter=0; styleSheetsCounter<document.styleSheets.length; styleSheetsCounter++)
    {   
        currentStyleSheet = document.styleSheets[styleSheetsCounter];
        var stylesheetRules = new Array(); 

        //get the collection of rules from the stylesheet
        if (currentStyleSheet.cssRules)  // Standards Compliant
        {
           stylesheetRules = currentStyleSheet.cssRules;
        }
        else
        {         
           stylesheetRules = currentStyleSheet.rules;  // IE 
        }

        //loop through the rules to find the one that you're looking for
        for (ruleCounter=0; ruleCounter<stylesheetRules.length; ruleCounter++)
        {
            //We need to convert the strings to lower case for comparison purposes because the actual selectors in the rule will always have
            //all HTML tag references in full uppercase regardless of how the actual selector was declared in the stylesheet.  (The class names
            //and IDs seem to remain as they were declared in the stylesheet.)  This is confusing and people forget to uppercase the tags 
            //when passing in the selectorString parameter.
            if (stylesheetRules[ruleCounter].selectorText.toLowerCase()==selectorString.toLowerCase())
            {
                cssRule = stylesheetRules[ruleCounter];
            }
        }
    }
    
    return cssRule;
}
