// Universal Related Select Menus - v2.02 19991104
// (Dynamically-sized related menus using JS 1.1's new Option cmd)
// by Andrew King aking@internet.com & Michael Guitton saramaca@mail.dotcom.fr
// Copyright (c) 1999 internet.com Corp. All Rights Reserved.
// Originally published and documented at http://www.webreference.com
//
// Modified by Paul Hemetsberger (paul@hemetsberger.com) to fit the special 
// needs of the polar website.
// Update 04.11.2002: remove hidden entries when "-----------" is selected
// Update 01.11.2002: set first option of all dropdowns to blank
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


var baseurl = "";
var v = false;
var m = null;
// Set all menu trees to null to avoid any error in JavaScript 1.0 browsers -->

var urpm = self; // frame work-around
// urpm = parent.dummy; // Set the target to the relevant frame
// Comment out the above if you don't intend to use frames

function get(form)
{
	// loop thru document.forms property and exit w/ current form index
	var num = -1;
	for (var i = 0; i < document.forms.length; i++)
	{
		if (document.forms[i] == form)
		{
			num = i; // save form index
			break;
		}
	}
	return num; // returns current form index
}

function sindex(num, offset, elt)
{
	// sel finds selected index of num + offset's form elt's element
	// in this case elt is always 0, or first select menu in each form
	if ((num + offset) == 0)
	{
		var sel = cat_id; //todo: other 3 main params
	}
	else
	{
		var sel = document.forms[num + offset].elements[elt].selectedIndex - 1; //-1 because of the default blank option
		if (sel < 0)
		{
			sel = 0;
		}
	}
	return sel;
}

function jmp(form, elt)
{
	if(form.name=="f1")
	{
		alert(pf_hinweistext);
		return;
	}
	if (form != null)
	{
		with (form.elements[elt])
		{
			if (options[selectedIndex].value == "" || options[selectedIndex].value == "#")
			{
				//if last box is empty get previous
				jmp(document.forms[get(form)-1],0);
			}
			else if (0 <= selectedIndex)
			{
				urpm.location = baseurl + options[selectedIndex].value;
			}
		}
	}
}

function clear_subselects(num, elt)
{
	if (num != -1)
	{
		num++; // reference next form, assume it follows in HTML
		with (document.forms[num].elements[elt])
		{
			for (var i = options.length - 1; 0 <= i; i--)
			{
				options[i] = null; // null out options in reverse order (bug work-around)
			}
			options[0] = new Option('------------------', '#')
		}  
		if(num < get(document.forms["f4"]))
		{
			// clear subsequent dropdown
			clear_subselects(num, elt);
		}
	}
}

function update(num, elt, m)
{
	// updates submenus - form(num)'s menu options
	// alert("num: "+num+" elt: "+elt+" m: "+m);
	if (num != -1)
	{
		clear_subselects(num, elt);
		num++;
		if (num == 1 || document.forms[num-1].elements[elt].options[document.forms[num-1].elements[elt].selectedIndex].value!="#")
		{
			with (document.forms[num].elements[elt])
			{
				if (num == 1)
				{
					options[0] = new Option(pf_auswahltext[cat_id + 1], '#');
				}
				else
				{
					options[0] = new Option(pf_auswahltext[0], '#');
				}
				for (var i = 0; i < m.length; i++) 
				{
					options[i+1] = new Option(m[i].text, m[i].value); // fill up next menu's items
				}
				options[0].selected = true; // default to 1st menu item, windows bug kludge
			}
		}
	}
}

function relate(form, elt, tree, depth)
{
	// relate submenus based on sel of form - calls update to redef submenus
	if (v)
	{
		var num = get(form); // fetch the current form index
		var a = tree;        // set a to be the tree array
		while (a != null && --depth != -1)
		{
			// traverse menu tree until we reach the corresponding option record
			a = a[sindex(num, -depth, elt)];
		}
		// at depth 3, should end up w/ something like a[i][j][k]
		// where each index holds the value of s(elected )index in related form select elts
		if (a != null && a.length)
		{
			// if a array exists and it has elements,
			// feed update() w/ this record reference
			update(num, elt, a); 
			return;
		}
	}
	// if a hasn't any array elements or new Option unsupported then end up here ;)
	// IR: Sprung nur bei Klick gewünscht
	// jmp(form, elt); // make like a live popup
}

// Internet Explorer 4+ bug fix:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each SELECT, 
// so it gets it wrong.
//
// Thanks to Peter Belesis (pbel@internet.com) for pointing this out.

function resetIE()
{
	for (var i = 0; i < document.forms.length; i++)
	{
		document.forms[i].reset();
	}
}

function pfinit()
{
	changemenu(cat_id);
	document.images["pfimg"].src = pfim[cat_id].src;
	if (document.all)
	{
		resetIE();
	}
}

window.onload = pfinit;

// Check if Option constructor is supported
if ((typeof(Option) + "") != "undefined")
{
	v = true;
}

// This constructor works equally well for 2D,3D and over
function O(text, value, submenu)
{
	this.text = text;
	this.value = value;
	this.length = 0;
	if (submenu != null) 
	{
		// submenu is an array of options...
		for (var i = 0; i < submenu.length; )
		{
			this[i] = submenu[i];
			this.length = ++i;
		}
	}
}

function changemenu(menunum)
{
	//0 = prozesse
	//1 = systeme
	//2 = software
	//3 = konfigurationen
	cat_id=menunum;
	a=m[cat_id];
	update(0, 0, a);
}
