// JavaScript Document
/* Code contained hereing ©2008 Howard J. Hanna III — Interactive Productions
   email: howard (dot) hanna (at) hhanna.com
  
  Use is granted to Blue Sky Productions, Inc and Allan Kobernick as support file in larger e-learning project.
  Redistribution and/or sale of this copyrighted code is prohibited without express permission from its creator.
  
   exceptions: functions readCookie and writeCookie are Dreamweaver CS3 snippets
*/
function resetCookie(name, value, hours){
	destroyCookie(name);
	writeCookie(name, value, hours);
	//alert("reading cookie and value is:"+readCookie(name));
}
function destroyCookie(name) {
		writeCookie(name, '', -1);
}

	

<!--
//This function reads the cookie
//and returns the cookie's value as a string
function readCookie(name) {
  var cookieValue = "";
  var search = name + "=";

  if(document.cookie.length > 0) { 
    offset = document.cookie.indexOf(search);
    if (offset != -1) { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) { end = document.cookie.length; }
	  cookieValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return cookieValue;
}


//This function writes the cookie
//each argument MUST be supplied
//name is a string, value is a string, hours is a number.
function writeCookie(name, value, hours){
  var expire = "";

  if(hours != null) {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
//  alert("name is:"+name+"value is:"+value+"hours is:"+hours);
  document.cookie = name + "=" + escape(value) + expire;
}

//Gets or sets the cookie value
//get_set_skip is a string to determine what you want to do (get, set, or skip the cookie value)
function myScore(get_set_skip, name, quest_num, value, hours) {
//Pass the parameters you'll use to read/write the cookie
	if (get_set_skip.toLowerCase() == 'skip') {
	//read the cookie and see if a value exists.
	//if no value exists, write a value indicating the question is skipped,
	//if a value exists do nothing.
		//First, read the cookie.
		var sTmp = readCookie(name);
		//alert("I just read the cookie and the value is:" + sTmp);
		
		//Split the string on the color delimiter
		//This will put the question number-score string pairs into an array
		var aTmp = sTmp.split(":");
		//need to handle if nothing to split.
		
		//Create an array to hold the score
		var aScore = new Array();
		
		//Loop through the array holding the pair strings
		for (i=0;i<aTmp.length;i++) {
			//Split the question number-score pairs on their comma delimiter.
			//This will put them in an array: aAry[0] holds the question number; aAry[1] holds the score.
			var aAry = aTmp[i].split(',');
			
			//check to see if a value exists for current question
			
			
			//Populate an array.
			//Set the new array's index value the same as the question number, adjusted to be zero-based for the array index
			//and set that equal to the score string.
			aTmp[Number(aAry[0])-1] = aAry[1];
		}
		//alert("the current questions value in the cookie is:"+aTmp[quest_num - 1]);
		if ( (aTmp[quest_num - 1] == "") || (aTmp[quest_num -1] == undefined ) ) {
			//either no cookie exists, or no value exists for the question
			//so, write a cookie and/or value
			myScore('set', name, quest_num, value, 24);
		}
	}

	//This part reads the cookie, and returns an array
	if (get_set_skip.toLowerCase() == 'get') {
		//First, read the cookie.
		var sTmp = readCookie(name);
		//alert("I just read the cookie and it's value is: " + sTmp);
		
		//Split the string on the colon delimiter.
		//This will put the question number-score string pairs into an array.
		var aTmp = sTmp.split(":");
		
		//Create an array to hold the score
		var aScore = new Array();
		
		//Loop through the array holding the pair strings
		for (i=0;i<aTmp.length;i++) {
			//Split the question number-score pairs on their comma delimiter.
			//This will put them in an array: aAry[0] holds the question number; aAry[1] holds the score.
			var aAry = aTmp[i].split(',');
			
			//Populate an array.
			//Set the new array's index value the same as the question number, adjusted to be zero-based for the array index
			//and set that equal to the score string.
			aTmp[Number(aAry[0])-1] = aAry[1];
		}

		return aTmp;
	}


	//This part adds the current question number and score to the cookie
	if (get_set_skip.toLowerCase() == 'set') {
		//You want to set the cookie.
		/*	So, we'll need a few things:
				1. the current question's score;
				2. the current question number;
				3. all the past question's number-score pairs, which are housed in the cookie.
		*/
		
		//The current score was passed to our function as the value argument
		var currentScore = value;
		
		//Get the current question number.
		//In this example, we use a textfield to hold the question number.
		//var nIndex = Number(document.forms[0].question.value);
		
		//Get all the past question's number-score pairs from the cookie.
		//This is returned as an array from our function.
		var aPairs = myScore('get', name);
		
		/*
		Now, we have everything we need.
		So, we have to do a few things:
			1. 	Populate the question number-score pair array
				with the current question number and score,
				making the question number the index,
				and the score the element's value. I.e.: questionArray[number] = score
				
			2.	Turn the question number-score array into a string.
			
			3.	Set the cookie with the new string.
		*/
		
		
		//Remember, array's use a zero-based index.
		//So since we start with question 1, nIndex will equal 1.
		//We have to correct that so the index will start at zero.
		nIndex = quest_num - 1;
		
		//Now, set the array index as the question number
		//and set the element to the current question's score.
		aPairs[nIndex] = currentScore;
		
//		alert("The array's index is:"+nIndex);
//		alert("The score is:"+currentScore);
		
		
		//Instantiate the new string
		var sTmp = '';

		//Loop through the question number-score array.
		for (i=0;i<aPairs.length;i++) {
			//Turn the question number-score array index and element into a string, delimited by a comma
			//remember, to re-adjust the question number by adding one to it.
			sTmp += (i+1) + "," + aPairs[i];

			//If not the last array entry
			if (i != aPairs.length - 1) {
				//Add a colon as a delimiter between pairs
				//e.g. "questNum1,score1:questNum2,score2:questNum3,score3"
				sTmp += ":";
			}
		}
		writeCookie(name,sTmp,hours);
		//alert("I just wrote this to the cookie:\n     name:" + name + "\r     value=" + sTmp + "\r     expire in:" + hours);
	}

}


/*
function getScore() {
	var aScore = myScore('get', 'AA_scoringCookie');
	var score = 0;
	for (i=0;i<aScore.length;i++) {
		score += Number(aScore[i]);
	}
	//alert("You answered "+score+" out of "+aScore.length+" correctly.");
	
	//if you want a percentage score, do a little math	
	//score = Math.round((score/aScore.length) * 100);
	
	//remove any floating-point errors
	//var sScore = score.toString();
	//if (sScore.length > 2) {
		//sScore = sScore.slice(sScore.lastIndexOf(".")+1, sScore.length);
		//score = Number(sScore);
	return score;
	}
	//alert("Your percentage is: "+score+"%");
	
	//return score;
//}
*/
function passing(name) {
	//get the score
	var score = getScore(name);

	//check score against criteria
	if (score < 14) {
		MM_goToURL('parent','m1l1p22.htm?score='+score);
	}else{
		MM_goToURL('parent','m1l1p23.htm?score='+score);
	}
}


function getURLscore() {
	//get the search portion of the url (everything after the ? )
	var sNameValue = document.location.search;  //sNameValue = "score=##";

	var sValue = sNameValue.slice(sNameValue.lastIndexOf("=")+1,sNameValue.length);
	
	return sValue;
}



function getScore(name) {
		//get score array
		var aScore = myScore('get', name);
		
		//score holder
		var score = 0;
		
		//loop through score array
		for (i=0;i<aScore.length;i++) {
			//convert string to number
			var nScore = Number(aScore[i]);
			//if score is greater than zero, then it was answered
			if ( nScore >= 0 ) { 
				//add score to score holder
				score += nScore;
			}
		}
		return score;
}

function getNumAnswered(name) {
	var aScore = myScore('get', name);
	var nAnswered = 0;
	
	for (i=0;i<aScore.length;i++) {
		//convert string to number
		var nScore = Number(aScore[i]);
		//if score is greater than zero, then it was answered
		if ( nScore >= 0 ) { 
			//increment number of questions answered
			nAnswered++;
		}
	}
	return nAnswered;
}

function getNumQuestions(name) {
	var aScore = myScore('get', name);
	return aScore.length;
}