var splitter='|';
var firstLetter = 'A';
var asciiStart = firstLetter.charCodeAt(0);
var yellow = '#FFEC19';//#'ffff66'


function waitSet()
{
	setTimeout('document.images["img_busy"].src="Resources/Images/busy2.gif"', 200);
}

function openMaxPopup(url, name)
{
	width = screen.availWidth-100;

	leftoffset = (screen.availWidth-width)/2; 

	window.open(url, name, 'toolbar=NO,top=0,left=0,location=NO,status=YES,menubar=no,resizable=YES,		scrollbars=YES,outerHeight=' + screen.availHeight + ',width=' + width +  ",left="+leftoffset);

}
function sendActivity() {
   url = "http://v3webservices.prevueonline.com/AssessmentActivity.asmx/AddTestSectionBrowserActivity?authHash=6f75a0635a4f7d3f5b9af029401f8e53&testSectionID=" + testSectionID +
                            "&userCandidateID=" + candidateID + "&request=" + requestCounter + "&testSession=" + testSession + "&typeID=" + testTypeID;
   document.getElementById('logActivity').src = url;
    requestCounter++;                         
}
//answer id:  the cell id of the hidden input which stores the answer for hte panel
//answer type:  the answer type of the question, eg. Letter..
//position: position of the cell in the array of answers
//maxPosition:  the maximum possible position of a cell in an array of answers
//rootCellID: the root cell  
function alter(answerID,answerType,position,maxPosition,rootCellID,defColor)
  {	
		
		var rootIDString=rootCellID.id;

	
		//array of cell id strings
		var cellID = new Array();
		var counter = 1;
		
		//Array of string answers 
		//For 'Letter' then A,B,C,D
		//For 'Number' then 1,2,3,4 etc.
		var answer = new Array();
			
		//take out the question number at the end
		var rootPrefix = rootIDString.substring(0,rootIDString.length-1);

		for (i = 1; i <= maxPosition; i++)
		{
			if (i == position)
			{
				if (answerType=='Letter')
				{
					answer[i]=String.fromCharCode(asciiStart+i-1);
				}
				else if (answerType=='Number')
				{
					answer[i]=String(i);
				}
			}
			else if (i != position)
				{
					cellID[counter]= rootPrefix + String(i);
					counter=counter+1;
				}
		}


		//if the value of this cell is the value of answer[position]
		//as determined above, then remove the background colour -- this 
		//is if you click on an answer, it removes it from being an answer
    	if (document.forms[0].elements[answerID].value == answer[position]) 
		{
			rootCellID.style.backgroundColor = defColor;
			document.forms[0].elements[answerID].value = '0';
			
		}
		else  //setting the new answer
		{


			rootCellID.style.backgroundColor = yellow;
			document.forms[0].elements[answerID].value = answer[position];
		
			//make sure all the other cells are not coloured
			for (i = 1; i <= maxPosition-1; i++)
			{
				try
				{
				(document.getElementById(cellID[i]) != null)? document.getElementById(cellID[i]).style.backgroundColor = defColor : null;
				}
				catch(errorObject)
				{
				alert('error = ' + errorObject.description);
				}
			}
		}			
 }
 
function alterwrong(answerID,answerType,position,maxPosition,rootCellID)
  {	
		var rootIDString=rootCellID.id;
		var cellID = new Array();
		var counter = 1;
		var answer = new Array();
			
		var rootPrefix = rootIDString.substring(0,rootIDString.length-1);

		for (i = 1; i <= maxPosition; i++)
		{
			if (i == position)
			{
				if (answerType='Letter')
				{
					answer[i]=String.fromCharCode(asciiStart+i-1);
				}
				else if (answerType='Number')
				{
					answer[i]=String(i);
				}
			}
			else if (i != position)
				{
					cellID[counter]= rootPrefix + String(i);
					counter=counter+1;
				}
		}
	
		  
    	if (document.forms[0].elements[answerID].value == answer[position]) 
		{
			rootCellID.style.backgroundColor = '#ffffff';
			document.forms[0].elements[answerID].value = '';
	}
		else
		{
			rootCellID.style.backgroundColor = '#ff0000';
			document.forms[0].elements[answerID].value = answer[position];

		
			for (i = 1; i <= maxPosition-1; i++)
			{
				try
				{
				(document.getElementById(cellID[i]) != null)? document.getElementById(cellID[i]).style.backgroundColor = '#ffffff': null;
				}
				catch(errorObject)
				{
				alert('error = ' + errorObject.description);
				}
			}
		}			
 }
 

function confirmFinished(message)
{   var agree=confirm(message);
     if (agree)
	   return true ;
     else
	   return false ;
}

function confirmTimed(message)
{   
	if (timeLeft>0)
	{
		var agree=confirm(message);
		if (agree)
		return true ;
		else
		return false ;
	}
}

//An array of questions, each question holds an array of answer positions (eg. for hidden word in ALTAVES 
//an answer of LVE  would have answer positions of 145
var questionAnswerArray = new Array();

//An array of questions, each question holds the length of the answer, as in the above example, 3
var questionAnswerLengthArray = new Array();

//adds the hidden button
function textAddHidden(
	//letterbutton that was clicked
	buttonSource, 
	//answer field
	answerFieldID) 
{
	//alert(questionAnswerArray[1].length);

	//The RGB Is for firefox
	if (buttonSource.style.backgroundColor.toUpperCase() != yellow.toUpperCase()
		&& buttonSource.style.backgroundColor.toUpperCase() != 'RGB(255, 236, 25)')	
	{
		//if selected new, then make yellow
		buttonSource.style.backgroundColor=yellow;
		
		//split the buttonSource id to get the values
		var jssplit = buttonSource.id.split(splitter);
		
		//ID of the question this button belongs to
		var questionID = jssplit[1];
		
		//value of the button that was clicked eg. 'A'
		var buttonPosition = jssplit[2];
		
		//if the answer array for this question is null, then create it and set the length
		if (!(questionAnswerArray[questionID])) 
		{
			questionAnswerArray[questionID]= new Array();
			questionAnswerLengthArray[questionID]=1
		}
		else 
		{
		     questionAnswerLengthArray[questionID]+=1
		}
		
		//set the button position in the question answer array as the last item (will adjust later if nec)
		questionAnswerArray[questionID][questionAnswerLengthArray[questionID]] = buttonPosition;
		
		//get the answer field
		var answerField = document.forms[0].elements[answerFieldID];
		
		var i=0;
	
		//set the answer to include the buttonPosition at the beginning
		if (parseInt(buttonPosition) < parseInt(questionAnswerArray[questionID][1])) 
		{
			//alert('addfirst')
			answerField.value = buttonSource.innerHTML + answerField.value;
			
			//adjust the other positions int he answer
			for (i=questionAnswerLengthArray[questionID];i>=1;i--)
			{
				questionAnswerArray[questionID][i]=questionAnswerArray[questionID][i-1];		
			}
			
			//set the first part of the question's array to the buttonposition
			questionAnswerArray[questionID][1]=parseInt(buttonPosition);
		}
		else if //if the buttonPosition is greater than the first, but less than the last position as stored
		//in the question array, then need to insert the button text into the middle
		(
			parseInt(buttonPosition) > parseInt(questionAnswerArray[questionID][1]) 
			&& 
			parseInt(buttonPosition) < parseInt(questionAnswerArray[questionID][questionAnswerLengthArray[questionID]-1]))
		{
			//alert('addmiddle')
			var j=0;
			
			//for each item in the answerpositionarray
			for (j=0;j<questionAnswerLengthArray[questionID];j++)
			{
				//if the button selected is less than the question array position
				//then insert it there and reset all the answers afterwards
				if (parseInt(buttonPosition) < parseInt(questionAnswerArray[questionID][j])) 
				{
					//set the answer to include the button source in the middle
					answerField.value = answerField.value.substring(0,j-1) + buttonSource.innerHTML + answerField.value.substring(j-1);
					
					//adjust all the answer proceeding
					for (i=questionAnswerLengthArray[questionID];i>=j;i--)
					{
						questionAnswerArray[questionID][i]=questionAnswerArray[questionID][i-1];		
					}
					
					//Set the questionAnswerArray with the current position
					questionAnswerArray[questionID][j]=parseInt(buttonPosition);
					
					//set j to this so it breaks out of for loop
					j=questionAnswerLengthArray[questionID];
				}
			}
		}
		else  //otherwise add to the end
		{
			//alert('addend')
			answerField.value += buttonSource.innerHTML;
		}
	}
}

//adds the anagram letters in the order they are clicked
function textAddAnagram(
buttonSource, 
answerFieldID) 
{

	//The RGB Is for firefox
	if (buttonSource.style.backgroundColor.toUpperCase() != yellow.toUpperCase()
	&& buttonSource.style.backgroundColor.toUpperCase() != 'RGB(255, 236, 25)')	{
	
		buttonSource.style.backgroundColor=yellow;
		
		//splitter for getting values
		var jssplit = buttonSource.id.split(splitter);
		
		var questionID = jssplit[1];
		
		var buttonPosition = jssplit[2];
		
  		if (!(questionAnswerArray[questionID])) 
		{
			questionAnswerArray[questionID]= new Array();
			questionAnswerLengthArray[questionID]=1
		}
		else 
		{
		     questionAnswerLengthArray[questionID]+=1
		}
		
		questionAnswerArray[questionID][questionAnswerLengthArray[questionID]]=buttonPosition;
		
		var answerField = document.forms[0].elements[answerFieldID];
		
		answerField.value += buttonSource.innerHTML;
	}
}

///removes a character from the answer
function textErase(
//id of the answer field
answerFieldID) 
{
	//get the values
	var jssplit = answerFieldID.split(splitter);
		
	//get the questionID
	var questionID = jssplit[1];
	
	//get the answers
	var answerRootID = jssplit[0];
	
	//answer field
	var answerField = document.forms[0].elements[answerFieldID];
	
	//if the answerField has some values to erase...
	if (answerField.value.length != 0) {
	
		//remove the last character
		answerField.value = answerField.value.substring(0, answerField.value.length - 1);
		
		//get the value of the answer
		var cellident=new String(answerRootID + splitter + questionID + splitter + questionAnswerArray[questionID][questionAnswerLengthArray[questionID]]); 
		
		document.getElementById(cellident).style.backgroundColor='#ffffff';
		
		questionAnswerArray[questionID].length=questionAnswerArray[questionID].length-1;
		
		questionAnswerLengthArray[questionID] -= 1;
	}
}

//an array with an entry for each question, set to true if a period has already been selected for that question
var periodselected= new Array();

//Adds a numerical value to the numerical panel
function textAddNumerical(
	//the button that was clicked
	source, 
	//the answer field
	answerFieldID) {

	//splits the source into three components
	//the ID, the position of the cell, and the value of the cell (eg. 1 or .)
	var jssplit = source.id.split(splitter);
	
	//questionID the source belongs to
	var numberQuestionID = jssplit[1];
		
	//value of the button that was clicked
	var numberButtonValue = jssplit[2];
	
	var numberanswer = source.innerHTML;

	//if a period has not yet been used for this question OR if the button was not a period
	if (periodselected[numberQuestionID]==false || numberButtonValue != "Period")
	{
	    //if answer has not been initialized, then initialize
		if (!(questionAnswerArray[numberQuestionID])) 
		{
			periodselected[numberQuestionID] = false;

			questionAnswerArray[numberQuestionID]= new Array();
			
			questionAnswerLengthArray[numberQuestionID] = 1
		}
		else 
		{

			    questionAnswerLengthArray[numberQuestionID] += 1
			
		}
		
		if(numberButtonValue == "Period" && questionAnswerLengthArray[numberQuestionID] == 1)
		{
			//if the answer is empty, then add a zero before the decimal sign
			 questionAnswerLengthArray[numberQuestionID] += 1
			 numberanswer = '0' + numberanswer;
        }
			
		
		questionAnswerArray[numberQuestionID][questionAnswerLengthArray[numberQuestionID]]=numberButtonValue;
		
		var field = document.forms[0].elements[answerFieldID];
		
		field.value += numberanswer;
		
		//sets that this question has already used the period
		if (numberButtonValue=="Period") 
		{
			periodselected[numberQuestionID]=true;
		}
	}
}

function textEraseNumerical(fieldID) {
	var jssplit = fieldID.split(splitter);
	var lineid = jssplit[1];
	var linename = jssplit[0];
	var field = document.forms[0].elements[fieldID];
	if (field.value.length != 0) 
	{
		if (field.value.substring(field.value.length - 1,field.value.length)==".") 
		{
			periodselected[lineid]=false;
		}
		field.value = field.value.substring(0, field.value.length - 1);
		questionAnswerArray[lineid].length=questionAnswerArray[lineid].length-1;
		questionAnswerLengthArray[lineid]-=1;
	}
}

var timerValue;
var timeLeft;
var time1 = new Date();
var oldTime;

function statusTimer(timeIsUpStr, secondsRemainStr, minutesRemainStr)
{
	var time1 = new Date();
	time1 = time1.getTime();
		
	timeLeft=(timerValue-(Math.floor((time1-oldTime)/1000)));

	
	if (timeLeft < 1) { 
		window.status = timeIsUpStr;
		
				document.getElementById('TimerClock').innerHTML = '0:00';
		document.getElementById('TimerClock').innerText = '0:00';
		
		//document.getElementById('Timer').value = timeIsUpStr;
		//document.getElementById('Div_Timer').innerHTML = timeIsUpStr;
		alert(timeIsUpStr) 
		//document.formQuestions.elements['timedsubmit'].value = 'YES' 
		//document.getElementById('SectionPanel_submitID').focus();
		document.getElementById('SectionPanel_Btn_Submit').click();
	} 
	else { 
		curmin = Math.floor(timeLeft / 60) 
		cursec = timeLeft % 60 
		curtime = ''
		
		var oldMinRemStr = minutesRemainStr;
		var oldSecRemStr = secondsRemainStr;
		
		if (cursec < 10) cursec = '0' + cursec 
		if (curmin!=0) 
		{
			minutesRemainStr = minutesRemainStr.replace('**currentminutes**', curmin);
			minutesRemainStr = minutesRemainStr.replace('**currentseconds**', cursec);
			curtime = minutesRemainStr;
			//curtime = curmin + ':' + cursec + ' remaining' 
		}
		else 
		{
			secondsRemainStr = secondsRemainStr.replace('**currentseconds**', cursec);
			curtime = secondsRemainStr;
			//curtime = cursec + ' seconds remaining' 
		}
		
		window.status = curtime 
		

		document.getElementById('TimerClock').innerHTML = curtime;
		document.getElementById('TimerClock').innerText = curtime;


		setTimeout("statusTimer('" + timeIsUpStr + "', '" + oldSecRemStr  + "', '" + oldMinRemStr + "')",1000); 
	} 
}

function showAll(divWaiting,divQuestions,timeIsUpStr, secondsRemainStr, minutesRemainStr) 
{

	if (document.getElementById)
	{
		oldTime = new Date();
		oldTime = oldTime.getTime();
		//if (timerValue>0) {timerValue=10}

		if (document.getElementById(divWaiting) != null)
		{
			document.getElementById(divWaiting).style.visibility = 'hidden';
			document.getElementById(divQuestions).style.visibility = 'visible';
		}
		document.body.style.cursor = 'default';
	}
	if (timerValue > 0)
	{

		window.setTimeout("statusTimer('" + timeIsUpStr + "', '" + secondsRemainStr  + "', '" + minutesRemainStr + "')",1000); 
	}
} 

function onLoad(divWaiting,divQuestions,timeIsUpStr, secondsRemainStr, minutesRemainStr)
{	
	showAll(divWaiting,divQuestions,timeIsUpStr, secondsRemainStr, minutesRemainStr);
	try 
	{	
		window.opener.document.forms.refresh1.submit()
	} 
	catch (e) 
	{
	}
}