//following line needed for cross domain use in Oracle iLearning
//var API = window;
var myAPI;
var findAPITries = 0;
var LMSInitialized;
var myIntervalID;

if (fileLMS)
{
	checkAccess('../');
	var myTempUser = getCookieValue("username","");
	var myTempPwd = getSubCookieValue("pwd",getCookieValue("KU_"+myProgID+"_"+myTempUser,""),"");
  var myCurSCO = getURLValue("sco");
}

function findAPI(win)
{
	while ( (win.API == null) && (win.parent != null) && (win.parent != win))
	{
		findAPITries++;
		if (findAPITries > 7)
		{
			return null;
		}

		win = win.parent;
	}
	return win.API;

}

function getAPI()
{
	var theAPI = findAPI(window);
	
	if ((theAPI == null) && (window.opener != null) && (typeof(window.opener) != "undefined"))
	{
		theAPI = findAPI(window.opener);
	}
	return theAPI
}

function runCMI(myStartPage,myX,myY,myS) {

	if (!fileLMS)
		myAPI = getAPI();
	else
		myAPI = null;

	var myLocation = null;
	var myURL = "nothing";
	var myPage = "page001";
	if ((myStartPage != null) && (myStartPage != ""))
		myPage = myStartPage;
	var myWidth = "790";
	if ((myX != null) && (myX != ""))
		myWidth = myX;
	var myHeight = "575";
	if ((myY != null) && (myY != ""))
		myHeight = myY;
	var myScroll = "no";
	if ((myS != null) && (myS != ""))
		myScroll = myS;
	var myStatus = null;
	
	if (myAPI != null)
	{
		if (myAPI.LMSInitialize('') == "true")
			LMSInitialized = true;
			
		if (myAPI.LMSGetLastError() != 0)
			LMSInitialized = false;
	}

	if (LMSInitialized)
	{
		//get location
		myLocation = myAPI.LMSGetValue("cmi.core.lesson_location");
		if (myLocation != '')
		{
			myLocation = myLocation.toLowerCase();
			if ((myLocation != 'begin') && (myLocation != 'end') && (myLocation != '0') && (myLocation != '1'))
			{
				if (myLocation.substring(0,4) == 'page')
					myPage = myLocation;
				else
					alert('Unable to recognize Bookmark.  Will default to the beginning of this module.');
			}
		}
		if (myPage != "page001")
		{
			myAns = confirm("Do you wish to continue where you left off?"," ")
			if (myAns == false)
				myPage = "page001";
		}
		
		//set status if needed
		myStatus = myAPI.LMSGetValue("cmi.core.lesson_status");
		myStatus = myStatus.toLowerCase();
		if ((myStatus == 'not attempted') || (myStatus == 'n') || (myStatus == 'na'))
		{
			myAPI.LMSSetValue("cmi.core.lesson_status","incomplete");
		}
	}
	else
	{
		//see if there is a file-based LMS available
		if (fileLMS)
		{		
			//get values from URL
			myStatus = getURLValue("status");
			//set status if needed and get bookmark
			myStatus = myStatus.toLowerCase();
			if ((myStatus == 'na') || (myStatus == '') || (myStatus == 'c'))
			{
				mypageNum = myPage.substring(4,myPage.length)
				if (myStatus != 'c')
					setSCOStatus(myCurSCO,mypageNum);
			}
			else
				mypageNum = myStatus;
			if (Math.abs(mypageNum) > 0)
				myPage = "page" + mypageNum;
			if (myPage != "page001")
			{
				myAns = confirm("Do you wish to continue where you left off?"," ")
				if (myAns == false)
					myPage = "page001";
			}
		}
		//else
			//alert("Unable to connect to a LMS.");
	}

	if (W3CDOM)
	{
		document.getElementById("PleaseWait").style.visibility = 'hidden';
		document.getElementById("Running").style.visibility = 'visible';
	}
	else
	{
		if (InternetExplorer)
		{
			PleaseWait.style.visibility = 'hidden';
			Running.style.visibility = 'visible';
		}
		else
		{
			document.PleaseWait.visibility = 'hidden';
			document.Running.visibility = 'visible';
		}
	}
	
	if (getURLValue("location",9)=="yes")
		var myLocation = "yes";
	else
		var myLocation = "no";
		
	ConWin=open(myPage + ".htm","ContentWin","scrollbars=" + myScroll + ",resizeable=no,width=" + myWidth + ",height=" + myHeight + ",toolbar=no,directories=no,menubar=no,status=yes,location=" + myLocation + ",");
	myIntervalID = setInterval("CheckWin();",1000);
}

function CheckWin()
{
	if (ConWin.closed)
	{
		//get last location and use if valid
		myPage="page001.htm";
		if (LMSInitialized)
		{
			myLocation = myAPI.LMSGetValue("cmi.core.lesson_location");
			if (myLocation != '')
			{
				myLocation = myLocation.toLowerCase();
				if ((myLocation != 'begin') && (myLocation != 'end') && (myLocation != '0') && (myLocation != '1'))
				{
					if (myLocation.substring(0,4) == 'page')
						myPage = myLocation + ".htm";
				}
			}
		}
		ExitAU(myPage,"0");
	}
}

function sendBookmark(myLoc)
{
	myLoc = myLoc.toLowerCase();
	myStart = myLoc.length - 11;
	myEnd = myLoc.length - 4;
	myPage = myLoc.substring(myStart,myEnd);
	if (LMSInitialized)
	{
		myAPI.LMSSetValue("cmi.core.lesson_location",myPage);
		myAPI.LMSCommit('');
		//alert(myAPI.LMSGetLastError());
	}
	if (fileLMS)
	{
		//get status
		myStatus = getSCOStatus(myCurSCO);
		//set status if needed
		if (myStatus != 'c')
		{
			mypageNum = myPage.substring(4,myPage.length)
			setSCOStatus(myCurSCO,mypageNum);
		}
	}
}

function ExitAU(myLoc,myDone)
{
	//stop timer
	if (myIntervalID != "")
		clearInterval(myIntervalID);
		
	if ((LMSInitialized) || (fileLMS))
	{
		myPage = "end";
		myScore = 0;
		myLoc = myLoc + '';
		myLoc = myLoc.toLowerCase();
		
		//get status
		if (LMSInitialized)
			myStatus = myAPI.LMSGetValue("cmi.core.lesson_status");
		else
		{
			myStatus = getSCOStatus(myCurSCO);
		}
		
		myStatus = myStatus.toLowerCase();
		
		if (myDone == "0")
		{
			//all I need to do is update location
			myStart = myLoc.length - 11;
			myEnd = myLoc.length - 4;
			myPage = myLoc.substring(myStart,myEnd)
			//myScore = Math.abs(myPage.substring(4,7));
		}
		
		if (myDone == "1")
		{
			//set status if necessary and get score
			if (LMSInitialized)
			{
				if (myStatus == "incomplete")
					myAPI.LMSSetValue("cmi.core.lesson_status","completed");
			}
			else
			{
				if (fileLMS)
					setSCOStatus(myCurSCO,"c");
			}
			
			myStart = myLoc.length - 11;
			myEnd = myLoc.length - 4;
			myTemp = myLoc.substring(myStart,myEnd)
			//myScore = Math.abs(myTemp.substring(4,7));
		}
		
		if (myDone == "2")
		{
			if (LMSInitialized)
				myAPI.LMSSetValue("cmi.core.lesson_status","passed");
			else
			{
				if (fileLMS)
					setSCOStatus(myCurSCO,"c");
			}
				
			myScore = myLoc;
		}
		
		if (myDone == "3")
		{
			if (myStatus == "incomplete") 
			{
				myAPI.LMSSetValue("cmi.core.lesson_status","failed");
			}
			myScore = myLoc;
		}
		
		//set location and score
		if (LMSInitialized)
		{
			myAPI.LMSSetValue("cmi.core.lesson_location",myPage);
      if ((myDone == "2") || (myDone == "3"))
  			myAPI.LMSSetValue("cmi.core.score.raw",myScore);
			myAPI.LMSCommit('');
			myAPI.LMSFinish('');
		}
	}
	if (W3CDOM)
	{
		document.getElementById("Running").style.visibility = 'hidden';
		document.getElementById("Done").style.visibility = 'visible';
	}
	else
	{
		if (InternetExplorer)
		{
			Running.style.visibility = 'hidden';
			Done.style.visibility = 'visible';
		}
		else
		{
			document.Running.visibility = 'hidden';
			document.Done.visibility = 'visible';
		}
	}
	
	if (fileLMS)
		location.href="../relogin.htm?username=" + myTempUser + "&password=" + myTempPwd;
}


