// initialize global variables
var detectableWithVB = false;
var pluginFound = false;


function goURL(daURL) {
    // if the browser can do it, use replace to preserve back button
    if(javascriptVersion1_1) {
	window.location.replace(daURL);
    } else {
	window.location = daURL;
    }
    return;
}

function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
    // check for redirection
    if( redirectURL && ((pluginFound && redirectIfFound) || 
	(!pluginFound && !redirectIfFound)) ) {
	// go away
	goURL(redirectURL);
	return pluginFound;
    } else {
	// stay here and return result of plugin detection
	return pluginFound;
    }	
}

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}

function detectFlash(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectDirector(redirectURL, redirectIfFound) { 
    pluginFound = detectPlugin('Shockwave','Director'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
    }
    // check for redirection
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectQuickTime(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectReal(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectWindowsMedia(redirectURL, redirectIfFound) {
    pluginFound = detectPlugin('Windows Media Player');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

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;
	// for each plugin...
	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++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin


// Here we write out the VBScript block for MSIE Windows
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('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}

/* -----------------------------------------------------------------
  Cookies functions for first time visit
 ----------------------------------------------------------------- */

function getCookie(name) {
    var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie(name,value,expires,path,domain,secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function deleteCookie(name,path,domain) {
    if (getCookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
		";expires=";
}


function isCookieEnabled() {
   if (document.all) return navigator.cookieEnabled;
   setCookie('testcookie',today.getTime());
   var tc = getCookie('testcookie');
   deleteCookie('testcookie');
   return (tc == today.getTime());
}


/* -----------------------------------------------------------------
  function for brower redirection
 ----------------------------------------------------------------- */

function redir(bro) {
if (confirm("Your browser version is out to date. Do you want to upgrade it now for a better user experience? (Highly Recommended)")) {

	var redirectURL = "/vgn-ext-templating/v/index.jsp?vgnextoid=99aa6e404a7f1010VgnVCM1000001da9c80aRCRD&check="+bro;
	if (document.images) window.location.replace(redirectURL); // if Netscape
		else window.location = redirectURL; // else if not Netscape

} else {
//alert ("Go straight to the site without browser detection");
}

	
}

function browserUpgrade(){
	setCookie('alreadyVisitedOnce','yes');
	var NS, IE, aaa, OP, Moz, Saf, CW, PDA, LX;
	var NSVer, IEVer, OPVer, MozVer;

	NS = (navigator.appName == "Netscape") && (navigator.vendor != "Firefox");
	IE = navigator.appVersion.indexOf("MSIE")!=-1;
	OP = navigator.userAgent.indexOf("Opera") > -1;
	Moz = navigator.vendor == "Firefox";
	Saf = navigator.platform == "MacPPC";
	CW = navigator.userAgent.indexOf("DigExt") > -1; //Is the browser a Carphone Warehouse?
	PDA = navigator.userAgent.indexOf("PPC") > -1; // Is the device a PDA?
	LX = navigator.platform.indexOf("Linux") > -1; //Is the OS platform Linux?
	// OD = navigator.userAgent == null; // OD = Old Devices
  
	if (NS) {
		if (navigator.vendorSub == null) {
		navvensub = navigator.appVersion;
		NSVer = navvensub.substr(navvensub.indexOf(" ").length, 1);
		NSVer = parseFloat(NSVer);
		} else {
		// using vendorSub object, as appVersion comes out as 5.0 (from Mozilla/5) also if it is NS 6 or NS 7 !!!
		navvensub = navigator.vendorSub;
		NSVer = navvensub.substr(navvensub.indexOf(".").length, 1);
		NSVer = parseFloat(NSVer);
			}
		}
		
	if (Moz) {
		//MozVer = parseFloat(navigator.vendorSub);
		/*this used to be fine previously. FireFox uses different ways to define the vendorSub dependin on the release.
		FireFox .93	  = 0.9.3
		FireFox 1.0PR = 0.1
		FireFox .93   = 1.0.3
		*/
		MozVer = navigator.vendorSub.split(".");
			if (MozVer[2] == null) {
			MozVer = MozVer[0]+MozVer[1];
			} else {
			MozVer = MozVer[0]+MozVer[1]+MozVer[2];
			}
		}
		
	else if (IE) {
		navag = navigator.userAgent;
		IEVer = navag.substr(navag.indexOf("MSIE ")+("MSIE ").length, 4);
		IEVer = parseFloat(IEVer); 
		    
		if (OP) {
			OPVer = navag.substr(navag.indexOf("Opera ")+("Opera ").length, 4);
			OPVer = parseFloat(OPVer);
			}
		}
  
	if ((NS) && !(Saf) && !(LX) && (NSVer < 7)) {
			redir("Netscape");
	}
	
	if (Moz) {// in case browser is Mozilla Firefox
			if (MozVer<103) { // and navigator.vendorSub is less than 1.0.3
			redir("Mozilla");
			}
	}
	
	else if ((IE) && !(CW) && !(PDA) && !(LX)) {
		if ((OP) && (OPVer < 8)) {
				redir("Opera");
		} 
		else if(IEVer <= 5) {  
				redir("IE");
			}
		}
}

/* -----------------------------------------------------------------
  function for Expand/Contract blocks
 ----------------------------------------------------------------- */

function display_flip(i,p) {
			el=document.getElementById(p)
			c = el.className = (el.className=="displayNone") ? "displayBlock" : "displayNone"

			if (c=="displayNone") {
				i.src=i.src.replace("contract","expand")
				i.setAttribute("alt","expand")
			} else {
				i.src=i.src.replace("expand","contract")
				i.setAttribute("alt","contract")
			}
		}
		
		
		
		
/* -----------------------------------------------------------------
  function for Flash Movies and alternative imagery
 ----------------------------------------------------------------- */
		
		// FLASH MOVIE
			var MM_contentVersion = 7;
			var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
			if ( plugin ) {
					var words = navigator.plugins["Shockwave Flash"].description.split(" ");
				    for (var i = 0; i < words.length; ++i)
				    {
					if (isNaN(parseInt(words[i])))
					continue;
					var MM_PluginVersion = words[i]; 
				    }
				var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
			}
			else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
			   && (navigator.appVersion.indexOf("Win") != -1)) {
				document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
				document.write('on error resume next \n');
				document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
				document.write('</SCR' + 'IPT\> \n');
			}
			
			
function flashMovie(version, filepath, width, height, bgcolor, quality, alternativeText, alternativeImage, altURL, altTitle, altTarget){
if ( MM_FlashCanPlay ) {
	var swf = '';
		swf += '<object';
		swf += ' width="'+width+'"';
		swf += ' height="'+height+'"';
		swf += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
		swf += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+',0,0,0">';
		swf += '<param name="movie" value="'+filepath+'">';
		swf += '<param name="wmode" value="transparent">';
		swf += '<param name="bgcolor" value="'+bgcolor+'">';
		swf += '<param name="quality" value="'+quality+'">';
			swf += '<embed src="'+filepath+'"';
			swf += ' width="'+width+'"';
			swf += ' height="'+height+'"';
			swf += ' wmode="transparent"';
			swf += ' bgcolor="'+bgcolor+'"';
			swf += ' quality="'+quality+'"';
			swf += ' type="application/x-shockwave-flash"';
			swf += ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
			swf += '<\/embed>';
		swf += '<\/object>';
		document.write(swf);

	}
	else{
	// image to replace flash movie
/*		//document.write('<div class="topBgImg"><img src="'+alternativeImage+'"></div>');
			if ((altURL=="") || (altURL==" ") || (altURL=="#")) {
				document.write("<img src=\""+alternativeImage+"\" width=\""+width+"\" height=\""+height+"\" alt=\""+alternativeText+"\" />");
			} else {
				document.write("<a href=\""+altURL+"\" title=\""+altTitle+"\" target=\""+altTarget+"\" />");
				document.write("<img src=\""+alternativeImage+"\" width=\""+width+"\" height=\""+height+"\" alt=\""+alternativeText+"\" />");
				document.write("</a>");
			}
		}
	*/
			// alternative content for flash movie
		document.write('<div style="font-family:verdana;color:#fff;font-size:1em;">'+ alternativeText +'</div>');		
	}		
}