//*****************************************************************************************
//
// JavaScript File
//
// Name:         
// Description:  List of java script functions used by TopNav control.
//
//*****************************************************************************************
//
// FUNCTIONS
//
// Name:         DetectBrowser
// Description:  Check for plugins based on current browser of the user and 
//				 creates “FlashPlugIn” cookie that contains value that determines whether 
//				 plug-in exists or not in user browser.
// Accepts:      None
// Returns:      None
//
// Name:         DetectPlugin
// Description:  Checks and iterates through all Active X objects to determine if Flash 
//				 Object is installed in the browser.
// Accepts:      None
// Returns:      None
//
// Name:         IsFlashFile
// Description:  Checks if a file has a .swf extension
// Accepts:      flashFileURL
// Returns:      boolean - true if the file is a flash file
//						 - false if the file is not a flash file
//
// Name:         readTopNavCookie 
// Description:  Search for a cookie and returns its value
// Accepts:      name - the keyname of the cookie
// Returns:      CookieValue - the value of the cookie
//
// Name:         writeTopNavCookie 
// Description:  Creates a cookie and assigns value to it
// Accepts:      name - the keyname of the cookie
//               value - the value of the cookie
// Returns:      None
//
// Name:         setDisplay
// Description:  Sets the initial state of the page
// Accepts:      obj - the id of the section to set the visibiliy
// Returns:      style - can take the value ‘none’ or ‘’ to hide or show the 
//                       section respectively. 
//
//
//*****************************************************************************************
//
// REVISION HISTORY
//
// Date Created: 03/12/2007
// Author: EVC
//
// Revision Date     Author     Description 
// -------------     ------     -----------         
// 
//*****************************************************************************************

var detectableWithVB = false;
var flashFile;


function DetectFlashPlugin()
{
	//contains value whether the plug-in exists in user browser
	var pluginFound = false;
	
	//retrieve the value of the FlashPlugin cookie
	var flashPlugInCookie =  readTopNavCookie('FlashPlugIn');
	
	//assign flash url to the a global variable
	flashFile = flashFileURL;
	
	//if cookie does not exist, create the cookie
	if (flashPlugInCookie == ''){
	
		//use the DetectPlugin function for Flash plugins
		pluginFound = DetectPlugin('Shockwave','Flash'); 
		
		// if not found, try to detect with VisualBasic
		if(!pluginFound && detectableWithVB)
		{			
			pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');		
		}		
		
		//save the value of the pluginFound variable to a cookie
		writeTopNavCookie('FlashPlugIn',pluginFound);		
	}
	else{				
		//if value of cookie is true, then set pluginFound value to true
		if (flashPlugInCookie == 'true') pluginFound = true;
	}
	
	//if plugin is found, display the section with flash and hide the section without flash.
	if(pluginFound && IsFlashFile(flashFile))
	{	
		setDisplay(getCWSObj('TopNavWithFlash'),'');
		setDisplay(getCWSObj('TopNavWithoutFlash'),'none');		
		setDisplay(getCWSObj('flashSpacer'),'');				
	}
	else{	
		setDisplay(getCWSObj('TopNavWithFlash'),'none');
		setDisplay(getCWSObj('TopNavWithoutFlash'),'');		
		setDisplay(getCWSObj('flashSpacer'),'none');				
	}		
}


function DetectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = DetectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) 
    {
		var pluginsArrayLength = navigator.plugins.length;
		// iterate plugins
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ )
		 {
			// loop through all desired names and check each against the current plugin name
			var numFound = 0;
			for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) 
			{
				// if desired plugin name is found in either plugin name or description
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
				(navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) 
				{
					// this name was found
					numFound++;
				}   
			}
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length)
			 {
				pluginFound = true;				
				break;
			 }
		}
    }
    return pluginFound;
}

function IsFlashFile(flashFileURL)
{
	var flashURL = flashFileURL.toLowerCase();
	
	if(flashURL.indexOf('.swf') != -1){
		return true;
	}
	else{
		return false;
	}

}

function readTopNavCookie(name)
{
  var cookieValue = '';
  var search = name + '=';
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(';', offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeTopNavCookie(name, value)
{
  try
  {
	document.cookie = name + '=' + escape(value) + '; path=/';
  }
  catch(Err)
  {}
}



function getCWSObj(id) 
{
	if (document.getElementById) {
		return document.getElementById(id);
		}
	else if (document.all) {
		getCWSObj = document.all[id];
		}
}

function setDisplay(obj,style)
{
	obj.style.display=style;
}

//OnLoad of JavaScript 
// VBScript to detect plugins for IE browsers
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');    

    document.writeln('</scr' + 'ipt>');
}
