// -------------------------------------------------------------------------------------------------------------------
// set 'change_this' cookie to state = active, and all others to latent
function change_state(change_this){
	var tmpContents = '';														// clear tmpContents;
	var cookie_to_change = change_this.replace(/[0-9]+/g,''); 					// strip all numbers off cookie name to get the type of cookie; _owner or _pet
	var cookie_regex = new RegExp("^" + cookie_to_change+"\\d*$");				// regex to match the name of the owner/pet cookie and any number
	
	for (var tmpCookname in bbcjs.cookies.cookieData){							// loop though all cookies.
		if (tmpCookname.match(cookie_regex)){  									// for every cookie that matches the regex above.
			if (tmpCookname == change_this){ 									// if cookie name matches the cookie to change
				tmpContents = unescape(bbcjs.cookies.cookieRaw[tmpCookname]);	// collect all the cookie data for cookie
				toggle_state(tmpCookname,tmpContents,'active');					// set this cookie to 'active' in toggle_state function
			}
			else {
				tmpContents = unescape(bbcjs.cookies.cookieRaw[tmpCookname]);	// collect all the cookie data for tmpCookname
				toggle_state(tmpCookname,tmpContents,'latent');					// set this cookie to 'latent' in toggle_state function
			}
		}
		else{
			// alert ('cookie not found');
		}
	}
}

// -------------------------------------------------------------------------------------------------------------------
// 

function toggle_state(cookie,contents,new_state){
	// alert ('setting ' + cookie + ' to ' + new_state);

	var pos = contents.indexOf("&state="); 						// find position of &state= in cookie contents
	var before = contents.substring(0, pos); 					// all content of cookie before '&state='
	
	set_state = '';
	new_data = '';
	
	// swap states
	if (new_state == 'latent'){
		var after = contents.substring(pos + 13, contents.length); //ie it used to be &state=active which is 13 chars long. Note: always keep no. of chars the same to swap.
		var set_state = '&state=latent';
	}
	else if (new_state == 'active'){
		var after = contents.substring(pos + 13, contents.length); //ie it used to be &state=latent which is 13 chars long. Note: always keep no. of chars the same to swap.
		var set_state = '&state=active';
	}
	else {
		bbcjs.trace('<b><font color=\"red\">State cannot be set for some reason\?</b></font>',1);
	}
	new_data = before + after + set_state; 						// new cookie contents
	bbcjs.trace('setting ' + cookie + ' to ' + new_state,3);
	bbcjs.cookies.setCookie(cookie, new_data, "+4w", '', '');	// set cookie
	bbcjs.trace(bbcjs.obj2list(bbcjs.cookies.cookieData),5);	// trace - cookie object data available to test harness window
}

// -------------------------------------------------------------------------------------------------------------------
// 
function set_score(test,score){
	var tmpCookname = '';

	for (var tmpCookname in bbcjs.cookies.cookieData){
		if (tmpCookname.match(/^typ_pet\d*$/)){ 								// for every 'typ_pet' cookie found
			if (bbcjs.cookies.cookieData[tmpCookname].state == 'active'){		// if the cookie has state=active
				// set variables for position and value and have a repeatable block.
				if (bbcjs.cookies.cookieData[tmpCookname].test){				// if the test key is found
					var test_new = bbcjs.cookies.cookieData[tmpCookname].test;
					var before = test_new.substring(0, test - 1);
					var after = test_new.substring(test, test_new.length);
					var test_new = before + '1' + after;
					bbcjs.cookies.cookieData[tmpCookname].test = test_new;
				}
				if (bbcjs.cookies.cookieData[tmpCookname].score){
				
					var score_new = bbcjs.cookies.cookieData[tmpCookname].score;
					var before = score_new.substring(0, test - 1);
					var after = score_new.substring(test, score_new.length);
					var score_new = before + score + after;
					bbcjs.cookies.cookieData[tmpCookname].score = score_new;
				}
				bbcjs.cookies.setCookie(tmpCookname,bbcjs.cookies.cookieData[tmpCookname], '+4w');	// set new owner cookie				
			}
		}
	}
}

// -------------------------------------------------------------------------------------------------------------------
// 

var showResults = '';									// global variable for inclusion in the page

function get_results(result){

var tmpCookname, testsArray, scoreArray, d, tests, scores, test_type, test_number, score, choice, totalScore = 0;


	for (tmpCookname in bbcjs.cookies.cookieData){
		if (tmpCookname.match(/^typ_pet\d*$/)){ 								// for every 'typ_pet' cookie found
			if (bbcjs.cookies.cookieData[tmpCookname].state == 'active'){		// if the cookie has state=active
			
				name = bbcjs.cookies.cookieData[tmpCookname].name;
				tests = bbcjs.cookies.cookieData[tmpCookname].test;
				scores = bbcjs.cookies.cookieData[tmpCookname].score;
				test_type = bbcjs.cookies.cookieData[tmpCookname].type;
				
				showResults += ("<table border=\"0\">");
				
				if (tests){
				
					if ((test_type == 'other') || (test_type == 'reptile')){
						showResults += ("<tr><td>Unfortunately there are no tests available for this type of pet. Please <a href=\"/nature/animals/pets/testyourpet/index_cookie.shtml\">select another pet</a></td></tr>");
					}				
					else {
						testsArray = tests.split("");							// split individual tests into array
						scoreArray = scores.split("");							// split individual scores into array
						
						if (result == 'all'){								// returns scores for all tests when the result in the call to the function is 'all'						
		      				for (d=0; d<testsArray.length; d++){
								test_number = d + 1;
								score = scoreArray[d];							// get the score
								if (scoreArray[d] == '5') choice = 'A';			// convert score into choice
								else if (scoreArray[d] == '3') choice = 'B';		// convert score into choice
								else if (scoreArray[d] == '1') choice = 'C';		// convert score into choice
								
	
								if (testsArray[d] == '1'){						// when test has been undertaken (eg it's value is 1)
									showResults += ("<tr><td>Test <span class=\"whiteFont\"><strong>" + test_number + "</strong></span> has been completed.</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td> You chose " + choice + ". <span class=\"cookie\"><a href=\"/nature/animals/pets/testyourpet/tests/" + test_type + "_" + test_number + "_results.shtml\">View results</a>.</span><br /></td></tr>");
									//showResults += ('<tr><td colspan=2 id=resulttext>'+getResultText(test_type, test_number)+'</td></tr>');
								}
								else {											// when test has NOT been undertaken (eg it's value is NOT 1)
									showResults += ("<tr><td>Test <strong><a href=\"/nature/animals/pets/testyourpet/tests/" + test_type + "_" + test_number + ".shtml\">" + test_number + "</a></strong> has not been completed.</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>\&nbsp\;</td></tr>");
								}
								
							}
							//calculate the total score for fish
							if (test_type == 'fish'){
			      				for (d=0; d<testsArray.length; d++){
									score = scoreArray[d];							// get the score
									totalScore += parseInt(score);
								}
									showResults += ("<tr><td colspan=\"3\"><br><table height=\"24\" width=\"630\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" bgcolor=\"#99A179\"<tr><td align=\"center\"><span class=\"whiteFont\"><b>The total score for " + name + " is " + totalScore + ".</span></b><br /></td></tr></table></td></tr>");							
							}
							//calculate the total score for other pet types, stating from test 2 as test 1 is a handedness test
							else {
			      				for (d=1; d<testsArray.length; d++){
									score = scoreArray[d];							// get the score
									totalScore += parseInt(score);
								}
									showResults += ("<tr><td colspan=\"3\"><br><table height=\"24\" width=\"630\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" bgcolor=\"#99A179\"><tr><td align=\"center\"><span class=\"whiteFont\"><b>The total score for " + name + " is " + totalScore + ".</span></b><br /></td></tr></table></td></tr>");							
							}
						}		
										
						else {
		      				for (d=0; d<testsArray.length; d++){
							test_number = d + 1;
							score = scoreArray[d];							// get the score
							if (scoreArray[d] == '5') choice = 'A';			// convert score into choice
							else if (scoreArray[d] == '3') choice = 'B';		// convert score into choice
							else if (scoreArray[d] == '1') choice = 'C';		// convert score into choice
						
								 if (test_number == result){					// returns score for individual test when the test_number is the same as the result in the call to the function
									showResults += ("<tr><td><span class=\"whiteFont\"><b>Test " + test_number + " has been completed. You chose " + choice + ".</span></b><br /></td></tr>");
								}
							}
						}
					}
				}
				// there are no tests for reptiles or other pets so provide this message. Needs testing.<br>

				else{
						showResults += ("<tr><td>Unfortunately there are no tests available for this type of pet. Please <a href=\"/nature/animals/pets/testyourpet/index_cookie.shtml\">select another pet</a></td></tr>");
				}
	
				
				showResults += ("</table>");
			}
		}
    }
}
/*
function getResultText (a,b)
{
	return "<h3>"+a+", "+b+"</h3>";
}*/
