//these functions are used by 01, 03 and 04
function fnDynamicAnswers	(s){	document.all("possibleAnswers").innerHTML=s }
function fnDynamicComment	(s){	document.all("comment").innerText = s }
function fnDynamicButton	(s){ 	document.all("nextbutton").innerHTML =s }
function fnDynamicStarter	(s){	document.all("starter").innerHTML=s}
function fnDynamicNavHeading	(s){	document.all("navheading").innerHTML=s}
function fnDynamicTextBox	(s){ 	document.all("textbox").innerHTML=s}
function fnDynamicBottomNav	(s){ 	document.all("bottomNav").innerHTML=s}

function fnClearAllDynamicSpans(){
	s="";
	fnDynamicAnswers(s);
	fnDynamicComment(s);
	fnDynamicButton(s);
	fnDynamicNavHeading(s);
	fnDynamicQuizQuestion(s);
	//fnDynamicStarter(s);
	fnDynamicTextBox(s);
	fnDynamicBottomNav(s);
	document.mark.src="/a/images/quiz/mark_blank.gif";
}

function buildNavigation(theSelection){
	//**********   builds the top navigation strip for 1, 3 and 4
	var p="/a/images/quiz/" //path
	var wb= " width='19 ' border='0' "
	standardEndStr=wb+"height='20'></a>"
	selectedEndStr=wb+"height='23'></a>";
	navEndStr= standardEndStr
	navStStr= "<a href='javascript:buildNavigation("
	
	//***** build previous button***********
	navStr="<a href='javascript:goPreviousQuestion(\""+theSelection+"\")'><img src='"+p+"previous_up.gif' alt='Previous'"+wb+"height='20'></a>"

	introgif = (theSelection=="intro") ? "intro_dn.gif" : "intro_up.gif";
	navEndStr = (theSelection=="intro") ? selectedEndStr : standardEndStr;	
	navStr=navStr+navStStr+"\"intro\")'><img src='"+p+introgif+"' alt='Introduction' "+navEndStr;

	examplegif = (theSelection=="example") ? "example_dn.gif" : "example_up.gif";
	navEndStr = (theSelection=="example") ? selectedEndStr : standardEndStr;
	navStr=navStr+navStStr+"\"example\")'><img src='"+p+examplegif+"' alt='Example' "+navEndStr;
	
	//*******  build question number buttons - red for wrong, green for right, blue for neutral
	navEndStr=standardEndStr;
	i=1;
	while (i<=questionCount){
		gifType="untried";	
		sa=scoreArray[i-1];
		if (sa==1) gifType= "correct";	
		if (sa==0) gifType="wrong";

		if (i==theSelection) {gifType="dn";navEndStr=selectedEndStr}
		navStr=navStr+navStStr+i+")'><img src='"+p+i+"_"+gifType+".gif' alt='Question "+i+"' "+navEndStr;
		navEndStr=standardEndStr;
		i++;
	}	

	//*****    build marks button***********
	marksgif = (theSelection=="marks") ? "marks_dn.gif" : "marks_up.gif";
	navEndStr = (theSelection=="marks") ? selectedEndStr : standardEndStr;
	navStr=navStr+navStStr+"\"marks\")'><img src='../images/quiz/"+marksgif+"' alt='My Total Score' "+navEndStr;

	//*****     build next button***********
	navStr=navStr+"<a href='javascript:goNextQuestion(\""+theSelection+"\")'><img src='"+p+"next_up.gif' alt='Next' width='20' height='20' border='0'></a>";
	document.all("quiznavigation").innerHTML=navStr;

	//*****     build dynamic button under text box
	var s= "  border='0'></a>"
	theNextStr="<a href='javascript:goNextQuestion(\""+theSelection+"\")'><img src='"+p+"next_question.gif'  alt='Next Page'"+s;
	//theCheckStr="<a href='javascript:fnCheckAnswer(\""+theSelection+"\")'><img src='"+p+"check_my_answer.gif' alt='Check My Answer'"+s;
	//theShowAnswersStr="<a href='javascript:fnShowAnswers()'><img src='"+p+"show_answers.gif' alt='Show Possible Answers'"+s;
	fnDynamicButton(theNextStr );
	

	buildContent(theSelection);
	
}

function fnDynamicQuizQuestion	(s){ 	
//used in quiz types 1,3 and 4
	document.all("quizquestion").innerHTML=s;
	if (s!==""){
		document.all("questioncell").style.backgroundColor="#D7E7F8";
	}else{
		document.all("questioncell").style.backgroundColor="transparent";
	}
}

function fnSetUp01_03_04(){ 
//used in quiz types 1,3 and 4
	defaultTextBoxValue="Write the full sentence here, with the correct pronunciation."
	questionCount=((questionArray.length)/7)-2;
	pageArray=["intro","example"];
	currentPage=-1;
	scoreArray=[];
	answerArray=[];
	fTextBoxDefault=1;
	//document.all("worksheetHeading").innerHTML=worksheetheading;

	var i=0
	while (i<questionCount){
		i++;
		var c=pageArray.length
		pageArray[c]=i
		scoreArray[c-2]=-1
		answerArray[c-2]="";
	}
	pageArray[c+1]="marks";
	//--end of 01,03 and 04 shared setup------
}

function fnRemoveEndSpaces(s){
	//32 is the ascii for a space. Repeat until all spaces are removed from end of string
	while(s.charCodeAt((s.length)-1)==32) { s=s.substring(0,(s.length)-1) }
	return s;
}

function fnRemoveStartSpaces(s){
	//32 is the ascii for a space. Repeat until all spaces are removed from start of string
	while(s.charCodeAt(0)==32) { s=s.substring(1,(s.length)) }
	return s;
}

function fnRemoveMultipleSpaces(s){

	var sNew=""
	var fAddSpace=0;
	var sc=s.length;
	var i=0;
	while (i<sc){	
		var s2=s.charCodeAt(i);
		if (s2==32){
			var fAddSpace=1;
		}else{
			
			if (fAddSpace==1){
				var sNew=sNew+" ";
				var fAddSpace=0;
			}
			var sNew=sNew+s.charAt(i);
		}
		i++
	}
return sNew;
}


function fnRemoveEndReturns(s){
	//paragraph returns are made up of ascii 10 and 13
	while((s.charCodeAt((s.length)-1)==10)||(s.charCodeAt((s.length)-1)==13)||(s.charCodeAt((s.length)-1)==32)){ 
		s=s.substring(0,(s.length)-1);
	}
	return s;
}

function fnRemoveStartReturns(s){
	//paragraph returns are made up of ascii 10 and 13, space is 32
	while((s.charCodeAt(0)==10)||(s.charCodeAt(0)==13)||(s.charCodeAt(0)==32)){
		s=s.substring(1,(s.length));
	}
	return s;
}


function fnPluralise(theSingleWord, thePluralWord, theNumber){
	theWord=(theNumber==1) ? theSingleWord :  thePluralWord;
	return theWord;
}

function getPos(theArray,theValue){
	len=theArray.length;
	i=0;
	while (i<len){
		a=""+theArray[i]
		if (a==theValue) return i;
		i++;
	}
}


function fnRandomiseArray(theArray){
	oldArray=[]
	for (i=0;i<theArray.length;){
		oldArray[i]=theArray[i];i++
	}
	var contents=oldArray
	newArray=[]
	var i=0
	var random
	while (i<contents.length){
		random=Math.floor(Math.random()*contents.length)
		if (contents[random]!="selected"){
			newArray[newArray.length]=(contents[random])
			contents[random]="selected"
			i++
		}
	} 
	return newArray
}

function fnReverseArray(oldArray){
	var newArray=[];
	while (oldArray.length!=0){
		//get the lastElement and delete from array
		oldArrayCount=oldArray.length;
		e=oldArray[oldArrayCount-1]
		oldArray.length=oldArrayCount-1
		newArrayCount=newArray.length
		newArray[newArrayCount]=e
	}
	return newArray;
}

function fnRandomNum(intFrom, intTo){
	if (intFrom==intTo){
		r=intFrom;
	}else{
		c=intTo-intFrom;
		r=Math.round((Math.random())*c);
	}
	return r
}

function fnSaveFooter(){
	theFooter=document.all("eeefooter").innerHTML;
	document.all("eeefooter").innerHTML="";
}

function fnReturnLevelButton(){
	var fileName=""+window.location;
	//var fileNameEnd=theURL.length;
	//var fileNameStart=(theURL.lastIndexOf("/"))+1;
	//var fileName=theURL.slice(fileNameStart,fileNameEnd);
	//var sectionCode=theURL.slice(fileNameEnd-5,fileNameEnd-4);
	//var myRegExp=/_level_1/g;
	sectionCode="0";
	if (/_lev_1_/.test(fileName)){sectionCode="1"}
	if (/_lev_2_/.test(fileName)){sectionCode="2"}
	if (/_lev_3_/.test(fileName)){sectionCode="3"}
	if (/_lev_all_/.test(fileName)){sectionCode="all"}
	if (sectionCode=="all"){
		sectionCode="0";
		var fileName=window.history.previous;
alert(history.previous);if (fileName==undefined){fileName="-"}
		//var fileNameEnd=theURL.length;
		//var fileNameStart=(theURL.lastIndexOf("/"))+1;
		//var fileName=theURL.slice(fileNameStart,fileNameEnd);
		if (/_lev_1_/.test(fileName)){sectionCode="1"}
		if (/_lev_2_/.test(fileName)){sectionCode="2"}
		if (/_lev_3_/.test(fileName)){sectionCode="3"}
	}
	if (sectionCode!="0"){
		return("<a href='/a/eee/content_level_"+sectionCode+".htm'><img src='/a/images/nav/level_button_"+sectionCode+".gif' width='71' height='22' border='0'></a>");
	}else{
		return("");
	}
}
