/* Copyright © 2001 PureWorks, Inc.  All Rights Reserved. */

var strContentDir;
var strMovieLocation;

var intNumSlides;
var intSlideNum;
var intStartOfNavigation;
var intStartOfSkin;
var blnIntroEndsWithSkin;
var intStartOfContentDir;
var strNav;
var strNavType;
var blnUseExitTransitions;
var blnHasSoundOption;
var intStartOfOverlay;
var strLanguage;
var strExpertEmail;
var strExpertName;
var strTitle;
var intBookmarkCommitFrequency;
var intBookmarkCommitCounter;
var strLMSStandard;

var strBookmarkedLocation;

var blnPlaySound;
var blnResuming
var blnPaused;

var blnNavIsLoaded;
var blnSlideIsLoaded;
var blnSkinIsLoaded;
var blnOverlayIsLoaded;
var blnOverlayIsVisible;

var blnLMSFinishCalled;
var blnReachedLastSlide;
var blnExecutingCommand;	//used to prevent double clicks from sending duplicate requests
var blnReadyForNextCommand; //used to ensure that double clicks don't perform 2 quick actions

var intTildaCounter = 0;	//3 tildas in a row will bring up the debug window
var winDebug = null;

var objContent;
var objLoader;
var objLMS;

var blnIE;

var CallFunctionIntervalID = "";
var AdvanceAfterIntervalID = "";
var WaitOnReadyIntervalID = "";
var SetProgressFunctionIntervalID = ""

function Initialize(strDir){
	
	//initialize the content directory
	strContentDir = strDir;
	if (strContentDir.charAt(strContentDir.length - 1) != "/"){
		strContentDir += "/";
	}
		
	//check that the browser is IE4+ or NS4+ and has Java enabled, then set a global variable telling us if it's IE or Netscape
	var intBrowserVersion
	var strBrowserName
		
	intBrowserVersion = parseInt(navigator.appVersion)
	strBrowserName = navigator.appName
	
	if (strBrowserName.indexOf("Internet Explorer") == -1 && strBrowserName.indexOf("Netscape") == -1){
		alert("Your browser (" + strBrowserName + ") is not supported.  \nPlease try again using Netscape Navigator or Microsoft Internet Explorer.");
		return false;
	}
	
	if (intBrowserVersion < 4){
		alert("This content cannot be played because your browser version (" + intBrowserVersion + ") is less than 4.0, please upgrade and try again.");
		return false;
	}
	
	if (!navigator.javaEnabled()){
		alert("This content cannot be played because your browser does not have Java enabled.");
		return false;
	}
	
	if (strBrowserName.indexOf("Internet Explorer") != -1){
		blnIE = true;
	}
	else{
		blnIE = false;
	}
	
	
	//set up the page to check keyboard events
	if (! blnIE) {
		document.captureEvents(Event.KEYPRESS)
	}
		
	document.onkeypress = CheckForCommand;
	
	blnPaused = false;
	blnNavIsLoaded = false;
	blnLMSFinishCalled = false;
	blnOverlayIsLoaded = false;
	blnOverlayIsVisible = false;
	blnSkinIsLoaded = false;
	blnReachedLastSlide = false;
	blnExecutingCommand = false;
	blnReadyForNextCommand = true;
	intBookmarkCommitCounter = 0;

	objContent = window.document.content;	
	objLoader = window.document.ContentLoader;
	
	if (typeof(objContent) == "undefined" || typeof(objLoader) == "undefined"){
		WriteToDebug("Unable to acquire objects:")
		WriteToDebug("objContent=" + typeof(objContent));
		WriteToDebug("objLoader=" + typeof(objLoader));
		
		alert("Error - unable to acquire necessary objects, content may not play properly and results may not be recorded.  Please contact technical support.");
		return false;
	}
	
	
	WriteToDebug("In Initialize, Version=" + VERSION + "  Last Modified=" + window.document.lastModified);
	WriteToDebug("Acquired Objects and Validated Browser (" + strBrowserName + " " + navigator.appVersion + ")");
	WriteToDebug("Attempting to Get Data From Server");
	
	if (! GetDataFromServer(strContentDir)){
		return false
	}
	
	if (blnIE){
		window.document.title = strTitle;
	}
	
	WriteToDebug("Using " + strLMSStandard + " standard");
	
	eval ("objLMS = obj" + strLMSStandard.toUpperCase() + "Standard;");
	
	if (objLMS == "undefined"){
			WriteToDebug("Error - Unidentified LMS Standard, Using NONE Object ******");
			objLMS = objNONEStandard;		
	}
	
	WriteToDebug("Initializing LMS");
	
	if (! objLMS.Initialize() ){
		return false;
	}

	SetSoundOption(1);
	SetResumeOption(0);
	
	if (!blnIE){
		//neeed to jump through hoops to get these strings in the same format for comparison	
		strLanguage = ConvertString(strLanguage);
		DEFAULT_LANGUAGE = ConvertString(strLanguage);
		
		DEFAULT_LANGUAGE = DEFAULT_LANGUAGE;
	}
	
	if (strLanguage != DEFAULT_LANGUAGE){
		intSlideNum = LANGUAGE_SLIDE;
		LoadSlide(false);
	}
	
	strBookmarkedLocation = GetBookmarkLocation();
	
	if (strBookmarkedLocation != "" || blnHasSoundOption){
		intSlideNum = OPTIONS_SLIDE;
	}
	else{
		intSlideNum = 0;
	}
	
	LoadSlide(false);
	
	return true;
}


function ConvertString(str){
	var strTemp;
	strTemp = new String(str);
	strTemp = strTemp.toString();
	return strTemp;
}


function GetDataFromServer(strContentDir){

	if (! objLoader.GetDataFromServer("./" + strContentDir, window.document.cookie)){
		alert("Content could not be found or is malformed, please contact your system administrator.");
		blnFoundContentDir = false;			//from index.html
		return false;
	}
	
	
	WriteToDebug("Retrieved Data From Server - ");
	
	intStartOfNavigation = objLoader.intStartOfNavigation;
	intStartOfNavigation       = objLoader.GetStartOfNavigation();
	WriteToDebug("intStartOfNavigation=" + intStartOfNavigation);
	
	blnIntroEndsWithSkin       = objLoader.GetIntroEndsWithSkin();
	WriteToDebug("blnIntroEndsWithSkin=" + blnIntroEndsWithSkin);
	
	strNav                     = ConvertString(objLoader.GetNavigation());
	WriteToDebug("strNav=" + strNav);
	
	intStartOfSkin             = objLoader.GetStartOfSkin();
	WriteToDebug("intStartOfSkin=" + intStartOfSkin);
	
	blnUseExitTransitions      = objLoader.GetUseExitTransitions();
	WriteToDebug("blnUseExitTransitions=" + blnUseExitTransitions);
	
	blnHasSoundOption          = objLoader.GetHasSoundOption();
	WriteToDebug("blnHasSoundOption=" + blnHasSoundOption);
	
	strNavType                 = ConvertString(objLoader.GetNavigationType());
	WriteToDebug("strNavType=" + strNavType);
	
	intStartOfContentDir       = objLoader.GetStartOfContentDir();
	WriteToDebug("intStartOfContentDir=" + intStartOfContentDir);
	
	intStartOfOverlay          = objLoader.GetStartOfOverlay();
	WriteToDebug("intStartOfOverlay=" + intStartOfOverlay);
	
	strLanguage                = ConvertString(objLoader.GetLanguage());
	WriteToDebug("strLanguage=" + strLanguage);
	
	strExpertEmail             = ConvertString(objLoader.GetExpertEmail());
	WriteToDebug("strExpertEmail=" + strExpertEmail);
	
	strExpertName              = ConvertString(objLoader.GetExpertName());
	WriteToDebug("strExpertName=" + strExpertName);
	
	strTitle                   = ConvertString(objLoader.GetTitle());
	WriteToDebug("strTitle=" + strTitle);
	
	intBookmarkCommitFrequency = objLoader.GetBookmarkCommitFrequency();
	WriteToDebug("intBookmarkCommitFrequency=" + intBookmarkCommitFrequency);
	
	strLMSStandard             = ConvertString(objLoader.GetLMSStandard());
	WriteToDebug("strLMSStandard=" + strLMSStandard);
	
	LookForQueryStringOverRide();
	
	WriteToDebug("Searched for QueryString Overrides");
	
	intStartOfNavigation = parseInt(intStartOfNavigation);
	if (isNaN(intStartOfNavigation)){
		WriteToDebug ("Warning: Start of Navigation is not a number, resetting to " + CONST_NONE);
		intStartOfNavigation = CONST_NONE;
	}
	
	WriteToDebug("Converted intStartOfNavigation to int =" + intStartOfNavigation);
	
	intStartOfSkin = parseInt(intStartOfSkin);
	if (isNaN(intStartOfSkin)){
		WriteToDebug ("Warning: Start of Skin is not a number, resetting to " + CONST_NONE);
		intStartOfSkin = CONST_NONE;		
	}
	
	WriteToDebug("Converted intStartOfSkin to int =" + intStartOfSkin);
	
	intStartOfContentDir = parseInt(intStartOfContentDir);
	if (isNaN(intStartOfContentDir)){
		WriteToDebug ("Warning: Start of Content Dir is not a number, resetting to 0");
		intStartOfContentDir = 0;		
	}
	
	WriteToDebug("Converted intStartOfContentDir to int =" + intStartOfContentDir);
	
	intStartOfOverlay = parseInt(intStartOfOverlay);
	if (isNaN(intStartOfOverlay)){
		WriteToDebug ("Warning: Start of Overlay is not a number, resetting to " + CONST_NONE);
		intStartOfOverlay = CONST_NONE;		
	}
	
	
	WriteToDebug("Converted intStartOfOverlay to int =" + intStartOfOverlay);
	
	WriteToDebug("Getting Number of slides");
	intNumSlides = objLoader.GetNumSlides();
	
	
	intNumSlides = parseInt(intNumSlides);
	if (isNaN(intNumSlides)){
		WriteToDebug("Error: Number of Slides is not an integer");
		alert("Error - unable to determine the length of the content.");
		return false;
	}
	
	return true;
}


function LookForQueryStringOverRide(){

	var strTempNav;
	var strTempNavType;
	var strTempLanguage;
	var strTempLMSStandard;
	
	var strQueryString;
	
	strQueryString = new String(window.location.search);
	
	strTempNav = GetQueryStringValue("Navigation", strQueryString);
	strTempNavType = GetQueryStringValue("NavType", strQueryString);
	strTempLanguage = GetQueryStringValue("Language", strQueryString);
	strTempLMSStandard = GetQueryStringValue("LMSStandard", strQueryString);
	
	strTempNav = UnEncode(strTempNav);
	strTempNavType = UnEncode(strTempNavType);
	strTempLanguage = UnEncode(strTempLanguage);
	strTempLMSStandard = UnEncode(strTempLMSStandard);
	
	if (strTempNav.length > 0){
		WriteToDebug("Overriding Nav to '" + strTempNav + "' from querystring.");
		strNav = strTempNav;
	}
	
	if (strTempNavType.length > 0){
		WriteToDebug("Overriding Nav Type to '" + strTempNavType + "' from querystring.");
		strNavType = strNavType;
	}
	
	if (strTempLanguage.length > 0){
		WriteToDebug("Overriding Language to '" + strTempLanguage + "' from querystring.");
		strLanguage = strTempLanguage
	}
	
	if (strTempLMSStandard.length > 0){
		WriteToDebug("Overriding standard to '" + strTempLMSStandard + "' from querystring.");
		strLMSStandard = strTempLMSStandard;
	}

}


function UnEncode(str){
	str = new String(str);
	str = str.replace(/\+/g, "%20");
	return unescape(str);
}


function GetQueryStringValue(strElement, strQueryString)
{

	var aryPairs;
	var i;
	var intEqualPos;
	var strArg = "";
	var strValue = "";
	
	//get rid of the leading "?"
	strQueryString = strQueryString.substring(1);
		
	//split into name/value pairs
	aryPairs = strQueryString.split("&");
	
	strElement = strElement.toLowerCase();
	
	//search each querystring value and return the first match
	for (i=0; i < aryPairs.length; i++){
	
		intEqualPos = aryPairs[i].indexOf('=');
		
		if (intEqualPos != -1){
			strArg = new String(aryPairs[i].substring(0,intEqualPos));
			strArg = strArg.toLowerCase();
			
			if (strArg == strElement){
				strValue = aryPairs[i].substring(intEqualPos+1);
				return new String(strValue);
			}
		}
	} 
	
	//if we didn't find a match, return an empty string
	return "";
}



//SetSoundOption(0=no sound, 1=sound)
function SetSoundOption(intOption){

	intOption = parseInt(intOption);
	
	if (intOption == 0){
		blnPlaySound = false;
		strMovieLocation = strContentDir + "nosound/";
	}
	else {
		blnPlaySound = true;
		strMovieLocation = strContentDir;
	}
}


//SetResumeOption(0=start over, 1=resume from last location)
function SetResumeOption (intOption){
	
	intOption = parseInt(intOption);
	
	if (intOption == 0){
		blnResuming = false;
	}else{
		blnResuming = true;
	}
	
}


function LoadNextSlide(){

	var blnIsStopped
	
	WriteToDebug("In Load Next Slide, SlideNum=" + intSlideNum);
	
	//kill any previous timed function calls
	if (AdvanceAfterIntervalID != ""){
		window.clearInterval(AdvanceAfterIntervalID);
		AdvanceAfterIntervalID = "";
	}
	
	blnIsStopped = HasSlideStoppedItSelf();
	
	WriteToDebug("Is Stopped=" + blnIsStopped);	
	
	if (blnIsStopped){
		//set stopped=play
		objContent.SetVariable("_level" + GetCurrentLayer() + ":Stopped", "Play");
		
		//play
		objContent.TPlay("_level" + GetCurrentLayer() + "/");
			
		//if UseExitTransitions, AdvanceAfterDonePlaying
		if (blnUseExitTransitions){
			AdvanceAfterIntervalID = window.setInterval("AdvanceAfterDonePlaying();", 250)
		}
	}
	else{
		WriteToDebug("Advancing to next slide");
		if (blnResuming){
			intSlideNum = strBookmarkedLocation;
			intSlideNum = parseInt(intSlideNum);
			if (isNaN(intSlideNum)){
				WriteToDebug("Error: Bookmarked Location is not an integer, starting from beginning");
				intSlideNum = 0;
			}
		}
		else{
			if (intSlideNum < (intNumSlides - 1)) {
				intSlideNum ++;
			}
			else if(intSlideNum == (intNumSlides - 1)){
				intSlideNum = FINISH_SLIDE;
			}
		}
		LoadSlide(blnResuming);
		blnResuming = false;	
	}
}

function LoadPreviousSlide(){
	WriteToDebug("In Load Previous Slide, SlideNum=" + intSlideNum);
	intSlideNum --;
	if (intSlideNum < intStartOfNavigation){intSlideNum=intStartOfNavigation}
	LoadSlide(false);
}



function FlashResumeAfterPause(){
	WriteToDebug("In Resume After Pause, SlideNum=" + intSlideNum);
	if (blnPaused && !HasSlideStoppedItSelf()){
		objContent.TPlay("_level" + GetCurrentLayer() + "/")
		blnPaused = false;
	}
}


function FlashPause(){
	WriteToDebug("In Pause, SlideNum=" + intSlideNum);
	objContent.TStopPlay("_level" + GetCurrentLayer() + "/");
	blnPaused = true;
}


function GetCurrentLayer(){
	//layer 0 is an empty slide
	//layer 1 is the logos, intro and skin
	//layer 2 is the actual presentation
	
	if (intSlideNum < intStartOfSkin){
		return LEVEL_SKIN;
	}
	else{
		return LEVEL_SLIDES;
	}
}



function LoadSlide(blnLoadAllPreviousBackgrounds){
	
	WriteToDebug("In Load Slide, SlideNum=" + intSlideNum + "  " + strMovieLocation);
	
	var intLayer;
	var strSlide;
	var strTempLocation;
	
	if (CallFunctionIntervalID != ""){
		window.clearInterval(CallFunctionIntervalID);
		CallFunctionIntervalID = "";
	}
	
	intLayer = GetCurrentLayer();
	
	//load up the skin when we start the presentation and the intro doesn't already end with it
	//(if the intro ends with the skin, we don't load it again b/c it generates a flicker)
	//or load it if we are resuming from a bookmark
	if ((intSlideNum==intStartOfSkin && !blnIntroEndsWithSkin) || 
	    (blnLoadAllPreviousBackgrounds && intSlideNum>=intStartOfSkin && intStartOfSkin != CONST_NONE)
	   ){
		WriteToDebug("Loading Skin");
		LoadMovie(LEVEL_SKIN, "../" + strMovieLocation + "skin.swf");		
	}


	if ((intSlideNum==intStartOfNavigation && !blnNavIsLoaded) ||
	    (blnLoadAllPreviousBackgrounds && intSlideNum>=intStartOfNavigation && intStartOfNavigation != CONST_NONE)
	   ){
		WriteToDebug("Loading Nav");
		ShowNav();
	}


	if (intStartOfOverlay != CONST_NONE && !blnOverlayIsLoaded){
		WriteToDebug("Loading Overlay");
		LoadMovie(LEVEL_OVERLAY, "../" + strMovieLocation + "overlay.swf");
	}

	if ((intSlideNum == intStartOfOverlay && !blnOverlayIsVisible) || 
	    (blnLoadAllPreviousBackgrounds && intSlideNum>=intStartOfOverlay && intStartOfOverlay != CONST_NONE)
	   ){
		WriteToDebug("Playing Overlay");
		objContent.TPlay("_level" + LEVEL_OVERLAY);
		blnOverlayIsVisible = true;
	}
	
	if (intSlideNum == OPTIONS_SLIDE){
		LoadOptionSelection();
		ResetBookmarkLocation();
	}
	else if(intSlideNum == FINISH_SLIDE){
		LoadFinishSlide();
		ResetBookmarkLocation();
	}
	else if(intSlideNum == LANGUAGE_SLIDE){
		LoadLanguageSlide();
	}
	else{
		strSlide = objLoader.GetSlide(intSlideNum)
		
		if (intSlideNum >= intStartOfContentDir && intStartOfContentDir != CONST_NONE){
			strTempLocation = "../" + strMovieLocation + strSlide
		}
		else{
			strTempLocation = strSlide;
		}
		
		WriteToDebug("Loading Movie - " + strSlide);
		LoadMovie(intLayer, strTempLocation);
		
		SetBookmarkLocationToCurrentSlide();
		SetProgress();
		
		if(intSlideNum == (intNumSlides - 1)){
			blnReachedLastSlide = true;
		}
		
	}
	
	
	//kill any previous timed function calls
	if (AdvanceAfterIntervalID != ""){
		window.clearInterval(AdvanceAfterIntervalID);
		AdvanceAfterIntervalID = "";
	}

	
	//before the start of the actual presentation, we want to advance automatically, so
	//we wait for the object to be loaded, then we periodically check for the slide to be done
	if (intSlideNum < intStartOfNavigation && intSlideNum >= 0){
		WriteToDebug("In Auto-Advance");
		
		AdvanceAfterIntervalID = window.setInterval("AdvanceAfterDonePlaying();", 500)	
	}
	
}


function LoadLanguageSlide(){
	
	//load a slide containing all of the appropriate text, 
	//if a file name is not specified, make it into a txt file
	
	var strTempLang;
	
	strLanguage = new String(strLanguage);
		
	if (strLanguage.indexOf(".") < 0){
		strTempLang = strLanguage + ".txt";
	}
	else{
		strTempLang = strLanguage;
	}
	
	WriteToDebug("Loading Language File - " + strTempLang);
	
	//set a variable in the root telling languageLoader what file to load
	objContent.SetVariable("_level" + LEVEL_EMPTY + ":strLanguageFile", strTempLang);
	LoadMovie(LEVEL_LANGUAGE, "languageLoader.swf");

}

function LoadFinishSlide(){
	WriteToDebug("Load Finish Slide");

	if (blnNavIsLoaded) {MakeLevelInvisible(LEVEL_NAV);}
	if (blnOverlayIsLoaded) {MakeLevelInvisible(LEVEL_OVERLAY);}
	if (blnSkinIsLoaded) {MakeLevelInvisible(LEVEL_SKIN);}
	
	LoadMovie(LEVEL_SLIDES, "complete.swf");
}



function LoadOptionSelection(){
	
	WriteToDebug("Load Options Section")

	LoadMovie(LEVEL_SKIN, "options.swf");
	
	//kill any previous timed function calls
	if (CallFunctionIntervalID != ""){
		window.clearInterval(CallFunctionIntervalID);
		CallFunctionIntervalID = "";
	}
	
	WriteToDebug("calling CallFunctionAfterLoaded")
	CallFunctionIntervalID = window.setInterval("CallFunctionAfterLevelIsLoaded(1, 'LoadSpecificOptions();')", 100);
	
}


function LoadSpecificOptions(){
	
	WriteToDebug("Loading Specific Options")
	
	if (CallFunctionIntervalID != ""){
		window.clearInterval(CallFunctionIntervalID);
		CallFunctionIntervalID = "";
	}

	if (blnHasSoundOption){
		WriteToDebug("Loading Sound Option")
		objContent.TGotoFrame("_level1.sound/", 2);
	}
	
	if (strBookmarkedLocation != "" && strBookmarkedLocation != "0"){
		WriteToDebug("Loading Bookmark Option")
		objContent.TGotoFrame("_level1.resume/", 2);
	}
}


function HasSlideStoppedItSelf(){
	
	if (blnIE) {
		return IEHasSlideStoppedItself();
	}
	else{
		return NSHasSlideStoppedItself();
	}
}


function NSHasSlideStoppedItself(){
	var strStopped = "";
	
	strStopped = objContent.GetVariable("_level" + GetCurrentLayer() + ":Stopped");
	
	strStopped = new String(strStopped)
	strStopped = strStopped.toUpperCase();
	
	if (strStopped == "STOP"){
		return true;
	}
	else{
		return false;
	}
}


function CallFunctionAfterLevelIsLoaded(intLevel, strFunction){
	
	var blnLoaded = false;
	
	if (blnIE){
		blnLoaded = IEIsMovieLoaded(intLevel)
	}
	else{
		blnLoaded = NSIsMovieLoaded(intLevel)
	}
	
	if (! blnLoaded){
		return;
	}
	else{
		eval(strFunction);
	}
	
}


function NSIsMovieLoaded(intLevel){

	var intFramesLoaded
	var intTotalFrames
	var blnLoaded	
	
	blnLoaded = false
	
	intTotalFrames = objContent.TGetProperty("_level" + intLevel + "/", 5);	//prop 5 = total frames - 0 based
	intFramesLoaded = objContent.TGetProperty("_level" + intLevel + "/",  12);  //prop 12 = frames loaded
	
	intTotalFrames = parseInt(intTotalFrames);
	intFramesLoaded = parseInt(intFramesLoaded);
	
	if (isNaN(intTotalFrames) || intTotalFrames == null || intTotalFrames == "") {
		blnLoaded = false;
	}
	else{
		blnLoaded = (intFramesLoaded >= (intTotalFrames - 1))
	}
	
	return blnLoaded;

}


function AdvanceAfterDonePlaying(){
	var blnPlaying;
	var intLayerID
	
	intLayerID = GetCurrentLayer()

	if (blnIE){
		blnPlaying = IEIsCurrentSlideLessThanTotalSlides(intLayerID)
	}
	else{
		blnPlaying = NSIsCurrentSlideLessThanTotalSlides(intLayerID)
	}
	
	if (blnPlaying){
		return;
	}
	else{
		LoadNextSlide();
		return;
	}
}


function NSIsCurrentSlideLessThanTotalSlides(intLayerID){
	var intTotalSlides
	var intCurrentSlide
	
	intCurrentSlide = objContent.TGetProperty("_level" + intLayerID + "/",  4);  //prop 4 = current frame
	intTotalSlides = objContent.TGetProperty("_level" + intLayerID + "/", 5);	//prop 5 = total frames - 0 based
	
	intCurrentSlide = parseInt(intCurrentSlide);
	intTotalSlides = parseInt(intTotalSlides);
	
	if (isNaN(intCurrentSlide) || intCurrentSlide == null || intTotalSlides==null){
		return false;
	}
	else{
		return (intCurrentSlide < intTotalSlides)
	}
	
}


function SetProgressAndExpertInfo(){

	SetProgress();
	SetExpertInfo();
	
	if (SetProgressFunctionIntervalID != ""){
		window.clearInterval(SetProgressFunctionIntervalID);
		SetProgressFunctionIntervalID = "";
	}	
}

function SetExpertInfo(){
	objContent.SetVariable("_level" + LEVEL_NAV + ":ExpertEmail", strExpertEmail);
	objContent.SetVariable("_level" + LEVEL_NAV + ":ExpertName", strExpertName);
	objContent.SetVariable("_level" + LEVEL_NAV + ":ErrorMessage", "");
}


function ShowNav(){
	
	strNavType = new String(strNavType);
	strNavType = strNavType.toUpperCase();
	
	if (strNavType == TYPE_FLASH){
		LoadMovie(LEVEL_NAV, strNav + ".swf");
		
		if (CallFunctionIntervalID != ""){
			window.clearInterval(CallFunctionIntervalID);
			CallFunctionIntervalID = "";
		}
	
		WriteToDebug("calling CallFunctionAfterLoaded for SetProgress;SetExpertInfo")
		SetProgressFunctionIntervalID = window.setInterval("CallFunctionAfterLevelIsLoaded(LEVEL_NAV, 'SetProgressAndExpertInfo();')", 100);
		
	}
	else if (strNavType == TYPE_HTML){
		void showLyr(strNav)
	}
	else{
		alert("Invalid Nav Type " + strNavType + ". Check configuration settings.")
	}
	
}


function HideNav(){
	strNavType = new String(strNavType);
	strNavType = strNavType.toUpperCase();
	
	if (strNavType == TYPE_FLASH){
		if (blnNavIsLoaded) {MakeLevelInvisible(LEVEL_NAV)}
	}
	else if (strNavType == TYPE_HTML){
		void hide(strNav)
	}
	else{
		alert("Invalid Nav Type + " + strNavType + ". Check configuration settings.")
	}
		
	blnNavIsLoaded = false;
}


function LoadMovie(intLevel, strMovie){

	objContent.LoadMovie(intLevel, strMovie);
	
	switch(intLevel){
	
		case LEVEL_NAV:
			blnNavIsLoaded = true;
		break;
		
		case LEVEL_SLIDES:
			blnSlideIsLoaded = true;
		break;
		
		case LEVEL_SKIN:
			blnSkinIsLoaded = true;
		break;
		
		case LEVEL_OVERLAY:
			blnOverlayIsLoaded = true;
		break;
					
	}
}


function CheckForCommand(e) {
	var intKeyCode = 0;			
	if (blnIE) {
		e = window.event;
		intKeyCode = e.keyCode;
	}
	else if (! blnIE) {
		intKeyCode = e.which;
	}
			
	if ((intKeyCode == ASCII_RETURN || intKeyCode == ASCII_SPACE) && blnNavIsLoaded){
		if (!blnPaused){
			Next();
		}
	}


	if (intKeyCode == ASCII_TILDA){
		intTildaCounter++;
		if (intTildaCounter == 3){
			intTildaCounter = 0;
			ShowDebugWindow();
		}
	}
	else{
		intTildaCounter = 0;
	}
	
}



function WriteToDebug(strInfo){
	
	if (objLoader != null){
		objLoader.WriteToDebug(window.location.pathname, strInfo);
	}
}


function ShowDebugWindow(){
	winDebug = window.open("", "Debug", "width=600,height=300,resizable,scrollbars");
	winDebug.document.open("text/plain");
	winDebug.document.write(objLoader.GetDebugInfo().toString());
}


function GetBookmarkLocation(){
	var strBookmark;
	
	strBookmark = objLMS.GetBookmark();
	
	WriteToDebug ("Retrieved Bookmark: " + strBookmark);
	return strBookmark;
}

function SetBookmarkLocationToCurrentSlide(){

	if (intSlideNum != OPTIONS_SLIDE){
		WriteToDebug("Setting bookmark to " + intSlideNum);		
		
		objLMS.SetBookmark(intSlideNum);
		
		intBookmarkCommitCounter ++;
		if (intBookmarkCommitCounter >= intBookmarkCommitFrequency){
			objLMS.CommitData();
			intBookmarkCommitCounter = 0;
		}		
	}
}

function ResetBookmarkLocation(){
	WriteToDebug("Resetting Bookmark");
	objLMS.SetBookmark("");
}


function SetProgress(){

	var blnNavLayerIsLoaded
	
	WriteToDebug("In SetProgress, NavIsLoaded=" + blnNavIsLoaded)
	
	if (blnIE){
		blnNavLayerIsLoaded = IEIsMovieLoaded(LEVEL_NAV)
	}
	else{
		blnNavLayerIsLoaded = NSIsMovieLoaded(LEVEL_NAV)
	}
	
	if (blnNavIsLoaded && blnNavLayerIsLoaded){
		objContent.SetVariable("_level" + LEVEL_NAV + ":intMaxProgress", (intNumSlides - intStartOfNavigation));
		objContent.SetVariable("_level" + LEVEL_NAV + ":intCurrentProgress", (intSlideNum - intStartOfNavigation) + 1);
	
	}	
}






function ResetReadyForNextCommand(){
	blnReadyForNextCommand = true;
	
	if (WaitOnReadyIntervalID != ""){
		window.clearInterval(WaitOnReadyIntervalID);
		WaitOnReadyIntervalID = "";
	}
		
}



function Next(){
	if (blnReadyForNextCommand){
	
		blnReadyForNextCommand = false;
		
		LoadNextSlide(blnUseExitTransitions);
		
		
		if (WaitOnReadyIntervalID != ""){
			window.clearInterval(WaitOnReadyIntervalID);
			WaitOnReadyIntervalID = "";
		}
	
		WriteToDebug("calling CallFunctionAfterLoaded for ResetReadyForNextCommand - Next")
		WaitOnReadyIntervalID = window.setInterval("CallFunctionAfterLevelIsLoaded(LEVEL_NAV, 'ResetReadyForNextCommand();')", 1000);

		
	}
}

function Back(){
	
	if (blnReadyForNextCommand){
	
		blnReadyForNextCommand = false;
		
		LoadPreviousSlide();
		
		
		if (WaitOnReadyIntervalID != ""){
			window.clearInterval(WaitOnReadyIntervalID);
			WaitOnReadyIntervalID = "";
		}
	
		WriteToDebug("calling CallFunctionAfterLoaded for ResetReadyForNextCommand - Previous")
		WaitOnReadyIntervalID = window.setInterval("CallFunctionAfterLevelIsLoaded(LEVEL_NAV, 'ResetReadyForNextCommand();')", 1000);

		
	}	
}

function Pause(){
	FlashPause();
}

function UnPause(){
	FlashResumeAfterPause();
}

function Punt(){
	Finish(EXIT_TYPE_PUNT);
}

function Restart(){
	intSlideNum = intStartOfNavigation;
	LoadSlide(false);
}

function Finish(strExitType){

	var strStatusAfterCompletion
	var blnOk = true;
	if (!blnLMSFinishCalled){
		
		WriteToDebug("Exiting with exit type=" + strExitType)
		
		blnLMSFinishCalled = true;
		
		blnOk = objLMS.Finish(blnReachedLastSlide, strExitType);
		
		ClearMovies();
		
	}
	
	if (! blnOk){
		alert("Error - content did not complete properly.\nPlease contact technical support.");
	}
	
	return blnOk;
}

function ClearMovies(){
	if (blnSlideIsLoaded) {MakeLevelInvisible(LEVEL_SLIDES);}
	if (blnNavIsLoaded) {MakeLevelInvisible(LEVEL_NAV);}
	if (blnOverlayIsLoaded) {MakeLevelInvisible(LEVEL_OVERLAY);}
	if (blnSkinIsLoaded) {MakeLevelInvisible(LEVEL_SKIN);}
}



function SendEmailFromFlash(strArgs){

	//strArgs is formatted ToName + \n + ToAddress + \n + FromName + \n + FromAddress + \n
	//					   + Subject + \n + Body
	
	var strToName;
	var strToAddress;
	var strFromName;
	var strFromAddress
	var strSubject;
	var strBody;
	
	var intPos;
	var intOldPos;
	
	var blnSuccess = true;
	
	var strErrorMessage = "Error communicating with server, mail may not have been sent.";
	
	WriteToDebug("In SendEmailFromFlash strArgs=" + strArgs);
		
	strArgs = new String(strArgs);

	intPos = strArgs.indexOf("\n");
	strToName = strArgs.substr(0, intPos);

	intOldPos = intPos;
	intPos = strArgs.indexOf("\n", intPos + 1);
	strToAddress = strArgs.substr(intOldPos + 1, (intPos - intOldPos) - 1);
	
	if(! ValidEmailAddress(strToAddress)){
		blnSuccess = false;
		strErrorMessage = "Invalid recipient, please contact your system administrator.";
	}
	
	intOldPos = intPos;
	intPos = strArgs.indexOf("\n", intPos + 1);
	strFromName = strArgs.substr(intOldPos + 1, (intPos - intOldPos) - 1);

	intOldPos = intPos;
	intPos = strArgs.indexOf("\n", intPos + 1);
	strFromAddress = strArgs.substr(intOldPos + 1, (intPos - intOldPos) - 1);
	
	if (strFromAddress.length == 0){
		blnSuccess = false;
		strErrorMessage = "Invalid e-mail address, please enter a valid e-mail address.";
	}
	
	intOldPos = intPos;
	intPos = strArgs.indexOf("\n", intPos + 1);
	strSubject = strArgs.substr(intOldPos + 1, (intPos - intOldPos) - 1);

	strBody = strArgs.substr(intPos + 1);
	
	if (blnSuccess){
		blnSuccess = SendEmail(strToName, strToAddress, strFromName, strFromAddress, strSubject, strBody);
	}
	
	if (blnSuccess){
		//tell flash to goto success frame
		objContent.TGotoFrame("_level" + LEVEL_NAV, FLASH_EMAIL_SUCCESS_FRAME);
		objContent.TPlay("_level" + LEVEL_NAV);
	}
	else{
		//give flash an error message and tell it to goto error frame
		objContent.SetVariable("_level" + LEVEL_NAV + ":ErrorMessage", strErrorMessage);
		objContent.TGotoFrame("_level" + LEVEL_NAV, FLASH_EMAIL_FAIL_FRAME);
		objContent.TPlay("_level" + LEVEL_NAV);
	}
	
}


function ValidEmailAddress(tmp) {
	var str = new String(tmp);
	var len = str.length
	var exclude=/[^@\'\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	if(((str.search(exclude) == -1)&&(str.search(check)) != -1)&&(str.search(checkend) != -1)) return(true);
	return(false);
}

function SendEmail(strToName, strToAddress, strFromName, strFromAddress, strSubject, strBody){
	
	var blnSuccess;
	var strPath;
	
	
	strPath = window.document.location.pathname;
	strPath = strPath.slice(0, strPath.lastIndexOf("/") + 1);
	strPath = strPath + EMAIL_ACCEPTOR_PAGE;
	
	blnSuccess = objLoader.SendEmail(window.document.location.host, strPath, strToName, strToAddress, strFromName, strFromAddress, strSubject, strBody, window.document.cookie);
	SetCookies();
	
	
	return blnSuccess;
}








function content_DoFSCommand(command, args) { 
	
	//prevent double clicks from counting twice and prevent anything from happening after finish is called
	
	if (! blnExecutingCommand && ! blnLMSFinishCalled){		
		
		blnExecutingCommand = true;
		
		WriteToDebug("Command recieved from flash: command=" + command + " args=" + args)
	
		command = new String(command);
		command = command.toUpperCase();
	
		switch (command){
	
			case "NEXT":
				Next();
			break;
			
			case "BACK":
				Back();
			break;
			
			case "PAUSE":
				Pause();
			break;

			case "UNPAUSE":
				UnPause();
			break;

			case "PUNT":
				Punt();
			break;				

			case "RESTART":
				Restart();
			break;
			
			case "FINISH":
				Finish(EXIT_TYPE_FINISHED);
			break;
			
			case "SETSOUNDOPTION":
				SetSoundOption(args);
			break;
			
			case "SETRESUMEOPTION":
				SetResumeOption(args);
			break;
			
			case "EMAIL":
				SendEmailFromFlash(args);
			break;		
	
		} //closes switch
		
		blnExecutingCommand = false;
		
	}//closes if (!blnExecutingCommand)
} 



function MakeLevelInvisible(intLevel){
	objContent.TSetProperty("_level" + intLevel + "/", 7, 0);
}


function MakeLevelVisible(intLevel){
	objContent.TSetProperty("_level" + intLevel + "/", 7, 1);
}



   
function AddFormElement(strElement, strValue){
    objLoader.AddFormElement(strElement, strValue);
}
    
function RemoveFormElement(strElement){
    objLoader.RemoveFormElement(strElement);
}
    
function ClearForm(){
    objLoader.ClearForm();
}
    
function SubmitForm(strServer, strReceivingPage){
	var strReturn;
	strReturn = objLoader.SubmitForm(strServer, strReceivingPage, window.document.cookie);
	SetCookies();
	return strReturn;
}


function SetCookies(){
	var strCookie;
	
	strCookie = objLoader.GetNextCookie() + "";
	strCookie = new String(strCookie);
	while (strCookie.length > 0 ){
		window.document.cookie = strCookie;
		strCookie = objLoader.GetNextCookie();
	}
	
}


