var intOpCount=-1
var stringSubject=""
var stringJS
var intCols=5
var topNumbers=new Array(intCols)
var bottomNumbers=new Array(intCols)
var sumNumbers=new Array(intCols)
var rightAnswer=new Array(intCols+1)
var carryAnswer=new Array(intCols)
var strCarryAnswer=new Array(intCols)
var intStep=0
var topTopNumbers=new Array(intCols)
var page=""
var arrTop
var arrBottom
var arrAnswer
var arrProducts
var arrDoubleProducts
var intTopFactor
var intBottomFactor
var intAnswer
var arrProductCarry
var strTable
var strExplain
var arrCarry=new Array(intCols)
var intOutStep=0
var boolContinue=1
var intCheckContinue
var intZeros
var boolAdd=0


function divide(numerator, denominator){
var quotient=0
while (numerator>denominator){
quotient++
numerator-=denominator
}
return (quotient)
}



function IntegerToArray1(intNumber){
var strNumber=intNumber+""
this.length=strNumber.length
for(var i=0; i<this.length; i++)
	this[i]=strNumber.slice(i,i+1)
}





function WholeNumberMultiplication(){//START
var i, j, intBottom, intTop
arrProductCarry=new Array(intCols)
/* ******************************************************************* */
if(intStep==0 && boolAdd==0){//ONLY DO THIS DURING THE ZERO STEP
strTable=""
/* INITIALIZE THE PROBLEM AND MULTIPLY */
intTopFactor=Math.floor(Math.pow(10,intCols)*(.1+.9*(Math.random())))
intBottomFactor=Math.floor(Math.pow(10,intCols)*(.1+.9*(Math.random())))
intAnswer=intTopFactor*intBottomFactor
arrAnswer=IntegerToArray(intAnswer)
arrTop=IntegerToArray(intTopFactor)
arrBottom=IntegerToArray(intBottomFactor)
//intCheckContinue=arrBottom.length//KEEP COUNT ON THE OUTER LOOP
arrProducts=new Array(intCols)
arrDoubleProducts=new Array(intCols)
arrCarry=new Array(intCols)
for(intBottom=0; intBottom<intCols; intBottom++){//THE PRODUCT
        arrProducts[intBottom]=arrBottom[intBottom]*intTopFactor
        if(arrBottom[intBottom]==0){
             arrDoubleProducts[intBottom]=new Array(intCols+1)
             for(i=0; i<=intCols; i++){                 
                 arrDoubleProducts[intBottom][i]=0                 
              }
        }else{
           arrDoubleProducts[intBottom]=IntegerToArray(arrProducts[intBottom])
        }                
	arrCarry[intBottom]=new Array(intCols)	        
}//THE PRODUCT
intCheckContinue=intCols
for(intBottom=0; intBottom<intCols; intBottom++){//CARRY
for(intTop=1; intTop<arrDoubleProducts[intBottom].length; intTop++){//FOR
if(intCols<arrDoubleProducts[intBottom].length){//IF THE NUMBER OF COLUMNS IS SMALL
arrCarry[intBottom][intTop-1]=	arrDoubleProducts[intBottom][intTop]-(arrBottom[intBottom]*arrTop[intTop-1])%10
if(arrCarry[intBottom][intTop-1]<0)
	arrCarry[intBottom][intTop-1]+=10
}else{//IF THE NUMBER OF COLUMNS IS SMALL
arrCarry[intBottom][intTop]=	arrDoubleProducts[intBottom][intTop]-(arrBottom[intBottom]*arrTop[intTop])%10
if(arrCarry[intBottom][intTop-1]<0)
	arrCarry[intBottom][intTop-1]+=10
}//IF THE NUMBER OF COLUMNS IS SMALL
}//FOR
}//CARRY
//PRINT THE PROBLEM
strTable="<h2>The Problem</h2>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=0; j<intCols-intStep/2; j++){
	strTable+="<td>"+arrTop[j]+"</td>"
}
for(j=intCols-intStep/2; j<intCols; j++)
	strTable+="<td>"+arrTop[j]+"</td>"
strTable+="</tr><tr>"
strTable+="<td>&#215;</td>"
for(j=0; j<intCols; j++)
	strTable+="<td>"+arrBottom[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"+
"</table>"
parent.rbottom.document.write(strTable)
intStep++
}//ONLY DO THIS DURING THE ZERO STEP 
/* ****************************************************************** */
else if(intStep%2==0 && (intStep%(2*intCols))>0 && boolAdd==0){//EVEN BUT NEITHER ZERO NOR THE END; MULTIPLY, BUT DON'T CHANGE 
strTable=""
strTable+="<h3>Step "+(intStep/2)+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=0; j<intCols-(intStep%(2*intCols))/2-1; j++){//FOR LOOP
	strTable+="<td>"+arrTop[j]+"</td>"
}//FOR LOOP
for(j=intCols-(intStep%(2*intCols))/2-1; j<intCols; j++){//FOR LOOP
if(arrCarry[intCols-1-divide(intStep,2*intCols)][j]>0){//IF THERE IS A CARRY
strTable+="<td><span style='font-size:60%;vertical-align:top'>"+
arrCarry[intCols-1-divide(intStep,2*intCols)][j]+"</span>"+arrTop[j]+"</td>"
}//IF THERE IS A CARRY
else{//IF THERE IS NOT A CARRY
strTable+="<td>"+arrTop[j]+"</td>"
}//IF THERE IS NOT A CARRY
}//FOR LOOP
strTable+="</tr><tr>"
strTable+="<td>&#215;</td>"
for(j=0; j<intCols; j++)
	strTable+="<td>"+arrBottom[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"
for(i=0; i<divide(intStep,2*intCols); i++){//INSERT THE OLD ROWS UNDER THE EQUAL SIGN
strTable+="<tr>"
for(j=arrDoubleProducts[intCols-1-i].length-1; j>=0; j--){//ADD ADDITIONAL ROW; 
strTable+="<td>"+
arrDoubleProducts[intCols-1-i][arrDoubleProducts[intCols-1-i].length-1-j]+"</td>"
/*
intZeros=""
for(i=0; i<divide(intStep,2*intCols); i++){//STACK UP THE ZEROS
intZeros+="<td>0</td>"
}
strTable+=intZeros 
*/
}//ADD ADDITIONAL ROW//********************
strTable+="</tr><tr>"
}//INSERT THE OLD ROWS UNDER THE EQUAL SIGN	
for(j=0; j<intCols-(intStep%(2*intCols))/2+1-divide(intStep,2*intCols); j++)	
strTable+="<td>&nbsp;</td>"
for(j=(intStep%(2*intCols))/2-1; j>=0; j--)//INSERT THE NEW ROWS
strTable+="<td>"+arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][arrDoubleProducts[intCols-1-divide(intStep,2*intCols)].length-1-j]+ "</td>"//*********************
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
intStep++
}//EVEN BUT NEITHER ZERO NOR THE END; MULTIPLY, BUT DON'T CHANGE
/* ******************************************************************* */
else if(intStep%2==0 && (intStep%(2*intCols))==0 && boolAdd==0){//THE LAST EVEN SHOW THE ENTIRE ROW
strTable=""
strTable+="<h3>Step "+(intStep/2)+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=0; j<intCols; j++){//FOR LOOP
if(arrCarry[intCols-1-divide(intStep,2*intCols)][j]>0){//IF THERE IS A CARRY
strTable+="<td><span style='font-size:60%;vertical-align:top'>"+
arrCarry[intCols-1-divide(intStep,2*intCols)][j]+"</span>"+arrTop[j]+"</td>"
}//IF THERE IS A CARRY
else{//IF THERE IS NOT A CARRY
strTable+="<td>"+arrTop[j]+"</td>"
}//IF THERE IS NOT A CARRY
}//FOR LOOP
strTable+="</tr><tr>"
strTable+="<td>&#215;</td>"
for(j=0; j<intCols; j++)
	strTable+="<td>"+arrBottom[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"
for(i=0; i<divide(intStep,2*intCols)+1; i++){//INSERT THE OLD ROWS UNDER THE EQUAL SIGN
strTable+="<tr>"
for(j=arrDoubleProducts[intCols-1-i].length-1; j>=0; j--){//ADD ADDITIONAL ROW
strTable+="<td>"+
arrDoubleProducts[intCols-1-i][arrDoubleProducts[intCols-1-i].length-1-j]+"</td>"
}//ADD ADDITIONAL ROW
intZero=""
for(j=0; j<i; j++){
intZero+="<td>0</td>"
}
strTable+=intZero
strTable+="</tr>"
}//INSERT THE OLD ROWS UNDER THE EQUAL SIGN	
intStep++
if(intStep>2*intCols*intCols-2){
	boolAdd=1
        strTable+="<tr><td style='background-color:black;height:.5'"+ 
        "colspan="+(2*intCols+1)+"></td></tr>"
}
strTable+="</table>"
parent.rbottom.document.write(strTable)
        
}//THE LAST EVEN SHOW THE ENTIRE ROW
/* ******************************************************************* */
else if(intStep%2==1 && ((intStep)%(2*intCols))==2*intCols-1 && boolAdd==0){//THE LAST ODD
strExplain="<h3>Step "+(intStep)+"</h3>"+
"For the last of the multiplications by "+ arrBottom[intCols-1-divide(intStep,2*intCols)]+ ", i.e., with "+arrTop[intCols-(intStep%(2*intCols)+1)/2]+
", we get " 
if((arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+1)/2])>0){
strExplain+=(arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+1)/2])
}
strExplain+=(arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)-1)/2])+
". Write it down. <br> Now begin the next line."
if(divide(intStep,2*intCols)>1){
strExplain+="We put "+(divide(intStep,2*intCols))+" zeros down on the new line. "
}else if(divide(intStep,2*intCols)==1){
strExplain+="We put a zero down on the new line. "
}
parent.parent.leftFrame.ltop.document.write(strExplain)
intStep++
}//THE LAST ODD
/* ******************************************************************* */
else if (intStep%2==1 && boolAdd==0){//FOR THE ODD STEPS
strExplain="<h3>Step "+(intStep)+"</h3>"+
"We multiply "+ arrBottom[intCols-1-divide(intStep,2*intCols)]+" by "+arrTop[intCols-(intStep%(2*intCols)+1)/2]
if(arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+1)/2]>0){
strExplain+=" and add the carry-over "+arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+1)/2]
}
strExplain+=" getting "
if(arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+3)/2]>0){
strExplain+=arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+3)/2]
}
strExplain+=arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][arrDoubleProducts[intCols-1-divide(intStep,2*intCols)].length-1-(intStep%(2*intCols)-1)/2]+
". We put down the "+arrDoubleProducts[intCols-1-divide(intStep,2*intCols)][arrDoubleProducts[intCols-1-divide(intStep,2*intCols)].length-1-(intStep%(2*intCols)-1)/2]
if(arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+3)/2]>0){
strExplain+=" and carry the "+
arrCarry[intCols-1-divide(intStep,2*intCols)][intCols-(intStep%(2*intCols)+3)/2]
}
strExplain+="."
parent.parent.leftFrame.ltop.document.write(strExplain)
intStep++
}//FOR THE ODD STEPS
/* ******************************************************************* */
else if (boolAdd==1){//THE ADDITION PHASE



//parent.rbottom.document.write(strTable)





}//THE ADDITION PHASE
}//START




function IntegerToArray(intIn){
var strIn=intIn+""
var intLength=(strIn).length
arrOut=new Array(intLength)
var i
for(i=0; i<intLength; i++){
arrOut[i]=strIn.slice(i,i+1)
}
return arrOut
}


function WholeNumberSubtractionwithZeros(){//a
var i, j, t, b, temp
var strTable
var strExplain
var strPad
var intPlace
var strZeros=""
var strNines=""
var intBeginZeros
var intEndZeros

if(intOpCount<2*intCols){//Should this run? 0
intOpCount++
intPlace=Math.round(intOpCount/2)
/* IS THIS THE FIRST CYCLE?  ***********************************  */
if(intOpCount==0){// c
carryAnswer[0]=0
strCarryAnswer[0]=""
strExplain=""
strPad=""
intBeginZeros=Math.round(intCols*Math.random()/2)+1
intEndZeros=
Math.min(Math.round(intCols*Math.random()/2)+intBeginZeros,intCols-2)
/* INITIALIZE THE ARRAY. **************************************  */
/* AFTER THE ZEROS ********************************************  */
topNumbers[intCols-1]=Math.round(8*Math.random())+1
/* MAKE SURE TOP > BOTTOM ******************************************  */
bottomNumbers[intCols-1]=Math.round((topNumbers[intCols-1]-1)*Math.random())+1
if(topNumbers[intCols-1]>=bottomNumbers[intCols-1])
if(bottomNumbers[intCols-1]>1){
   bottomNumbers[intCols-1]=bottomNumbers[intCols-1]-1
}else{
   topNumbers[intCols-1]=topNumbers[intCols-1]+1
}
topTopNumbers[intCols-1]=""
for (j=intEndZeros+1; j<intCols-1; j++){// d
topNumbers[j]=Math.round(9*Math.random())
topTopNumbers[j]=""
bottomNumbers[j]=Math.round(9*Math.random())
}// d
/* DURING THE ZEROS ********************************************  */
for(j=intBeginZeros; j<intEndZeros+1; j++){
     topNumbers[j]=0
     topTopNumbers[j]=""
     bottomNumbers[j]=Math.round(9*Math.random())
}
/* BEFORE THE ZEROS ********************************************  */
/* MAKE SURE TOP < BOTTOM ******************************************  */
topNumbers[intBeginZeros-1]=Math.round(5*Math.random())
topTopNumbers[intBeginZeros-1]=""
bottomNumbers[intBeginZeros-1]=Math.round(5*Math.random())+3
for(j=0; j<intBeginZeros-1; j++){
     topNumbers[j]=Math.round(9*Math.random())
     topTopNumbers[j]=""
     bottomNumbers[j]=Math.round(9*Math.random())
}
/*INITIALIZATION FINISHED. *********************************************  */
/* FIND THE DIFFERENCE, THE CARRY, AND THE CORRECT ANSWER. *************  */
for (j=0; j<intCols; j++){// g
sumNumbers[j]=topNumbers[j]-bottomNumbers[j]+carryAnswer[j]
if(sumNumbers[j]<0){// h
	carryAnswer[j+1]=-1
	rightAnswer[j]=sumNumbers[j]+10
}// h
else{// i
	carryAnswer[j+1]=0
	rightAnswer[j]=sumNumbers[j]
}// i
}// g
/*PRINT OUT THE PROBLEM. *******************************************  */
intStep++
strTable="<h3>Step "+intStep+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>&nbsp;</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
}// c
/*NOT THE FIRST LOOP, BUT AN ODD LOOP. *************************  */
/*IN ODD LOOPS, WE CHECK TO SEE IT THERE IS BORROWING. IF THERE IS, WE BORROW. ********  */
/* BEGIN A NEW CYCLE. *************************************  */
if(intOpCount%2==1){// b
var testIt=0	//Determine if we must borrow from the next column.
/* IF WE MUST BORROW FROM THE NEXT COLUMN: ************************* */
/* WE HAVE TO START THE EXPLANATION HERE BECAUSE THE DATA WILL BE WIPED OUT AFTER THIS STEP. **************************************** */
strExplain="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"
strExplain+="<h3>Step "+(intStep+1)+"</h3>"+
"You can't take " 
if(topTopNumbers[intPlace-1]==""){	
      if(topNumbers[intPlace-1]<bottomNumbers[intPlace-1]){	
/* IF THE NUMBER TO BE BORROWED FROM IS 0, TRY A NEW PLAN */
if(topNumbers[intPlace]==0){//aa
strExplain+=bottomNumbers[intPlace-1]+" from "+topNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topNumbers[intPlace-1]+
". But the next digit over in the top number is 0 and we can't borrow from 0."
topNumbers[intPlace-1]=topNumbers[intPlace-1]+10
i=0

while (topNumbers[intPlace+i]==0){//bb
strZeros+="0"
strNines+="9"
topTopNumbers[intPlace+i]=9
topNumbers[intPlace+i]="<strike>"+topNumbers[intPlace+i]+"</strike>"
i++
}// while bb
strExplain+=" So we consider the next numbers on the top as "+
""+topNumbers[intPlace+i]+""+strZeros + 
" and borrow one from <i>that</i>, reducing that to "+
""+(topNumbers[intPlace+i]-1)+""+strNines + "."
topTopNumbers[intPlace+i]=topNumbers[intPlace+i]-1
topNumbers[intPlace+i]="<strike>"+topNumbers[intPlace+i]+"</strike>"
testIt=1
}//aa
else{//cc
strExplain+=bottomNumbers[intPlace-1]+" from "+topNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topNumbers[intPlace-1]+
".  \n So you borrow 1 from the "+topNumbers[intPlace]+", making the "+
topNumbers[intPlace]+" into "+(topNumbers[intPlace]-1)+"."+
"The "+topNumbers[intPlace-1]+" becomes "+
(topNumbers[intPlace-1]+10)+"."
topNumbers[intPlace-1]+=10
topTopNumbers[intPlace]=topNumbers[intPlace]-1
topNumbers[intPlace]="<strike>"+topNumbers[intPlace]+"</strike>"
testIt=1
}//cc	
}
}else{
      if(topTopNumbers[intPlace-1]<bottomNumbers[intPlace-1]){	
strExplain+=bottomNumbers[intPlace-1]+" from "+topTopNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topTopNumbers[intPlace-1]+
".  \n So you borrow 1 from the "+topNumbers[intPlace]+", making the "+
topNumbers[intPlace]+" into "+(topNumbers[intPlace]-1)+"."+
"The "+topTopNumbers[intPlace-1]+" becomes "+
(topTopNumbers[intPlace-1]+10)+"."
topTopNumbers[intPlace-1]+=10
topTopNumbers[intPlace]=topNumbers[intPlace]-1
topNumbers[intPlace]="<strike>"+topNumbers[intPlace]+"</strike>"
testIt=1
      }
}
if(testIt==1){
intStep++
strTable=""
strTable+="<h3>Step "+intStep+"</h3>"
strTable+="<table border='0'>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5'" 
strTable+="colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/2)-1; j--)
	strTable+="<td>  &nbsp;</td>"
for(j=Math.round(intOpCount/2)-2; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr></table>"
parent.parent.leftFrame.ltop.document.write(strExplain)
parent.rbottom.document.write(strTable)
}
rightJS(stringSubject, ArFrame)
}// b
/* IT'S AN EVEN CYCLE ******************************************  */
/* IN EVEN CYCLES WE DO THE SUBTRACTION ************************  */
else if(intOpCount>0){// bbb 
intStep++
strTable=""
strTable+="<h3>Step "+intStep+"</h3>"
strTable+="<table border='0'>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5'" 
strTable+="colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/2); j--)
	strTable+="<td>  &nbsp;</td>"
for(j=Math.round(intOpCount/2)-1; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
strExplain="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"+
"<h3>Step "+intStep+"</h3>"+
"We subtract "
if(topTopNumbers[intPlace-1]==""){
     strExplain+=topNumbers[intPlace-1]+"-"+bottomNumbers[intPlace-1]	
}
else{
     strExplain+=topTopNumbers[intPlace-1]+"-"+bottomNumbers[intPlace-1]
}
strExplain+=" and get "+rightAnswer[intPlace-1]+"."
parent.parent.leftFrame.ltop.document.write(strExplain)
rightJS(stringSubject, ArFrame)
}// bbb
}// 0
}// a




function WholeNumberSubtraction(){//a
var i, j, t, b, temp
var strTable
var strExplain
var strPad
var intPlace
var strZeros=""
var strNines=""

if(intOpCount<2*intCols){//Should this run? 0
intOpCount++
intPlace=Math.round(intOpCount/2)
/* IS THIS THE FIRST CYCLE?    ****************************  */
if(intOpCount==0){// c
carryAnswer[0]=0
strCarryAnswer[0]=""
strExplain=""
strPad=""
/* INITIALIZE THE ARRAY. **************************************  */
topNumbers[intCols-1]=Math.round(8*Math.random())+1
bottomNumbers[intCols-1]=Math.round(8*Math.random())+1
topTopNumbers[intCols-1]=""
for (j=0; j<intCols-1; j++){// d
topNumbers[j]=Math.round(9*Math.random())
topTopNumbers[j]=""
bottomNumbers[j]=Math.round(9*Math.random())
}// d
/* MAKE SURE THE TOP > THE BOTTOM; OTHERWISE, SWITCH THE TWO. ************  */
if(arrayToInteger(topNumbers.reverse())<arrayToInteger(bottomNumbers.reverse())){// e
for (j=0; j<intCols; j++){// f
temp=topNumbers[j]
topNumbers[j]=bottomNumbers[j]
bottomNumbers[j]=temp
}// f
}// e
topNumbers.reverse()
bottomNumbers.reverse()
/*INITIALIZATION FINISHED. *********************************************  */
/* FIND THE DIFFERENCE, THE CARRY, AND THE CORRECT ANSWER. ******************  */
for (j=0; j<intCols; j++){// g
sumNumbers[j]=topNumbers[j]-bottomNumbers[j]+carryAnswer[j]
if(sumNumbers[j]<0){// h
	carryAnswer[j+1]=-1
	rightAnswer[j]=sumNumbers[j]+10
}// h
else{// i
	carryAnswer[j+1]=0
	rightAnswer[j]=sumNumbers[j]
}// i
}// g
/*PRINT OUT THE PROBLEM. *******************************************  */
intStep++
strTable="<h3>Step "+intStep+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>&nbsp;</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
}// c
/*NOT THE FIRST LOOP, BUT AN ODD LOOP. *************************  */
/*IN ODD LOOPS, WE CHECK TO SEE IT THERE IS BORROWING. IF THERE IS, WE BORROW. ********  */
/* BEGIN A NEW CYCLE. *************************************  */
if(intOpCount%2==1){// b
var testIt=0	//Determine if we must borrow from the next column.
/* IF WE MUST BORROW FROM THE NEXT COLUMN: ************************* */
/* WE HAVE TO START THE EXPLANATION HERE BECAUSE THE DATA WILL BE WIPED OUT AFTER THIS STEP. **************************************** */
strExplain="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"
strExplain+="<h3>Step "+(intStep+1)+"</h3>"+
"You can't take " 
if(topTopNumbers[intPlace-1]==""){	
      if(topNumbers[intPlace-1]<bottomNumbers[intPlace-1]){	
/* IF THE NUMBER TO BE BORROWED FROM IS 0, TRY A NEW PLAN */
if(topNumbers[intPlace]==0){//aa
strExplain+=bottomNumbers[intPlace-1]+" from "+topNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topNumbers[intPlace-1]+
". But the next digit over in the top number is 0 and we can't borrow from 0."
topNumbers[intPlace-1]=topNumbers[intPlace-1]+10
i=0
while (topNumbers[intPlace+i]==0){//bb
strZeros+="0"
strNines+="9"
topTopNumbers[intPlace+i]=9
topNumbers[intPlace+i]="<strike>"+topNumbers[intPlace+i]+"</strike>"
i++
}// while bb
strExplain+=" So we consider the next numbers on the top as "+
""+topNumbers[intPlace+i]+""+strZeros + 
" and borrow one from <i>that</i>, reducing that to "+
""+(topNumbers[intPlace+i]-1)+""+strNines + "."
topTopNumbers[intPlace+i]=topNumbers[intPlace+i]-1
topNumbers[intPlace+i]="<strike>"+topNumbers[intPlace+i]+"</strike>"
testIt=1
}//aa
else{//cc
strExplain+=bottomNumbers[intPlace-1]+" from "+topNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topNumbers[intPlace-1]+
".  \n So you borrow 1 from the "+topNumbers[intPlace]+", making the "+
topNumbers[intPlace]+" into "+(topNumbers[intPlace]-1)+"."+
"The "+topNumbers[intPlace-1]+" becomes "+
(topNumbers[intPlace-1]+10)+"."
topNumbers[intPlace-1]+=10
topTopNumbers[intPlace]=topNumbers[intPlace]-1
topNumbers[intPlace]="<strike>"+topNumbers[intPlace]+"</strike>"
testIt=1
}//cc	
}
}else{
      if(topTopNumbers[intPlace-1]<bottomNumbers[intPlace-1]){	
strExplain+=bottomNumbers[intPlace-1]+" from "+topTopNumbers[intPlace-1]+
" because "+
bottomNumbers[intPlace-1]+" is greater than "+topTopNumbers[intPlace-1]+
".  \n So you borrow 1 from the "+topNumbers[intPlace]+", making the "+
topNumbers[intPlace]+" into "+(topNumbers[intPlace]-1)+"."+
"The "+topTopNumbers[intPlace-1]+" becomes "+
(topTopNumbers[intPlace-1]+10)+"."
topTopNumbers[intPlace-1]+=10
topTopNumbers[intPlace]=topNumbers[intPlace]-1
topNumbers[intPlace]="<strike>"+topNumbers[intPlace]+"</strike>"
testIt=1
      }
}
if(testIt==1){
intStep++
strTable=""
strTable+="<h3>Step "+intStep+"</h3>"
strTable+="<table border='0'>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5'" 
strTable+="colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/2)-1; j--)
	strTable+="<td>  &nbsp;</td>"
for(j=Math.round(intOpCount/2)-2; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr></table>"
parent.parent.leftFrame.ltop.document.write(strExplain)
parent.rbottom.document.write(strTable)
}
rightJS(stringSubject, ArFrame)
}// b
/* IT'S AN EVEN CYCLE ******************************************  */
/* IN EVEN CYCLES WE DO THE SUBTRACTION ************************  */
else if(intOpCount>0){// bbb 
intStep++
strTable=""
strTable+="<h3>Step "+intStep+"</h3>"
strTable+="<table border='0'>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topTopNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>-</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5'" 
strTable+="colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/2); j--)
	strTable+="<td>  &nbsp;</td>"
for(j=Math.round(intOpCount/2)-1; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
strExplain="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"+
"<h3>Step "+intStep+"</h3>"+
"We subtract "
if(topTopNumbers[intPlace-1]==""){
     strExplain+=topNumbers[intPlace-1]+"-"+bottomNumbers[intPlace-1]	
}
else{
     strExplain+=topTopNumbers[intPlace-1]+"-"+bottomNumbers[intPlace-1]
}
strExplain+=" and get "+rightAnswer[intPlace-1]+"."
parent.parent.leftFrame.ltop.document.write(strExplain)
rightJS(stringSubject, ArFrame)
}// bbb
}// 0
}// a



function WholeNumberAddition(){
if(intOpCount<3*intCols-2){//Should this run? 0
intOpCount++
var j
var strTable
var strExplain
var strPad
if(intOpCount%3==1){//Beginning of a new cycle. Start a new column.
carryAnswer[0]=0
strCarryAnswer[0]=""
if(intOpCount==1){//If it's the first time around. a
for (j=0; j<intCols; j++){//Initialize the arrays. b
topNumbers[j]=Math.round(9*Math.random())
bottomNumbers[j]=Math.round(9*Math.random())
sumNumbers[j]=topNumbers[j]+bottomNumbers[j]
carryAnswer[j+1]=Math.floor((carryAnswer[j]+sumNumbers[j])/10)
rightAnswer[j]=(carryAnswer[j]+sumNumbers[j])%10	
if(carryAnswer[j+1]>0)
	strCarryAnswer[j+1]="<sup>"+carryAnswer[j+1]+"</sup>"
else	
	strCarryAnswer[j+1]=""
}//end for b
}//end if a
intStep++
strTable=""
strTable+="<h3>Step "+intStep+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/3)+1; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
for(j=Math.round(intOpCount/3); j>=0; j--)
	strTable+="<td>"+strCarryAnswer[j]+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>+</td>"
for(j=intCols-1; j>=0; j--)
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5'" 
strTable+="colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/3)+1; j--)
	strTable+="<td>&nbsp;</td>"
strTable+="<td>"+(sumNumbers[j]+carryAnswer[j])+"</td>"
j--
for(j=Math.round(intOpCount/3)-1; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
strPad=""
strPad+="<html><head></head><body><h3>Step "+intStep+"</h3>"
strExplain=""
strExplain="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"+
"<h3>Step "+intStep+"</h3>"
if(carryAnswer[Math.round(intOpCount/3)]>0){//There is a carry-over. d
	strPad+=topNumbers[Math.round(intOpCount/3)]+"+"+
		bottomNumbers[Math.round(intOpCount/3)]+"+"+
		carryAnswer[Math.round(intOpCount/3)]+"="+
		(topNumbers[Math.round(intOpCount/3)]+
		bottomNumbers[Math.round(intOpCount/3)]+
		carryAnswer[Math.round(intOpCount/3)])
	strExplain+="We add the numbers in the next left column, in this case "+
	topNumbers[Math.round(intOpCount/3)]+
	" and "+bottomNumbers[Math.round(intOpCount/3)]+
	" and add the carry-over of "+carryAnswer[Math.round(intOpCount/3)]+
	"</i>. <br>Press to continue."+
	"</body></html>"
}//end if d
else{//There is no carry over. e
if(intStep<2){//If it's the first step. g
strExplain+=
"We now add the first column of numbers on the right column, "+
" in this case "+ topNumbers[Math.round(intOpCount/3)]+
" and "+bottomNumbers[Math.round(intOpCount/3)]+
". <br>Press to continue."
strPad+=topNumbers[Math.round(intOpCount/3)]+"+"+
bottomNumbers[Math.round(intOpCount/3)]+"="+
(topNumbers[Math.round(intOpCount/3)]+bottomNumbers[Math.round(intOpCount/3)])
}//end if e
else{ //It's not the first step, but there's no carry. h
strExplain+="We now add the next column over on the left, "+
" in this case "+ topNumbers[Math.round(intOpCount/3)]+
" and "+bottomNumbers[Math.round(intOpCount/3)]+
". <br>Press to continue."
strPad+=topNumbers[Math.round(intOpCount/3)]+"+"+
		bottomNumbers[Math.round(intOpCount/3)]+"="+
		(topNumbers[Math.round(intOpCount/3)]+
		bottomNumbers[Math.round(intOpCount/3)])+"<br>"
topNumbers[Math.round(intOpCount/3)]+bottomNumbers[Math.round(intOpCount/3)]
}//end else h
}//end if e
parent.parent.leftFrame.ltop.document.write(strExplain)
parent.parent.leftFrame.lbottom.document.write(strPad)
rightJS(stringSubject, ArFrame)//The program has trouble with "frame."
}//end if
/* ***************** Conditional second step ********************* */
/* ***************** In this step, we put in the carry-over. ***** */
else if(intOpCount%3==2 && carryAnswer[Math.round(intOpCount/3)]>0){
//Is there a carry-over?
intStep++

strTable=""
strTable+="<html><head>"+
"<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"+
"</head><body>"
strTable+="<h3>Step "+intStep+"</h3>"
strTable+="<table border='0'>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/3)+1; j--)
	strTable+="<td>"+topNumbers[j]+"</td>"
for(j=Math.round(intOpCount/3); j>=0; j--)
	strTable+="<td>"+strCarryAnswer[j]+topNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td>+</td>"
for(j=intCols-1; j>=0; j--) 
	strTable+="<td>"+bottomNumbers[j]+"</td>"
strTable+="</tr>"
strTable+="<tr><td style='background-color:black;height:.5' colspan="+(intCols+1)+"></td></tr>"
strTable+="<tr><td>&nbsp;</td>"
for(j=intCols-1; j>=Math.round(intOpCount/3); j--)
	strTable+="<td>&nbsp;</td>"
for(j=Math.round(intOpCount/3)-1; j>=0; j--)
	strTable+="<td>"+rightAnswer[j]+"</td>"
strTable+="</tr>"
strTable+="</table>"
strTable+="</body></html>"
parent.rbottom.document.write(strTable)
strExplain="<html><head>"
strExplain+="<link href='ICTCMJS.css' rel='stylesheet' type='text/css'>"
strExplain+="</head><body>"
strExplain+="<h3>Step "+intStep+"</h3>"
strExplain+="The sum,  "+ 
(sumNumbers[Math.round(intOpCount/3)-1]+carryAnswer[Math.round(intOpCount/3)-1])+
", is two digits, so we must divide by 10 and replace it with the remainder, i.e. "+
(sumNumbers[Math.round(intOpCount/3)-1])%10+
" and carry the quotient, i.e. "+
Math.floor(sumNumbers[Math.round(intOpCount/3)-1]/10)+
". <br>Press to continue."
strExplain+="</body></html>"
parent.parent.leftFrame.ltop.document.write(strExplain)
rightJS(stringSubject, ArFrame)
}//end else if 
}//0

}//end function








function arrayToInteger(arrayIn){
var stringIn=arrayIn.toString()
var stringWords=stringIn.split(",")
var stringOut=""
for(var i=0; i<stringWords.length; i++)
	stringOut+=stringWords[i]
return parseInt(stringOut)
}





function WholeNumberMultiplication1(){//START
var i, j, intBottom, intTop
arrProductCarry=new Array(intCols)



if(intStep==0&&(intStep%intCols)==0){//ONLY DO THIS DURING THE ZERO STEP
strTable=""
/* INITIALIZE THE PROBLEM AND MULTIPLY */
intTopFactor=Math.floor(Math.pow(10,intCols)*(.1+.9*(Math.random())))
intBottomFactor=Math.floor(Math.pow(10,intCols)*(.1+.9*(Math.random())))
intAnswer=intTopFactor*intBottomFactor
arrAnswer=IntegerToArray(intAnswer)
arrTop=IntegerToArray(intTopFactor)
arrBottom=IntegerToArray(intBottomFactor)
arrProducts=new Array(intCols)
arrDoubleProducts=new Array(intCols)
arrCarry=new Array(intCols)
for(intBottom=0; intBottom<intCols; intBottom++){//THE PRODUCT
	arrProducts[intBottom]=arrBottom[intBottom]*intTopFactor
	arrDoubleProducts[intBottom]=IntegerToArray(arrProducts[intBottom])
	arrCarry[intBottom]=new Array(intCols)	
}//THE PRODUCT
intCheckContinue=intCols
for(intBottom=0; intBottom<intCols; intBottom++){//CARRY
for(intTop=1; intTop<arrDoubleProducts[intBottom].length; intTop++){//FOR
if(intCols<arrDoubleProducts[intBottom].length){//IF THE NUMBER OF COLUMNS IS SMALL
arrCarry[intBottom][intTop-1]=
	arrDoubleProducts[intBottom][intTop]-(arrBottom[intBottom]*arrTop[intTop-1])%10
if(arrCarry[intBottom][intTop-1]<0)
	arrCarry[intBottom][intTop-1]+=10
}else{//IF THE NUMBER OF COLUMNS IS SMALL
arrCarry[intBottom][intTop]=
	arrDoubleProducts[intBottom][intTop]-(arrBottom[intBottom]*arrTop[intTop])%10
if(arrCarry[intBottom][intTop-1]<0)
	arrCarry[intBottom][intTop-1]+=10
}//IF THE NUMBER OF COLUMNS IS SMALL
}//FOR
}//CARRY
//PRINT THE PROBLEM
strTable=arrCarry
strTable+="<h2>The Problem</h2>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=0; j<intCols-intStep/2; j++){
	strTable+="<td>"+arrTop[j]+"</td>"
}
for(j=intCols-intStep/2; j<intCols; j++)
	strTable+="<td>"+arrTop[j]+"</td>"//WHY NOT COLLAPSE THEM?
strTable+="</tr><tr>"
strTable+="<td>&#215;</td>"
for(j=0; j<intCols; j++)
	strTable+="<td>"+arrBottom[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr>"+
"</table>"
parent.rbottom.document.write(strTable)
intStep++

}//ONLY DO THIS DURING THE ZERO STEP
else if(intStep%2==0){//EVEN BUT NOT ZERO; MULTIPLY, BUT DON'T CHANGE 
strTable=""
strTable+="<h3>Step "+(intStep/2)+"</h3>"+
"<table border='0'>"+
"<tr><td>&nbsp;</td>"
for(j=0; j<intCols-intStep/2-1; j++){//FOR LOOP
	strTable+="<td>"+arrTop[j]+"</td>"
}//FOR LOOP
for(j=intCols-intStep/2-1; j<intCols; j++){//FOR LOOP
if(arrCarry[intCols-1-(intStep%intCols)][j]>0){//IF THERE IS A CARRY
strTable+="<td><span style='font-size:60%;vertical-align:top'>"+
arrCarry[intCols-1-(intStep%intCols)][j]+"</span>"+arrTop[j]+"</td>"
}//IF THERE IS A CARRY
else{//IF THERE IS NOT A CARRY
strTable+="<td>"+arrTop[j]+"</td>"
}//IF THERE IS NOT A CARRY
}//FOR LOOP
strTable+="</tr><tr>"//CHANGE intCols TO LENGTH OF BOTTOM THROUGHTOUT THE PROGRAM
strTable+="<td>&#215;</td>"
for(j=0; j<intCols; j++)
	strTable+="<td>"+arrBottom[j]+"</td>"
strTable+="</tr>"+
"<tr><td style='background-color:black;height:.5'"+ 
"colspan="+(intCols+1)+"></td></tr><tr>"//LENGTHEN THIS BY THE OUTER LOOP
for(i=0; i<(intStep%intCols); i++){//INSERT THE OLD ROWS UNDER THE EQUAL SIGN
for(j=arrDoubleProducts[intCols-1-i].length-1; j>=0; j--){//ADD ADDITIONAL ROW
strTable+="<td>"+
arrDoubleProducts[intCols-1-i][arrDoubleProducts[intCols-1-(intStep%intCols)].length-1-j]+"</td>"
}//ADD ADDITIONAL ROW
strTable+="</tr><tr>"
}//INSERT THE OLD ROWS UNDER THE EQUAL SIGN	
for(j=0; j<intCols-intStep/2+1; j++)//INSERT THE NEW ROWS	
strTable+="<td>&nbsp;</td>"
for(j=intStep/2-1; j>=0; j--)//INSERT THE NEW ROWS
strTable+="<td>"+arrDoubleProducts[intCols-1-(intStep%intCols)][intCols-j]+ "</td>"
strTable+="</tr></table>"
parent.rbottom.document.write(strTable)
intStep++
}//FOR THE EVEN STEPS
else if (intStep%2==1){//FOR THE ODD STEPS
strExplain="<h3>Step "+(intStep)+"</h3>"+
"We multiply "+ arrBottom[intCols-1-(intStep%intCols)] + " by "+ arrTop[intCols-(intStep+1)/2]

parent.parent.leftFrame.ltop.document.write(strExplain)
intStep++
}//FOR THE ODD STEPS
}//START
