/////////////////////////////////////////
// 	COOKIE AND TIMER SCRIPTS
/////////////////////////////////////////

///////////////////////////////////////////
//		COOKIE FRAMEWORK
//////////////////////////////////////////
expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)

// SET COOKIE
function setCookie(cookieName, cookieValue) {	// SETS
	parent.content.document.cookie = cookieName + "=" + cookieValue + ";expires=" + expireDate.toGMTString()
}
// LOAD COOKIE
function cookieVal(cookieName) {
	thisCookie = parent.content.document.cookie.split("; ");
	for(i=0; i<thisCookie.length; i++) {					// LOOP THROUGH ALL COOKIE ELEMENTS
		if (cookieName == thisCookie[i].split("=")[0]) {	// IF FOUND COOKIE NAME
			return thisCookie[i].split("=")[1]				// RETURN COOKIE VALUE
		}
	}
}

// DELETE COOKIE
function deleteCookie(cookieToDelete, cookieValue){
	if (parent.content.document.cookie != "") {
	   	thisCookie = parent.content.document.cookie.split("; ");
		expireDate = new Date;
	   	expireDate.setDate(expireDate.getDate()-1);
	    for (i=0; i<thisCookie.length; i++) {
			cookieName = thisCookie[i].split("=")[0]
			if (cookieName == cookieToDelete) {				// FOUND COOKIE TO DELETE
		  	    parent.content.document.cookie = cookieName + "=" + cookieValue + ";expires=" + expireDate.toGMTString()
				alert(cookieName + " cookie deleted.");	
			}
			//else { alert("No " + cookieName + " cookie found") };
		}
	}
}

/////////////////////////////////////////
// 	COOKIE VARIABLES 
/////////////////////////////////////////

var answered;	// ARRAYS TO STORE THE COOKIE VALUES
var wrong; 

var formExamType;				// FORM FIELD EXAM TYPE
var formNumberQuestions;		// FORM FIELD NUMBER OF QUESTIONS

var unAnswered;					// COUNT OF UNANSWERED QUESTIONS STORED IN COOKIE
var availableWrong; 			// COUNT OF WRONG QUESTIONS STORED IN COOKIE
var thisArea;

/////////////////////////////////////////
// 	COOKIE SCRIPTS
/////////////////////////////////////////

// LOAD COOKIES AND STORE IN "ANSWERED" AND "WRONG" ARRAY
function loadCookies(examtype) { 
	unAnswered = 0;
	availableWrong = 0;
		
	// PTH ADDED HERE
	answered = new Array(100);
	wrong  = new Array(100);
	
	// LOAD ANSWERED QUESTIONS COOKIE
	var thisArray = cookieVal('answered_questions');	
	if (thisArray) {							// IF COOKIE EXISTS
		answered = thisArray.split(",");			// SPLIT ANSWERED COOKIE INTO AN ARRAY 
		for (i=0; i < answered.length; i++) { 
			if (answered[i] == 0) { 
				unAnswered++; 						// COUNT NUMBER OF UNANSWERED
			}
		}
	}
	else { unAnswered = 100; }
	//alert("FOR TESTING: # of unanswered questions loaded from cookie is " + unAnswered);	// FOR TESTING


	// LOAD INCORECT QUESTIONS COOKIE
	var thisWrongArray =  cookieVal('incorrect_questions');	// SET ANSWERED TO 
	if (thisWrongArray) {		
		wrong = thisWrongArray.split(",");
		for (i=0; i < wrong.length; i++) { 
			if (wrong[i] == 1) { 
				availableWrong++; 
			}
		}
	}
	//alert("FOR TESTING: # of incorrect questions loaded from cookie is " + availableWrong);	// FOR TESTING
	
	if (examtype == 'cookie') {	// COOKIE BASED EXAM
		// PULL FORM FIELDS
		formNumberQuestions = 	parent.main.document.cookie.NUMBERQUESTIONS.value;// GET NUMBER QUES FROM FORM
		formExamType = parent.main.document.cookie.EXAMTYPE.selectedIndex;	// CHOOSE EXAM FILTER
		formAreaChoice = parent.main.document.cookie.AREACHOICE.selectedIndex;	// CHOOSE EXAM FILTER
		
		// MAKE SURE NUMBER OF QUESTIONS FIELD HAS A VALUE
		if ((formNumberQuestions == "") || (isNaN(formNumberQuestions)) || (formNumberQuestions < 1)){
			var prompt1 = prompt("Please enter the number questions desired:", 100);
			if(prompt1) {
				formNumberQuestions = prompt1;
			} 
			else { parent.content.document.location.href = 'testcookie.htm';}
		}
		
		////////////////////////// TEST EXAM FILTER
		// NO FILTER
		if (formExamType == 0) {		
			getAreaQuestions(formAreaChoice, formNumberQuestions);	// LOADS AREA CHOICE AND # OF QUESTIONS
		}
		// ONLY UNANSWERED QUESTIONS
		else if (formExamType == 1){	
			if (unAnswered == 0) {		// NO QUESTIONS AVAILABLE IN 
				alert("No unanswered questions remaining. Please clear this cookie to reset the data.");
				parent.content.document.location.href = 'testcookie.htm';
			}
			else {
				getUnansweredQuestions(formAreaChoice, formNumberQuestions);
			}
		}
		// ONLY INCORRECT ANSWERS
		else if (formExamType == 2) {	
			if (availableWrong == 0 ) {	// NO WRONG QUESTIONS COOKIE OR ITS BLANK
				alert("No incorrect answers cookie was found or it contained no questions. Please complete an exam with cookies enabled or select an exam with different criteria.");
				parent.content.document.location.href = 'testcookie.htm';
			}
			else {
			 	getWrongQuestions(formAreaChoice, formNumberQuestions);
			}
		}
	}
}

//////////////////////////////////////
// STORE COOKIES
//////////////////////////////////////
function storeCookies() {

	
	// PTH ADDED HERE		
	answered.length = 100;
	wrong.length = 100;
	
	unAnswered = 0;
	availableWrong = 0;
///////////// STORE ANSWERED COOKIE
	for(i=0;i<answered.length;i++) { // MAKE SURE A VALUE IS IN EACH ITEM OF THE ARRAY
		if(answered[i] != 1) {
			answered[i] = 0;
			unAnswered++;
		}
	}

	//alert("FOR TESTING: number of unanswered questions stored: " + unAnswered);
	var storeAnswered = answered.toString();	// CONVERT ANSWERED ARRAY BACK TO STRING
	setCookie("answered_questions", storeAnswered);


///////////// STORE WRONG COOKIE
	for(i=0;i<wrong.length;i++) { 
		if(wrong[i] != 1) {
			wrong[i] = 0;
		}
		else if (wrong[i] == 1) {
			availableWrong++;
		}
	}

	//alert("FOR TESTING: number of incorrect questions stored: " + availableWrong);
	var storeWrong = wrong.toString();	// CONVERT ANSWERED ARRAY BACK TO STRING
	setCookie("incorrect_questions", storeWrong);
}

//////////////// TEST COOKIE

function storeTestCookie(value) {
	setCookie('test_cookie', value);	// SET TEST COOKIE WITH VALUE INPUT
	test = cookieVal('test_cookie');
}




