// Array function...

function makeArray() {
var args=makeArray.arguments;
    for (var i=0; i < args.length; i++) {
    this[i]=args[i];
    }
this.length=args.length;
}

// This function determines which page is selected and goes to it...

function goPage(form) {
i=form.menu.selectedIndex;            
    if (i != 0) {
//    window.parent.location.href=urls[i];  
    parent.location.href=urls[i];  
    }
}

function goto_page(page){
   parent.location.href=page;
}

function set_progress_bar() {
   document.prog_bar.src=prog_bar_src;
   document.prog_bar.alt=prog_bar_alt;
}

function EvalCompleted() {
var comment_string;
var today=new Date();
var year=today.getYear();
var i;

      if (year < 1000 ) {year += 1900}; 
            document.EvalForm.date.value=(today.getMonth()+1)+"/"+today.getDate()+"/"+year;
     return true;
}


//timer variables

var prevTime;
var startTime;
var savedTime;
var duration;
//set cookie time to live to 2 hr.
var time_to_live=7200000;
// #minutes * 60 sec * 1000 ms
// page expires set to 5 minutes
var page_expires=300000;
// policy page expires set to 10 minutes
var policy_page_expires=600000;
// test page expires set to 15 minutes
var test_page_expires=900000;
// glossary page expires set to 8 minutes
var glossary_page_expires=480000;
// time credited when page expires set to 1 minute
var page_expired_add_time=60000;
// #hrs * 60 min * 60 sec *1000 ms
// total program elapsed time set to 1 hr 57 min
// allowing 3 minutes to take the test
var total_elapsed_time=7020000;
var total_time_text="1 hr 57 min";
var page_expired=0;
var saved_page_list;


//timer functions

//this function converts the accumulated
//time to a text string.

function displayTime(duration) {
var seconds=Math.floor(duration/1000);
var minutes=Math.floor(seconds/60);
var hours=Math.floor(minutes/60);

if (minutes < 60) return minutes+" min";
else return hours+" hr "+(minutes % 60)+" min";
}

//this function updates the accumulated time
//when the page loads.

function viewTimeCount(){
now = new Date();
var endTime = now.getTime();
var total_duration = displayTime(prevTime + (endTime - startTime));
//var total_duration = displayTime(prevTime + duration);
document.getElementById('elapsed_time').innerHTML = total_duration;
}

//this function is called when a page is loaded to start
//accumulating elapsed time for that page, and displays 
//the elapsed time.

function start_timer(page_expires){
var today = new Date();
startTime=today.getTime();
var total_time;
   
check_user_login();
set_progress_bar();
prevTime=getCookie("timeCount"); 
savedTime=getCookie("savedtimeCount");
//alert("prevTime is "+prevTime+" savedTime is "+savedTime);
setCookie("previous_page",parent.location.href);
if (savedTime != null && savedTime != 0)
{
//this user was in the DB
   if (prevTime != null && prevTime != 0)
   {
//check which is larger and use the larger accumulated time amount.
      if (parseInt(prevTime) > parseInt(savedTime))
      {
         prevTime=parseInt(prevTime);
      }
      else
      {
        prevTime=parseInt(savedTime);
      }
   }
   else
   {
        prevTime=parseInt(savedTime);
   }
   deleteCookie("savedtimeCount");
//   setCookie("savedtimeCount",0);
   setCookie("timeCount",prevTime);
}
else if (prevTime == null)
{
//this user was not in the DB and just starting the program 
   prevTime=0;
   setCookie("timeCount",0);
}
else
{
// get accumulated time so far to start with
   prevTime=parseInt(prevTime);
}
   setTimeout('times_up()', page_expires);
   viewTimeCount();
}

//this function is called when a page is loaded to start
//accumulating elapsed time for that page.  It does not 
//display the elapsed time (like on the index and start
// pages).

function start_index_timer(page_expires){
var today = new Date();
startTime=today.getTime();
   
prevTime=getCookie("timeCount"); 
savedTime=getCookie("savedtimeCount");
setCookie("previous_page",parent.location.href);
if (savedTime != null && savedTime != 0)
{
//this user was in the DB
   if (prevTime != null && prevTime != 0)
   {
//check which is larger and use the larger accumulated time amount.
      if (parseInt(prevTime) > parseInt(savedTime))
      {
         prevTime=parseInt(prevTime);
      }
      else
      {
        prevTime=parseInt(savedTime);
      }
   }
   else
   {
        prevTime=parseInt(savedTime);
   }
   deleteCookie("savedtimeCount");
//   setCookie("savedtimeCount",0);
   setCookie("timeCount",prevTime);
}
else if (prevTime == null)
{
//this user was not in the DB and just starting the program 
   prevTime=0;
   setCookie("timeCount",0);
}
else
{
// get accumulated time so far to start with
   prevTime=parseInt(prevTime);
}
   setTimeout('times_up()', page_expires);
}

//this function is called from the start page of the program.

function start_psh(page_expires){
var today = new Date();
startTime=today.getTime();
   
prevTime=getCookie("timeCount"); 
savedTime=getCookie("savedtimeCount");
//alert("prevTime is "+prevTime+" savedTime is "+savedTime);
setCookie("previous_page",parent.location.href);
if (savedTime != null && savedTime != 0)
{
//this user was in the DB
   if (prevTime != null && prevTime != 0)
   {
//check which is larger and use the larger accumulated time amount.
      if (parseInt(prevTime) > parseInt(savedTime))
      {
         prevTime=parseInt(prevTime);
      }
      else
      {
        prevTime=parseInt(savedTime);
      }
   }
   else
   {
        prevTime=parseInt(savedTime);
   }

   deleteCookie("savedtimeCount");
//   setCookie("savedtimeCount",0);
   setCookie("timeCount",prevTime);
}
else if (prevTime == null)
{
//this user was not in the DB and just starting the program 
   prevTime=0;
   setCookie("timeCount",0);
}
else
{
   prevTime=parseInt(prevTime);
}
   setTimeout('times_up()', page_expires);
}

//this function is called from the exit page of the program.

function exit_psh(page_expires){
var today = new Date();
startTime=today.getTime();
var total_time;
   
prevTime=getCookie("timeCount"); 
savedTime=getCookie("savedtimeCount");
if (savedTime != null && savedTime != 0)
{
// this user was in the DB

   if (prevTime != null && prevTime != 0)
   {
//check which is larger and use the larger accumulated time amount.
      if (parseInt(prevTime) > parseInt(savedTime))
      {
         prevTime=parseInt(prevTime);
      }
      else
      {
        prevTime=parseInt(savedTime);
      }
   }
   else
   {
        prevTime=parseInt(savedTime);
   }
   setCookie("timeCount",prevTime);
   deleteCookie("savedtimeCount");
}
else if (prevTime == null)
{
//this user was not in the DB and just starting the program 
   prevTime=0;
   setCookie("timeCount",0);
}
else
{
// get accumulated time so far to start with
   prevTime=parseInt(prevTime);
}
   setTimeout('times_up()', page_expires);
   viewTimeCount();
}

function check_user_login()
{
  var firstname;

  firstname = getCookie("FirstName");
  if (firstname == null)
  {
         alert("You must login and provide your name.  The next screen will return you to the sign-in screen to provide that information.");
	   location.href="start.shtml";
   }
}

function update_elapsed_time(){
var end_page = new Date();
var endTime = end_page.getTime();
var page_duration = endTime-startTime;
var now=new Date();
var exp_date;

//store elapsed time in a cookie that will expire in 2 hours.
//   exp_date=endTime + time_to_live;
//   now.setTime(exp_date);

if (page_expired)
  setCookie("timeCount",prevTime+page_expired_add_time);
else
{ 
   setCookie("timeCount",prevTime+page_duration);
   page_expired=0;
}
}

function times_up(){
var exp_date;
var now;
var total_duration;

  alert("You have spent too long on this page, therefore your total elapsed time has stopped accumulating.  You may continue with the program, or if you need to exit the program now, your total time spent will be saved.  Click on the 'Exit & Save' button in the navigation bar to exit the program and save your accumulated time.");
  page_expired=1;
  location.reload();
}

function check_total_time(total_elapsed_time) {
now = new Date();
var endTime = now.getTime();
var total_duration = prevTime + (endTime - startTime);
//var total_duration = prevTime + duration;
var total_duration_string = displayTime(total_duration);
var check_policy;

if (total_duration < total_elapsed_time)
{
   alert("You have not spent at least "+total_time_text+", the required amount of time reviewing the material. You have spent "+total_duration_string+".  The next screen will provide a list of supplemental material you may have overlooked.");
  location.href="supplemental.php";
}
else
   location.href="test.htm";
}

function report_total_time(){
now = new Date();
var endTime = now.getTime();
var total_duration = prevTime + (endTime - startTime);
var total_duration_string = displayTime(total_duration);
  
//setCookie("timeCount",total_duration);
return total_duration_string;
}


//cookie read-write functions

function setCookie(name, value, expires, path, domain, secure)
{
	
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
   endstr=document.cookie.length;
return unescape(document.cookie.substring(offset,endstr));
}

//function to return the value of the cookie specified by "name".

function getCookie (name) {

var arg = name+"=";
var alen = arg.length;
var clen = document.cookie.length;
var i=0;

while (i < clen) {
  var j=i+alen;
  if (document.cookie.substring (i,j) == arg)
    return getCookieVal(j);
  i=document.cookie.indexOf(" ",i)+1;
  if (i == 0) break;
}
return null;
}

function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function isNumberString (InString)  {
	if(InString.length==0) 
		return (false);
	RefString="1234567890";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)  
			return (false);
	}
	return (true);
}







