/*Official No Print, No Copy and Forced Submission on Time-up for Blackboard Tests, 
Teaching and Learning Center, Illinois Central College, 2008
visit http://www.icc.edu/innovation/PDFS/blackboard/BBTests_NoPrintNoCopyFeb08.pdf
for instructions on using this script with your Blackboard tests
(scripts below are taken from the best of the best, cleaned up, compiled and put in one js file)*/

//This script disables the right mouse button and the ctrl and alt keys

function IE(e) 
//traps right-click in IE
{
     if (navigator.appName == "Microsoft Internet Explorer" && (event.button == "2" || event.button == "3"))
     {
	  	alert("No right-click");		 
        return false;
     } 
}
function NS(e) 
//traps right-click in Netscape/firefox, et al
{
     if (document.layers || (document.getElementById && !document.all))
     {
          if (e.which == "2" || e.which == "3")
          {
			  	alert("No right-click");
                return false;
          }
     }
}

function handlePress(e) {
//traps ctrl and alt keys
  var evtobj=window.event? event : e;
  if (evtobj.ctrlKey || evtobj.altKey) {
	  alert("Can't use Ctrl or alt Key");
	return false;
  }
}

//added Jan 10, 2007 to stop text selection in Firefox
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}


disableSelection(document.body)


document.onselectstart=new Function('return false');function ds(e){return false;}function ra(){return true;}document.onmousedown=ds;document.onclick=ra;

document.onmousedown=IE;
document.onmouseup=NS;
document.oncontextmenu=new Function("return false");
document.onkeydown=handlePress;

// additional script added to force submission of the test on time up - February 2008
// from http://david.carter-tod.com/
//http://www.princeton.edu/as/bbfaq/Questions/quizzes.htm#timed
document.writeln("<input type='image' name='submit' border='0' alt='' src='/images/spacer.gif' onclick='javascript:return submitIt();'>");
// END KDC 1-25-2006; KP 6-6-2007.

useBeep = true;
window.OnError = null;

var statusMsg;
var timerId = null;
var timerRunning = false;

Seconds = 0;
Minutes = 0;
Hours   = 0;
SecondsBeforeWarn = 0;

var startTime;
var warningTime;
var warningPosted = 0;
// KDC 1-25-2006; KP 6-6-2007. For submitting when time's up.
var endTime;
// END KDC 1-25-2006; KP 6-6-2007.

function showtime()
{
  currentTime = new Date().getTime();

  elapsedMS = currentTime - startTime;

  Hours   = Math.floor( elapsedMS / 3600000 );
  Minutes = Math.floor( ( elapsedMS - (Hours*3600000) ) / 60000 );
  Seconds = Math.floor( ( elapsedMS - (Hours*3600000) - (Minutes*60000) ) / 1000 );

  if (Seconds != 1) { var ss="s" } else { var ss="" };
  if (Minutes != 1) { var ms="s" } else { var ms="" };
  if (Hours   != 1) { var hs="s" } else { var hs="" };

  timerDiv.innerHTML=ASSESSMENT_RESOURCES.getFormattedString("elapsed_time", new Array(''+Hours, ''+Minutes, ''+Seconds));

  if ( (currentTime > warningTime) && (currentTime < (warningTime+2000)) && (! warningPosted) )
  {
    showTimerAlert(warningMin, warningSec);
    warningPosted = 1;
  }

  // KDC 1-25-2006; KP 6-6-2007. For submitting when time's up.
  if ( currentTime >= endTime )
  {
    stopClock();
    (document.getElementsByName("submit").item(0)).click();
    return false;
  }
  // END KDC 1-25-2006; KP 6-6-2007.

  //window.status = statusMsg;
  timerId = setTimeout("showtime()", 500);
  timerRunning = true;
}

// KDC 1-25-2006; KP 6-6-2007. For submitting when time's up.
function submitIt()
{
  document.forms[0].submit();
  return true;
}
//END KDC 1-25-2006; KP 6-6-2007.

function stopClock()
{
  if (timerRunning)
  {
    clearTimeout(timerId);
  }
  timerRunning = false;
}

function startClock(elapsedSec, timeLimitSec, timeLeftSecWarning)
{
  stopClock();

  startTime = new Date().getTime() - (elapsedSec * 1000);
  warningTime = startTime + (timeLimitSec - timeLeftSecWarning) * 1000;
  warningMin = Math.floor(timeLeftSecWarning / 60);
  warningSec = Math.floor( timeLeftSecWarning - (warningMin * 60) )
  // KDC 1-25-2006; KP 6-6-2007. For submitting when time's up.
  // Submit 10 seconds early to overcome time it takes to submit
  // (internal clock doesn't stop).
  endTime = startTime + timeLimitSec * 1000 - 10000;
  // END KDC 1-25-2006; KP 6-6-2007.
  timerDiv=document.createElement("div");
  timerDiv.className="timerDiv";
  document.body.appendChild(timerDiv);

  showtime();
}

function showConfirm(message)
{
  return ( confirm(Message) ? true : false );
}

/**
 * This would be simpler if we could ignore seconds when there is at least a minute left
**/
function showTimerAlert(warningMin, warningSec)
{
  if (warningMin < 0) { warningMin = 0; }
  if (warningSec < 0) { warningSec = 0; }

  var messageKey;

  if (warningMin > 1 && warningSec > 1)
  {
    messageKey = "alert.time_left.minutes_seconds";
  }
  else if (warningMin > 1 && warningSec == 1)
  {
    messageKey = "alert.time_left.minutes_second";
  }
  else if (warningMin > 1 && warningSec == 0)
  {
    messageKey = "alert.time_left.minutes";
  }
  else if (warningMin == 1 && warningSec > 1)
  {
    messageKey = "alert.time_left.minute_seconds";
  }
  else if (warningMin == 1 && warningSec == 1)
  {
    messageKey = "alert.time_left.minute_second";
  }
  else if (warningMin == 1 && warningSec == 0)
  {
    messageKey = "alert.time_left.minute";
  }
  else if (warningMin == 0 && warningSec > 1)
  {
    messageKey = "alert.time_left.seconds";
  }
  else if (warningMin == 0 && warningSec == 1)
  {
    messageKey = "alert.time_left.second";
  }
  else // if (warningMin == 0 && warningSec == 0)
  {
    messageKey = "alert.time_left.none";
  }

  alert( ASSESSMENT_RESOURCES.getFormattedString( messageKey,
          new Object( {minutes: ''+warningMin, seconds: ''+warningSec} ) ) );
}