// START OF CONFIG SECTION, WHERE PARTS OF THE QUIZ CAN BE CUSTOMISED

var ie4Bounce = "/science/humanbody/mind/surveys/art/articles_index.shtml"; // location to bounce IE4 users to from detect on first page
var cookieCreatedURL = "d.shtml"; // location to bounce to after the initial cookie has been created (used once on first page)

// list of random quiz pages
var pageList = new Array();
pageList[0] = "p1";
pageList[1] = "p2";
pageList[2] = "p3";
pageList[3] = "p4";
pageList[4] = "p5";
pageList[5] = "p6";
pageList[6] = "p7";
pageList[7] = "p8";
pageList[8] = "p9";
pageList[9] = "p10";
pageList[10] = "p11";
pageList[11] = "p12";
pageList[12] = "p13";
pageList[13] = "p14";
pageList[14] = "p15";
pageList[15] = "p16";
pageList[16] = "p17";
pageList[17] = "p18";
pageList[18] = "p19";
pageList[19] = "p20";
pageList[20] = "p21";
pageList[21] = "p22";
pageList[22] = "p23";
pageList[23] = "p24";

// initial pages to put before random pages
var startPages = new Array();
startPages[0] = "d";
startPages[1] = "pi";

// end pages to put at the end of the random pages
var endPages = new Array();

// endPages[0] = "sc";
endPages[0] = "sf";
endPages[1] = "ppi";
endPages[2] = "pp1";
endPages[3] = "pp2";
endPages[4] = "pp3";
endPages[5] = "pp4";
endPages[6] = "pp5";
// endPages[7] = "pc";
endPages[7] = "r";
endPages[8] = "pff";

if (bbcjs.cookies.cookieData.art_active) {
	// alert("Cookies exist");
	bbcjs.trace("Cookies exist", 3);
	active = bbcjs.cookies.cookieData.art_active.active;
	active_cookie = bbcjs.cookies.cookieData["art_"+active];
	current_page = bbcjs.cookies.cookieData["art_"+active].current_page;
	// alert("active: "+active+", active_cookie: "+active_cookie);
} else {
	// alert("No cookies currently exist");
}

// FUNCTION SHOULD ONLY BE CALLED ONCE, FROM THE INDEX PAGE. FUNCTION DELETES OLD COOKIES AND SETS NEW ONE UP
function createNewUser(form) {
	// alert("createNewUser called");
	
	// ***********************************************
	// 1. first, erase all previous art cookies
	// ***********************************************
	
	num_art_cookies = 0; // number of art cookies
	name_art_cookies = new Array(); // array to store the name(s) of the art cookies
	var tempI = 0; // temp. increment variable

	// count all art cookies, these all have the prefix "art_"
	for (var tmpCookname in bbcjs.cookies.cookieData){
		if (tmpCookname.match(/^art_.*$/)){
			name_art_cookies[tempI] = tmpCookname.match(/^art_.*$/);
			tempI++;
			num_art_cookies ++;						
		}
	}

	// delete all the art cookies
	for (var i=0; i<name_art_cookies.length; i++) {
		// alert("found this cookie: "+name_art_cookies[i]+", but deleting it now");
		bbcjs.cookies.deleteCookie(name_art_cookies[i]);
	}
	
	// ***********************************************
	// 2. bounce all IE4 users somewhere else
	// ***********************************************

	if(!document.getElementsByTagName && document.all) {
		window.location.replace(ie4Bounce);
	}
	
	// ***********************************************
	// 3. create a uniquely-named art cookie
	// ***********************************************

	// unique id - will return a unique number for the ID
	ID = ''; 			// global variable that will return a unique ID;

	var internal_date = new Date();						// create a new date object
	var internal_now = internal_date.getTime(); 		// returns the time in the number of milliseconds between midnight (GMT) on 1/1/1970 and the new Date.
	if (typeof(ip)=="undefined") var ip = "1.1.1.1";
	ip = Number(ip.replace(/\./g, ""));					// replace all '.' in the ipaddress captured in the page and convert to a number.
	ID = internal_now * ip;								// ID master value
	id_char = String(ID);								// id_char is ID but converted to a string
	if (id_char.length > 20){							// if the length of the string is more than 20 characters (ie if ID is more than 20 numbers)
		ID = ID / 1000;									// then divide the number by 1000 to get this number down (over 20chars will return a number with e+21 (to the power of))
	}
	ID = String(ID);									// convert ID now to a string
	ID = ID.slice(3,17);								// capture the value from this string between the 4th and 17th characters of this ID - they are the most variable of the sequence of numbers.
	// alert("ID: "+ID);

	bbcjs.cookies.setCookie("art_"+ID, "current_page=0", "+1w"); // creates the cookie
	bbcjs.cookies.setCookie("art_active", "active="+ID, "+1w"); // creates a reference to the newly-created cookie
	createPageList();											// call page list creating function
	//location.href = (cookieCreatedURL);
	return false;
}

function createPageList() { 										// set order of the pages
//	alert("function createPageList()");
	// alert("createPageList called");
		/* Array shuffler */
		/* old method below...
		var len = pageList.length;
		var i = len;

		while (i--) {
			var p = Math.round(Math.random(len));
			var t = pageList[i];
			pageList[i] = pageList[p];
			pageList[p] = t;
		}
		*/
		
		var i = pageList.length;
		if ( i == 0 ) return false;
		while ( --i ) {
		   var j = Math.floor( Math.random() * ( i + 1 ) );
		   var tempi = pageList[i];
		   var tempj = pageList[j];
		   pageList[i] = tempj;
		   pageList[j] = tempi;
   		}
		
/*		startPages.reverse();
	
		for (var i=0; i<startPages.length; i++) {
			pageList.unshift(startPages[i]);
		}
	
		for (var i=0; i<endPages.length; i++) {
			pageList.push(endPages[i]);
		}
*/	
		var startPagesMaster = startPages.join("_");
		var endPagesMaster = endPages.join("_");
		var tempStr = pageList.join("_");
	
		var finalString = startPagesMaster+"_"+tempStr+"_"+endPagesMaster;
//		alert(finalString);
		
		// convert new array back into a string, with correct delimeter
		var tempStr = pageList.join("_");
//		alert(tempStr);
		// alert("tempStr: "+tempStr);	
		//active_cookie.pageList = tempStr;
		//alert(bbcjs.cookies.cookieData.art_active.pageList);
		
if (bbcjs.cookies.cookieData.art_active) {
	// alert("Cookies exist");
	active = bbcjs.cookies.cookieData.art_active.active;
	active_cookie = bbcjs.cookies.cookieData["art_"+active];
	current_page = bbcjs.cookies.cookieData["art_"+active].current_page;
	// alert("active: "+active+", active_cookie: "+active_cookie);
} else {
	// alert("No cookies currently exist");
}
		
		// alert("active is: "+active);
		
//		active_cookie.pageList = tempStr;
		active_cookie.pageList = finalString;
		active_cookie.current_page = 0;
		active_cookie.next_page = 0;
		bbcjs.cookies.setCookie("art_"+active, active_cookie, "+1y");
		
		location.href = (cookieCreatedURL);
		
		//bbcjs.cookies.cookieData.art_active.pageList = tempStr;
		//active_cookie.current_page = 0;
		//bbcjs.cookies.setCookie("art_"+active, "pageList="+tempStr, "+1y");
}





//alert("[goNextPage b4] current_page: "+current_page);
// returns the page name and filepath where the app should go next
function getNextPage(){
	// alert("getNextPage called");
	// split the art_list into a temp array
	var pageListTempArray = bbcjs.cookies.cookieData['art_'+active].pageList.split("_");
	var theNextPageVar = active_cookie.next_page;
	var theNextPage = pageListTempArray[theNextPageVar];
	//alert("[getNextPage] The next page to visit is: "+theNextPage);
	//alert("active_cookie.next_page: "+active_cookie.next_page);
	var newPage = "/science/humanbody/mind/surveys/art/"+theNextPage+".shtml";
	return newPage;
}





//alert("[goNextPage b4] current_page: "+current_page);
// returns the page name and filepath where the app should go next
function goNextPage(){
	//alert("[goNextPage] current_page: "+current_page);
	// alert("goNextPage called");
	// split the art_list into a temp array
	var pageListTempArray = bbcjs.cookies.cookieData['art_'+active].pageList.split("_");
	//active_cookie.next_page = parseInt(active_cookie.current_page)+1;
	//bbcjs.cookies.setCookie("art_" + active, active_cookie, "+1y");
	var theNextPageVar = active_cookie.next_page;
	var theNextPage = pageListTempArray[theNextPageVar];
	//alert("current_page: "+current_page);
	//alert("pageListTempArray: "+pageListTempArray);
	//alert("[goNextPage] The next page to visit is: "+theNextPage);
	//alert("active_cookie.next_page: "+active_cookie.next_page);
	var newPage = "/science/humanbody/mind/surveys/art/"+theNextPage+".shtml";
	var newCurrentPage = active_cookie.current_page;
	var newNextPage = active_cookie.next_page;
	newCurrentPage = parseInt(newCurrentPage);
	newNextPage = parseInt(newNextPage);
	newCurrentPage+=1;
	newNextPage+=1;
	active_cookie.current_page = newCurrentPage;
	active_cookie.next_page = newNextPage;
	bbcjs.cookies.setCookie("art_" + active, active_cookie, "+1y");
	return newPage;
}



//add items to cookie (if id contains "add2cookie")
function add2Cookie(form){
	// alert("add2Cookie called");
	if (bbcjs.cookies.cookieData.art_active) {
		// alert("Cookies exist");
		bbcjs.trace("Cookies exist", 3);
		active = bbcjs.cookies.cookieData.art_active.active;
		active_cookie = bbcjs.cookies.cookieData["art_"+active];
		current_page = bbcjs.cookies.cookieData["art_"+active].current_page;
		// alert("active: "+active+", active_cookie: "+active_cookie);
	} else {
		// alert("No cookies currently exist");
	}

//trace("add2Cookie called");
//alert('in add2cookie, form = ' + form.name);
//whether anything added to cookie
var bln_cookie = false;	
//whether radio group checked
var done="";

// alert("gets here 1...");
	
		//check for elements to be added to cookie
	for (var i=0; i<form.length; i++){
		//alert(form[i].name+"=(id)"+form[i].id);
		if (form[i].id.indexOf("add2cookie") != -1){
		//alert(form[i].name+'is add cookie');
		
			
			if (form[i].type == "radio"){
			// alert("found a radio button");
			// alert("its name is: "+form[i].name);
			
			var theRadioName = form[i].name;
			
			var formObject = bbcjs.forms.form2object(form);
			
			// alert("its value is: "+formObject[theRadioName]);
			
			active_cookie[theRadioName] = formObject[theRadioName];
			bbcjs.cookies.setCookie("art_" + active, active_cookie, "+1y");
			// alert("value in the cookie now is: "+active_cookie[theRadioName]);
			
			// alert("gets here 2...");
			}
			else{
			//bbcjs.cookies.cookieData.user_cookie[form[i].name] = form[i].value;	
			//alert(form[i].name+'is adding to active');
			active_cookie[form[i].name] = form[i].value;	
		}				
		bln_cookie = true;								
	}
}

// alert("gets here 3...");

//if required, update cookie
if (bln_cookie){		
	bbcjs.cookies.setCookie("art_" + active, active_cookie, "+1y");
	}
}

//send score - to L2R
function sendScore(form){
	if (bbcjs.cookies.cookieData.art_active) {
		// alert("Cookies exist");
		//alert(form.action);
		bbcjs.trace("Cookies exist", 3);
		active = bbcjs.cookies.cookieData.art_active.active;
		active_cookie = bbcjs.cookies.cookieData["art_"+active];
		current_page = bbcjs.cookies.cookieData["art_"+active].current_page;
		// alert("active: "+active+", active_cookie: "+active_cookie);
	} else {
		// alert("No cookies currently exist");
	}
	
	// alert("sendScore called");
	
	// this is calling an error...fix!!!
	
	//alert("unid: "+unid);
	form.unid.value = bbcjs.cookies.cookieData.art_active.active;
	
	//set L2R end page
	//if (current_page <= tot_pages){
		//form.L2REndPage.value = "/science/humanbody/sex/" + current_page + ".shtml";
	//}
	//alert(form.L2REndPage.value);
	//alert(current_page);
	//alert(tot_pages);
	//form.submit();
	//return false;
}

