// Javascript Utility library with functions for:
// --Check for valid email address format
// --Bookmark the parent page
// --Browser detection functions, version, shockwave/flash/java plug-ins
// --Platform detection for operating system

// NOTE: depends on activex.vbs for IE detection functions


/*
This functions handle flash and shockwave detection for Plugins and ActiveX controls.
For activeX detection the detection_axdetect.vbs script should also be included in the page.
*/
function detection_shockwaveVersion()
{
	return (browser_ie() && !browser_mac())
		? detection_shockwaveAxVersion()
		: detection_shockwaveNsVersion();
}

function detection_flashVersion()
{
	return (browser_ie() && !browser_mac())
		? detection_flashAxVersion()
		: detection_flashNsVersion();
}

function detection_shockwaveNsVersion()
{
  // this function returns a floating point value which should
  // be the version of the Shockwave plugin or 0.0 this function
  // only returns useful information if called from Netscape or
  // IE Mac 5.0+

  // Set these local variables to avoid the Netscape 4 crashing bug.
  var thearray = navigator.plugins
  var arraylen = thearray.length

  // Step through each plugin in the array.
  for (var i=0; i < arraylen; i++)
  {
    // Set these local variables to avoid the Netscape 4 crashing bug.
    var theplugin = thearray[i]
    var thename   = theplugin.name
    var thedesc   = theplugin.description

    // If the plugin is Shockwave...
    if (thename.indexOf("Shockwave") != -1 && thename.indexOf("Director") != -1)
    {
      var versionString = thedesc.substring(thedesc.indexOf("version ") + 8);

      if (versionString.indexOf(".") > 0)
      {
        var versionMajor = versionString.substring(0,versionString.indexOf("."));
        var versionMinor = versionString.substring(versionString.indexOf(".") + 1);

        if (versionMinor.indexOf(".") > 0)
        {
        	versionMinor = versionMinor.substring(0,versionString.indexOf("."))
        		+ versionMinor.substring(versionMinor.indexOf(".") + 1)
        }

        return parseFloat(versionMajor + "." + versionMinor);
      }

      else return parseFloat(versionString);
    }
  }

  return 0.0;
}

//***************************
// Netscape detection,
// returns the version of Shockwave plugin found or 0.0

function detection_flashNsVersion()
{
  // this function returns a floating point value which should be the version of the Shockwave plugin or 0.0
  // this function only returns useful information if called from Netscape or IE Mac 5.0+

  // Set these local variables to avoid the Netscape 4 crashing bug.
  var thearray = navigator.plugins
  var arraylen = thearray.length

  // Step through each plugin in the array.
  for (var i=0; i < arraylen; i++) {
    // Set these local variables to avoid the Netscape 4 crashing bug.
    theplugin = thearray[i]
    thename   = theplugin.name
    thedesc   = theplugin.description

    // If the plugin is Flash...
    if (thename.indexOf("Shockwave") != -1 && thename.indexOf("Flash") != -1)
    {
		var versionString = thedesc.substring(thedesc.indexOf("Flash ") + 6);

		// Look for an " r".  Whatever's after the "r" is the minor version. For
		// example, "Flash 4.0 r12" is minor release 12 of Flash 4.
		var versionLoc = versionString.indexOf(" r");

		if (versionLoc != -1)
		{
			// If there is an "r", then everything before the " r" is the major version...
			var versionMajor = versionString.substring(0,versionLoc);

			// ...and everything after is the minor version.
			var versionMinor = parseInt(versionString.substring(versionLoc + 2));

			// pad with zeroes
			if (versionMinor < 10) versionMajor += "0";

			// Format the final version string as x.xyy where x.x is the major version
			// and yy is the minor release version.

			return parseFloat(versionMajor + versionMinor);
		}
		else return parseFloat(versionString);
    }
  }

  return 0.0;
}

//***************************
// For detecting the ActiveX of shockwave for ie win.
// Requires a vbscript function be included on the page
// to do the actual checking returns version found or 0.0

function detection_shockwaveAxVersion()
{
	// This function returns a floating point value which should be the version
	// of the Shockwave control or 0.0 this function should only be called from
	// Internet Explorer for Windows loop backwards through the versions until
	// we get a bite
	for (var i=8;i>0;i--) {
		var versionString = VBGetShockwaveVersion(i);
		// if we get 1.0 we assume it is actually 6.0 or less
		if (versionString != "0.0") return (versionString == "1.0" ? 6.0 : parseFloat(versionString));
	}
	return 0.0;
}

function detection_flashAxVersion()
{
	// This function returns a floating point value which should
	// be the version of the Shockwave control or 0.0.
	// This function should only be called from Internet Explorer
	// for Windows.

    // loop backwards through the versions until we get a bite
	for (var i=8;i>0;i--)
	{
		var versionNum = VBGetFlashVersion(i);
		if (versionNum != 0)
		{
			var versionMajor = Math.floor(versionNum / 65536);
			var versionMinor = versionNum % 65536;
			var versionMiddle = ".";
			for (var j=100;i>5;i/=10)
            {
                if (versionMinor < j) versionMiddle += "0";
            }
			return parseFloat(versionMajor + versionMiddle + versionMinor);
		}
	}
	return 0.0;
}

// ****************************
// Java detection
function detection_JavaEnabled() {

    return navigator.javaEnabled();
}

// Check the supplied player list to make sure the user has the correct technology installed.
function detection_checkPlayer( playerlist )
{
    if (playerlist == null || playerlist == "" || playerlist == "null" )
    {
        return true;
    }

	var playerArray = new Array();

    // Split the playerList if it's separated by ampersands
    if ( playerlist.indexOf("&") > -1 )
    {
        playerArray = playerlist.split( "&" );
    }
    else
    {
        playerArray[0] = playerlist;
    }

    for (var i=0; i < playerArray.length; i++ )
    {
        var detect = 0;
		var player = playerArray[i].split( "-" );
		var type = player[0];
		var reqver = parseFloat(player[1]);

        // If type is shockwave and the required version is less than 8.5, set reqver to 8.5
        if ( type=="sw" && reqver < 8.5 )
        {
			reqver = 8.5;
		}

		if ( type == "f" )
        {
            detect = detection_flashVersion();
        }
		else if ( type == "sw" )
        {
            detect = detection_shockwaveVersion();
        }
		else if ( type == "java" )
        {
            return detection_JavaEnabled(); // No version needed for Java, return true/false
        }

        // If the detected version is greater than or equal to required version user can play this content
        if (detect >= reqver)
        {
            return true;
        }
	}

	return false;
}

// ************************************************************************
// Browser detection functions
function browser_ie()
{
	return (document.all && !window.opera) ? true : false;
}

function browser_ns4()
{
	return ((navigator.appName == "Netscape")
		&& (parseFloat(navigator.appVersion) >= 4)
		&& (navigator.userAgent.indexOf("Netscape6") == -1)) ? true : false;
}

function browser_ns6()
{
  return (document.getElementById && !document.all || window.opera) ?  true : false;
}

function browser_ns71()
{
	return (document.queryCommandSupported ? true : false);
}

function browser_ns() { return browser_ns4() || browser_ns6(); }

function browser_mac() { return (navigator.appVersion.indexOf("Macintosh") != -1); }

function browser_getVersion(inUserAgent)
{
	if (!inUserAgent) inUserAgent = navigator.userAgent;

	if (inUserAgent.indexOf("MSIE ") != -1)
		return parseFloat(inUserAgent.substring(inUserAgent.indexOf("MSIE ") + 5));
	if (inUserAgent.indexOf("Netscape6/") != -1)
		return parseFloat(inUserAgent.substring(inUserAgent.indexOf("Netscape6/") + 10));
	if (inUserAgent.indexOf("Mozilla/") != -1)
		return parseFloat(inUserAgent.substring(inUserAgent.indexOf("Mozilla/") + 8));

	return 0;
}

function browser_getPlatform(agent)
{
   if (!agent) { agent = navigator.userAgent; }
   var platform = 'unknown'
   if      (agent.indexOf('Win') != -1)   { platform = 'win'; }
   else if (agent.indexOf('Mac') != -1)   { platform = 'mac'; }
   else if (agent.indexOf('IRIX') != -1)  { platform = 'irix'; }
   else if (agent.indexOf('Linux') != -1) { platform = 'linux'; }
   else if (agent.indexOf('BSD') != -1)   { platform = 'bsd'; }
   return platform;
}


// Validates an email address to make sure it is in the correct format
function util_isValidEmail( str )
{
    var at="@";
    var dot=".";
    var comma = ",";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);

    // if they enter a comma, all bets are off cause this is not technically valid but we want to accept to allow for multiple emails
    if ( str.indexOf( comma ) > -1 )
    {
        return true;
    }

    if (str.indexOf(at)==-1)
    {
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    {
        return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    {
          return false;
    }

    if (str.indexOf(at,(lat+1))!=-1)
    {
          return false;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    {
          return false;
    }

    if (str.indexOf(dot,(lat+2))==-1)
    {
          return false;
    }

    if (str.indexOf(" ")!=-1)
    {
          return false;
    }

    return true;
}

function bookmark_parent() {
	var is_4up = parseInt(navigator.appVersion);
	var is_mac = navigator.userAgent.toLowerCase().indexOf("mac")!=-1;
	var is_ie = navigator.userAgent.toLowerCase().indexOf("msie")!=-1;
	var thePage = opener.location.href;
	(thePage.lastIndexOf('#')!=-1)
		thePage = thePage.substring(0,thePage.lastIndexOf('#'));
	if (is_ie && is_4up && !is_mac)
	{
		window.external.AddFavorite(thePage,opener.document.title);
	} else {
		if (window.sidebar) {
			// do nothing, this is FF bug which only saves Bookmarks to re-open in the sidebar :(
			//window.sidebar.addPanel(opener.document.title, thePage,"");
		} else if( document.all ) {
			window.external.AddFavorite( thePage, opener.document.title);
		} else if( window.opera && window.print ) {
			return true;
		}
	}
}