<!--
PLATFORMS = new Array (' ','Left','Center','Right')
Mode = "automatic"
HELP_FLAG = true
Complement = new Array()
Complement[1] = new Array(2,3)
Complement[2] = new Array(1,3)
Complement[3] = new Array(1,2)

NOT = new Array()
NOT[1] = new Array()
NOT[1][2] = 3
NOT[1][3] = 2

NOT[2] = new Array()
NOT[2][1] = 3
NOT[2][3] = 1

NOT[3] = new Array()
NOT[3][1] = 2
NOT[3][2] = 1

STOP = false
Content = new Array()
oldContent = new Array()
TXT =""
LOG =""
Mode ="Terse"
OK_LARGE = false
function Help_pattern(){
if(HELP_FLAG == true){temp = document.forms.recipe.comment.value }
if(HELP_FLAG == false){
HELP_FLAG = true; Comment_pattern(temp);return}

Comment_pattern('_________________ Instructions __________________\n\n1. Start your experiments with a small value of n ( say n= 5).\n\n2. The "Report" option produces the complete sequence of optimal decisions so do not use it with large values of n.\n\n3. Make sure that Orgin and Destination are different.\n\n4. Use the "Iterate" button to generate an explanation for the iteration specified in the "Iteration no." field. This explanation pertains to the problem specified by the current setup (size, Origin, Destination).\n\n5. The feasible range of values for the Iteration no. is given in the parenthesized field next to it.\n\n6. The "Instructions" button is an on/off switch. ')
HELP_FLAG = false
}

function Change_n_pattern(){
n = document.forms.recipe.n.selectedIndex+1

if((n == 41)&& (OK_LARGE==false)){document.forms.recipe.iteration.value="You can now attempt to generate reports for large problems!";OK_LARGE = true;document.forms.recipe.n.selectedIndex = 9; n = 10; }
if((n == 41)&& (OK_LARGE==true)){document.forms.recipe.n.selectedIndex = 39; n = 40;}
N = Math.pow(2,n) -1
document.forms.recipe.range.value='1,...,' + N
}

function Compute_pattern(){
var n = document.forms.recipe.nn.selectedIndex+1
document.forms.recipe.nnp2.value=Math.pow(2,n) 
}

function Solve_pattern(){
n = document.forms.recipe.n.selectedIndex+1
if((n>10)&&(OK_LARGE == false)){Too_Large_Solve(n);return}
if(Initialize_pattern() == false){return}
DOIT_pattern()
}

function Too_Large_Solve(m){
var txt = 'Hello! \n\nYou asked for a report on a problem of size n='+ m +'. It is very unlikely that your browser will have enough memory to store this report.\n\nWe suggest that you use the red "iterate" and "next" buttons to generate individual optimal decisions for this problem.\n\nIf you insist on going ahead with generating the report for this problem, select the ">9" option in the Problem Size menu and click the "report" button.  Good luck!'
OK_LARGE = true
Comment_pattern(txt) 
}

function Initialize_pattern(){
n = 1*document.forms.recipe.n.options.selectedIndex+1
N = Math.pow(2,n) -1
TXT =""
if( (n > 9) && (OK_LARGE == false)){
promp = confirm('Just a friendly reminder of the 2^n Curse. Are you sure you want go ahead with this?')
	if(promp == false){return false}
						}
a = 1+document.forms.recipe.origin.options.selectedIndex
A =PLATFORMS[a]
b = 1+document.forms.recipe.destination.options.selectedIndex
B = PLATFORMS[b]
if( a == b){alert("The Origin and Destination platform should be different!");return false}
c = NOT[a][b]
C = PLATFORMS[c]
N = Math.pow(2,n) -1
var n2 = n%2
if(n2 == 0){
	parity = 'Even'
	PATTERN = '(' + A + ',' + C + ',' + B + ',' + A+', ......)'
	Next_1 = new Array()
	Next_2 = new Array()
	Next_1[a] = c
	Next_1[b] = a
	Next_1[c] = b
         Next_2[a] = b
	Next_2[b] = c
	Next_2[c] = a}
else {parity = 'Odd'
	PATTERN = '(' + A + ',' + B + ',' + C + ','+ A + ', ......)'
	Next_1 = new Array()
	Next_2 = new Array()
	Next_1[a] = b
	Next_1[b] = c
	Next_1[c] = a
	Next_2[a] = c
	Next_2[b] = a
	Next_2[c] = b		
				}
Content[a] = ""
Content[b] = ""
Content[c] = ""
sa =""
for(j=1;j<=n;j++){ sa += ',' + j}
sa = sa.substring(1,sa.length)
Content[a] = sa
oldContent[1] = Content[1]
oldContent[2] = Content[2]
oldContent[3] = Content[3]
FIRST = a
SECOND = b
TOPS = new Array()
TOPS[a] = 1
TOPS[b] = Infinity
TOPS[c] = Infinity
TXT = TXT + '\nInitialization:\nn = ' + n + '\n(2^n)-1 = ' + N + '\nparity = ' + parity+ '\nPattern (smallest piece) = ' + PATTERN + '\nInitial state:\n  Left:   {' + Content[1] + '}\n  Center: {' + Content[2] + '}\n  Right:  {' + Content[3] + '}'
Comment_pattern(TXT)
return true
}

function DOIT_pattern(){
STOP = false
TXT += '\n\nMore is coming .......... '
Comment_pattern(TXT)
JJ=1
DOIT_now()
}


function DOIT_now(){
jj=JJ
oldContent[1] = Content[1]
oldContent[2] = Content[2]
oldContent[3] = Content[3]
	if(1 == jj%2){Move_First(jj) }
	else{Move_Second(jj)}
	document.forms.recipe.iteration.value= 'Iteration no. ' + jj 
if(jj>N){
	document.forms.recipe.iteration.value= 'Idle!'
	return}
if(STOP){
	STOP=false
	document.forms.recipe.iteration.value="Stopped!"
	return}
JJ += 1
ID_doit=setTimeout('DOIT_now()',0)
}

function Move_First(j){
var piece = 1
var from = PLATFORMS[FIRST]
too = Next_1[FIRST]
var to = PLATFORMS[too]
fro = FIRST
TOPS[too]= 1
FIRST = too
MoveFromTo(j,piece,fro,too)
return
}

function Move_Second(j){
var compl = Complement[FIRST]
var ac = compl[0]
var bc = compl[1]
var mintop = ac
if(TOPS[bc] < TOPS[ac] ){mintop = bc}
SECOND = mintop
var k = j
var piece = TOPS[SECOND]
var from = PLATFORMS[SECOND]
var too = NOT[FIRST][SECOND]
var to = PLATFORMS[too]
TOPS[too]=1*piece
MoveFromTo(j,piece,SECOND,too)
return
}

function MoveFromTo(j,p,f,t){
var sfrom = Content[f]
if(sfrom == ""){newfrom = ""}
	else {var i = sfrom.indexOf(',')
		if(i==-1){newfrom=""} 
	else{newfrom = sfrom.substring(i+1,sfrom.length)} 
		}
Content[f] = newfrom
if(newfrom == ""){ftop = Infinity}
	else {var i = newfrom.indexOf(',')
		if(i==-1){ftop=1*newfrom} 
	else{ ftop = 1*newfrom.substring(0,i)} 
		}
TOPS[f] = ftop
if(Content[t] == ""){Content[t] ="" + p}
else {Content[t] = "" + p + ',' + Content[t]}

if(Mode == "Terse"){Report_pattern(j,p,f,t)}
else {Arrange_Comment_pattern(j,p,f,t)}
}

function Arrange_Comment_pattern(j,p,f,t){
var txt ='\n___________________________________________\n'
txt += '\n               Iteration no. ' + j +  ':'
txt += '\nCurrent state:'
txt += '\n  Left:   {' + oldContent[1] + '}'
txt += '\n  Center: {' + oldContent[2] + '}'
txt += '\n  Right:  {' + oldContent[3] + '}'
txt += '\n\nOptimal Decision:'
txt += '\n    Move piece ' + p + ' from ' + PLATFORMS[f] + ' to ' + PLATFORMS[t]+'.'
txt += '\n\nNew state:'
txt += '\n   Left:   {' + Content[1] +'}\n   Center: {' + Content[2] + '}\n   Right:  {' + Content[3] + '}'
TXT += txt
LOG += txt
if(j !=N){return}
document.forms.recipe.iteration.value= 'Printing ..... '
Comment_pattern(TXT)
}

function Report_pattern(j,p,f,t){
var txt = '\nIteration no. ' + j +  ': Move ' + p + ' from ' + PLATFORMS[f] + ' to ' + PLATFORMS[t]+'.'
TXT += txt
if(j !=N){return}
document.forms.recipe.iteration.value= 'Printing ..... '
Comment_pattern(TXT)
}

function Comment_pattern(txt){
document.forms.recipe.iteration.value= 'Printing ..... '
document.forms.recipe.comment.value=txt
document.forms.recipe.iteration.value= 'Idle!'
}

function Explain_pattern(){
OK_LARGE = true
if(Initialize_pattern() == false){return}
var vv = document.forms.recipe.explain_iteration.value
if(isNaN(vv)){Comment_pattern('\Sorry!\n\nThe # field should specify a positive integer in the range specified in the Range field.');return}
ITno = vv- 0
if(isNaN(ITno)){Comment_pattern('Sorry, your browser cannot handle the large number of iteration specified in the # field. Use a smaller one.');return}

if(ITno < 1){Comment_pattern(
"\nSorry!\n\nThe # field must be a positive integer in the range specified by the Range field.");return}


if(ITno > N){Comment_pattern("\Sorry!\n\nThe iteration your requested (in the # field) is outside the feasible range {1,...,(2^n)-1} = {1,2,...," + N + "}"); return}
LIST2P = new Array()
SUM = new Array()
SUM[a] = N
SUM[b] =0
SUM[c] = 0

//DOIT_pattern()
N2 = ITno -1

for(var k=0;k<100;k++){
if(isNaN(N2)){alert('Your browser cannot handle such a large number of iterantions. Use a smaller number.');return}
if(N2 == 0){break}
var r = Math.log(N2)/Math.log(2)
r = Math.floor(r)
LIST2P[k] = r
N22 = N2 - Math.pow(2,r)
Comment_pattern('\n\k=' + k + '\nN2 = ' + N2 + '\nr = ' + r + '\nN22 = ' + N22 + '\nLIST = ' + LIST2P.toString())
N2 = N22
              }
txt = document.forms.recipe.comment.value
txt += '\nSUM = ' +  SUM.toString()

for(var j=0;j<LIST2P.length;j++){
var m = LIST2P[j]
//if(m == 0){break}
txt += '\nj= ' + j
txt += '\nm= ' + m
var mm = Math.pow(2,m) - 1 
mx = m+1
txt += '\nmx = ' + mx
if(1 == (mx%2)){to = Next_1[a]}
else{to = Next_2[a]}
txt += '\na = ' + a
txt += '\nTo = ' + to

alt = NOT[a][to]

txt += '\nalt = ' + alt

SUM[to] = SUM[to] + Math.pow(2,m)
SUM[alt] = SUM[alt] + mm
SUM[a] = SUM[a] - mm - Math.pow(2,m)

txt += '\nNew Sum = ' + SUM.toString()
a = alt 				
//Comment_pattern(txt)		
					}
Transform_pattern(1)
Transform_pattern(2)
Transform_pattern(3)
Display_Explain()
}

function Explain_Next(){
OK_LARGE = true
var i = document.forms.recipe.explain_iteration.value - 0
var n = document.forms.recipe.n.selectedIndex + 1
if(i  > (Math.pow(2,n)-2 )){return}
document.forms.recipe.explain_iteration.value = i + 1
Explain_pattern()
 }

function Explain_Previous(){
OK_LARGE = true
var i = document.forms.recipe.explain_iteration.value - 0
if(i < 2 ){return}
document.forms.recipe.explain_iteration.value = i - 1
Explain_pattern()
}

function Display_Explain(){
var jj = ITno
if((jj%2) == 1){Explain_First(jj);return}
else{Explain_Second(jj);return}
}

function Explain_First(j){
var piece = 1
var from = PLATFORMS[FIRST]
too = Next_1[FIRST]
var to = PLATFORMS[too]
var fro = FIRST
TOPS[too]= 1
FIRST = too
Explain_Move(j,piece,fro,too)
return
}

function Explain_Second(j){
var compl = Complement[FIRST]
var ac = compl[0]
var bc = compl[1]
var mintop = ac
if(TOPS[bc] < TOPS[ac] ){mintop = bc}
SECOND = mintop
var k = j
var piece = TOPS[SECOND]
var from = PLATFORMS[SECOND]
var too = NOT[FIRST][SECOND]
var to = PLATFORMS[too]
TOPS[too]=1*piece
Explain_Move(j,piece,SECOND,too)
return
}

function Explain_Move(j,p,f,t){
oldContent = new Array()
oldContent[1] = Content[1]
oldContent[2] = Content[2]
oldContent[3] = Content[3]

var sfrom = Content[f]
if(sfrom == ""){newfrom = ""}
	else {var i = sfrom.indexOf(',')
		if(i==-1){newfrom=""} 
	else{newfrom = sfrom.substring(i+1,sfrom.length)} 
		}
Content[f] = newfrom
if(newfrom == ""){ftop = Infinity}
	else {var i = newfrom.indexOf(',')
		if(i==-1){ftop=1*newfrom} 
	else{ ftop = 1*newfrom.substring(0,i)} 
		}
TOPS[f] = ftop
if(Content[t] == ""){Content[t] ="" + p}
else {Content[t] = "" + p + ',' + Content[t]}
var txt ='\n___________________________________________\n'
txt += '\n               Iteration no. ' + j +  ':'
txt += '\nCurrent state:'
txt += '\n  Left:   {' + oldContent[1] + '}'
txt += '\n  Center: {' + oldContent[2] + '}'
txt += '\n  Right:  {' + oldContent[3] + '}'
txt += '\n\nOptimal Decision:'
txt += '\n    Move piece ' + p + ' from ' + PLATFORMS[f] + ' to ' + PLATFORMS[t]+'.'
txt += '\n\nNew state:'
txt += '\n   Left:   {' + Content[1] +'}\n   Center: {' + Content[2] + '}\n   Right:  {' + Content[3] + '}'
LOG = LOG + txt
Comment_pattern(txt)
}

function Transform_pattern(e){
var s = ""
var minn=Infinity
var sum = SUM[e]
var ss = sum
for(var k=0;k<200;k++){
if(sum == 0){break}
var r = Math.log(sum)/Math.log(2)
r = 1+Math.floor(r)
minn = r
s =  r + ',' + s
sum = sum - Math.pow(2,r-1)}
s = s.substring(0,s.length-1)
TOPS[e] = minn
if(1 == minn){FIRST = e}
Content[e] = s
}

// --->

