this.toolVersion = "0.98";
this.genNums = 1; //Number of generated phrases
this.seedy = 0;
this.page = 0;
this.ws = {}; // multiselection
this.wp = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]; // avoid duplicate entries
this.brwPool = [
	{description:"This planet is most notable for Tibediedian Arma brandy but scourged by deadly edible grubs.",economy:2,government:1,ID:0,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Tibedied",population:36,productivity:11520,radius:4610,sun:true,techlevel:8},
	{description:"Qube is reasonably well known for its great dense forests but scourged by deadly civil war.",economy:5,government:7,ID:1,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Qube",population:37,productivity:16280,radius:5528,sun:true,techlevel:6},
	{description:"The world Leleer is very noted for its pink Leleerian Er plant plantations but beset by frequent civil war.",economy:3,government:3,ID:2,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Leleer",population:35,productivity:13720,radius:3149,sun:true,techlevel:7},
	{description:"This world is very fabled for the Biargeian Edible Poet.",economy:0,government:2,ID:3,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Biarge",population:47,productivity:22560,radius:4435,sun:true,techlevel:11},
	{description:"The world Xequerin is fabled for its weird volcanoes and the Xequerinian mountain lobstoid.",economy:3,government:4,ID:4,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Xequerin",population:32,productivity:14336,radius:3508,sun:true,techlevel:6},
	{description:"Tiraor is a revolting little planet.",economy:0,government:4,ID:5,inhabitant:"Blue Fat Insect",inhabitants:"Blue Fat Insects",name:"Tiraor",population:41,productivity:26240,radius:4780,sun:true,techlevel:9},
	{description:"The planet Rabedira is well known for its inhabitants' ancient loathing of sit coms but ravaged by dreadful civil war.",economy:1,government:2,ID:6,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Rabedira",population:36,productivity:15552,radius:3909,sun:true,techlevel:8},
	{description:"Lave is most famous for its vast rain forests and the Laveian tree grub.",economy:5,government:3,ID:7,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Lave",population:25,productivity:7000,radius:4116,sun:true,techlevel:4},
	{description:"This planet is mildly noted for the Zaatxeian deadly Ouenbeoid but plagued by lethal spotted craboids.",economy:0,government:7,ID:8,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Zaatxe",population:52,productivity:45760,radius:4076,sun:true,techlevel:11},
	{description:"This planet is mildly fabled for its inhabitants' eccentric love for tourists but beset by deadly edible moths.",economy:2,government:1,ID:9,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Diusreza",population:28,productivity:8960,radius:6360,sun:true,techlevel:6},
	{description:"Teaatis is mildly well known for Teaatisian vicious brew.",economy:6,government:1,ID:10,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Teaatis",population:16,productivity:2560,radius:5892,sun:true,techlevel:2},
	{description:"This world is mildly famous for its vast rain forests and the Riinusian tree grub.",economy:1,government:4,ID:11,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Riinus",population:42,productivity:24192,radius:6493,sun:true,techlevel:9},
	{description:"The planet Esbiza is most famous for its vast rain forests.",economy:0,government:2,ID:12,inhabitant:"Small Yellow Fat Humanoid",inhabitants:"Small Yellow Fat Humanoids",name:"Esbiza",population:35,productivity:16800,radius:5364,sun:true,techlevel:8},
	{description:"This planet is reasonably famous for its inhabitants' exceptional love for food blenders.",economy:4,government:6,ID:13,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Ontimaxe",population:35,productivity:16800,radius:6740,sun:true,techlevel:6},
	{description:"This world is most notable for its fabulous Cebetelaian lethal brandy but scourged by killer mountain Esbionoids.",economy:3,government:0,ID:14,inhabitant:"Fierce Red Bug-Eyed Lizard",inhabitants:"Fierce Red Bug-Eyed Lizards",name:"Cebetela",population:28,productivity:6272,radius:4290,sun:true,techlevel:6},
	{description:"This planet is most fabled for its inhabitants' ingrained silliness but scourged by deadly civil war.",economy:4,government:5,ID:15,inhabitant:"Fierce Bug-Eyed Lizard",inhabitants:"Fierce Bug-Eyed Lizards",name:"Ceedra",population:38,productivity:16416,radius:4341,sun:true,techlevel:7},
	{description:"The planet Rizala is mildly notable for Rizalaian lethal brandy.",economy:4,government:5,ID:16,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Rizala",population:42,productivity:18144,radius:6578,sun:true,techlevel:8},
	{description:"Atriso is mildly well known for its exotic cuisine and its inhabitants' ingrained silliness.",economy:5,government:3,ID:17,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Atriso",population:37,productivity:10360,radius:3127,sun:true,techlevel:7},
	{description:"This planet is plagued by frequent earthquakes.",economy:7,government:7,ID:18,inhabitant:"Bug-Eyed Frog",inhabitants:"Bug-Eyed Frogs",name:"Teanrebi",population:39,productivity:10296,radius:6102,sun:true,techlevel:6},
	{description:"The planet Azaqu is most famous for its pink oceans and Zero-G cricket.",economy:6,government:6,ID:19,inhabitant:"Red Slimy Lobster",inhabitants:"Red Slimy Lobsters",name:"Azaqu",population:37,productivity:11840,radius:6662,sun:true,techlevel:6},
	{description:"This world is ravaged by occasional solar activity.",economy:2,government:0,ID:20,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Retila",population:35,productivity:8960,radius:6575,sun:true,techlevel:8},
	{description:"The planet Sotiqu is famous for its exotic goat soup but ravaged by killer disease.",economy:7,government:0,ID:21,inhabitant:"Fierce Frog",inhabitants:"Fierce Frogs",name:"Sotiqu",population:20,productivity:1920,radius:4731,sun:true,techlevel:3},
	{description:"The world Inleus is most famous for the Inleusian spotted wolf.",economy:2,government:6,ID:22,inhabitant:"Harmless Slimy Rodent",inhabitants:"Harmless Slimy Rodents",name:"Inleus",population:49,productivity:31360,radius:6074,sun:true,techlevel:10},
	{description:"The world Onrira is mildly noted for the Onriraian deadly Esonatoid but plagued by lethal spotted yaks.",economy:0,government:7,ID:23,inhabitant:"Blue Frog",inhabitants:"Blue Frogs",name:"Onrira",population:60,productivity:52800,radius:6678,sun:true,techlevel:13},
	{description:"This planet is most notable for vicious Inzalodi gargle blasters but scourged by unpredictable earthquakes.",economy:4,government:3,ID:24,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Ceinzala",population:36,productivity:12096,radius:4114,sun:true,techlevel:7},
	{description:"The planet Biisza is most famous for its vast rain forests.",economy:4,government:5,ID:25,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Biisza",population:38,productivity:16416,radius:4597,sun:true,techlevel:7},
	{description:"This planet is most notable for its exotic night life but ravaged by frequent earthquakes.",economy:5,government:5,ID:26,inhabitant:"Large Bug-Eyed Lizard",inhabitants:"Large Bug-Eyed Lizards",name:"Legees",population:31,productivity:11160,radius:3076,sun:true,techlevel:5},
	{description:"The world Quator is scourged by deadly edible arts graduates.",economy:3,government:0,ID:27,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Quator",population:28,productivity:6272,radius:5390,sun:true,techlevel:6},
	{description:"The world Arexe is fabled for its exciting sit coms and its inhabitants' ancient loathing of sit coms.",economy:1,government:6,ID:28,inhabitant:"Furry Rodent",inhabitants:"Furry Rodents",name:"Arexe",population:44,productivity:31680,radius:6820,sun:true,techlevel:9},
	{description:"Atrabiin is cursed by killer edible Nuatoids.",economy:2,government:2,ID:29,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Atrabiin",population:29,productivity:11136,radius:3168,sun:true,techlevel:6},
	{description:"The world Usanat is a boring world.",economy:6,government:4,ID:30,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Usanat",population:23,productivity:5888,radius:4972,sun:true,techlevel:3},
	{description:"The world Xeesle is a boring planet.",economy:3,government:1,ID:31,inhabitant:"Large Blue Fat Feline",inhabitants:"Large Blue Fat Felines",name:"Xeesle",population:29,productivity:8120,radius:3381,sun:true,techlevel:6},
	{description:"Oreseren is a revolting little planet.",economy:3,government:1,ID:32,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Oreseren",population:33,productivity:9240,radius:5326,sun:true,techlevel:7},
	{description:"This planet is noted for its exotic fish meat.",economy:0,government:7,ID:33,inhabitant:"Blue Horned Lizard",inhabitants:"Blue Horned Lizards",name:"Inera",population:60,productivity:52800,radius:5970,sun:true,techlevel:13},
	{description:"This planet is mildly fabled for its inhabitants’ eccentric love for tourists but plagued by deadly earthquakes.",economy:7,government:3,ID:39,inhabitant:"Black Fat Feline",inhabitants:"Black Fat Felines",name:"Reorte",population:31,productivity:5208,radius:6419,sun:true,techlevel:5},
	{description:"The planet Oresri is cursed by dreadful civil war.",economy:5,government:4,ID:43,inhabitant:"Rodent",inhabitants:"Rodents",name:"Oresri",population:34,productivity:10880,radius:5178,sun:true,techlevel:6},
	{description:"The planet Leesti is reasonably fabled for Zero-G cricket and Leestiian evil juice.",economy:2,government:7,ID:55,inhabitant:"Human Colonial",inhabitants:"Human Colonials",name:"Leesti",population:50,productivity:35200,radius:3085,sun:true,techlevel:10},
	{description:"This planet is a tedious little planet.",economy:0,government:7,ID:82,inhabitant:"Black Fat Feline",inhabitants:"Black Fat Felines",name:"Ensoreus",population:52,productivity:45760,radius:3380,sun:true,techlevel:11},
	{description:"Teraed is a n unremarkable dump.",economy:0,government:2,ID:99,inhabitant:"Yellow Insect",inhabitants:"Yellow Insects",name:"Teraed",population:47,productivity:22560,radius:6087,sun:true,techlevel:11},
	{description:"Atage is an unremarkable planet.",economy:3,government:3,ID:103,inhabitant:"Red Bug-Eyed Lizard",inhabitants:"Red Bug-Eyed Lizards",name:"Atage",population:39,productivity:15288,radius:3298,sun:true,techlevel:8},
	{description:"The planet Eszaraxe is most famous for the Eszaraxeian spotted shrew and the Eszaraxeian mountain poet.",economy:7,government:0,ID:142,inhabitant:"Small Blue Slimy Frog",inhabitants:"Small Blue Slimy Frogs",name:"Eszaraxe",population:12,productivity:1152,radius:5349,sun:true,techlevel:1}
];
this.specials = {
	commanderBounty: "0 Cr.", //@B
	commanderName: "Jameson", //@C
	commanderRank: "Novice", //@R
	commanderStatus: "Clean", //@L
	currentShipDisplayName: "Cobra III", //@D
	currentShipName: "Cobra III", //@I
	currentSystem: {}, //@P
	currentTarget: "Python", //@T
	rndBrewName: ["Naga's Cure-All","Bubbly Nymph","Illustrious Gut","Whipping Mule Destroyer","Unbeatable Elephant","Blanching Brain's Tongue Teaser","Blistering Harpy's Punch"], // @Z (z)
	rndFirstName: ["Albi","Aloysius","August","Bascom","Bedford","Ben","Bill","Billy","Charlie","Chauncey","Clem","Columbus","Cormond","Drew","Eben","Franklin","Germaine","Gilbert","Gilford","Henry","Hobart","Hugh","Jag","Juli","Justice","Keira","Maynard","Milburn","Millard","Montgomery","Nestor","Norris","Osgood","Parley","Percibald","Quinby","Redmond","Roderson","Roy","Rufus","Stan","Timothy","Tris","Victor","Will","Winston"], //@F (f)
	rndShipName: ["The Watchful Resilience","The Dreebel Desire","The Perpetual Angel","The Ascendant Death","The Beneficent Presence","The Good Raptor","The Preponderant Morning Star","The Tranquil Intrepid","The Bountiful Lady","The Golden Retribution","The Bright Pride","The Greedy Beacon","The Long Wing Clipper","The Illustrious Voice","The Gallant Invincible","The Swift Growler","The Defiant Whirlwind","The Risky Whiplash","The Peerless Mariner","The Silent Ravishment","The Magnificent Ravager","The Silent Chaser","The Devil's Badger","The Flying Sovereign","The Gentle Majesty","The Predominant Tsunami","The Implacable Illumination","The Celestial Princess","The Unbearable Narwhal","The Honshar Moon","The Purest Grace","The Green Bravo","The Imperial Vendetta","The Baneful Wing Clipper","The Northwind Whirlpool","The Fearless Devastator","The Stainless Blessing","The Ole' Ranger","The Gypsy Gift","The One Obliteration","The Inapproachable Cutter","The Ocean's Vulture","The Pale Princess","The Ornamar Black Joke","The Imperial Freesail","The Stern Attendant","The Fleet-Footed Intrepid","The Regnant Obliteration","The Ascendant Maverick","The Grand Cyclone","The Celip Sunrise","The Coruscating Effulgence","The Impeccable Engager","The Shining Corvus","The Singing Executor","The Serene Mist","The Worm Adventure","The Worm Rapic","The Celip Artisan","The Night's Warrior","The Gilded Cutlass","The Ascendant Firestorm","The Ocean's Sentinel","The Strong Nemesis"],
	rndSurName: ["Alfnyr","Alfplen","Athebairn","Athefyre","Athesby","Bleakarm","Brassadder","Buctas","Chalkrat","Coldhand","Coldtree","Corsil","Corus","Daggersummer","Damarla","Dennistoun","Direwinter","Fanaera","Fasthair","Garrifour","Goldheart","Goodbranch","Goodthorn","Grombar","Helmdragon","Helmeagle","Hescel","Ignila","Illwolf","Jonstun","Lightsnow","Lyrlan","Lyrmer","Lyttleer","Marangan","Nocor","Nocwa","Odimar","Ol'Rogers","Oldearth","Sams","Sayhisa","Selnare","Tatary","Vidaburr","Vidana","Vidaper","Williamson","Wiserain"], //@N (n)
	rndSystem: [], //@S (s)
	idsA: ["I","II","III","IV","V","VI","VII","IX","X"],
	idsB: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
	idsC: ["0","1","2","3","4","5","6","7","8","9"]
};
// Add Oolite stuff
var clock = function(){days: 0};
if(typeof(__defineGetter__)!=="undefined") clock.__defineGetter__("days",function(){return(Date.parse("1 1 1970 00:34:44 GMT")+25);});
var baseChange = function(n,to,from){return(parseInt(n,from || 10).toString(to));}
// deepcopy
function clone(srcInstance){
	if(typeof(srcInstance)!=='object' || srcInstance===null) return srcInstance;
	var newInstance = srcInstance.constructor();
	for(var i in srcInstance) newInstance[i] = clone(srcInstance[i]);
	return newInstance;
};
// called by HTML
function init(form)
{
	var p = Math.floor(Math.random()*6);
	this.specials.currentSystem = this.brwPool[p]; // get @P
	var brwpl = this.brwPool.length;
	for(var s=0;s<brwpl;s++) if(s!==p) this.specials.rndSystem.push(this.brwPool[s]); // get @S
	// fill array with names for multiselection lists
	for(var prop in this.w){
		for(var drop in this.w[prop]){
			if(this.w[prop][drop].length) this.ws[""+prop+"."+drop+""] = this.w[prop][drop];
		}
	}
	// fill Samples drop-down list
	for(var prop in this.pool){
		var a = document.createElement("option");
		a.text = prop;
		form.Samples.appendChild(a);
	}
	form.Samples.selectedIndex = 1;
	form.Nums.selectedIndex = 0;
	generate_input(form, form.Samples.options[form.Samples.selectedIndex].text);
	change_nums(form, form.Nums.options[form.Nums.selectedIndex].text);
	// fill irregular verb list
	for(var prop in this.irregularVerbs){
		var a = document.createElement("option");
		a.text = prop;
		form.iverbs.appendChild(a);
	}
	// fill irregular noun list
	for(var prop in this.irregularNouns){
		var a = document.createElement("option");
		a.text = prop;
		form.inouns.appendChild(a);
	}
	// fill superlatives list
	for(var prop in this.superlatives){
		var a = document.createElement("option");
		a.text = prop;
		form.superlatives.appendChild(a);
	}
	// fill multiselection lists
	for(var i=1;i<19;i++){
		var w = "what"+i;
		var a = document.createElement("option");
		a.text = "-";
		form[w].appendChild(a);
		for(var prop in this.ws){
			var a = document.createElement("option");
			a.text = prop;
			form[w].appendChild(a);
		}
	}
	document.form.changeSeed.value = 0;
	make_poem(form);
	document.getElementById("info").firstChild.data = "Tool:"+this.toolVersion+", OXP:"+this.OXPVersion+", Data:"+this.dataBaseVersion;
};
// update for number of generated phrases
function change_nums(form)
{
	this.genNums = parseInt(form.Nums.options[form.Nums.selectedIndex].text);
};
// update for seed
function change_seed(form)
{
	this.seedy = parseInt(form.changeSeed.value);
};
// update multiselection
function change_list(where, sample_name)
{
	if(!where || !sample_name || !sample_name.text) return;
	var x = parseInt(where.substr(4));
	var w = "list"+x;
	var f = "field"+String.fromCharCode(64+x);
	if(sample_name.text === '-'){
		form[w].value = "";
		this.wp[x] = [];
		this.stdObj[f] = [];
	} else {
		if(form[w].value) form[w].value += "\n"+this.ws[sample_name.text].join("\n");
		else form[w].value = this.ws[sample_name.text].join("\n");
		if(this.wp[x].indexOf(sample_name.text)===-1) this.wp[x].push(sample_name.text);
		this.stdObj[f] = form[w].value.split("\n");
	}
	return;
};
// change Sample
function generate_input(form, sample_name)
{
	var b = document.getElementById("showBits");
	if(this.pool.hasOwnProperty(sample_name)){
		for(var i=1;i<19;i++){
			var x = "list"+i;
			var y = "field"+String.fromCharCode(64+i);
			form[x].value = this.pool[sample_name][y].join("\n");
		}
		form.sentences.value = this.pool[sample_name].sentences.join("\n");
		if(this.pool[sample_name].splitRules) form.sptRules.value = this.pool[sample_name].splitRules.join(",");
		else form.sptRules.value = "";
		if(this.pool[sample_name].custom){
			form.cstRules.value = this.pool[sample_name].custom.join(",");
			if(this.pool[sample_name].custom.length){
				var o = parseInt(this.pool[sample_name].custom[0]);
				b.firstChild.data = "- First entry must be number. Sample default: 0x"+baseChange(o,16);
				var bi = "Bit";
				for(var ii=0;ii<25;ii++){
					if(o&Math.pow(2,ii)) document.form[bi+ii].checked = true;
					else document.form[bi+ii].checked = false;
				}
			} else b.firstChild.data = "- First entry must be number.";
		} else {
			form.cstRules.value = "0x0";
			b.firstChild.data = "- First entry must be number.";
		}
	} else {
		clear_all(form);
		b.firstChild.data = "- First entry must be number.";
	}
	form.outtext.value = "";
	count_all_lines(form);
};
function clear_all(form)
{
	form.Samples.value = "-";
	for(var i=1;i<19;i++){
		var x = "list"+i;
		form[x].value = "";
	}
	form.sentences.value = "";
	form.sptRules.value = "";
	form.outtext.value = "";
	count_all_lines(form);
	this.wp = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]];
};
// update
function count_all_lines(form)
{
	for(var i=1;i<19;i++){
		var x = "list"+i;
		var y = "field"+String.fromCharCode(64+i);
		this.stdObj[y] = form[x].value.split("\n");
	}
	this.stdObj.sentences = form.sentences.value.split("\n");
	this.stdObj.splitRules = form.sptRules.value.split(",");
	if(this.stdObj.splitRules.length && this.stdObj.splitRules[0]!==""){
		var z = this.stdObj.splitRules.length;
		for(var r=0;r<z;r++) this.stdObj.splitRules[r] = parseInt(this.stdObj.splitRules[r]);
	}
	if(form.cstRules.value){
		this.stdObj.custom = form.cstRules.value.split(",");
		if(this.stdObj.custom && this.stdObj.custom.length){
			this.stdObj.custom[0] = parseInt(this.stdObj.custom[0]);
			var a = this.stdObj.custom;
			a[0] = "0x"+baseChange(a[0],16);
			form.cstRules.value = a.join(",");
		} else this.stdObj.custom[0] = 0;
	} else this.stdObj.custom[0] = 0;
};
function show_HelperComms()
{
	var a = document.getElementById("helperComms").style.visibility;
	if(!a || a==="hidden"){
		document.getElementById("helperComms").style.visibility = "visible";
	} else document.getElementById("helperComms").style.visibility = "hidden";
	return;
};
function show_HelperPool(form)
{
	var a = document.getElementById("helperPool").style.visibility;
	if(!a || a==="hidden"){
		document.getElementById("helperPool").style.visibility = "visible";
		changePoolList(1);
	} else document.getElementById("helperPool").style.visibility = "hidden";
	return;
};
function changePoolList(disp)
{
	var b = Object.keys(this.ws);
	if(this.page>b.length) this.page = 0;
	var c = document.getElementById("helperPool").childNodes[7];
	for(var i=this.page;i<this.page+35;i++){
		if(b.length>i){
			var boo = document.getElementById("helperPool").childNodes[7].childNodes[1+(i-this.page)*2];
			boo.textContent = b[i];
			boo.onclick = function(){changePoolArea(this.textContent);}
		} else {
			var boo = document.getElementById("helperPool").childNodes[7].childNodes[1+(i-this.page)*2];
			boo.textContent = "";
		}
	}
	if(disp) changePoolArea(b[0]);
	return;
};
function changePoolArea(what)
{
	var k = this.ws[what],r="",i=0;
	if(k){
		var kl = k.length-1;
		for(i;i<kl;i++) r += k[i]+", ";
		r += k[i];
		form.overviewPool.value = what+" :\n"+r;
	}
	return;
};
function changePoolPage()
{
	this.page += 35;
	changePoolList();
	return;
};
//Result of bits
function do_bitsFromChecked(form,use)
{
	var b = "Bit",c = 0;
	for(var i=0;i<25;i++){
		if(document.form[b+i].checked) c |= Math.pow(2,i);
	}
	form.bitResult.value = c;
	form.bitResultHex.value = "0x"+baseChange(c,16);
	if(use){
		var a = form.cstRules.value.split(",");
		a[0] = "0x"+baseChange(c,16);
		form.cstRules.value = a.join(",");
	}
	return;
};
function add_set(form)
{
	if(!form.addSetText.value){
		alert("Please, specify a name.");
		return;
	}
	var x = form.addSetText.value;
	if(typeof(this.pool[x])!=='undefined'){
		alert("Set already exists. Please specify a different name.");
		return;
	}
	count_all_lines(form);
	this.pool[x] = clone(this.stdObj); //deepcopy
	var a = document.createElement("option");
	a.text = x;
	form.Samples.appendChild(a);
	alert("Added new set: "+x);
};
function create_code(form)
{
	if(typeof(Object.keys)!=="undefined"){
		count_all_lines(form); //update
		var o = form.Samples.options[form.Samples.selectedIndex].text; // e.g. Sample name
		var st = clone(this.stdObj); //deepcopy
		var stk = Object.keys(st); // fieldA,fieldB...
		this.cc = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]];
		var stkl = stk.length;
		for(var i=0;i<stkl;i++){
			// compare against this.w
			var k = Object.keys(this.w); // n,v...
			var kl = k.length;
			for(var z=0;z<kl;z++){
				var c = Object.keys(this.w[k[z]]); // e.g. tragedy1
				var cl = c.length;
				for(var y=0;y<cl;y++){
					var root = this.w[k[z]][c[y]]; // holds array
					var equalFlag = true,rootl = root.length;
					for(var x=0;x<rootl;x++){
						if(st[stk[i]].indexOf(root[x])===-1){
							equalFlag = false;
							break;
						}
					}
					if(equalFlag){ // remove entries
						this.cc[i].push("this.w."+k[z]+"."+c[y])
						for(var w=0;w<rootl;w++) st[stk[i]] = arrRemoveByValue(st[stk[i]],root[w]);
					}
				}
			}
		}
		form.outCode.value = o+":"+pp(st); // code output
	} else form.outCode.value = "Browser not compatible.";
};
function arrRemoveByValue(arr,val){
	var arrl = arr.length;
	for(var i=0;i<arrl;i++){
		if(arr[i]===val){
			arr.splice(i,1);
			break;
		}
	}
	return(arr);
};
function pp(object,depth,embedded,spec){
	if(typeof(Object.keys)!=="undefined"){
		typeof(depth)==="number" || (depth = 0);
		typeof(embedded)==="boolean" || (embedded=false);
		var newline = false;
		var spacer = function(depth){
			var spaces = "";
			for(var i=0;i<depth;i++) spaces += "\t";
			return spaces;
		};
		var pretty = "";
		if(typeof(object)==="undefined") pretty += "undefined";
		else if(typeof(object)==="boolean" || typeof(object)==="number" ) pretty += object.toString();
		else if(typeof(object)==="string" && object!=="") pretty += "\"" + object + "\"";
		else if(object===null) pretty += "null";
		else if(object instanceof(Array)){
			if(object.length>0){
				var content = "";
				for(var item in object){
					content += pp(object[item], depth+1)+",";
					if(spec && item==="0") content = content.replace(/\"/g, "");
				}
				content = content.replace(/(?:,|[\n]|[\s])$/, "");
				pretty += "["+content+"]";
			} else pretty += "[]";
		} else if(typeof(object)==="object"){
			if(Object.keys(object).length>0){
				if(embedded) newline = true;
				var content = "";
				for(var key in object){
					if(this.cc && key.substr(0,1)==="f"){
						var ch = key.charCodeAt(5)-65,str = "";
						if(ch>-1 && ch<18){
							for(var q=0;q<this.cc[ch].length;q++){
								if(this.cc[ch][q]) str += ".concat("+this.cc[ch][q]+")";
							}
						} else str = "";
					} else str = "";
					if(key==="custom") content += spacer(depth + 1)+key.toString()+": "+pp(object[key],depth+2,true,true) + str +",\n";
					else content += spacer(depth + 1)+key.toString()+": "+pp(object[key],depth+2,true) + str +",\n";
				}
				content = content.replace(/,\n\s*$/, "").replace(/^\s*/,"");
				pretty += "{\n"+spacer(depth+1)+content+"\n"+spacer(depth)+"},\n";
			} else pretty += "{},\n";
		} else pretty += object.toString();
		return((newline?"\n"+spacer(depth):"") + pretty);
	} else return("Browser not compatible.")
};
// generate sentences
function make_poem(form)
{
	var ra = 0;
	if(document.form.retArr.checked) ra = 1;
	if(document.form.useGuards.checked) this.recGuard = true;
	else this.recGuard = false;
	form.outtext.value = "";
	count_all_lines(form);
	var nlines = this.genNums, boo = "", avL = 0;
	if(typeof(performance)!=="undefined" && typeof(performance.now)!=="undefined") var start = performance.now();
	for(var ilin=1;ilin<=nlines;ilin++){
		boo = this.buildPhrase(null,ra,this.seedy,0,1);
		if(ra) form.outtext.value += "P"+(boo[2]+1) + ": "+ boo[0] + "\n" + pp(boo[1]) + "\n";
		else form.outtext.value += "P"+(boo[1]+1) + ": "+ boo[0] + "\n";
		avL += boo[0].length;
	}
	if(typeof(performance)!=="undefined" && typeof(performance.now)!=="undefined") var diff = performance.now() - start;
	else var diff = "--";
	form.outtext.value = form.outtext.value + "\n" + (ilin-1) + " done in: "+diff+" ms. Length"+(ilin-1>1?" (average): ":": ")+(avL/(ilin-1));
	form.nods.value = "@P:"+pp(this.specials.currentSystem)+"\n@S:"+pp(this.specials.rndSystem[this.lastSys]);
	if(document.form.showLast.checked){
		if(ra) form.outtext.value += "\nLast words"+pp(boo[3]);
		else form.outtext.value += "\nLast words"+pp(boo[2]);
	}
};
