/* 
Usage Example: 
<input type=hidden name="1polku[c:\asd.txt]" value="1" sort="1asd" sortname="c:\asd.txt">
<input type=hidden name="3polku[c:\asd2.txt]" value="4" sort="1asd" sortname="c:\asd2.txt">
<input type=hidden name="2polku[c:\asd3.txt]" value="2" sort="1asd" sortname="c:\asd3.txt">
<input type=hidden name="4polku[c:\asd4.txt]" value="6" sort="1asd" sortname="c:\asd4.txt">
<input type=hidden name="5polku[c:\asd5.txt]" value="7" sort="1asd" sortname="c:\asd5.txt">
<input type=hidden name="6polku[c:\asd6.txt]" value="8" sort="1asd" sortname="c:\asd6.txt">
<select size="7" multiple sort="1asd" style="width:200px;"></select>
*/
window.oSortSelect = {
	swapValues:function(a,b){var t=a.value;a.value=b.value;b.value=t;a.xorigval.value=a.value;b.xorigval.value=b.value;},
	up:function(id)
	{
		var p=false;
		var el=document.getElementById(id);
		for(var i=0;i<el.options.length;i++)
		{
			if(el.options[i].selected)
			{
				if(i>0&&!p)
				{
					oSortSelect.swapValues(el.options[i],el.options[i-1]);
					el.insertBefore(el.options[i],el.options[i-1]);
					i--;
				}
				p=true;
			}
			else p=false;
		}
	},
	down:function(id)
	{
		var p=false;
		var el=document.getElementById(id);
		for(var i=el.options.length-1;i>=0;i--)
		{
			if(el.options[i].selected)
			{
				if(i<el.options.length-1&&!p)
				{
					oSortSelect.swapValues(el.options[i],el.options[i+1]);
					if(i==el.options.length-2)el.appendChild(el.options[i]);
					else el.insertBefore(el.options[i],el.options[i+2]);
					i++;
				}
				p=true;
			}
			else p=false;
		}
	},
	createButton:function(value,width,listener)
	{
		var b=document.createElement("INPUT");
		b.type="button";
		b.value=value;
		b.style.width=width;
		if(b.addEventListener){eval("b.addEventListener('click',"+listener+",false);");}
		else if(b.attachEvent){eval("b.attachEvent('onclick',"+listener+");");}
		return b;
	},
	slist:new Array(),
	init:function()
	{
		var s=document.getElementsByTagName("INPUT");
		for(var i=0;i<s.length;i++)
		{
			if(s[i].type=="hidden"||true)
			{
				var t=s[i].getAttribute("sort");
				if(t!=null&&t!=undefined&&t!="")
				{
					if(!in_array(t,oSortSelect.slist))
					{
						oSortSelect.slist.push(t);
						eval("oSortSelect.slist_"+t+"=new Array();");
					}
					eval("oSortSelect.slist_"+t+".push(s[i]);");
				}
			}
		}
		var s=document.getElementsByTagName("SELECT");
		for(var i=0;i<s.length;i++)
		{
			var t=s[i].getAttribute("sort");
			if(t!=null&&t!=undefined&&t!="")
			{
				if(in_array(t,oSortSelect.slist))
				{
					s[i].id="sortselect"+i;
					var b=oSortSelect.createButton("+",s[i].style.width,"function(){oSortSelect.up('"+s[i].id+"');}");
					s[i].parentNode.insertBefore(b,s[i]);
					s[i].parentNode.insertBefore(document.createElement("BR"),s[i]);

					var b=oSortSelect.createButton("-",s[i].style.width,"function(){oSortSelect.down('"+s[i].id+"');}");
					if(s[i].nextSibling!=null)s[i].parentNode.insertBefore(b,s[i].nextSibling);
					else document.appendChild(b);
					s[i].parentNode.insertBefore(document.createElement("BR"),b);

					eval("var l=oSortSelect.slist_"+t+";");
					for(var j=0;j<l.length;j++)
					{
						var o=document.createElement("OPTION");
						o.xorigval=l[j];
						o.value=l[j].value;
						o.text=l[j].getAttribute("sortname");
						for(var k=0;k<s[i].options.length;k++)
						{
							if(o.value<s[i].options[k].value)
							{
								s[i].add(o,(document.all?k:s[i].options[k]));
								break;
							}
						}
						if(k>=s[i].options.length)s[i].add(o,(document.all?k:null));
					}
				}
			}
		}
	}
}
if(window.addEventListener){window.addEventListener('load',oSortSelect.init,false);}else if(window.attachEvent){window.attachEvent('onload',oSortSelect.init);}
