// Copyright Reino Virta/SysDev
var PopupDoc = 'popup.html'

function Execute()
{
	var content;
	if (ContentSel.options[ContentSel.selectedIndex].text == 'Allt')
		content = '*';
	else
		content = ContentSel.options[ContentSel.selectedIndex].value;
	if (PeriodSel.options[PeriodSel.selectedIndex].text == 'Allt')
		parent.Base.Period = '*';
	else
		parent.Base.Period = PeriodSel.options[PeriodSel.selectedIndex].text;
	var grower;
	if (GrowerSel.options[GrowerSel.selectedIndex].text == 'Alla')
		grower = '*';
	else
		grower = GrowerSel.options[GrowerSel.selectedIndex].value;
	var type;
	if (TypeSel.options[TypeSel.selectedIndex].text == 'Allt')
		type = '*';
	else
		type = TypeSel.options[TypeSel.selectedIndex].value;
	var genera;
	if (GeneraSel.options[GeneraSel.selectedIndex].text == "Allt")
		genera = '*';
	else
		genera = GeneraSel.options[GeneraSel.selectedIndex].text;
	var group;
	if (GroupSel.options[GroupSel.selectedIndex].text == "Allt")
		group = '*';
	else
		group = GroupSel.options[GroupSel.selectedIndex].value;
	var hit;
	var value;	
	var l = parent.Base.Plants.length;
	parent.Base.Selection.length = 0;
	for (var i = 0;i < l;i++)
	{
		if (content == '*')
			hit = true;
		else
		{
			hit = false;
			switch (content)
			{
				case '1':
					if (parent.Base.Period == '*')
						hit = parent.Base.Plants[i].Stats.length;
					else
					{
						var m = parent.Base.Plants[i].Stats.length;
						for (var j = 0; j < m; j++)
							if (parent.Base.Plants[i].Stats[j].Year.toString() == parent.Base.Period)
							{
								hit = true;
								j = m;
							}
					}
					break;
				case '2':
					hit = parent.Base.Plants[i].Pictures.length;
					break;
				case '3':
					hit = parent.Base.Plants[i].Growers.length;
					break;
			}
		}
		if (hit)
		{
			if (grower == '*')
				hit = true;
			else
			{
				hit = false;
				var m = parent.Base.Plants[i].Growers.length;
				for (var j = 0; j < m; j++)
					if (parent.Base.Plants[i].Growers[j].Id.toString() == grower)
					{
						hit = true;
						j = m;
					}
			}
			if (hit)
			{
				value = parent.Base.Plants[i].Type.toString();
				if (type == '*' || type <= value && value <= type)
				{
					value = parent.Base.Genera[parent.Base.Plants[i].Genus].Name.charAt(0);
					if (genera == '*' || genera.charAt(0) <= value && value <= genera.charAt(2))
					{
						value = parent.Base.Genera[parent.Base.Plants[i].Genus].Group.toString();
						if (group == '*' || group <= value && value <= group)
							parent.Base.Selection[parent.Base.Selection.length] = i;
					}
				}
			}
		}
	}
	parent.Base.frames[1].window.document.location.reload();	
}

function PopupWindow(id,showPicture,showGrowers)
{
	this.Window = null;
	this.Id = id;
	InitPopupWindow(this,showPicture,showGrowers);
}

function ClosePopupWindows()
{
	var	l = parent.Base.PopupWindows.length;
	for(var i = 0;i < l;i++)
		if (parent.Base.PopupWindows[i] && parent.Base.PopupWindows[i].Window)
		{
			parent.Base.PopupWindows[i].Window.document.all.PopupMainFrameset.onunload = null;
			parent.Base.PopupWindows[i].Window.close();
		}
}

function ClearPopupWindow(index)
{
	if (parent.Base)
		parent.Base.PopupWindows[index] = null;
}

function GetPictures(id)
{
	var picts = new Array();
	if (parent.Base.Plants[id].Pictures)
		for (var i = 0;i < parent.Base.Plants[id].Pictures.length;i++)
			picts[picts.length] = parent.Base.Plants[id].Pictures[i].Id;
	return picts;
}

function GetGrowers(id)
{
	var growers = new Array();
	if (parent.Base.Plants[id].Growers)
		for (var i = 0;i < parent.Base.Plants[id].Growers.length;i++)
			growers[growers.length] = parent.Base.Plants[id].Growers[i].Id;
	return growers;
}

function InitPopupWindow(obj,showPicture,showGrowers)
{
	obj.Pictures = GetPictures(obj.Id);
	obj.Growers = GetGrowers(obj.Id);
	var n = obj.Pictures.length;
	if (n)
	{
		obj.ActivePicture = obj.Pictures[0];
		obj.ShowPicture = showPicture;
	}
	else
	{
		obj.ActivePicture = 0;
		obj.ShowPicture = false;
	}
	obj.ShowThumbnails = 1 < n || ! showPicture && n;
	if (obj.Growers.length)
		obj.ShowGrowers = showGrowers;
	else
		obj.ShowGrowers = false;
}

function RefreshPopupWindow(base,index)
{
	var height = 189;
	var pictFrame = 0;
	if (base.PopupWindows[index].ShowPicture)
	{
		pictFrame = 484;
		height += 408;
	}
	var tnailFrame = 0;
	if (base.PopupWindows[index].ShowThumbnails)
		tnailFrame = 120;
	var growerFrame = 0;
	if (base.PopupWindows[index].ShowGrowers)
	{
		var l = base.PopupWindows[index].Growers.length;
		if (l == 1)
			growerFrame = 31;
		else
			growerFrame = l * 26;
		height += growerFrame;
	}
	var text = base.Genera[base.Plants[base.PopupWindows[index].Id].Genus].Short;
	text += " ";
	text += RemoveHTML(base.Plants[base.PopupWindows[index].Id].Name);
	base.PopupWindows[index].Window.document.title = text;
	base.PopupWindows[index].Window.document.all.PopupMainFrameset.rows = pictFrame + ',*,' + growerFrame;
  base.PopupWindows[index].Window.document.all.PopupInfoFrameset.cols = '*,' + tnailFrame;
	base.PopupWindows[index].Window.resizeTo(652,height);
	if (base.PopupWindows[index].ShowPicture)
		base.PopupWindows[index].Window.open(PopupDoc,'pict');
	base.PopupWindows[index].Window.open(PopupDoc,'info');
	if (base.PopupWindows[index].ShowThumbnails)
		base.PopupWindows[index].Window.open(PopupDoc,'tnail');
	base.PopupWindows[index].Window.open(PopupDoc,'nav');
	if (base.PopupWindows[index].ShowGrowers)
		base.PopupWindows[index].Window.open(PopupDoc,'grower');
}

function ShowPopupWindow(id,showPicture,showGrowers)
{
	var hit = false;
	var	l = parent.Base.PopupWindows.length;
	for(var i = 0;i < l;i++)
		if (parent.Base.PopupWindows[i] && parent.Base.PopupWindows[i].Id == id)
		{
			parent.Base.ActivePopupWindow = i;
			InitPopupWindow(parent.Base.PopupWindows[i],showPicture,showGrowers);
			parent.Base.PopupWindows[i].Window.focus();
			i = l;
			hit = true;
			RefreshPopupWindow(parent.Base,parent.Base.ActivePopupWindow);
		}
	if (! hit)
	{
		for(var i = 0;i < l;i++)
			if (! parent.Base.PopupWindows[i])
			{
				parent.Base.ActivePopupWindow = i;
				parent.Base.PopupWindows[i] = new PopupWindow(id,showPicture,showGrowers);
				i = l;
				hit = true;
			}
		if (! hit)
		{
			parent.Base.ActivePopupWindow = l;
			parent.Base.PopupWindows[l] = new PopupWindow(id,showPicture,showGrowers);
		}
		parent.Base.PopupWindows[parent.Base.ActivePopupWindow].Window = parent.Base.open(PopupDoc,'','width=640,height=158,resizable=yes,scrollbars=no');
		setTimeout('RefreshPopupWindow(parent.Base,parent.Base.ActivePopupWindow)',200);
	}
}

function TogglePicture()
{
	parent.Base.PopupWindows[parent.Index].ShowPicture = ! parent.Base.PopupWindows[parent.Index].ShowPicture;
	if (parent.Base.PopupWindows[parent.Index].ShowPicture)
	{
		var n = parent.Base.PopupWindows[parent.Index].Pictures.length;
		parent.Base.PopupWindows[parent.Index].ShowThumbnails = 1 < n || ! parent.Base.PopupWindows[parent.Index].ShowPicture && n;
	}
	else
		parent.Base.PopupWindows[parent.Index].ShowThumbnails = true;
	parent.Base.ActivePopupWindow = parent.Index;
	RefreshPopupWindow(parent.Base,parent.Index);
}

function ToggleGrower()
{
	parent.Base.PopupWindows[parent.Index].ShowGrowers = ! parent.Base.PopupWindows[parent.Index].ShowGrowers;
	parent.Base.ActivePopupWindow = parent.Index;
	RefreshPopupWindow(parent.Base,parent.Index);
}

function ChangePlant(step)
{
	var n = 0;
	var l = parent.Base.Selection.length;
	var i = 0;
	while (parent.Base.Selection[i] != parent.Id && i < l)
		i++;
	if (i < l)
	{
		n = i + step;
		if (n < 0)
			n = l - 1;
		else if (n == l)
			n = 0;
	}
	parent.Base.PopupWindows[parent.Index].Id = parent.Base.Selection[n];
	parent.Base.PopupWindows[parent.Index].ActivePicture = 0;
	parent.Base.ActivePopupWindow = parent.Index;
	parent.Id = parent.Base.Selection[n];
	InitPopupWindow(parent.Base.PopupWindows[parent.Index],parent.Base.PopupWindows[parent.Index].ShowPicture,parent.Base.PopupWindows[parent.Index].ShowGrowers);
	RefreshPopupWindow(parent.Base,parent.Index);
}

function ChangePicture(pict)
{
	parent.Base.PopupWindows[parent.Index].ActivePicture = pict;
	if (parent.Base.PopupWindows[parent.Index].ShowPicture)
	{
		parent.open(PopupDoc,"pict");
		parent.open(PopupDoc,"info");
	}
	else
	{
		parent.Base.ActivePopupWindow = parent.Index;
		InitPopupWindow(parent.Base.PopupWindows[parent.Index],true,parent.Base.PopupWindows[parent.Index].ShowGrowers);
		parent.Base.PopupWindows[parent.Index].ActivePicture = pict;
		RefreshPopupWindow(parent.Base,parent.Index);
	}
}

function GetText(id,pict)
{
	var text = parent.Base.Genera[parent.Base.Plants[id].Genus].Name.italics();
	text += " ";
	text += parent.Base.Plants[id].Name;
	if (1 < arguments.length && parent.Base.Plants[id].Pictures)
		for (var i = 0;i < parent.Base.Plants[id].Pictures.length;i++)
			if (parent.Base.Plants[id].Pictures[i].Id == pict)
				if (parent.Base.Plants[id].Pictures[i].Text.length)
				{
					text += " ";
					text += parent.Base.Plants[id].Pictures[i].Text;
				}
	text += "<br>";
	if (parent.Base.Plants[id].Text.length)
		text += parent.Base.Plants[id].Text + "<br>";
	return text;
}

function GenusHeader(id)
{
	genus = parent.Base.Genera[id].Name;
	document.write('<tr bgcolor="#ffffcc">');
	document.write('<td><a name="');
	document.write(genus);
	document.write('"></a><font size=-1><b><i>');
	document.write(genus);
	document.write('</i></b></font></td>');
	document.write('<td>&nbsp</td>');
	document.write('<td>&nbsp</td>');
	document.write('<td><font size=-1>Jan</font></td>');
	document.write('<td><font size=-1>Feb</font></td>');
	document.write('<td><font size=-1>Mar</font></td>');
	document.write('<td><font size=-1>Apr</font></td>');
	document.write('<td><font size=-1>Maj</font></td>');
	document.write('<td><font size=-1>Jun</font></td>');
	document.write('<td><font size=-1>Jul</font></td>');
	document.write('<td><font size=-1>Aug</font></td>');
	document.write('<td><font size=-1>Sep</font></td>');
	document.write('<td><font size=-1>Okt</font></td>');
	document.write('<td><font size=-1>Nov</font></td>');
	document.write('<td><font size=-1>Dec</font></td>');
	document.write('</tr>');
}

function PlantRow(id)
{
	document.write('<tr><td><b><font color="#');
	switch(parent.Base.Plants[id].Type)
	{
		case 1:
			document.write('008000');
			break;
		case 2:
			document.write('0000FF');
			break;
		case 3:
			document.write('C00000');
			break;
		default:
			document.write('000000');
	}
	document.write('">•</font></b>&nbsp<a href="javascript:ShowPopupWindow(');
	document.write(id);
	document.write(',false,false)"><font size=-1>');
	document.write(parent.Base.Plants[id].Name);
	document.write('</font></a></td>');
	if (parent.Base.Plants[id].Pictures.length)
	{
		document.write('<td><a href="javascript:ShowPopupWindow(');
		document.write(id);
		document.write(',true,false)"><img src="./graphics/cambria16.jpg" alt="Visa bilder" border=0></a></td>');
	}
	else
		document.write('<td>&nbsp</td>');
	if (parent.Base.Plants[id].Growers.length)
	{
		document.write('<td><a href="javascript:ShowPopupWindow(');
		document.write(id);
		document.write(',false,true)"><img src="./graphics/info16.jpg" alt="Visa odlare" border=0></a></td>');
	}
	else
		document.write('<td>&nbsp</td>');
	var months = new Array(0,0,0,0,0,0,0,0,0,0,0,0);
	var value;
	var l = parent.Base.Plants[id].Stats.length;
	for (var i = 0;i < l;i++)
	{
		value = parent.Base.Plants[id].Stats[i].Year.toString();
		if (parent.Base.Period == '*' || parent.Base.Period <= value && value <= parent.Base.Period)
			for (var j = 0;j < 12;j++)
				months[j] += parent.Base.Plants[id].Stats[i].Months[j];
	}
	for (var i = 0;i < 12;i++)
		if (months[i])
		{
			document.write('<td><font size=-1>');
			document.write(months[i]);
			document.write('</font></td>');
		}
		else
			document.write('<td>&nbsp</td>');
	document.write('</tr>');
}

function GrowerRow(doc,pers,text)
{
	doc.write('<tr><td><font size=-1>');
	doc.write(parent.Base.Persons[pers].Name);
	doc.write('</font></td><td><font size=-1>');
	doc.write(parent.Base.Persons[pers].City);
	doc.write('</font></td><td><font size=-1>');
	doc.write(text);
	doc.write('</font></td><td>');
	if (parent.Base.Persons[pers].Mail)
	{
		doc.write('<a href="mailto:');
		doc.write(parent.Base.Persons[pers].Mail);
		doc.write('"><font size=-1>');
		doc.write(parent.Base.Persons[pers].Mail);
		doc.write('</font></a>');
	}
	doc.write('</td></tr>');
}

function ShowGrowers(doc,id)
{
	var l = parent.Base.Plants[id].Growers.length;
	for (var i = 0;i < l;i++)
		GrowerRow(doc,parent.Base.Plants[id].Growers[i].Id,parent.Base.Plants[id].Growers[i].Text);
}

function ShowData()
{
	document.write('<table align="center" border cols=15><colgroup span=1 bgcolor="#ffffcc" width="*"><colgroup span=2 align="center" width="18"><colgroup span=12 align="right" width="5%">');
	var genus = -1;
	var l = parent.Base.Selection.length;
	for (var i = 0;i < l;i++)
	{
		var id = parent.Base.Selection[i];
		if (parent.Base.Plants[id].Genus != genus)
		{
			genus = parent.Base.Plants[id].Genus;
			GenusHeader(genus);
		}
		PlantRow(id);
	}
	document.write('</table>');
}

function FileName(num)
{
	var filename = "00000";
	filename += num;
	filename = filename.substring(filename.length-6,filename.length);
	return filename;
}

function RemoveHTML(text)
{
	var temp = "";
	for (var i = 0; i < text.length;i++)
	{
		if (text.charAt(i) == '<')
		{
			while (text.charAt(i) != '>')
				i++;
			i++;
		}
		temp += text.charAt(i);
	}
	return temp;
}

