﻿// all javascript routines should be kept here

var homebaseWin;
var myUndefined;

function openHomeBase( id, unitName, url, width, height ) {


try 
{	// need try in case homebaseWin was closed and no longer exists	
	if ( homebaseWin != null && ( homebaseWin.width != width || homebaseWin.height != height ) ) 
	{
		closeHomeBase();
	}
} catch (e) { }

if ( width == null )
	width = 800;
	
if ( height == null )
	height = 600;

var screenWidth = width, screenHeight = height;
var resizable = 0;

if (document.all) {
	screenWidth = screen.availWidth - 15;
	screenHeight = screen.availHeight - 35;

} else if (document.layers || document.getElementById) {
	screenWidth = top.screen.availWidth;
	screenHeight = top.screen.availHeight;
}

if (screenWidth < width ||  screenHeight < height) {
	width = screenWidth;
	height = screenHeight;
	resizable = 1;
}
 
//var attr = "width=" + width + ",height=" + height + ",resizable=" + resizable + ",top=200,left=80,status=0,toolbar=0,scrollbars=0,location=0,menubar=0,directories=0";
var attr = "width=" + width + ",height=" + height + ",resizable=1,top=200,left=80,status=0,toolbar=0,scrollbars=1,location=0,menubar=0,directories=0";

if ( url.indexOf("?") == -1 ) {
	url += "?";
} else {
	url += "&";
}

var path = url + "unitId=" + id + "&unitDisplayName=" + escape( unitName ) + "&dispSBar=" + resizable + "&width=" + width + "&height=" + height;

if (path.indexOf("?unitName=") == -1 && path.indexOf("&unitName=") == -1) 
{
	if (encodeURIComponent) {
		path += "&unitName=" + encodeURIComponent(unitName);
	} else {
		path += "&unitName=" + escape(unitName);
	}
}
homebaseWin = window.open( path, "HomeBase", attr);

homebaseWin.focus();
		
}

var itestWin;
function openItestWin( id, unitName, url, width, height ) {

//alert("open itest");
if ( width == null )
	width = 800;
	
if ( height == null )
	height = 600;


var screenWidth = width, screenHeight = height;
var resizable = 0;

if (document.all) {
	screenWidth = screen.availWidth - 15;
	screenHeight = screen.availHeight - 35;

} else if (document.layers || document.getElementById) {
	screenWidth = top.screen.availWidth;
	screenHeight = top.screen.availHeight;
}

if (screenWidth < width ||  screenHeight < height) {
	width = screenWidth;
	height = screenHeight;
	resizable = 1;
}
 
var attr = "width=" + width + ",height=" + height + ",resizable=" + resizable + ",top=200,left=80,status=0,toolbar=0,scrollbars=0,location=0,menubar=0,directories=0";

if ( url.indexOf("?") == -1 ) {
	url += "?";
} else {
	url += "&";
}

var path = url + "unitId=" + id + "&unitDisplayName=" + escape( unitName ) + "&dispSBar=" + resizable + "&width=" + width + "&height=" + height;

if (path.indexOf("?unitName=") == -1 && path.indexOf("&unitName=") == -1) 
{
	if (encodeURIComponent) {
		path += "&unitName=" + encodeURIComponent(unitName);
	} else {
		path += "&unitName=" + escape(unitName);
	}
}
itestWin = window.open( path, "itestWin", attr);

itestWin.focus();
		
}


function closeHomeBase() {

if ( homebaseWin != null && !homebaseWin.closed ) {
	homebaseWin.close();
	homebaseWin = null;
	//alert("closeHomeBase()");
}

}

function handleLogout() {
closeHomeBase();

document.location.href = "/lib/logout.jsp";
}

function openLessonList( url ) {
// TODO: re-implement this
//document.location.href = url;
//alert( url );
}


function confirmPurchase( pack, url ) {

var msgPrefix = '<%= tf.convertToJScriptUnicode( tf.getString( language, "packages.jsp", "confirmBuyPackage") ) %>';

var answer = confirm( msgPrefix + " " + pack + "?");

if ( answer != 0 ) {
	document.location.href = url;
}

}

// generate the local time zone
function getUserTimezone() {
var now = new Date();

var hour = now.getTimezoneOffset() / 60;

var tz = "GMT";

if ( hour < 0 ) {
	hour *= -1;
	tz += "%2B";
	
} else {
	
	tz += "-";
}

if ( hour < 10 ) {
	tz += "0";
}

tz += hour;

//add minutes...
tz += ":00";

return tz;
}



function imageSwap( imgName, src ) {
document[ imgName ].src = src;
}


function getEngine(majorVersion, minorVersion, currentMajorVersion, currentMinorVersion, rootHref) {
	// by default return chinese installer
	return getEngine(majorVersion, minorVersion, currentMajorVersion, currentMinorVersion, rootHref, "zh_CN");
}

function getEngine(majorVersion, minorVersion, currentMajorVersion, currentMinorVersion, rootHref, nativeLanguage) {
	var installPath = "";
	// this is hardwired to the actual installers
	var INSTALLER_CHINESE_DEFAULT = "Client_Chinese_120110.exe";
	var INSTALLER_ENGLISH_DEFAULT = "Client_English_111203.exe";
	var INSTALLER_ENGLISH_JA_JP = "Client_English_111203_ja_JP.exe";
	var PATCHINSTALLERS_CHINESE_DEFAULT = [ "Client_Chinese_patch_5_11_120110.exe",
						"Client_Chinese_patch_5_10_120110.exe",
						"Client_Chinese_patch_5_6_120110.exe",
						"Client_Chinese_patch_5_3_120110.exe",
						"Client_Chinese_patch_5_0_120110.exe"
					];
	var PATCHINSTALLERS_ENGLISH_DEFAULT = [ "Client_English_patch_4_13_111203.exe",
						"Client_English_patch_4_10_111203.exe",
						"Client_English_patch_4_7_111203.exe",
						"Client_English_patch_4_5_111203.exe"
					];
	var PATCHINSTALLERS_ENGLISH_JA_JP = [ ];
	var PATCHINSTALLERS_CHINESE_DEFAULT_MINORVERS = [ 11, 10, 6, 3, 0 ];
	var PATCHINSTALLERS_ENGLISH_DEFAULT_MINORVERS = [ 13, 10, 7, 5 ];
	var PATCHINSTALLERS_ENGLISH_JA_JP_MINORVERS = [ ];

	var installerEnglish = INSTALLER_ENGLISH_DEFAULT;
	var installerChinese = INSTALLER_CHINESE_DEFAULT;
	var installersEnglishPatch = PATCHINSTALLERS_ENGLISH_DEFAULT;
	var installersEnglishPatchMinorVers =  PATCHINSTALLERS_ENGLISH_DEFAULT_MINORVERS;
	var installersChinesePatch = PATCHINSTALLERS_CHINESE_DEFAULT;
	var installersChinesePatchMinorVers =  PATCHINSTALLERS_CHINESE_DEFAULT_MINORVERS;
	if ( nativeLanguage == "ja_JP" ) {
		installerEnglish = INSTALLER_ENGLISH_JA_JP;
		installersEnglishPatch = PATCHINSTALLERS_ENGLISH_JA_JP;
		installersEnglishPatchMinorVers =  PATCHINSTALLERS_ENGLISH_JA_JP_MINORVERS;
	}

	if (majorVersion == 5) {
		var curminorval = currentMinorVersion * 1;
		for ( var i = 0; i < installersChinesePatchMinorVers.length; i++ ) {
			var minorvers = installersChinesePatchMinorVers[ i ];
			if (currentMajorVersion == majorVersion && curminorval >= minorvers) {
				if ( installersChinesePatch[i] != "" ) {
					installPath = "/download/" + installersChinesePatch[i];
				}
				break;
			}
		}
		if ( installPath == "" ) {
			if ( installerChinese != "" ) {
				installPath = "/download/" + installerChinese;
			}
		}
	} else if (majorVersion == 4) {
		var curminorval = currentMinorVersion * 1;
		for ( var i = 0; i < installersEnglishPatchMinorVers.length; i++ ) {
			var minorvers = installersEnglishPatchMinorVers[ i ];
			if (currentMajorVersion == majorVersion && curminorval >= minorvers) {
				if ( installersEnglishPatch[i] != "" ) {
					installPath = "/download/" + installersEnglishPatch[i];
				}
				break;
			}
		}
		if ( installPath == "" ) {
			if ( installerEnglish != "" ) {
				installPath = "/download/" + installerEnglish;
			}
		}
	}
	if (installPath != "") {
		return rootHref + installPath;
	}
	// not sure what to install, return null
	return null;
}


function getComputerId(aobj) {
	var computerId = "";
	if (aobj == myUndefined) {
		//it's not installed...for now, that's fine...
	} else {
		computerId=aobj.ID;
	}
	return computerId;
}

// returns 0 if not defined
// returns -1 if defined but not properly working
// returns 1 if defined and properly working
function isActiveXDefined(aobj) {
	if ( aobj == myUndefined ) {
		return 0;
	}
	if ( aobj.Version == myUndefined ) {
		return -1;
	}
	return 1;
}

// returns index of current version that is correctly installed
// returns index - engineVersions.length of current version that is corrupted
// returns engineVersions.length if no version is found
function getEngineVersionIndexFromActiveX(aobj, contentVersion, engineVersions) {
	var iContentVersion;
	var iNotFound = engineVersions.length;
	for (iContentVersion = 0; iContentVersion < engineVersions.length; iContentVersion++) {
		if (engineVersions[iContentVersion] == contentVersion) {
			break;
		}
		if (iContentVersion == engineVersions.length - 1) {
			//alert("mismatched arguments");
			return iNotFound;
		}
	}
	//alert("here we go...!");
	if ( aobj == myUndefined ) {
		//alert("activex not installed");
		return iNotFound;
	}
	// user has the control, so run some checks
	if ( aobj.Version == myUndefined ) {
		//alert("old activex");
		return iNotFound;
	}
	var iversion;
	var icorruptversion = iNotFound;
	for (iversion = engineVersions.length - 1; iversion >= 0; iversion--) {
		try {
			aobj.Version = engineVersions[iversion];
			//alert( "found version: " + iversion );
			return iversion;
		} catch (e) {
			if (e instanceof Error) {
				var errnum = e.number & 0x7FFFFFFF;
				if (errnum == 656360) {
					// Engine version not installed.
					//alert("Version Error: " + errnum + ": " + e.description);
				} else if (errnum == 656361) {
					// Engine version corrupt
					//alert("Version Error: " + errnum + ": " + e.description);
					if (icorruptversion == iNotFound) {
						icorruptversion = iversion;
					}
				} else if (errnum == 655798) {
					// Object doesn't support this property or method.
					// probably won't encounter this ever but just in case
					//alert("Version Error: " + errnum + ": " + e.description);
				} else {
					// unexplained error
					//alert("Version Error: " + errnum + ": " + e.description);
				}
			} else {
				// unexplained error
				//alert("Unexplained Error: "+e+" Call For Help!");
			}
		}
	}

	if (icorruptversion != iNotFound) {
		return icorruptversion - engineVersions.length;
	}

	return iNotFound;
}

function getFileListStatusFromActiveX(aobj, version, fileLists) {
	if ( aobj == myUndefined ) {
		//alert("activex not installed");
		return null;
	}
	try {
		// set the version in the ActiveX control
		//alert("setting engine to: " + version );
		aobj.Version = version;

		//alert("accessing with: " + version );
		//alert("accessing with: " + fileLists );
		s = document.myCtl.Check(fileLists);
		
	} catch (e) {
		if (e instanceof Error) {                           
			//alert("activex threw exception " + e.number + " " + e.description);
			//alert("An error occurred. Please click the Homebase and try again.");
		} else {
			//alert("activex threw custom exception");
			//alert("An error occurred. Please click the Homebase and try again.");
		}
		return null;
	}
	//alert(s);

	return s;
}

// returns 0 if activex was reset
// returns -1 if activex was not reset
// returns -2 if activex was not found
function resetActiveX(aobj) {
	if ( aobj == myUndefined ) {
		return -2;
	}
	try {
		aobj.Reset();
	} catch (e) {
		return -1;
	}

	return 0;
}

