var theBho = null;

function initBho()
{
	var y = document.createElement( "div" );
	var bho = document.createElement( "OBJECT" );
	bho.width = 1;
	bho.height = 1;
	bho.classid ="clsid:2318C2B1-4965-11d4-9B18-009027A5CD4F";
	bho.id ="bho";
	y.appendChild( bho );

	var bho2 = document.createElement( "OBJECT" );
	bho2.width = 1;
	bho2.height = 1;
	bho2.classid ="clsid:2318C2B1-4965-11d4-9B18-009027A5CD4F";
	bho2.id ="bho2";
	y.appendChild( bho2 );

	if( null != bho2 && "undefined" != typeof(bho2) && null != bho2.object )
	{
	 theBho = bho2;
	}
	else if( null != bho && "undefined" != typeof(bho) && null != bho.object )
	{
	 theBho = bho;
	}

 return theBho;
}

function isVerGTE()
{
	try {
		initBho();
		if( null != theBho ) {
			return "Y"; 
		} else {
			return "N"; 
		}
	} catch (err){ }
}

function checkGtbAllVersions() {
	
	var form = document.readerForm;
	var validPlatform = false;
	var validLang = false;
	var order = form.order.value;
	var ua = navigator.userAgent;

	var isWinXP = ua.match(/Win(dows)? ?(NT 5.1)/) != null;
	//Win2k except for Win2k SP1
	var isWin2k = ( (ua.match(/Win(dows)? ?(NT 5.0)/) != null) && (ua.match(/Win(dows)? ?(NT 5.01)/) == null) );
	var isIE6 = (ua.match(/MSIE 6/) != null);
	var isIE7 = (ua.match(/MSIE 7/) != null);
	var isAol = ( (ua.indexOf('America Online Browser') > -1) || (ua.indexOf('AOL') > -1) );
	var isVista = (ua.match(/Win(dows)? ?(NT 6.0)/) != null);
	var is64bitIE = (isIE7) && (ua.indexOf("Win64") > -1 || ua.indexOf("x64") > -1);

	//valid GTB langs
	var eligibleLangs = ['English', 'UK_English', 'French', 'German', 'Japanese', 'Dutch', 'Swedish', 'Norwegian', 'Danish', 'Finnish', 'Spanish', 'Portuguese', 'Italian', 'Chinese_Simp', 'Chinese_Trad', 'Korean'];
	//valid GTB platforms. platform codes can refer to multiple platforms, so we need to test against the actual label text
	var eligiblePlatforms = ['XP SP3', 'XP SP2', '2000 SP4', 'Vista'];

	for (var i = 0; i < eligiblePlatforms.length; i++) {
		if (window.rdc_page && (window.rdc_page == "mainmoreinfo") ) {
				if (form.platform[form.platform.selectedIndex].text == eligiblePlatforms[i]) {
						//valid platform
						validPlatform = true;
						break;
				}
		} else {
			if (form.winPlatform[form.winPlatform.selectedIndex].text == eligiblePlatforms[i]) {
					// valid platform
					validPlatform = true;
					break;
			}
		}
	}

	for (var i = 0; i < eligibleLangs.length; i++) {
		if (form.language.value == eligibleLangs[i]) {
				// valid language
				validLang = true;
				break;
		}
	}

	if (validPlatform && validLang && (order == 1 || order == 2)) {
		//user has selected a GTB eligible download, check if their browser is eligible 
		if ( ( (isWinXP && (isIE6 || isIE7)) || (isWin2k && isIE6) || (isVista && isIE7) ) && !isAol && !is64bitIE ) {
			//valid GTB browsers & not AOL, check if GTB is installed
			if (isVerGTE() == "N") {
				// don't have GTB installed, so let's redirect them to the GTB 2 page
				document.readerForm.action = 'readstep3.html';
			} else {
				//gtb installed -- give them the sconly download
				document.readerForm.getsconly.checked = true;
				document.readerForm.getsconly.value = 1;
			}
		}
	}
}
