/*************************************************************************
**
** Filename:  BrowserDetect.js
**
** File Description:
**    Detects the browser version and alerts the user if it is not
**    supported by this version of the Test Suite.
**
** Author: ADLI Project
**
** Company Name: Concurrent Technologies Corporation
**
** Module/Package Name:  none
** Module/Package Description: none
**
** Design Issues:
**
** Implementation Issues:
** Known Problems:
** Side Effects:
**
** References: ADL SCORM
**
***************************************************************************
**
** Advanced Distributed Learning Co-Laboratory (ADL Co-Lab) grants you
** ("Licensee") a non-exclusive, royalty free, license to use, and
** redistribute this software, provided that i) this copyright notice and
** license appear on all copies of the software; and ii) Licensee does not
** utilize the software in a manner which is disparaging to ADL Co-Lab.
**
** This software is provided "AS IS," without a warranty of any kind.  ALL
** EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
** ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
** OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  ADL Co-Lab AND ITS LICENSORS
** SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
** USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES.  IN NO
** EVENT WILL ADL Co-Lab OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
** PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
** INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
** THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
** SOFTWARE, EVEN IF ADL Co-Lab HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
** DAMAGES.
**
**************************************************************************/

    var Netscape = false;
    var IE = false;
    var browserName = null;
    var browserVersion = 0;
    var fullVersion = 0;
    
/*******************************************************************************
**
** Function: DetectBrowser()
** Inputs:  None
** Return:  None
**
** Description:
**    Determines the browser information, i.e. name and version.
**    
**    This function can detect the following browsers:
**
**    o Internet Explorer
**    o Netscape
**    
**    Note: If the browser is not recognised, empty strings are returned.
**
*******************************************************************************/
    function DetectBrowser()
    {   
        var browser = navigator.userAgent.toLowerCase();
                       
        if (browser.indexOf("netscape") > -1)
        {
            browserName = "Netscape";
            browserVersion = ExtractFullVersion(browser, "netscape");
            Netscape = true;
        }       
        else if (browser.indexOf("msie") > -1)
        {   
            browserName = "Internet Explorer";
            fullVersion = ExtractFullVersion(browser, "msie");
            browserVersion = ExtractShortVersion(fullVersion);
            IE = true;
        }       
        else
        {
            browserName = "";
            browserVersion = "";
        }
    }

/*******************************************************************************
**
** Function: DetectUnsupportedBrowser()
** Inputs:  None
** Return:  true/false
**
** Description:
**    Alerts the user if they are using a browser not supported by the current
**    version of the Test Suite.
**
*******************************************************************************/
	function DetectUnsupportedBrowser()
	{
	   DetectBrowser();
	   
	   // see if the browser and version are supported
	   if( ( (!IE) && (!Netscape) ) || 
	       ( (IE) && (navigator.userAgent.indexOf("Opera") > -1) ) ||
	       ( (IE) && (browserVersion < 6) ) ||
	       ( (Netscape) && (browserVersion < 7) ) )
	   {
	      alert("Browser not tested.");
	      return false;
	   }
	   else
	   {
	      return true;
	   }
	}    

/*******************************************************************************
**
** Function: ExtractShortVersion()
** Inputs:  version
** Return:  number
**
** Description:
**    Extracts a number from a version, e.g. "1.2.3.4" => 1.2.
**
*******************************************************************************/
    function ExtractShortVersion(version)
    {
        var number = "0";
        var numberList = new Array();
        
        if (version != "")
        {
            numberList = version.split(".");
            if (numberList.length > 1)
            {
                 number = numberList[0] + "." + numberList[1]; 
            }
            else
            {
                number = numberList[0]; 
            }
        }
        return number;
    }

/*******************************************************************************
**
** Function: ExtractFullVersion()
** Inputs:  source, pattern
** Return:  version
**
** Description:
**    Extracts the first version number after the specified pattern
**    in the specified source.  A version number is of the format
**    x[.y]*, hence 1.2 and 1.2.3 are both valid version numbers.
**
*******************************************************************************/
    function ExtractFullVersion(source, pattern)
    {
        var character;
        var found;
        var index = source.indexOf(pattern);
        var version = "";               
        if (index > 0)
        {
            found = false;
            index += pattern.length;
                while (index <= source.length)
                {
                    character = source.substr(index, 1);
                    if(((character >= "0") && (character <= "9")) || (character == "."))
                    {
                        version += character;
                    }
                    index++;
                }
        }
        return version;
    }

