var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "Unsupported";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| 0;
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		}
	]

};

function cookiesEnabled() {
	var cookieEnabled=(navigator.cookieEnabled)? true : false;
	
	//not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
	}	
	return cookieEnabled;
}

function getJavaVersion( browser ){
		var version = 0;
		try	{

			if( browser == 'Safari' ) {
				//version = java.lang.System.getProperty( "java.version" );
				version = document.Detector.getVersion();
			} else {
				version = document.getElementById('Detector').getVersion();	
			}

			version = version.substr(0,3)
		} catch(e) {
			//Java Plugin Not Installed
			version = 0;
		}
		
	return version;
}


function validateBrowser() {
	BrowserDetect.init();

	var forceError = false;
	
	// version of Flash required
	var requiredMajorVersion = 8;
	var requiredMinorVersion = 0;
	var requiredRevision = 0;
	
	var hasReqestedFlashVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	var hasCookiesEnabled = cookiesEnabled();	
	var javaVersion = getJavaVersion( BrowserDetect.browser );
	
	//check browser
	if( forceError || ( BrowserDetect.browser != 'Explorer' &&
		BrowserDetect.browser != 'Firefox' ) ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">' 
		+ '<h2>Your browser is not supported</h2>' 
		+ '<p>This site requires <em>Internet Explorer 5.5+</em> or <em>Firefox 2.0+</em>. <br/>' 
		+ 'Your current browser is <em>' + BrowserDetect.browser + '</em>. '
		+ 'To fix this, please <a href="http://www.mozilla.com/en-US/firefox/">Get Mozilla Firefox</a> or ' 
		+ '<a href="http://www.microsoft.com/windows/ie/downloads/default.mspx">Get Microsoft Internet Explorer</a>.</p>' 
		+ '</div></li>';		
	}
	
	//Check browser version
	if( forceError || ( BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 5 ) ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">' 
		+ '<h2>Your browser version is not supported</h2>' 
		+ '<p>This site requires <em>Internet Explorer, version 5.5+</em>. <br/>' 
		+ 'Your current browser is <em>Internet Explorer, version ' + BrowserDetect.version + '</em>. '
		+ 'To fix this, please <a href="http://www.microsoft.com/windows/ie/downloads/default.mspx">Get Microsoft Internet Explorer</a> to update your browser.</p>' 
		+ '</div></li>';		
	}	

	if( forceError || ( BrowserDetect.browser == 'Firefox' && BrowserDetect.version < 2 ) ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">' 
		+ '<h2>Your browser version is not supported</h2>' 
		+ '<p>This site requires <em>Firefox, version 2.0+</em>. <br/>' 
		+ 'Your current browser is <em>Firefox, version ' + BrowserDetect.version + '</em>. '
		+ 'To fix this, please <a href="http://www.mozilla.com/en-US/firefox/">Get Mozilla Firefox</a> to update your browser.</p>' 
		+ '</div></li>';		
	}	


		
	// Check cookies
	if( forceError || !cookiesEnabled() ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">' 
		+ '<h2>Cookies are not enabled</h2>' 
		+ '<p>Cookies need to be enabled for U Online to function correctly. <br />' 
		+ 'Your browser currently has cookies disabled. '
		+ 'To fix this, please <a href="http://invest.ttutc.com/requirements.html">Enable cookies on your browser</a>.</p>' 
		+ '</div></li>';
	}	
	
	/*
	// Check screen resolution
	if( screen.width > 1024 ) {
		warningCount++;
		alternateContent += 
		'<div class="warning">' 
		+ '<h2>Screen resolution too low</h2>' 
		+ '<p>Your screen resolution is too low.' 
		+ 'If you are experiencing problems viewing U Online we recommend you adjust your resolution to 1024x768 or higher following the instructions at ' 
		+ 'the <a href="http://invest.ttutc.com/requirements.html">Requirements Section</a>.</p>' 
		+ '</div>';
	}
	*/

	// Check Flash
	if (forceError || !hasReqestedFlashVersion) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">'
		+ '<h2>Incorrect or Missing Flash Player</h2><p>'
		//+ 'Your current Flash player is ' + detectedVersion + '<br/>'
		+ 'U Online requires <em>Adobe Flash Player, version ' + requiredMajorVersion + '</em>. <br/>'
		+ 'Your browser does not have correct version of Flash installed ('+ detectedVersion + '). '
		+ 'To fix this, please <a href="http://www.adobe.com/go/getflash/">Get Flash</a>.</p>'
		+ '</div></li>';
	}
	
	// Check JRE
	if (forceError || ( javaVersion < 1.4  && javaVersion != 0 ) ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">'
		+ '<h2>Incorrect Java Runtime</h2>'
		+ '<p>U Online requires the <em>Java Runtime, version 1.4+</em>. <br/>'		
		+ 'Your browser has <em>Java Runtime, version ' + javaVersion + '</em> installed. '
		//+ 'To fix this, please <a href="http://www.java.com/">Get Java</a> to update your Java Runtime.</p>'
		+ 'To fix this, please <a href="download-java.html">Get Java</a> to update your Java Runtime.</p>'
		+ '</div></li>';
	}
	
	// Check JRE
	if (forceError || javaVersion == 0 ) {
		errorCount++;
		alternateContent += 
		'<li><div class="error">'
		+ '<h2>Missing Java Runtime</h2>'
		+ '<p>U Online requires the <em>Java Runtime, version 1.4+</em>. <br/>'		
		+ 'Your browser does not have the Java Runtime installed. '
		//+ 'To fix this, please <a href="http://www.java.com/">Get Java</a>.</p>'
		+ 'To fix this, please <a href="download-java.html">Get Java</a>.</p>'
		+ '</div></li>';
	}	

}

function promptAccordingly() {
	if ( errorCount == 0 ) {
		//document.write('<div style="text-align: center; color: #280; font-weight: bold;" id="container"><p>Browser has passed validation and will be redirected to login page.</p></div>');
		goToLogin();
	} else {
		var summary = 
			'<h1>Your browser is not supported</h1>' 
			+ '<p class="summary">'
			+ 'Unfortunately you cannot access the UTC U-Online application from your current browser. You must fix the following ' 
			+ ( errorCount > 1 ? 'errors' : 'error' ) 
			+ ' and try again.</p>';
			
		var footnote = 
			'<p class="footnote">For more help, see the <a href="http://invest.ttutc.com/requirements.html">U-Online Requirements Section</a>. '
			+ 'You can return to <a href="http://invest.ttutc.com">UTC Online Home</a> and try again later. </p>' ;
		
		footnote += 
			'<p class="footnote">Although your browser is not fully capable of using the U-Online application, '
			+ 'you can still go directly to the login page. This will not be an ideal experience, and you should '
			+ 'use a browser that meets the <a href="http://invest.ttutc.com/requirements.html">requirements</a> before accessing U-Online.'
			+ '<br/><br/>'
			+ '<a href="https://uonline.ttutc.com/corp/BANKAWAY?Action.RetUser.Init.001=Y&AppSignonBankId=UTC&AppType=corporate/">' 
			+ '<img border="none" src="uonline-login-button.png" alt="Go directly to U-Online login" /></a></p>' ;
			
		var pre = 
			'<html lang="en"><head>	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Browser Validation</title></head><body>\n';
		
		var post =
			'\n</body></html>';
		
		//document.clear();
		//document.write( pre );
		document.write( '<div id="container">' + summary );	
		document.write( '<ul>' + alternateContent + '</ul>' );			
		document.write( footnote + '</div>' );			
		//document.write( post );
	}	
}

function goToLogin() {
	//window.location.replace( "https://uonline.ttutc.com/corp/BANKAWAY?Action.RetUser.Init.001=Y&AppSignonBankId=UTC&AppType=corporate/" );
	window.location.replace( "approve.html" );
	
}

