var iu_left=0;
var poemwidth=0;
var maxtries=2000;
var debugvar=true;
var findhits=24;
var findextrahits=5;
var wrdanim=[["wrd1",50,0],["wrd2",79,0],["wrd3",123,0]];
var wrdspots=[[1, 2, 3],[3, 2, 1],[1, 3, 2],[2, 1, 3],[3, 1, 2],[2, 3, 1]];
var wrdmix=0;

var facul=[0,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,39916800,479001600,6227020800,87178291200]
var poem="";
var wrd; 
var t1;
var t2;
var t3;
var hitsfound=0;
var numlines=0;
var statuspoem="";
var statusselection="";
var statuslines = new Array(200);
var numselected=1;
var pernum=0;
var hitschecked=0;
var debug=false;
var editmode=false;
var publication="";
var divshown=0;
var browsercomp=0;
var topdivh=60;
var menudivw=90;
var menudivh=90;
var submenu1=[[0,"Permugram?"],[7.25,"Structure"],[21.31,"Program"],[30.35,"History"],[66.22,"Colophon"]];
var submenu2="<a href='javascript:make_poem(true)'>Generate</a>";
var submenu3=[[0,"English"],[50.06,"Nederlands"]];

var windowWidth=0;
var windowHeight=0;
var wordcount=0;
var hitlist="|words to mix=128|mix to words=1|to words mix=5|mix words to=162|words mix to=21|to mix words=898|";

var poemlines = new Array(200,2);

function make_poem(newpoem)
{
	var i=0;
	var j=0;
	var k=0;
	var m=0;
	var o;
	var newnumlines=0;
	var woord=""
	var txt="";
	var sptxt="";
	var totaal=0;
	var p=0;
//alert(numlines);
	statuspoem="";
	statusselection="";
	submenu2="<a href='javascript:make_poem(true)'>Generate</a><br>";
	if (editmode==true && newpoem==false)
	{
		o=document.getElementById("txtedited");
		publication=o.value;
		window.open('print_poem.html','win', ' top=100, left=100, width=600, height=600, resizable=no, scrollbars=no, toolbar=no, menubar=no, status=no, fullscreen=no');
	}
	else
		{
		editmode=false;
		if (newpoem==false && wrd.length>2)
		{	findhits+=findextrahits;
			poem="";
			for (i=1;i<=numlines;i++)
			{	o=document.getElementById("opts"+i);
				if (o)
				{	if (o.selectedIndex>0)
					{	newnumlines++;
						p=o.selectedIndex;
						o=document.getElementById("opt"+i);
						poem+=add_line(newnumlines,o.innerHTML,true,p);
					}
				}
			}
			numlines=newnumlines;
			newnumlines-=1;
		}
		else
		{	hitsfound=0;
			numlines=1;
			newnumlines=0;
			numselected=1;
		}
		o=document.getElementById("txtinput");
		woord=o.value;
		woord=trim(woord);
		pernum=numlines;
		if (woord.indexOf(" ")>0)
		{	
			wordcount=countwords(woord);
			if (numlines==1 && newnumlines==0)
			{	poem=add_line(numlines,woord,true,1);
				statuslines[numlines]=1;
			}
			wrd=woord.split(" ");
			if (newpoem)
			{	statusselection = numselected+" lines of "+wordcount+" selected";
			}
			if (wrd.length<14)
			{	totaal=facul[wrd.length];
			}
			else
			{	totaal=facul[14];
			}
			for (i=1;i<totaal-newnumlines;i++)
			{	for (;;)
				{	
					txt=make_line()
					if (poem.indexOf(txt)<0)
					{	break;
					}
					pernum++;
					if (pernum>=maxtries)
					{	break;	
				//		alert(maxtries +" x ");
					}
				}
				if (numlines>=200 || pernum>=maxtries)
				{	break;
				}	
				numlines++;
				poem+=add_line(numlines,txt,false,0);
				statuslines[numlines]=0;
			}
		}
		else
		{	poem="<div id='txtpoem' style='position:absolute; width:650; height:100; border-width:0px; border-style:groove; border-color:#aaaaff; overflow:auto'>"
		}
		o=document.getElementById("txtpoem");
		o.innerHTML=poem;
		o=document.getElementById("rear");
		o.disabled=false;
		if (numlines==200)
		{	o.value="Rearrange & add";
		}
		else
		{	o.value="Rearrange permutations";
		}
		submenu2+="<a href='javascript:make_poem(false)'>Rearrange</a><br>";
		showstatus();
		o=document.getElementById("pub");
		if (o)
		{	
			o.disabled=false;
			o.value="Edit and publish";
		}
		submenu2+="<a href='javascript:publish_poem()'>Edit</a><br>";
		o=document.getElementById("process");
		if (o)
		{	
			o.disabled=true;
			o.value=" ";
		}
		o=document.getElementById("blok4");
		o.innerHTML=submenu2;
		setwaitcursor(false);
		gethits();
	}
}

function reorderselection(line,sel)
{	var i=0;
	var j=0;
	var k=0;
	var m=0;
	var s=0;
	var t=0;
	var nsearch=1;
	var nshow=1;
	var o;
	poem="";
	s=sel;
//check highest selected

	for (i=1;i<=numlines;i++)
	{	if (statuslines[i]>t)
		{	t=statuslines[i];
		}
	}
	if (s>t+1)
	{	
		s=t+1
	}
	if (statuslines[line]>0)
	{
		if (sel>t)
		{	s=t;
		}
		if (s==0)
		{
			for (i=0;i<numlines;i++)
			{	if (statuslines[i]>statuslines[line])
				{	statuslines[i]--;
				}	
			}
		}
		else
		{
			if (s>=statuslines[line])
			{	
	//			alert("up");
				for (i=0;i<numlines;i++)
				{	if (statuslines[i]<=s && statuslines[i]>statuslines[line])
					{	statuslines[i]--;
					}	
				}
			}
	
			if (s<statuslines[line])
			{	
	//			alert("neer");
				for (i=0;i<numlines;i++)
				{	if (statuslines[i]>=s && statuslines[i]<=statuslines[line])
					{	statuslines[i]++;
					}
				}
			}
		}
	}
	else
	{
		for (i=0;i<numlines;i++)
		{	if (statuslines[i]>=s)
			{	if (statuslines[i]==wordcount)
				{	statuslines[i]=0;
				}
				else
				{	statuslines[i]++;
				}
			}
		}		
	}

//move lines

//	for (i=1;i<=numlines;i++)
//	{	
//		o=document.getElementById("opts"+i)
//		k=o.selectedIndex;	
//alert(k+" - "+s);
//		if (line!=i)
//		{	if (statuslines[i]>=s)
//			{	statuslines[i]=
//			}		
//			o=document.getElementById("opts"+line);
//			o.selectedIndex=s;
//		}
//	}
	statuslines[line]=s;
//	alert(statuslines[1]+"."+statuslines[2]+"."+statuslines[3]+"."+statuslines[4]+"."+statuslines[5]+"."+statuslines[6]+"."+statuslines[7]+"."+statuslines[8]+"."+statuslines[9]+"."+statuslines[10]);

//get selected lines
	numselected=0;
	for (j=1;j<wordcount+1;j++)
	{	
		for (i=1;i<=numlines;i++)
		{	
//			o=document.getElementById("opts"+i)
//			k=o.selectedIndex;	
			k=statuslines[i];
//alert(k);
			if (k==nsearch)
			{	o=document.getElementById("opt"+i);
				if (o)
				{	poem+=add_line(nsearch,o.innerHTML,true,nshow);
					numselected++;
				}
				nsearch++;
				nshow++;
			}
		}
	}
//add unselected
	for (i=1;i<=numlines;i++)
	{	
//		o=document.getElementById("opts"+i)
//		k=o.selectedIndex;	
		k=statuslines[i];
		if (k<1 || k>wordcount)
		{	o=document.getElementById("opt"+i);
			if (o)
			{	poem+=add_line(nsearch,o.innerHTML,true,0);
			}
			nsearch++;
		}
	}
//set present status

	o=document.getElementById("txtpoem");
	o.innerHTML=poem;
	for (i=1;i<=numlines;i++)
	{	
		o=document.getElementById("opts"+i)
		statuslines[i]=o.selectedIndex;
	}
	showstatus();
	showhits();
	gethits();
}

function countwords(intxt)
{	var txt="";
	var chktxt;
	txt=intxt.replace("_"," ");
	chktxt=txt.split(" ");
	return chktxt.length;
}

function windowsizes()
{	var o;
	var i=0;
	var poemhmin=190;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		browsercomp=6;

	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;

	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;

	}
	ui_left=Math.floor((windowWidth-1000-(browsercomp*2))/2);
	if (ui_left<0) 
	{	ui_left=0;
	}
	o=document.getElementById("txtpoem");
	o.style.height=windowHeight-topdivh-poemhmin-browsercomp;
	for (i=1;i<=3;i++)
	{	o=document.getElementById("txtpage"+i);
		o.style.top=topdivh;
		o.style.height=windowHeight-topdivh-browsercomp*3-15;
		if (i==2)
		{	o.style.overflow="hidden";
		}
		o.style.left=ui_left+90;
	}
	o=document.getElementById("blok1");
	o.style.top=topdivh;
	o.style.width=menudivw-browsercomp;
	o.style.height=menudivh-browsercomp*2;
	o.style.zIndex=10;
	o.style.left=ui_left;
	o.style.cursor="pointer";
	o=document.getElementById("blok2");
	o.style.top=topdivh+100;
	o.style.width=menudivw-browsercomp;
	o.style.height=menudivh-browsercomp*2;
	o.style.zIndex=10;
	o.style.left=ui_left;
	o.style.cursor="pointer";
	o=document.getElementById("blok3");
	o.style.top=topdivh+200;
	o.style.width=menudivw-browsercomp;
	o.style.height=menudivh-browsercomp*2;
	o.style.zIndex=10;
	o.style.left=ui_left;
	o.style.cursor="pointer";
	o=document.getElementById("blok4");
	o.style.left=ui_left+899+(browsercomp*3.5);
	o.style.borderLeft="0px solid black";
	o.style.fontSize="10pt";
	o.style.fontWeight="normal";
	o.style.textAlign="right";
	o.style.paddingLeft=0;
	o.style.paddingRight=5;
	o.style.paddingTop=2;
	o.style.top=topdivh;
	o.style.width=menudivw-browsercomp;
	o.style.height=menudivh-browsercomp*.9;	
	o.style.zIndex=10;
	poemwidth=787;
	o=document.getElementById("txttitle");
	o.style.width=poemwidth+(browsercomp*3);
	o=document.getElementById("txtinput");
	o.style.width=poemwidth+(browsercomp*3);
	o=document.getElementById("txtpoem");
	o.style.width=poemwidth+browsercomp;
}	

function checkhits(txt)
{	var iftxt="";
	var o;
//	alert(hitlist+" - " + txt);
	if (hitlist.indexOf("|"+txt+"=")<0)
	{
//		alert("nu: "+txt+": "+hitlist.indexOf(txt));
		hitschecked++;
		iftxt="%22"+txt.replace(/ /g,"%20")+"%22";
		o=document.getElementById("hits");
		if (o)
		{	o.innerHTML="<iframe id='calchits' frameborder=0  marginwidth=1 marginheight=1 width=0 height=20 scrolling=no align='right' src='http://www.permugram.org/googlehits.php?q=%22"+txt+"%22'></iframe>";
		}
	}
}

function storehits(txt)
{	var h=-1;
	var htxt="";

	var obj = document.getElementById('calchits'); 

	if (obj)
	{	if (obj.contentWindow) 
		{ 
		//	alert('innerHTML = ' + obj.contentWindow.document.body.innerHTML); 
		//	if (obj.contentWindow.document.body)
		//	{	
				htxt=obj.contentWindow.document.body.innerHTML;
		//	}
		} 
		else 
		{	if (obj.document) 
			{ 
//				alert('iframe.document.body.innerHTML = ' + obj.document.body.innerHTML); 
				htxt=obj.document.body.innerHTML;
			} 
			else if (obj.contentDocument) 
			{ 
//				alert('iframe.contentDocument.body.innerHTML = ' + obj.contentDocument.body.innerHTML); 
				htxt=obj.contentDocument.body.innerHTML;
			} 
		}
	}
	if (htxt.length>0)	
	{	htxt=htxt.replace(/,/g,"");
//alert(htxt);
		if (hitlist.indexOf("|"+txt+"=")<0)
		{	hitlist+=txt+"="+htxt+"|";
			hitsfound++;
//alert(hitsfound);
			if (hitsfound>=findhits || hitsfound>=numlines)
			{	//alert("stop");
				t3=window.clearInterval(t3);
			}
		}
		h=(htxt)*1;
		if (h==0 && htxt!="0")
		{	h=-1;
		}
	}
	return h;
}

function pausecomp(millis)
{	var date = new Date();
	var curdate=null;
	do {curdate = new Date()}
	while (curdate-date<millis);
}

function gethits()
{	
	checkhits('permugram');
	if (t3)
	{	t3=window.clearInterval(t3);
	}
	t3=window.setInterval("gethit()",4000);
}

function gethit()
{	var o;
	var obj;
	var i=0;
	var j=0;
	var k=0;
	var m=0;
	var ptxt="";
	var stxt="";
	for (i=1;i<=numlines;i++)
	{	o=document.getElementById("opt"+i);
		if (o)
		{				
			stxt=o.innerHTML
			o=document.getElementById("opts"+i);
			if (o)
		//	{	if (o.selectedIndex>0)
				{	
					k=hitlist.indexOf("|"+stxt+"=");
					if (k<0)
					{	
						window.setTimeout("checkhits('"+stxt+"')",1000);
						window.setTimeout("storehits('"+stxt+"')",2500);
						window.setTimeout("showhits()",2600);
						break;
					}
				}
		//	}
		}
	}
	showhits();
//	alert(hitlist);
}

function showhits()
{	var o;
	var obj;
	var i=0;
	var j=0;
	var k=0;
	var m=0;
	var p=0;
	var realhitsfound=0;
	var ptxt="";
	var stxt="";
	var atxt="";
	for (i=1;i<=numlines;i++)
	{	o=document.getElementById("opt"+i);
		if (o)
		{				
			stxt=o.innerHTML
			{	
				k=hitlist.indexOf("|"+stxt+"=");
				if (k>=0)						
				{	
//alert(k + " " +stxt);
					ptxt=hitlist.substring(k+stxt.length+2,10000);
					k=ptxt.indexOf("|");
					ptxt=ptxt.substring(0,k);
					o=document.getElementById("opth"+i);
					if (o)
					{	
						p=(ptxt)*1;
						atxt=(ptxt)+" hit";
						if (p!=1)
						{	atxt+="s";
						}
						o.alt=atxt;
						o.title=atxt;
//	alert(p);
						o.height=10;
						if (p>1000000)
						{	m=60
						}
						else
						{
							if (p>100000)
							{	m=53
							}
							else
							{
								if (p>10000)
								{	m=43 + p/10000
								}
								else
								{	if (p>1000)
									{	m=33 + p/1000
									}
									else
									{
										if (p>100)
										{	m=23 + p/100
										}
										else
										{
											if (p>10)
											{	m=13 + p/10
											}
											else
											{	m=3 + p
											}
										}
									}
								}
							}
						}
//alert(m);
						o.width=m;
						if (p==0)
						{	o.src="grey.gif";
						}
						else
						{	o.src="yellow.gif";
							realhitsfound++;					
						}

					}
				}
			}
		}
	}
	if (realhitsfound>=wordcount)
	{
		o=document.getElementById("process");
		if (o)
		{	
			o.disabled=false;
			o.value="Automatic permugram";
		}
		if (submenu2.indexOf("Auto")<0)
		{	submenu2+="<a href='javascript:process_poem(true)'>Automatic</a>";
			o=document.getElementById("blok4");
			o.innerHTML=submenu2;
		}
	}
}

function add_line(nr,txt,chk,num)
{	var sptxt="";
	var chtext="";
	var seltxt="";
	var outtxt="";
	var i=0;
	outtxt+="<select id='opts"+nr+"' onchange='reorderselection("+nr+",selectedIndex)' style='width:60; text-align:right;'>"
	sptxt ="&nbsp;&nbsp;"
	if (nr<100) sptxt="&nbsp;&nbsp;&nbsp;&nbsp;"
	if (nr<10) sptxt="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
	outtxt+="<option>"+sptxt+nr+"</option>";
	sptxt =""
	for(i=1;i<=wordcount;i++)
	{
		if (i<100) sptxt="&nbsp;&nbsp;"
		if (i<10) sptxt="&nbsp;&nbsp;&nbsp;&nbsp;"
		if (num>0 && num==i)
		{	outtxt+="<option selected>&bull;&nbsp;"+sptxt+(i)+"</option>";
		}
		else
		{	outtxt+="<option>&bull;&nbsp;"+sptxt+(i)+"</option>";
		}
	}
	outtxt+="</select>";

	outtxt+="&nbsp;&nbsp;&nbsp;&nbsp;<span id='opt"+nr+"'>"+txt+"</span>&nbsp;&nbsp;&nbsp;&nbsp;"
	outtxt+="<img id='opth"+nr+"' alt='' title='' src='yellow.gif' width=0 height=0 align='absmiddle'>";
//	outtxt+="<span id='opth"+nr+"' style='width:80; font-size:8pt; font-weight:lighter; text-align:right;'>"+"-"+"</span>";
//	if (num==1)
//	{	outtxt+="<span style='width:40; font-size:8pt; font-weight:lighter; text-align:left;'>&nbsp;hits</span>";
//	}
	outtxt+="<br>";
//alert(outtxt);
	return outtxt
}

function setwaitcursor(onoff)
{	if (onoff)
	{	document.body.style.cursor = 'wait';
	}
	else
	{	document.body.style.cursor = 'default';
	}
}

function trim(value) 
{
  	value = value.replace(/^\s+/,''); 
  	value = value.replace(/\s+$/,'');	
	do
	{
  		value = value.replace('  ',' ');
  	} while (value.indexOf('  ')>=0);
  	return value;
}

function showstatus()
{	var txt1="";
	var txt2="";
	o=document.getElementById("status");
	if (numlines>1)	
	{
		if (numlines==200)
		{	statuspoem=numlines+ " of more permutations"
		}
		else
		{	statuspoem="all "+numlines+ " permutations";
		}
		if (debug)
		{	txt1+="("+(pernum)+" cycles) "
		}
	
		statusselection = numselected+" line"
		if (numselected!=1) 
		{	statusselection+="s"
		}
		statusselection+=" of "+wordcount+" selected";
		
		if (numselected<wordcount)
		{	txt2=": <font color=red>select "+(wordcount-numselected)+ "!</font>" 
		}
	//	if (numselected>wordcount)
	//	{	txt2=": <font color=red>deselect "+(numselected-wordcount)+ "!</font>" 
	//	}
		if (numselected==wordcount)
		{	txt2=": <font color=green>permugram!</font>";
		}
		o.innerHTML="Status: " +statuspoem +txt1+"; " + statusselection + txt2;
	}
	else
	{
		o.innerHTML="Status: at least 2 words are needed to make a permugram.";
	}	
}

function make_line()
{	var j=0;
	var k=0;
	var txt="";
	var nrs="nums: ";
	for (j=0;j<wrd.length;j++)
	{	for (;;)
		{	k=Math.floor(Math.random()*wrd.length);
			if (nrs.indexOf(" "+(k)+" ")<0)
			{	break;
			}
		}	
		nrs+=" "+(k)+" ";
		txt+=wrd[k]
		if (j<wrd.length-1)
		{	txt+=" ";
		}
	}
	return txt;
}

function start_poem(onoff)
{
	if (onoff) document.body.style.cursor = 'wait';
	window.setTimeout("make_poem("+onoff+")",500)
}

function scrollbox()
{	var o;
	var s=0;
	var t=0;
	var h=0;
	var sh=0;
	var p=0;
	if (divshown!=2)
	{	o=document.getElementById("txtpage"+divshown);
		t=parseInt(o.style.top);
		h=parseInt(o.style.height);
		sh=parseInt(o.scrollHeight);
		s=parseInt(o.scrollTop);
		o=document.getElementById("blok4");
		p=t+(h*(s/sh));
		if (p>t+h-90) p=t+h-90;
		o.style.top=p;
	//alert("scrollbox: "+((p-t)/h)*100);
		if (divshown==1)
		{	o.innerHTML=makesubmenu(divshown,submenu1,((p-t)/h)*100);
		}
		else
		{	o.innerHTML=makesubmenu(divshown,submenu3,((p-t)/h)*100);
		}
	}
}

function showdiv(nr)
{	var i=0;
	var h=0;
	var t=0;
	var h=0;
	var sh=0;
	var s=0;

	for (i=1;i<=3;i++)
	{	
		o=document.getElementById("blok"+i);
		if (i==nr)
		{	
			o.style.color="red";
			o.style.borderRight="0px groove #9999ff";
			o.style.backgroundImage="url(permlight.gif)";
			if (browsercomp>0)
			{	o.style.width=menudivw-browsercomp;
			}
		}
		else
		{	o.style.borderRight="1px groove #000044";
			o.style.color="black";
			o.style.backgroundImage="url(permmiddle.gif)"
			if (browsercomp>0)
			{	o.style.width=menudivw-browsercomp-1;
			}
		}
		o=document.getElementById("txtpage"+i);
		if (i==nr)
		{	o.style.visibility="visible";
			t=parseInt(o.style.top);
			h=parseInt(o.style.height);
			sh=parseInt(o.scrollHeight);
			s=parseInt(o.scrollTop);
		}
		else
		{	o.style.visibility="hidden";
		}
	}
	divshown=nr;
	if (nr==1)
	{	
		o=document.getElementById("blok4");
		p=t+(h*(s/sh));
		o.style.top=p;
		o.innerHTML=makesubmenu(1,submenu1,0);
	}
	if (nr==2)
	{	o=document.getElementById("blok4");
		o.style.top=t+((h-90)/2);
		o.innerHTML=submenu2;
	}
	if (nr==3)
	{
		o=document.getElementById("blok4");
		p=t+(h*(s/sh));
		o.style.top=p;
		o.innerHTML=makesubmenu(3,submenu3,0);
	}
	divshown=nr;
}

function makesubmenu(menunr,menuval,spot)
{	var i=0;
	var txt="";
	var spotused=false;
	for (i=0;i<menuval.length;i++)
	{	
//alert("spot: "+Math.floor(spot)+" menu: "+Math.floor(menuval[i][0]));
		if (Math.abs(spot-menuval[i][0])<3)
		{	txt+="<font color='red'>"+menuval[i][1]+"</font>";
		}
		else
		{	txt+="<a href=javascript:gotospot("+menunr+","+menuval[i][0]+")>"+menuval[i][1]+"</a>";
		}
		if (i<menuval.length-1)
		{	txt+="<br>";
		}
	}
	return txt;
}
//"<a href=javascript:gotospot(3,0)>English</a><br><a href=javascript:gotospot(3,45.16)>Nederlands</a>"
function hotdiv(nr, onoff)
{	var i=0;
	for (i=1;i<=3;i++)
	{	o=document.getElementById("blok"+i);
		if (i==nr && onoff==true)
		{	
			o.style.backgroundImage="url(permlight.gif)"
		}
		else
		{	
			if (i!=divshown)
			{	
				o.style.backgroundImage="url(permmiddle.gif)"
			}
		}
	}

}

function publish_poem()
{	var o;
	var ok=false;
	var txt="";
	var i=0;
	submenu2="<a href='javascript:make_poem(true)'>Generate</a><br>";
	if (editmode)
	{	
		o=document.getElementById("txtedited");
		publication=o.innerHTML;
		window.clipboardData.setData('Text',o.innerHTML);
	}
	else
	{	editmode=true;
		o=document.getElementById("rear");
		{	
			o.disabled=false;
			o.value="Print";
		}
		submenu2+="<a href='javascript:make_poem(false)'>Print</a><br>";
		o=document.getElementById("pub");
		{	
			o.disabled=false;
			o.value="Copy to clipboard";
		}
		submenu2+="<a href='javascript:publish_poem()'>Copy</a><br>";
		o=document.getElementById("process");
		{	
			o.disabled=false;
			o.value="E-mail";
		}
		submenu2+="<a href='javascript:process_poem()'>E-mail</a><br>";
		o=document.getElementById("txttitle");
		txt=o.value+"\n\n";
		for (i=1;i<=numlines;i++)
		{	o=document.getElementById("opts"+i);
			if (o)
			{	if (o.selectedIndex>0)
				{	
					o=document.getElementById("opt"+i);
					txt+=o.innerHTML.replace("_"," ")+"\n";
				}
			}
		}		
		o=document.getElementById("txtpoem");
		txt=txt.replace('_',' ');
		o.innerHTML="<textarea id='txtedited' style='border-width:0px; width:"+(770+(browsercomp*3))+"px; font-family:Verdana; font-size:11pt; font-weight:600; height:"+((windowHeight)-270 +(browsercomp))+"px; '>"+txt+"</textarea>";
		
	}
	o=document.getElementById("blok4");
	o.innerHTML=submenu2;
}

function process_poem()
{
	if (editmode==false)
	{	auto_poem();
	}
	else
	{	mail_poem();
	}
}


function mail_poem()
{	var o;
	var txt1="";
	var txt2="";
	var i=0;
	o=document.getElementById("txtedited");
	if (o)
	{	txt1=o.value;
		for (i=0;i<txt1.length;i++)
		{	if (txt1.charCodeAt(i)==13)
			{	txt2+="%0a";
			}
			if (txt1.charCodeAt(i)>31)
			{	txt2+=txt1.substring(i,i+1);
			}
		}
	}
	window.location.href="mailto:info@permugram.org?subject=Permugram&body=Permugram submitted to be published:%0a____%0a%0a"+txt2+"%0a____%0a%0aAuthor: ?%0aCreation date: ?";	
}

function auto_poem()
{	var i=0;
	var j=0;
	var h=0;
	var k=0;
	var o;
	var txt;
	var hitarray=new Array(numlines);
	for (i=1;i<=numlines;i++)
	{	o=document.getElementById("opth"+i);
		if (o)
		{	h=parseInt(o.alt);
			if (h==0)
			{	h=parseInt(o.title);
			}
			if (h>0)
			{	hitarray[i]=h;
			}
		}
	}
//	alert(hitarray);
	hitarray.sort(function(a,b){return b-a});
//	alert(hitarray);
	for (j=0;j<wordcount;j++)
	{	k=hitarray[j];
		for (i=1;i<=numlines;i++)
		{	o=document.getElementById("opth"+i);
			if (o)
			{	h=parseInt(o.alt);
				if (h==0)
				{	h=parseInt(o.title);
				}
				if (h==k)
				{	reorderselection(i,j+1)
				}
			}
		}					
	}
}

function gotospot(dv,spot)
{	var o;
	var h=0;
	var s=0;
	o=document.getElementById("txtpage"+dv);
	h=parseInt(o.scrollHeight);
	s=h*(spot/100)
	o.scrollTop=s;
	o=document.getElementById("blok4");
//alert("gotospot: "+spot);
	o.innerHTML=makesubmenu(3,submenu3,spot);
}

function testalert()
{	
	var s=0;
	var t=0;
	var h=0;
	var sh=0;
	var p=0;
	var o;
	o=document.getElementById("txtpage"+divshown);
	t=parseInt(o.style.top);
	h=parseInt(o.style.height);
	sh=parseInt(o.scrollHeight);
	s=parseInt(o.scrollTop);
//	alert("Top: "+t+" Height: "+h+" Scrollheight: "+sh+" Scrolltop: "+s+" Spot: "+(Math.floor((s/sh)*10000)/100)+"\nGenereted options: "+pernum+"\nHits checked: "+hitschecked);
	alert("Scroll %: "+(Math.floor((s/sh)*10000)/100)+"\nGenerated options: "+pernum+"\nHits checked: "+hitschecked+ "\n\nHits: "+hitlist);
}

function startmix()
{	setmix(false);
	t1=window.setInterval("setmix(true)",20000);
}

function movewords()
{	var i=0;
	var o;
	var movestep=1;
	var p=0;
	var busy=0;
	for (i=0;i<3;i++)
	{
		o=document.getElementById(wrdanim[i][0]);
		p=parseInt(o.style.left);
//alert(p + " - " +wrdanim[i][2]);

		if (p<wrdanim[i][2])
		{	o.style.left=p+movestep;
			busy++;
		}
		else
		{	if (p>wrdanim[i][2])
			{	o.style.left=p-movestep;
				busy++;
			}
		}
	}
	if (busy==0) 
	{	t2=window.clearInterval(t2);
	}
}

function setmix(slide)
{	var ltwrd=650+ui_left;
	var ltnext=0;
	var o;
	o=document.getElementById(wrdanim[wrdspots[wrdmix][0]-1][0]);
	wrdanim[wrdspots[wrdmix][0]-1][2]=ltwrd;
	if (slide==false)
	{	o.style.left=ltwrd;
		o=document.getElementById("wrd0");
		o.style.left=ui_left+90;
	}
	ltnext=wrdanim[wrdspots[wrdmix][0]-1][1];
	o=document.getElementById(wrdanim[wrdspots[wrdmix][1]-1][0]);
	wrdanim[wrdspots[wrdmix][1]-1][2]=ltwrd+ltnext;
	if (slide==false)
	{	o.style.left=ltwrd+ltnext;
	}
	ltnext+=wrdanim[wrdspots[wrdmix][1]-1][1];
	o=document.getElementById(wrdanim[wrdspots[wrdmix][2]-1][0]);
	wrdanim[wrdspots[wrdmix][2]-1][2]=ltwrd+ltnext;
	if (slide==false) 
	{	o.style.left=ltwrd+ltnext;
	}
	else
	{	
		t2=window.setInterval("movewords()",10);
	}

	wrdmix++;
	if (wrdmix>5) 
	{	wrdmix=0;
	}
}

function write_poem(creationdate,authorname,title,poemtxt)
{	var i=0;
	var txt="";
	var margintxt="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	var poemlines=poemtxt.split("|");
	txt=creationdate;
//	if (authorname.length>0)
//	{	txt += " - "+authorname;
//	}	
	txt+="<br><br>";
	if (title.length>0)
	{	txt += margintxt+"<i>"+title+"</i><br><br>";
	}	
	for (i=0;i<poemlines.length;i++)
	{	txt+=margintxt+poemlines[i]+"<br>";
	}
	txt+="<br>";
	return (txt);
}
