// this.js -- MUST BE IN SAME FOLDER AS THE CBT 
/* FLASH CANNOT AUTOMATICALLY SET UP THE HIDDEN CBT DATA FORMS ON THE HOST HTML PAGE.  
...EACH CBT-HOST HTML PAGE USE ITS OWN SCRIPT TO WRITE THE CORRECT NUMBER OF HIDDEN FIELDS WHEN IT LOADS, because, 
(1) If Flash calls the JS functions, the browser will write the new html to a new window instead of this host-page.  
(2) If the host page calls a Flash function that returns the numActivities variable, it will end up calling the function before Flash has completely loaded; thus, the HTML page can't get the numActivities variable when loading the tutorial. 
(3) For each CBt, you have to tweek this JS file, store it in the same directory as the .swf so that it conforms to the 
number of fields necessary for the tutorial it hosts.
/*
TEMPORARY USE OF EMAIL TO TRACK COMPLETIONS AND TO TRACK OTHER USAGE THAT IS NOT COMPLETE
The hidden forms are updated after every saved task in the Flash CBT.
If a user completes the tutorial and presses "Send Completion Email" from the progress page, this script will send
the CPOD content manager a completion email containing full tracking data and using the subject line as the key for sorting the email by rule in Outlook.  It uses a "completed" status code and the user's first and last name in the subject line.

If a user (cbt complete or incomplete) exits the CBT using any other method, a different full-data email will be generated, again using the subject line to sort by rule in Outlook.  Since no name would be collected, the subject line would be %-complete followed by unit-name. This could be used to report 
*/
var g_numCpodFormHeaderFields = 8; // value needed for dynamically indexing Activity fields 
/*  DO NOT EDIT BELOW THIS LINE */
var g_movieName; // returned from getMovieName();
var g_CpodSubject;
var remoteExitHit = false; // Gets reset by arg1 when AS calles populate_UserForms()
					// Forces a JS email-send action.  Prevents duplicate action when browser closes after RemoteExit has been hit.
var emailType = 0; // 0=no-name report, 1=named-completion;
var userSendingEmail = false; // false avoids sending emails when hidden fields are being updated w/o exit or progress emailing.
var dateStr = ""; // see date function below
var theTimeOut; // Sometimes server has ignored form submissions - so use timeout and see if it helps.
var minCompleteScore = 1.00; // reset when form headers are updated - used to branch between browser-exit subject-lines
var currentScore = 0; // reset during form updating, but used upon browser close to reroute last-min. passing scores to CMgr.
var fname = "User";
var lname = "Unknown"; // set when form headers are updated - used by various functions even by browser-exit subject-line.
var emailNotSent = true;
var emailingON = false;
var CBT_name;
var UserPctComplete;
var UserActiveTime;
var UserElapsedSecs;
var recClass;

// Called upon Browser by onunload= event in body tag 
function unload_sendData(){		
	/*
	If unloading page because the tutorial remote exit button was hit, then AS has already processed everything - drop through.
	If user HIT BROWSER-CLOSE, then BROWSER WON'T TALK TO FLASH - you get whatever is in the form, run this code.
	*/
	//alert("remoteExitHit = " + remoteExitHit + ", currentScore = " + currentScore + ", minScore = " + minCompleteScore);
	
	
	if((remoteExitHit==false)  && emailNotSent){
		var integerScore = Math.round(currentScore*100);
		var integerMinScore = Math.round(minCompleteScore*100);
		if(integerScore>100){ integerScore = 100; }
		
		if(integerScore<integerMinScore){ 						
			document.cpodEmail.subject.value = "AUP CBT: " + integerScore + "%"; 
		}
		else{ document.cpodEmail.subject.value = "AUP CBT COMPLETE"; }
		//alert("Calling sendEmail()");
		sendEmail();		// No need call reset_ExitVariables() because page is closing no matter what!
	}
	
}
// remoteExitHit=true, is called by fillActivityStatusFields() before exit.
function close_tutorial(){
	userSendingEmail = true; // YOU MAY NEED TO DISABLE THE EMAIL ON EXIT FUNCTIONALITY FOR SOME TUTORIALS
	if(emailNotSent){ sendEmail(); }// No name-fetching email panel open - so none to close.
	self.close();
}
function reset_ExitVariables(){
		remoteExitHit = false; 
		userSendingEmail = false;
		//getMovieName resolves its string argument to a Flash object reference based on browser type.
		//to avoid problems directly naming the object id in Mozilla etc.
		g_movieName.reset_ExitVars();// resets AS exit variables.
}
function confirmComplete(path){
		window.location.href = path;
}
// Following function (and all its nested functions) are called every time AS nextTask() is called w/in the tutorial.
// This updates HTML forms regularly and userSending determines if data is sent via email.
function fill_FormHeaders(exitBtnHit, userSending, minScore, firstName, lastName, tutorialName, UPC, time, secs)
{
	emailingON = true;
	if(exitBtnHit){ remoteExitHit = true; }
	if(userSending){ userSendingEmail = true; }
	minCompleteScore = minScore;
	UserPctComplete = UPC;	
	currentScore = UserPctComplete*.01;
	fname = firstName;
	lname = lastName;
	CBT_name = tutorialName;
    UserActiveTime = time;
    UserElapsedSecs = secs;
	
	var integerScore = Math.round(currentScore*100);
	var integerMinScore = Math.round(minCompleteScore*100);
	if(integerScore>100){ integerScore = 100; }
	
	if(integerScore<integerMinScore){ 	g_CpodSubject = "AUP CBT: " + integerScore + "%"; }
	else{ g_CpodSubject = "AUP CBT COMPLETE" ; }
	fill_CpodFormHeader();
	//fill_CmFormHeader();
	//FILLING ACTIVITY STATUS FORM FIELDS  - see notes in AS_to_JS.as
	//alert("Ready to call.... AS - fillFormStatusFields");
	g_movieName.fillFormStatusFields();
	
}
// CONTENT MANAGER & CPOD FACILITATOR FORM
// fn, ln, tutorialName, UPC, time, secs,  UPR, UPL, UDL, O1PC, O2PC, O3PC, O4PC, O1PD, O2PD, O3PD, O4PD
//function fill_CmFormHeader(fn, ln, tutorialName, UPC, time, secs, UPR, UPL, UDL, O1PC, O2PC, O3PC, O4PC, O1PD, O2PD, O3PD, O4PD)
function fill_CpodFormHeader(){		

	//alert("fill_CpodFormHeader()");
	// POPULATE MULTIPLE HIDDEN FIELDS.	
	document.cpodEmail.subject.value = g_CpodSubject;
	document.cpodEmail.Assessment_Name.value = CBT_name;
	document.cpodEmail.User_Score.value = UserPctComplete + "%" ;
	document.cpodEmail.User_Active_Time.value = UserActiveTime;	
	document.cpodEmail.Elapsed_Seconds.value = UserElapsedSecs;
	//alert("Bottom form Header");
	return;
	
}
function fill_ActivityStatusFields(numActivities, statusLabels, activity_names, pctComplete){
var uc, str;

	//alert("Top JS fill_ActivityStatusFields()...numActivities = " + numActivities + ", hit enter for the loop");
	// FILLING HIDDEN FIELDS IN BOTH FORMS
	for(var x=0; x<numActivities; ++x){
		document.cpodEmail.elements[(g_numCpodFormHeaderFields+x)].value = (x+1) + ">  " + pctComplete[x];	
		
	}
	//alert("Filled status fields");
	// WHAN ALL ARE FILLED CHECK TO SEE IF USER HAS HIT EXIT OR HAS HIT SEND MY COMPLETION EMAIL
	// This updates data while user is taking the CBT and checks if swf-exit-buttn has been hit just in case.
	// If browser exit was hit, this functiionis not called, rather unload_sendData() above sends the email.
	if(remoteExitHit){ 
		close_tutorial();// sends email if user chooses to close after given JS alert.			
	}
	//if User is sending completion email, UNLIKE training CBTs, Tests also close the CBT window.
	else{
		if(userSendingEmail){	sendEmail();		}
	}
	//END OF FIELD FILLING - JUST FALL THROUGH
	//note:  For some unknown reason, JS cannot call AS nextTaskBranch() and get it to respond at this point. However,
	//       AS fillFormStatusFields() will execute the call and does wait for JS to complete.  This was tested by
	//       putting a JS alert right here before returning to the AS.
}
// EMAIL SENDING ROUTINES
function sendEmail(){
		document.cpodEmail.Assessment_Name.value = g_tutorialTitle; 
		if(document.cpodEmail.User_Active_Time.value == "") { document.cpodEmail.User_Active_Time.value = "0 hr. 0 min. 0 sec."; }
		document.cpodEmail.submit();
		emailNotSent = false;
		//alert("Submitted the form.");
		if(userSendingEmail){ theTimeOut = window.setTimeout("delayForServer()", 8000);	}
}
function delayForServer(){
	//alert("Waiting 8 sec. for server, should close email panel.");
	window.clearTimeout(theTimeOut);
	userSendingEmail = false;  // DON'T NEED TO RESET SINCE WE'RE CLOSING USER OUT OF QUIZ AFTER EMAIL IS SENT.
	// This AS checks its own exitHit to decide whether to tell JS to close browser.
	g_movieName.closeEmailPanel();	
}

<!--  FIXED UNEDITABLE FUNCTIONS CALLED WHEN HTML PAGE LOADS -->
// CREATE MULTIPLE FORMS ON THIS PAGE:   called by in-body JS after tutorial Flash has been loaded.
//   exitBtnHit, userSending, userEmail, subject, firstName, lastName, tutorialName,  UPC, time, secs,  UPR, UPL, UDL, O1PC, O2PC, O3PC, O1PD, O2PD, O3PD
function create_CPODForm(){	
	document.writeln('<'+'form name="cpodEmail" action="http://www.montgomerycollege.edu/cgi-bin/fmail1.pl" method="post" target="newwindow">');
	document.writeln('<'+'input name="recipient" type=hidden value="">');
	document.writeln('<'+'input name="subject" type=hidden value="">');
	document.writeln('<'+'input name="Assessment_Name" type=hidden value="">');	
	document.writeln('<'+'input name="User_Score" type=hidden value="">');	
	document.writeln('<'+'input name="User_Active_Time" type=hidden value="">');
	document.writeln('<'+'input name="Elapsed_Seconds" type=hidden value="">');
	document.writeln('<'+'input name=":" type="hidden" value="">');
	document.writeln('<'+'input name="thankurl" type=hidden value="">');
	var fieldname, ext;
	//Create enough fields for your tutorial
	for(var x=0; x<g_numActivities; ++x){
		document.writeln('<'+'input name="ACTIVITY" type="hidden" value="">');	
	}
	document.writeln('<'+'/form>');
	// WRITE DEFAULT VALUES TO THE FORM
	var datestring = dateString();
	document.cpodEmail.recipient.value = g_defaultRecipient;
	document.cpodEmail.thankurl.value = g_thankyouURL; 
}
function dateString(){
	var curdate = new Date();
	var wday = curdate.getDate();
	var wdayStr = wday.toString();	
	if(wday <10){ wdayStr = "0" + wdayStr };
	
	var month = curdate.getMonth();	
	++month;
	var monthStr = month.toString();
	if(month <10){ monthStr = "0" + monthStr };
	
	var year = curdate.getYear();
	var yearStr = year.toString();
	var subyr = yearStr.substr(2, 2);
	return monthStr + "/" + wdayStr + "/" + subyr;
	
}
/* This function called by host html and resolves the string movieName to a Flash-object-reference based on browser type. 
   ...Used by JS to call AS functions.
*/
function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {		
	    g_movieName = window[movieName]
    }
    else {
  	  g_movieName = document[movieName]
    }
}
//  -------------    CONTROL OF EXTERNAL FLASH MOVIES IMPORTED INTO THE TUTORIAL 
function unloadClip_saveTask(){	
		//alert("In unloadClip.");  Captivate1 does call this function.
		g_movieName.unload_movie("saveTask");
}
function jsMessage(str){
	alert(str);
}



