
function level(lid, lname)
{
	this.lid = lid;
	this.lname = lname;
	this.lcount = 0;
	this.lchildren = new Array();
	
	this.add = function(c) {
		this.lchildren[this.lcount] = c;
		this.lcount++;
	}

	this.fillSelect = function(sel) {
		$("#"+sel.id).empty();

		// first option
		/*opt = document.createElement("option");
		opt.value = '-1';
		opt.zvire = null;
		txt = document.createTextNode('Choose...');
		opt.appendChild(txt);
		sel.appendChild(opt);
		*/
	
				
		for (i=0; i<this.lchildren.length; i++) {
			opt = document.createElement("option");
			opt.value = this.lchildren[i].lid;
			opt.ll = this.lchildren[i];
			txt = document.createTextNode(this.lchildren[i].lname);
			opt.appendChild(txt);
			sel.appendChild(opt);
		}
	}
}

function hint(sel)
{
	if (sel.selectedIndex < 1) {
		// "Choose..." -> clear all SELECTs
		if (sel.lnext) {
			delSel = sel.lnext;
			while (delSel) {
				d = delSel.lnext;
				delSel.parentNode.removeChild(delSel);
				delSel = d;
			}
			sel.lnext = null;
		}
		return;
	}
		
	level = sel.getElementsByTagName('option');
	level = level[sel.selectedIndex];
	levelInstance = level.ll;
		
	//leaf -> nothing todo
	if (levelInstance.lcount < 1) {
		//alert('DEBUG: lcount < 1, lcount: '+levelInstance.lcount);	
	}
	else {
		//something far next to remove?
		if (sel.lnext) {
			levelInstance.fillSelect(sel.lnext);
			delSel = sel.lnext.lnext;
			while (delSel) {
				d = delSel.lnext;
				delSel.parentNode.removeChild(delSel);
				delSel = d;
			}
			sel.lnext.lnext = null;
		}
		else { //no more SELECTs -> create one
			obj = sel.parentNode;
			newSel= document.createElement("select");
			newSel.id = levelInstance.lid;
			obj.appendChild(newSel)
			
			$('#'+newSel.id).bind("change", function(e){hint(this);});
			$('#'+newSel.id).attr('name', sel.name)
			$('#'+newSel.id).css('display', 'block');
			$('#'+newSel.id).css('margin-left', '25%');
			$('#'+newSel.id).css('margin-top', '2px');
					    ;
			levelInstance.fillSelect(newSel);
			sel.lnext = newSel;
		}
				
		//only one selection -> select and go next
		if (levelInstance.lcount == 1) {
			sel.lnext.selectedIndex = 1;
			hint(sel.lnext);
		}
		else {
			//else choose - "Choose..."
			sel.lnext.selectedIndex = 0;
		}
	}
}
