
var DEBUG = 1;
var count = 0;


// =================================================
// this function is LMS SPECIFIC! 
// it calls the LMS menu/toc structure
// each LMS usually does this differently
function getTOC( relPath ) {

	// finish sco
	endSCO();
	this.location = relPath + "/index.html"
		//top.window.close();
}

// = = = = = == = = = = = = = = = = = = =
function mailpage()
{
  mail_str = "mailto:Cont.Ed.TechnicalAssistance@occ.treas.gov?subject=" + document.title + " Web-Based Training";
  mail_str += "&body= Comment on page";
  mail_str += ": " + location.href; 
  location.href = mail_str;
}

function openNewWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


// =================================================
// start up a new sco. Generally called in the onLoad of body tag 
// of the first page of a SCO
function beginSCO() {

	LMSInitialize();
	// get existing status
	var status = LMSGetValue( "cmi.core.lesson_status" );
	
	error_report( 'get lesson_status after' );
	
	if (status == "not attempted") {
		// the student is now attempting the lesson
		result = LMSSetValue( "cmi.core.lesson_status", "incomplete" );
	
	} else {
		// Ask user if they want to return to their last
		// bookmarked location.
		// If they do, go there
		// If they don't, then stay here and save current page
		// as the last visited page.
		var lastpage = getLessonLocation();

		// make sure we have a last page to return to.
		if ( lastpage == '' ) {
			setBookmark( window.location );
			return;
		}

		// we have a bookmarked location
		

		if ( confirmBookmark( lastpage ) ) {
			returnToBookmark( lastpage );
		} else {
			setBookmark( window.location );
		}		
	} 
}


// confirm that you want to return to the last bookmarked page
function confirmBookmark( title ) {

        result = confirm('Do you wish to return to your last location (' + title + ')?');

        if ( result ) {
                return true;
        } else {
                return false;
        }

}

// =================================================
// Set the lesson_status of the SCO to complete
// This DOES NOT end or finish the SCO. 
// Generally called on the last of a SCO
// that does not contain any testing
function completeSCO() {

	<!-- setBookmark( window.location );	 -->
	setLessonStatus( "completed" );
	setScore("90");
}

function endSCO() {

	LMSSetValue( "cmi.core.exit", "logout" );

	// commit all our data so we know it gets stored
	LMSCommit();

	// NOTE: LMSFinish will unload the current SCO.  All processing
	//       relative to the current page must be performed prior
	//		 to calling LMSFinish.
	LMSFinish();
}

// =================================================
// see which core functions the LMS supports
function supportCheck( LMSobject ) {	
	
	// default object to core if none provided
	if ( LMSobject == "" ) { LMSobject = "core"; }
	
	// get comma separated value of supported elements from LMS
	var obj_children = LMSGetValue( "cmi." + LMSobject + "._children" );
	
	// split into array based on comma
	var kids = obj_children.split(',');
	
	// open the window
	w = window.open(	"", 
						"LMS_support", 
						"resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=400,height=300");
	
	// write to it
	w.document.open();
	w.document.writeln("<html><head><title>LMS SCORM " + LMSobject + " Support</title>");
	w.document.writeln("</head><body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000ff\" vlink=\"#800080\" alink=\"#ff3333\"><h2>cmi." + LMSobject + " supported elements</h2><ul>");
	
	for ( var i=0; i < kids.length; i++ ) {
		w.document.writeln("<li>" + kids[i] + "</li>");
	}
	w.document.writeln("</ul></body></html>");
	w.document.close();
	
	// bring it to the front
	w.focus();
}







//-----------LESSON STATUS-------------
function getLessonStatus() {

  return LMSGetValue( "cmi.core.lesson_status" );
}


function setLessonStatus( status ) {

	// need to filter for valid status values
	// then send the data if we're ok.
	
	LMSSetValue( "cmi.core.lesson_status", status );           
}







//------------ LESSON LOCATION ----------------
function getLessonLocation() {

	return LMSGetValue( "cmi.core.lesson_location");
}

function setLessonLocation( this_location ) {

	LMSSetValue( "cmi.core.lesson_location", this_location );
	LMSCommit();
}

function getFilename ( this_location ) {
	
	// get path part of URL
	var this_pathname = this_location.pathname;
	
	// split on /
	var dirs = this_pathname.split('/');
	// filename is last element
	return dirs[dirs.length-1];	
}

function setBookmark( this_location ) {
	
	
	var filename = getFilename( this_location ); 
	
	// set lesson location value 
	setLessonLocation( filename );
	
	// set current value to suspend in case they leave.
	LMSSetValue( "cmi.core.exit", "suspend" );
}

function returnToBookmark( lastpage ) {

	var this_location = window.location;
	
	var this_pathname = this_location.pathname;
	var new_pathname  = "";
	
	
	// split on /
	var dirs = this_pathname.split('/');
	// set filename (last value) to the lastpage we got
	dirs[dirs.length-1] = lastpage;
	
	for ( var i=0; i < dirs.length; i++ ) {
		new_pathname = new_pathname  + "/" + dirs[i];
	}
	// remove first character
	new_pathname = new_pathname.slice(1);
	
	//alert2( this_pathname + "\n" + new_pathname );
	
	// set the location parameter with the new pathname
	// and apparently jump to the page
	this_location.pathname = new_pathname;
}



// ------------SCORE----------------    

function setScore( score ) {			
	// the value must be sent as a string... so try eval ing it.
	//LMSSetValue( "cmi.core.score.raw", score.toString() );
	
	LMSSetValue( "cmi.core.score.raw", score );
	LMSCommit();
}
	
	




//=====================================================
// error reporting function
function error_report( note ) {

	if ( DEBUG == 0 ) { return; }
	count++;

	var lastError  = LMSGetLastError();

	if (lastError != 0 ) {
	
		var errorStr   = LMSGetErrorString( lastError );
		var diagnostic = LMSGetDiagnostic( lastError )
	
		// open the window
		w = window.open(	"", 
							"LMS_support" + count, 
							"resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=400,height=300");
		
		// write to it
		w.document.open();
		w.document.writeln("<html><head><title>LMS SCORM Error</title>");
		w.document.writeln("</head><body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000ff\" vlink=\"#800080\" alink=\"#ff3333\"><h2>" + note + "</h2>");
		w.document.writeln("<p>error code: " + lastError );
		w.document.writeln("<p>error string: " + errorStr );
		w.document.writeln("<p>diagnostic: " + diagnostic );
		w.document.writeln("</body></html>");
		w.document.close();
		
		// bring it to the front
		w.focus();
	}

}

//=====================================================
// error reporting function
function alert2( note ) {

	if ( DEBUG == 0 ) { return; }
	
	// open the window
	w = window.open(	"", 
						"LMS_support" + count, 
						"resizable=1,toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,copyhistory=0,width=400,height=300");
	
	// write to it
	w.document.open();
	w.document.writeln("<html><head><title>LMS SCORM Error</title>");
	w.document.writeln("</head><body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000ff\" vlink=\"#800080\" alink=\"#ff3333\">");
	w.document.writeln("<p>" + note );
	w.document.writeln("</body></html>");
	w.document.close();
	
	// bring it to the front
	w.focus();
}
