var Problem = 1
var Problems = 8
chapter = "c15s1"
path = "/oltutor/mt5/c15s1/images/"

var Answerstring

onImageArray = new Array()
offImageArray = new Array()
problem = new Array()
checked = new Array()
answer = new Array()
var score = 0

function RemoveFocus(form){
document.Practice.Answer.blur()
document.Practice.Score.blur()
document.Practice.UserAnswer.focus()
}


function handler(e){
  if ( navigator.appName == "Netscape" )
	var keycode = new Number(e.which)
	else
	var keycode = (window.event.keyCode)
	
	if ( keycode == "" ) return
	if ( keycode == 13) CheckAnswer('smileyimage', document.forms[0], Problem)
} 


function Initialize() {
document.Practice.Answer.value=""
document.Practice.UserAnswer.value=""
document.Practice.UserAnswer.focus()
document.Practice.Answer.blur()
document.Practice.Score.blur()
}

if (document.images){

for (var i =0; i <= Problems; i++)
{
   	problem[i] = new Image()
  	checked[i] = 0
}

nextblack = new Image()
nextglow = new Image()
backglow = new Image()

smiley = new Image()
smileyblack = new Image()
sad = new Image()

for (i=1; i<=Problems; i++) {

problem[i].src = path + chapter + "p" + i + ".gif"
}

path = "/oltutor/mt5/images/"

smiley.src = path + "smiley.gif"
smileyblack.src = path + "smileyblack.gif"
sad.src = path + "sad.gif"
nextblack.src = path + "nextblack.gif"
backglow.src = path + "backglow.gif"
nextglow.src = path + "nextglow.gif"

answer[1] = 'D'
answer[2] = 'E'
answer[3] = 'C'
answer[4] = 'D'
answer[5] = 'E'
answer[6] = "B"
answer[7] = "B"
answer[8] = "A"



for (var i=0; i<=7; i++) {
	onImageArray[i] = new Image()
  	offImageArray[i] = new Image()
}

path="/newimages/"

onImageArray[0].src = path+"homeglo.gif"
onImageArray[1].src = path+"companynewsglo.gif"
onImageArray[2].src = path+"productsglo.gif"
onImageArray[3].src = path+"orderglo.gif"
onImageArray[4].src = path+"onlinetutorglo.gif"
onImageArray[5].src = path+"supportglo.gif"
onImageArray[6].src = path+"registerglo.gif"
onImageArray[7].src = path+"investorsglo.gif"

offImageArray[0].src = path+"home.gif"
offImageArray[1].src = path+"companynews.gif"
offImageArray[2].src = path+"products.gif"
offImageArray[3].src = path+"order.gif"
offImageArray[4].src = path+"onlinetutor.gif"
offImageArray[5].src = path+"support.gif"
offImageArray[6].src = path+"register.gif"
offImageArray[7].src = path+"investors.gif"
}

function ChangeImageOn(imagename,i)
{

if(document.images){
document [imagename].src = onImageArray[i].src
}
}

function ChangeImageOff(imagename,i)
{
if (document.images){
document [imagename].src = offImageArray[i].src
}
}
// Function to open format Window
function openformatwindow()
{
 var formatwindow = window.open('c15s1format.html','Format','scrollbars=yes,maximize=no,resizeable=no,WIDTH=320,HEIGHT=250')
 }

function openlessonwindow(){
var lessonwindow = window.open('c15s1lesson.html','','scrollbars=yes,maximize=no,resizeable=no,WIDTH=450,HEIGHT=500')
}

function NextProblem(ProblemImage, smileyimage, form)
{

if(Problem == Problems) {
alert ('This is the last Problem!')
}

if(Problem < Problems) {
Problem++;
document [ProblemImage].src = problem[Problem].src
if(Problem == Problems){ 
document.Practice.NextImage.src = nextblack.src
document.Practice.PrevImage.src = backglow.src
}
else
{
document.Practice.NextImage.src = nextglow.src
document.Practice.PrevImage.src = backglow.src
}

if(checked[Problem] == 1) {
document.Practice.UserAnswer.value = "Problem Completed"
document.Practice.Answer.value = answer[Problem]
document [smileyimage].src = smileyblack.src
}
else
{
document.Practice.UserAnswer.value = ""
document.Practice.Answer.value = ""
document.Practice.UserAnswer.focus(); 
document [smileyimage].src = smileyblack.src
}

}
}

function PrevProblem(ProblemImage, smileyimage, form)
{


if(Problem == 1){
alert ('This is the first Problem!')
}

if (Problem >1) {
Problem--;
document [ProblemImage].src = problem[Problem].src
document.Practice.NextImage.src = nextglow.src

if(Problem == 1){
document.Practice.PrevImage.src = nextblack.src
document.Practice.NextImage.src = nextglow.src
}



if(checked[Problem] == 1) {
document.Practice.UserAnswer.value = "Problem Completed"
document.Practice.Answer.value = answer[Problem]
document [smileyimage].src = smileyblack.src
}
else
{
document.Practice.UserAnswer.value = ""
document.Practice.Answer.value = ""
document.Practice.UserAnswer.focus(); 
document [smileyimage].src = smileyblack.src
}

}
}

function CheckAnswer(smileyimage, form, Problem)
{
// Compare User Answer and answer.

var index, lastindex

if (checked[Problem] == 1) {

if (Problem < Problems){
if(CheckAllComplete() == true) return
else
alert ('Click on Next Problem')
return
}

if(Problem == Problems){
if(CheckAllComplete() == true) return
else
alert ('Click on Previous Problem')
return
}

} // end of if

Answerstring = document.Practice.UserAnswer.value
Answerstring = processstring(Answerstring)
if(Answerstring != "")
	 checked[Problem] = 1
else
{
document.Practice.UserAnswer.focus()
return
}
// Added processstring for correctanswer

CAnswerstring = processstring(answer[Problem])
//CAnswerstring = answer[Problem]

if(CAnswerstring.indexOf(" R",0) >0) {
//Remove Spaces in both Answerstring and CAnswerstring containing Remainder Answers
while ((index = CAnswerstring.indexOf(" ",0)) >0){
lastindex = CAnswerstring.length-1
CAnswerstring = CAnswerstring.substring(0, index) + CAnswerstring.substring(index+1, lastindex+1)
}

while ((index = Answerstring.indexOf(" ",0)) >0){
lastindex = Answerstring.length-1
Answerstring = Answerstring.substring(0, index) + Answerstring.substring(index+1, lastindex+1)
}

CAnswerstring = CAnswerstring.toUpperCase()
Answerstring = Answerstring.toUpperCase()

//Ready to compare strings containing remainder answers
CompareAnswers(CAnswerstring, Answerstring, CAnswerstring, smileyimage, form)
return
} // contains remainder answers - end of if with remainder


CAnswerstring = CAnswerstring.toUpperCase()
Answerstring = Answerstring.toUpperCase()

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

if(CAnswerstring.charAt(0) == "$"){
lastindex = CAnswerstring.length -1
CAns1 = CAnswerstring.substring(1,lastindex+1) // $ Removed

CAns2 = CAns1
while((index = CAns2.indexOf(",",0))>0){
CAns2 = CAns2.substring(0,index) + CAns2.substring(index+1, CAns2.length)
} // $ and , removed

CAns3 = CAnswerstring
while ((index = CAns3.indexOf(",",0))>0) {
CAns3 = CAns3.substring(0,index) + CAns3.substring(index+1,CAns3.length)
} //only , removed but $ retained

if (CompareAnswers(CAnswerstring, Answerstring, CAnswerstring, smileyimage, form) == true){
	return}
else if(CompareAnswers(CAns1, Answerstring, CAnswerstring, smileyimage, form) == true){
	alert ("Don't forget to include $ symbol")
	return
	}
else if(CompareAnswers(CAns2, Answerstring, CAnswerstring, smileyimage, form) == true){
	     			if(CAnswerstring.indexOf(",") >0) {
					alert ("Don't forget to include  $ and , symbols")}
					else
					alert ("Don't forget to include $ symbol")
	return
	}
else if(CompareAnswers(CAns3, Answerstring, CAnswerstring, smileyimage, form) == true){
	     			if(CAnswerstring.indexOf(",") >0) {
					alert ("Don't forget to include , symbols")}
	return
	}

//remove commas both sides (inconsistent commas by user)-assumes user entered $ symbol
if(CAnswerstring.indexOf(",") >0) {
CAns3 = CAnswerstring
while ((index = CAns3.indexOf(",",0))>0) {
CAns3 = CAns3.substring(0,index) + CAns3.substring(index+1,CAns3.length)
} //only , removed but $ retained
Ans3 = Answerstring
while ((index = Ans3.indexOf(",",0))>0) {
Ans3 = Ans3.substring(0,index) + Ans3.substring(index+1,Ans3.length)
} // commas removed but $ retained
if(CompareAnswers(CAns3, Ans3, CAnswerstring, smileyimage, form) == true){
	alert ("Include commas consistently !!")
	return
}
}

//remove $ and commas on both sides - assumes user enterd $ but entered commas inconsistently
if(CAnswerstring.indexOf(",") >0) {
lastindex = CAnswerstring.length -1
CAns2 = CAnswerstring.substring(1,lastindex+1) // $ Removed

while((index = CAns2.indexOf(",",0))>0){
CAns2 = CAns2.substring(0,index) + CAns2.substring(index+1, CAns2.length)
} // $ and , removed

lastindex = Answerstring.length -1
Ans2 = Answerstring.substring(1,lastindex+1) // $ Removed

while((index = Ans2.indexOf(",",0))>0){
Ans2 = Ans2.substring(0,index) + Ans2.substring(index+1, Ans2.length)
} // $ and , removed

if(CompareAnswers(CAns2, Ans2, CAnswerstring, smileyimage, form) == true){
	alert ("Include commas consistently !!")
	return
}
}
} //Processed Answers with $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


if (CAnswerstring.indexOf(",",0) >0){
//CAnswerstring has commas, assumes user did not include any commas
CAns = CAnswerstring
while ((index = CAns.indexOf(",",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
	if(CompareAnswers(CAns, Answerstring, CAnswerstring, smileyimage, form) == true){
		alert("Don't forget to include , symbols")
		return
		}
//CAnswerstring has commas, assumes used commas inconsistently
//check for inconsistent commas

CAns = CAnswerstring
Ans = Answerstring

if (commaconsistency(CAns, Ans) == false) {
while ((index = CAns.indexOf(",",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}

while ((index = Ans.indexOf(",",0))>0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
}
	if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		alert("Include commas consistently")
		return
		}
} 
} // end of if consistency = false


if (CAnswerstring.indexOf(" ",0) > 0) {
// Spaces inside: Remove spaces and use commas logic
var TempCAnswer= CAnswerstring

while ((index = TempCAnswer.indexOf(" ",0))>0) {
	TempCAnswer = TempCAnswer.substring(0,index) + TempCAnswer.substring(index+1,TempCAnswer.length)
}

if (CAnswerstring.indexOf(",",0) > 0) {
//CAnswerstring has commas, assumes user did not include any commas

CAns = TempCAnswer
while ((index = CAns.indexOf(",",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
	if(CompareAnswers(CAns, Answerstring, CAnswerstring, smileyimage, form) == true){
		alert("Don't forget to include , symbols")
		return
		}

//CAnswerstring has commas, assumes user specified commas inconsistently
CAns = TempCAnswer
Ans = Answerstring

if (commaconsistency(CAns, Ans) == false) {
CAns = TempCAnswer
while ((index = CAns.indexOf(",",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
Ans = Answerstring
while ((index = Ans.indexOf(",",0))>0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
}
	if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		alert("Correct - Include commas consistently")
		return
		}
}

// CAnswerstring has commas and spaces.  User has entered commas consistently but no spaces at all

CAns = TempCAnswer
Ans = Answerstring

	if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		return
		}

} // end of if for commas
} // end of if for spaces


if (CAnswerstring.indexOf(",",0) > 0) {
// commas inside: Remove commas
var TempCAnswer= CAnswerstring

while ((index = TempCAnswer.indexOf(",",0))>0) {
	TempCAnswer = TempCAnswer.substring(0,index) + TempCAnswer.substring(index+1,TempCAnswer.length)
}

if (CAnswerstring.indexOf(" ",0) > 0) {
//CAnswerstring has spaces, assumes user did not include any spaces

CAns = TempCAnswer
while ((index = CAns.indexOf(" ",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
	if(CompareAnswers(CAns, Answerstring, CAnswerstring, smileyimage, form) == true){
		//alert("Correct - Please include appropriate spaces")
		return
		}

//CAnswerstring has spaces, assumes user specified spaces inconsistently
CAns = TempCAnswer
Ans = Answerstring

if (spaceconsistency(CAns, Ans) == false) {
CAns = TempCAnswer
while ((index = CAns.indexOf(" ",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
Ans = Answerstring
while ((index = Ans.indexOf(" ",0))>0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
}
	if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		alert("Correct - Please Include spaces consistently")
		return
		}
}

// CAnswerstring has commas and spaces.  User has entered spaces consistently but no commas at all
// Already Covered elsewhere

} // end of if for spaces
} // end of if for commas


//CAnswer has both spaces and commas.  User entered commas (Inconsistent)and spaces (Consistent or Inconsistent)
//Remove commas and spaces and compare
if((CAnswerstring.indexOf(",",0)>0) && (CAnswerstring.indexOf(" ",0)>0)) {
CAns = CAnswerstring
Ans = Answerstring

// Assume the user entered the answer with spaces and commas correctly, Check for this first.

if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		return
		}

while ((index = CAns.indexOf(" ",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
Ans = Answerstring
while ((index = Ans.indexOf(" ",0))>0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
}

while ((index = CAns.indexOf(",",0))>0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}

while ((index = Ans.indexOf(",",0))>0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
}

if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		alert("Correct - Please include commas consistently and spaces as appropriate")
		return
		}
}


//Answer has spaces but has no commas
if ((index = CAnswerstring.indexOf(" ",0)) > 0) {

CAns = CAnswerstring
Ans = Answerstring
if (spaceconsistency(CAns, Ans) == false) {

CAns = CAnswerstring
while ((index = CAns.indexOf(" ",0)) > 0) {
	CAns = CAns.substring(0,index) + CAns.substring(index+1,CAns.length)
}
Ans = Answerstring
while ((index = Ans.indexOf(" ",0)) > 0) {
	Ans = Ans.substring(0,index) + Ans.substring(index+1,Ans.length)
} // end of while
if(CompareAnswers(CAns, Ans, CAnswerstring, smileyimage, form) == true){
		alert ("Don't forget to include proper spaces")
		return
} //end of if

} // end of space consistency
}

if(CompareAnswers(CAnswerstring, Answerstring, CAnswerstring, smileyimage, form) == true) return

if (document.images){
document [smileyimage].src = sad.src
//document.Practice.Answer.value = answer[Problem]
document.Practice.Answer.value = CAnswerstring
document.Practice.Score.value = "Score: "+ score + " of " + Problems
}

} // end of function CheckAnswer





function CompareAnswers(CAnswer, Answer, CAnswerstring, smileyimage, form)
{
if (CAnswer == Answer && document.images) {
document [smileyimage].src = smiley.src
score++;
document.Practice.Score.value = "Score: "+ score + " of " + Problems
document.Practice.Answer.value = CAnswerstring
CheckAllComplete()
return true
}
else
{
CheckAllComplete()
return false
} // end of if
}  // end of function CompareAnswers

/////////////////////////////
function CheckAllComplete()
{

for (var i=1; i <= Problems; i++)
{
if (checked[i] == 0) return false
}

alert ('CONGRATULATIONS ! YOU HAVE COMPLETED THE SESSION ')
return true
}   	

function processstring(str)
{
var length = str.length

//Remove Trailing blanks
var lastindex = length-1
while(str.indexOf(" ",lastindex)==lastindex)
{
str=str.substring(0,lastindex)
length--
lastindex--
}

//remove leading blanks
while(str.charAt(0) == " ")
{
str=str.substring(1, lastindex + 1)
length--
lastindex--
}

//Remove remainder values
length = str.length
lastindex = length-1
var check=str.indexOf(" R0",0)
if(check > 0) str = str.substring(0, lastindex-2)
check=str.indexOf(" r0",0)
if(check > 0) str = str.substring(0, lastindex-2)
check=str.indexOf("R0",0)
if(check > 0) str = str.substring(0, lastindex-1)
check=str.indexOf("r0",0)
if(check > 0) str = str.substring(0, lastindex-1)

lastindex = str.length -1
//Remove trailing zeros
if( str.indexOf('.',0) > 0)
{
length=str.length
while(str.indexOf('0',lastindex) == lastindex)
{
str = str.substring(0,lastindex)
lastindex--
}


//Remove terminating decimal
length = str.length
lastindex = str.length -1
//alert("inside decimal" + lastindex);
if (str.indexOf('.',lastindex ) == lastindex)
	str=str.substring(0,lastindex)
}

if (str.charAt(0) == ".") str = "0" + str

return str;
}

function commaconsistency(CAns, Ans) {
//CAnswerstring has commas, assumes used commas inconsistently
//check for inconsistent commas

startindex = 0
consistency = true
while ((index=CAns.indexOf(",",startindex)) >0) {
	
	if (index == Ans.indexOf(",",startindex)) 
	startindex = index+1
	else
	{
	consistency = false
	break;
	}
}

return consistency

} // end of commaconsistency

function spaceconsistency(CAns, Ans) {
//CAnswerstring has spaces, assumes used spaces inconsistently
//check for inconsistent spaces

startindex = 0
consistency = true
while ((index=CAns.indexOf(" ",startindex)) >0) {
	
	if (index == Ans.indexOf(" ",startindex)) 
	startindex = index+1
	else
	{
	consistency = false
	break;
	}
}

return consistency

} // end of commaconsistency


