/*
	bpflash_embed.js
	
	Implementation of JavaScript functions useful for embedding the
	BioPassword Flash client control in a web page.
	
	The bpInsertFlash function embeds the Flash client on a web page. In order
	to bypass the 'Press SPACEBAR or ENTER to activate and use this control'
	message in the updated IE version, this function must exist in a seperate
	".js" file.
	
	To use functions found in this file, you should include the following line
	somewhere in the '<head>' portion of your HTML file:
	
	<head>
		<script language="vbScript" type="text/vbscript" src="flash/bpflash_IEVersionCheck.vbs"></script>
		<script language="JavaScript" type="text/javascript" src="flash/bpflash_embed.js"></script>
	</head>
	
	(Note that the path to this file may need to be modified in the 'src' value)
	
	Usage example:
	...
	<body onload='bpSetFocus("BPCitrixEnroll")'>
		...
		<script language="JavaScript" type="text/javascript">
		<!--
			bpInsertFlash("BPCitrixEnroll", "flash/BPCitrixEnrollment.swf", "370", "270", "#ffffff",
				"Domain=domainf&SubmitPage=bpenroll.aspx&FlashType=Enrollment&"
				+ "UserName=134795f22f683f667c6772c4de4a93d9&Password=134795f22f683f667c6772c4de4a93d9&"
				+ "EncryptKey=451fe8f60f84573792ab26ccfba85ea346d4307148e61d7da2d9cf8b1c4474f3&Samples=9");
		//-->
		</script>
		...
	</body>
	...
*/

/*
//    Flash detection helper functions...    
*/


function MsgUpgradeFlash()
{
	alert('Please upgrade the Flash Player to version 7.0 or later!');
}
 
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 7;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// the version of javascript supported
var jsVersion = 1.0;
// -----------------------------------------------------------------------------


// Detect Client Browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;

// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
}
 
// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 7r14 returns 7.14
// If called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
// *** NOTE This function REQUIRES the prior inclusion/declaration of the vbscript function VBGetSwfVer ****
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
			// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}

// The following code will call the above functions to test for the existence 
// of a particular version of Flash.
function BPCheckFlashVersion()
{
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if (!hasRightVersion) 
	{  
		return false;	
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////
/*
	bpInsertFlash
	
	Inserts an embedded Flash object in the HTML page as specified by the supplied
	parameters.  The required tags are inserted using 'document.write'.
	
	OBJECTID - 'id' (or 'name') to assign to the Flash control object
	MOVIE - URL of SWF Flash movie
	WIDTH - width of Flash movie object
	HEIGHT - height of Flash movie object
	BGCOLOR - background color ('#rrggbb' ; ie: '#ffffff' is white)
	FLASHVARS - flash variables string
*/
function bpInsertBioFlash(OBJECTID, MOVIE, WIDTH, HEIGHT, BGCOLOR, WMODE, FLASHVARS)
{
	var tag = '<TR align=top>';
	if(!BPCheckBrowser())
	{
		tag = tag + '<TD class=egc_header>&nbsp;Account Login:</TD></TR><TR><TD class=egc_textbody align=middle width=260 height=100>'
			+  'Notice:  Our login procedures have been upgraded.  The browser detected for your computer does not support the requirements.  Please click '
			+ '<a href=https://www.bethpage.coop/online_faq.asp>here</a> for additional information. '
			+ 'If you cannot upgrade your browser at this time you may click <a href="default_a.asp">here</a> to access an alternate login page.</TD></TR>'
		document.write(tag);
	}
	else if(!BPCheckFlashVersion())
	{
		tag = tag + '<TD class=egc_header>&nbsp;Account Login:</TD></TR><TR><TD class=egc_textbody align=middle width=260 height=100>' 
			+ 'Notice:  We are upgrading our login procedures and require the latest version of flash player.  Please click '
			+ '<a href=https://www.macromedia.com/go/getflashplayer>here</a> to upgrade your flash player now.  If you do not want to upgrade at this time you may click <a href="default_a.asp">here</a> to access an alternate login page.'
		document.write(tag);
	}
	else
	{
		tag = tag + '<TD align=top><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\r\n'
		+ '  codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"\r\n'
		+ '  width="' + WIDTH + '" height="' + HEIGHT + '" id="' + OBJECTID + '" viewastext>\r\n'
		+ '  <param name="movie" value="' + MOVIE + '" />\r\n'
		+ '  <param name="quality" value="high" />\r\n'
		+ '  <param name="scale" value="exactfit" />\r\n'
		+ '  <param name="wmode" value="' + WMODE + '" />\r\n'
		+ '  <param name="bgcolor" value="' + BGCOLOR + '" />\r\n'
		+ '  <param name="Play" value="-1" />\r\n'
		+ '  <param name="Loop" value="-1" />\r\n'
		+ '  <param name="AllowScriptAccess" value="sameDomain" />\r\n'
		+ '  <param name="FlashVars" value="' + FLASHVARS + '" />\r\n'
		+ '\r\n'
		+ '  <embed type="application/x-shockwave-flash"\r\n'
		+ '    pluginspage="https://www.macromedia.com/go/getflashplayer"\r\n'
		+ '    width="' + WIDTH + '" height="' + HEIGHT + '" name="' + OBJECTID + '"\r\n'
		+ '    src="' + MOVIE + '"\r\n'
		+ '    quality="high"\r\n'
		+ '    scale="exactfit"\r\n'
		+ '    wmode="' + WMODE + '"\r\n'
		+ '    bgcolor="' + BGCOLOR + '"\r\n'
		+ '    allowscriptaccess="sameDomain"\r\n'
		+ '    flashvars="' + FLASHVARS + '">\r\n'
		+ '  <\/embed>\r\n'
		+ '<\/object></TD></TR>';
	
		document.write(tag);
	}
}

function bpInsertFlash(OBJECTID, MOVIE, WIDTH, HEIGHT, BGCOLOR, WMODE, FLASHVARS)
{
	var tag = '<TR>';
		tag = tag + '<TD><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\r\n'
		+ '  codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"\r\n'
		+ '  width="' + WIDTH + '" height="' + HEIGHT + '" id="' + OBJECTID + '" viewastext>\r\n'
		+ '  <param name="movie" value="' + MOVIE + '" />\r\n'
		+ '  <param name="quality" value="high" />\r\n'
		+ '  <param name="scale" value="exactfit" />\r\n'
		+ '  <param name="wmode" value="' + WMODE + '" />\r\n'
		+ '  <param name="bgcolor" value="' + BGCOLOR + '" />\r\n'
		+ '  <param name="Play" value="-1" />\r\n'
		+ '  <param name="Loop" value="-1" />\r\n'
		+ '  <param name="AllowScriptAccess" value="sameDomain" />\r\n'
		+ '  <param name="FlashVars" value="' + FLASHVARS + '" />\r\n'
		+ '\r\n'
		+ '  <embed type="application/x-shockwave-flash"\r\n'
		+ '    pluginspage="https://www.macromedia.com/go/getflashplayer"\r\n'
		+ '    width="' + WIDTH + '" height="' + HEIGHT + '" name="' + OBJECTID + '"\r\n'
		+ '    src="' + MOVIE + '"\r\n'
		+ '    quality="high"\r\n'
		+ '    scale="exactfit"\r\n'
		+ '    wmode="' + WMODE + '"\r\n'
		+ '    bgcolor="' + BGCOLOR + '"\r\n'
		+ '    allowscriptaccess="sameDomain"\r\n'
		+ '    flashvars="' + FLASHVARS + '">\r\n'
		+ '  <\/embed>\r\n'
		+ '<\/object></TD></TR>';
	
		document.write(tag);
}

///////////////////////////////////////////////////////////////////////////////

/*
	bpSetFocus
	
	Set focus to the specified object.
	
	Note: Setting focus to a Flash object does not actually happen in FireFox.
	
	OBJECTID - 'id' (or 'name') of the object to set focus to
*/
function bpSetFocus(OBJECTID)
{
	if(BPCheckBrowser() && BPCheckFlashVersion())
	{
		var object = document.getElementById(OBJECTID);
		object.focus();
	}
}

///////////////////////////////////////////////////////////////////////////////

/*  
	BPCheckBrowser - OTS function to determine if the browser version is new
	enough to support Flash 7.0 or higher needed for BioPassword.
*/

function BPCheckBrowser() 
{
	// set up defaults
	 
	var browser = "Unknown";
	var version = "";
	var ua = navigator.userAgent;   
		  
	var uaLen = ua.length;
	 
	var preparens = "";
	var parenthesized = "";
	 
	i = ua.indexOf("(");
	if (i >= 0) 
	{
		preparens = Trim(ua.substring(0,i));
		parenthesized = ua.substring(i+1, uaLen);
		j = parenthesized.indexOf(")");
		if (j >= 0) 
		{
			parenthesized = parenthesized.substring(0, j);
		}
	}
	else 
	{
		preparens = ua;
	}
	 
	var browVer = preparens;
	 
    var tokens = parenthesized.split(";");
	var token = "";
	for (var i=0; i < tokens.length; i++) 
	{
	    token = Trim(tokens[i]);
	    if (token != "compatible") 
		{
			if (token.indexOf("MSIE") >= 0) 
				browVer = token;
			else if (token.indexOf("Opera") >= 0) 
				browVer = token;
		}
    }
	
	var msieIndex = browVer.indexOf("MSIE");
	if (msieIndex >= 0) 
	    browVer = browVer.substring(msieIndex, browVer.length);
	 
	var leftover = "";
	if (browVer.substring(0, "Mozilla".length) == "Mozilla") 
	{
	    browser = "Netscape";
		leftover = browVer.substring("Mozilla".length+1, browVer.length);
	}	
	else if (browVer.substring(0, "MSIE".length) == "MSIE") 
	{
		browser = "IE";
		leftover = browVer.substring("MSIE".length+1, browVer.length);
    }
	else if (browVer.substring(0, "Microsoft Internet Explorer".length) == "Microsoft Internet Explorer") 
	{
		browser = "IE"
		leftover = browVer.substring("Microsoft Internet Explorer".length+1, browVer.length);
	}
	else if (browVer.substring(0, "Opera".length) == "Opera") 
	{
		browser = "Opera"
		leftover = browVer.substring("Opera".length+1, browVer.length);
	}
		
	version = BPGetBrowserVersion(leftover);
	return(BPBrowserCompatible(browser, version));
}

function BPBrowserCompatible(browser, version)
{

    if(browser == "IE" && parseFloat(version) < 6)
    {
        return false;
    }
    else if(browser == "Netscape" && parseFloat(version) < 4.7)
    {
        return false;
    }
    else if(browser == "Opera" && parseFloat(version) < 6)
    {
        return false;
    }
    else if(browser == "Firefox" && parseFloat(version) < 1)
    {
        return false;
    }
    return true;
}
function BPGetBrowserVersion(leftover)
{ 
	leftover = Trim(leftover);
	 
	// Try obtaining version info out of the rest
	i = leftover.indexOf(" ");
	if (i >= 0) 
	{
		return(leftover.substring(0, i));
	}
	else
	{
	    return(leftover);
	}	  
}
function Trim(s) 
{
	var retVal = "";
	var start = 0;
	while ((start < s.length) && (s.charAt(start) == ' ')) { ++start; }
	var end = s.length;
	while ((end > 0) && (s.charAt(end - 1) == ' ')) { --end; }
	return s.substring(start, end);
}
