//  ---------------------------------------------------------------
//  The following function replaces the fromString with the toString
//  ---------------------------------------------------------------
function replaceSubstring(inputString, fromString, toString) {
// Goes through the inputString and replaces every occurrence of fromString with toString
var temp = inputString;
if (fromString == "") {
return inputString;
}
if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
while (temp.indexOf(fromString) != -1) {
    var toTheLeft = temp.substring(0, temp.indexOf(fromString));
    var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
    temp = toTheLeft + toString + toTheRight;
}
} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
var midStrings = new Array("~", "`", "_", "^", "#");
var midStringLen = 1;
var midString = "";
// Find a string that doesn't exist in the inputString to be used
// as an "inbetween" string
while (midString == "") {
    for (var i=0; i < midStrings.length; i++) {
    var tempMidString = "";
    for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
    if (fromString.indexOf(tempMidString) == -1) {
        midString = tempMidString;
        i = midStrings.length + 1;
    }
    }
} // Keep on going until we build an "inbetween" string that doesn't exist
// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
while (temp.indexOf(fromString) != -1) {
    var toTheLeft = temp.substring(0, temp.indexOf(fromString));
    var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
    temp = toTheLeft + midString + toTheRight;
}
// Next, replace the "inbetween" string with the "toString"
while (temp.indexOf(midString) != -1) {
    var toTheLeft = temp.substring(0, temp.indexOf(midString));
    var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
    temp = toTheLeft + toString + toTheRight;
}
} // Ends the check to see if the string being replaced is part of the replacement string or not
return temp; // Send the updated string back to the user
} 

var correctAnswers = 0;  // Questions answered correctly try 1
var correctAnswers2 = 0; // Questions answered correctly try 2
var correctAnswers3 = 0; // Questions answered correctly try 2

var percentageCorrect =0;
var percentageCorrect2 = 0;
var percentageCorrect3 = 0;	
var percentageCorrect4 = 0;	

		//  ---------------------------------------------------------------
		//  Function: scoreAnswer
		//  Use: Scores the answer that was just selected for the identified
		//  question
		//  ---------------------------------------------------------------
function scoreAnswer(strQuestion) {

var i;	
var objRadio = document.all('R' + strQuestion);	
var TotalQuestions = document.all('Questions').value;	
var enteredValue;

//document.all('baritem').style.posTop = 300 + document.body.scrollTop;


//  Determine which radio button was selected for the above question
for( i=0; i<objRadio.length; ++i)

//  Retrieve the value from the selected radio button
if( objRadio[i].checked)
enteredValue = objRadio[i].value;


//  If the answer is correct determine whether or not it
//  was answered on the first ry.	
if (enteredValue == document.all('Q' + strQuestion).value)
{

if (document.all('FirstTryQ' + strQuestion).value == ''){

//  Update the question so that the correct answer is
//  hi-lited in green and the 
document.all('FirstTryQ' + strQuestion).value = 'YES';

document.all('Q' + strQuestion + 'R' + enteredValue).style.background = '#00FF00';

document.all('Q' + strQuestion + 'T').innerHTML = replaceSubstring(document.all('Q' + strQuestion + 'RE').value, '[vbQuote]', '"');

document.all('Q' + strQuestion + 'T').style.border = '0.1cm solid #00FF00';

document.all('QuestionDone' + strQuestion).value = 'YES';
for( j=0; j<objRadio.length; ++j)
objRadio[j].disabled = true;

correctAnswers = correctAnswers + 1;

if (correctAnswers == 0)
percentageCorrect = 0;
else 
percentageCorrect =Math.round (correctAnswers / TotalQuestions * 100);

}
else{
if (document.all('SecondTryQ' + strQuestion).value == ''){
document.all('SecondTryQ' + strQuestion).value = 'YES';

document.all('Q' + strQuestion + 'R' + enteredValue).style.background = '#00FF00';

document.all('Q' + strQuestion + 'T').innerHTML = replaceSubstring(document.all('Q' + strQuestion + 'RE').value, '[vbQuote]', '"');

document.all('Q' + strQuestion + 'T').style.border = '0.1cm solid #00FF00';

document.all('QuestionDone' + strQuestion).value = 'YES';

for( j=0; j<objRadio.length; ++j)
    objRadio[j].disabled = true;

correctAnswers2 = correctAnswers2 + 1;

}
}
}
else{
if (document.all('FirstTryQ' + strQuestion).value == ''){
document.all('FirstTryQ' + strQuestion).value = 'NO';

document.all('Q' + strQuestion + 'R' + enteredValue).style.background = '#FCFF00';

}
else{			
document.all('SecondTryQ' + strQuestion).value = 'NO';	

changeAnswer = document.all('Q' + strQuestion).value - 1;

document.all('Q' + strQuestion + 'R' + enteredValue).style.background = '#FF0000';

document.all('Q' + strQuestion + 'R' + enteredValue).style.color = '#FFFFFF';

objRadio[changeAnswer].checked = true;
changeAnswer = changeAnswer + 1;

document.all('Q' + strQuestion + 'R' + changeAnswer).style.background = '#00FF00';

document.all('Q' + strQuestion + 'T').innerHTML = replaceSubstring(document.all('Q' + strQuestion + 'RE').value, '[vbQuote]', '"');

document.all('Q' + strQuestion + 'T').style.border = '0.1cm solid #00FF00';

document.all('QuestionDone' + strQuestion).value = 'YES';
for( j=0; j<objRadio.length; ++j)
objRadio[j].disabled = true;
	
}		
}

//  Determine if the user has finished taking the test

var QuestionsAnswered = 0;
for (strCount=1; strCount<=TotalQuestions;++strCount)
{
if (document.all('QuestionDone' + strCount).value == 'YES')

{
QuestionsAnswered = QuestionsAnswered + 1;

}		
}


correctAnswers3 = QuestionsAnswered - (correctAnswers2 + correctAnswers);
percentageCorrect2 =Math.round (correctAnswers / QuestionsAnswered * 100);
percentageCorrect3 =Math.round (correctAnswers2 / QuestionsAnswered * 100);
percentageCorrect4 = 100 - (percentageCorrect2 + percentageCorrect3); 


function launch(newURL, newName, newFeatures, orgName) {
var remote = open(newURL, newName, newFeatures);
if (remote.opener == null)
remote.opener = window;
remote.opener.name = orgName;
return remote;
}
	
function launchUsers() {
var TestID = document.all('TestID').value;	
myRemote = launch("record.asp?tid=" + TestID + "&finished=" + QuestionsAnswered + "&score=" + percentageCorrect2 + "&firsttry=" + correctAnswers + "&secondtry=" + correctAnswers2 + "&secondtrycorrect=" + percentageCorrect3 + "&wrong=" + correctAnswers3 + "&wrongpercent=" + percentageCorrect4,
            "myRemote",
            "height=400,width=500,alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=1,toolbar=0,z-lock=0,zindex=1",
            "myWindow");
}
	
var lbanswerquestions = true;
//for (strCount=1; strCount<=TotalQuestions;++strCount)
//{
//  if (document.all('QuestionDone' + strCount).value == '')
//  {
//    lbanswerquestions = false;
//  }		
//}

//  If the user finished answering all of the questions then
//  update the scoring area.

// if all test is complete alert with Done.
if (QuestionsAnswered == TotalQuestions)
{
  	launchUsers(); 
  	//alert(TotalQuestions);
}
else
{
    //alert(TotalQuestions);
    //alert(QuestionsAnswered);
}

var results;
if (lbanswerquestions == true){

//document.all('score3Little').innerHTML =  QuestionsAnswered +  "  of  "  + TotalQuestions;
//document.all('scoreLittle').innerHTML =  "1st Try = " + correctAnswers;
//document.all('score4Little').innerHTML =  percentageCorrect2 + "%";


//document.all('score2Little').innerHTML = "2nd Try = " + correctAnswers2;
//document.all('score4L').innerHTML =  percentageCorrect3 + "%";

//document.all('scoreWrong1').innerHTML = "Wrong = " + correctAnswers3;
//document.all('scoreWrong2').innerHTML =  percentageCorrect4 + "%";


document.all('excellent').style.backgroundColor = '#ffffff';
document.all('good').style.backgroundColor = '#ffffff';
document.all('fair').style.backgroundColor = '#ffffff';
document.all('poor').style.backgroundColor = '#ffffff';
document.all('below').style.backgroundColor = '#ffffff';

if (percentageCorrect2 >= 90)
{
document.all('excellent').style.backgroundColor = '#00FF00';
} 
else if (percentageCorrect2 < 90 && percentageCorrect2 >=80)
{
document.all('good').style.backgroundColor = '#00FF00';
} 
else if (percentageCorrect2 < 80 && percentageCorrect2 >=70)
{
document.all('fair').style.backgroundColor = '#00FF00';
}
else if (percentageCorrect2 < 70 && percentageCorrect2 >=60)
{
document.all('poor').style.backgroundColor = '#00FF00';
} 
else if (percentageCorrect2 < 60)
{
document.all('below').style.backgroundColor = '#00FF00';
}
}
}