function list_getvalue(list,onlyselected,separator)
{
	var result = "";
	if(separator == undefined)
		separator = ";";

	for(var index=0;index < list.length;index++)
	{
		if(onlyselected)
		{
			if(list.options[index].selected)
				result += list.options[index].value + separator;
		}
		else
			result += list.options[index].value + separator;
	}

	if(result != "")
		result = result.substr(0,result.length-separator.length);

	return result;
}

function list_gettext(list,onlyselected,separator)
{
	var result = "";

	for(var index=0;index < list.length;index++)
	{
		if(onlyselected)
		{
			if(list.options[index].selected)
				result += list.options[index].text + separator;
		}
		else
			result += list.options[index].text + separator;
	}

	if(result != "")
		result = result.substr(0,result.length-1);

	return result;
}

function list_inlist(list,onlyselected,value)
{
	var result = false;
	var index = 0;
	while((index < list.length) && (!result))
	{
		if( (onlyselected) && (list.options[index].selected) )
		{
			if(list.options[index].value == value)
				result = true;
		}
		else
			if(list.options[index].value == value)
				result = true;

		index++;
	}
	return result;
}

function list_add(list,value,text)
{
	var index = list.length;
	list.length++;
	list.options[index].value = value;
	list.options[index].text = text;
}

function list_strToComparable(text)
{
	return text.toString().toLowerCase().replace(/\s+/g,"\x20").replace(/ö|ő/g,'o').replace(/ü|ű|ú/g,'u').replace(/é/g,"e").replace(/á/g,"a").replace(/í/g,"i").replace(/[^a-z0-9\x20]/g,"");
}

function list_sortadd(list, value, text)
{
	var l = list.length;
	var e = l;
	var s = 0;
	var option = document.createElement('option');
	option.innerHTML = text;
	option.value = value;
	var index = -1;
	var value = list_strToComparable(value);

	do
	{
		var c = Math.floor((e+s)/2);

		if( (c>0) && (value < list_strToComparable(list.options[c-1].value)) )
			e = c-1;
		else if( (c<l) && (value > list_strToComparable(list.options[c].value)) )
			s = c+2;
		else
			index = ((c==l) || (value < list_strToComparable(list.options[c].value)) ) ?  c : (c+1);
	}
	while( index<0 );

	if( c<l )
		list.insertBefore(option, list.options[index]);
	else
		list.appendChild(option);
}

function list_sortaddtext(list,value,text)
{
	var l = list.length;
	var e = l;
	var s = 0;
	var option = document.createElement('option');
	option.innerHTML = text;
	option.value = value;

	var index = -1;
	var text = list_strToComparable(text);

	do
	{
		var c = Math.floor((e+s)/2);

		if( (c>0) && (text < list_strToComparable(list.options[c-1].text) ) )
			e = c-1;
		else if( (c<l) && (text > list_strToComparable(list.options[c].text) ) )
			s = c+2;
		else
			index = ((c==l) || (text < list_strToComparable(list.options[c].text) ) ) ?  c : (c+1);

	}
	while( index<0 );

	if( c<l )
		list.insertBefore(option, list.options[index]);
	else
		list.appendChild(option);
}

/*
function list_sortadd(list,value,text)
{
	var index = 0;
	var c = 0;

	while((c < list.length) && (list.options[c].value < value))
		c++;

	list.length++;
	for(index=list.length-1;index > c;index--)
	{
		list.options[index].value = list.options[index-1].value;
		list.options[index].text = list.options[index-1].text;
	}

	list.options[c].value = value;
	list.options[c].text = text;
}

function list_sortaddtext(list,value,text)
{
	var index = 0;
	var c = 0;

	while((c < list.length) && (list.options[c].text < text))
		c++;

	list.length++;
	for(index=list.length-1;index > c;index--)
	{
		list.options[index].value = list.options[index-1].value;
		list.options[index].text = list.options[index-1].text;
	}

	list.options[c].value = value;
	list.options[c].text = text;
}
*/

function list_getnum(list)
{
	return list.length;
}

function list_getselnum(list)
{
	var result = 0;

	for(var index=0;index < list.length;index++)
		if(list.options[index].selected)
			result++;

	return result;
}

function list_del(list,onlyselected)
{
	if(onlyselected)
	{
		var index = 0;
		while(index < list.length)
		{
			if(list.options[index].selected)
			{
				for(var c=index;c < list.length-1;c++)
				{
					list.options[c].value = list.options[c+1].value;
					list.options[c].text = list.options[c+1].text;
					if(list.options[c+1].selected)
						list.selectedIndex = c;
					else
						list.selectedIndex = -1;

					list.options[c].selected = list.options[c+1].selected;
				}

				list.options[list.length-1] = null;
			}
			else
				index++;
		}
	}
	else
	{
		for(var index=0;index < list.length;index++)
			list.options[index] = null;
		list.length = 0;
	}
}

function list_select(list)
{
	for(var index=0;index < list.length;index++)
		list.options[index].selected = true;
}

function list_selectvalue(list,value)
{
	for(var index=0;index < list.length;index++)
		if(list.options[index].value == value)
			list.options[index].selected = true;
}

function list_unselectvalue(list,value)
{
	for(var index=0;index < list.length;index++)
		if(list.options[index].value == value)
			list.options[index].selected = false;
}

function list_selectSinglevalue(list,value)
{
	for(var index=0;index < list.length;index++)
	{
		if(list.options[index].value == value)
		{
			list.selectedIndex = index;
			return true;
		}
	}

	return false;
}


function list_moveup_option(list,index)
{
	if(index > 0)
	{
		var value = list.options[index-1].value;
		var text = list.options[index-1].text;
		list.options[index-1].value = list.options[index].value;
		list.options[index-1].text = list.options[index].text;
		list.options[index].value = value;
		list.options[index].text= text;
		list.selectedIndex = index-1;
	}
}

function list_movedown_option(list,index)
{
	if(index < list.length-1)
	{
		var value = list.options[index+1].value;
		var text = list.options[index+1].text;
		list.options[index+1].value = list.options[index].value;
		list.options[index+1].text = list.options[index].text;
		list.options[index].value = value;
		list.options[index].text= text;
		list.selectedIndex = index+1;
	}
}

//==========================================================================================================
// Desc: atmasolja az egyik listabol az elemeket egy masikba.Ha onlyselected erteke igaz , akkor csak
//		 azokat masolja at , melyek ki vannak jelolve.
//
//==========================================================================================================
function list_copy(src,dest,onlyselected)
{
	d=dest.length;
	for(c=0;c<src.length;c++)
		if((!onlyselected) || (onlyselected && src.options[c].selected))
		{
			if(dest.options[d])
			{
				dest.options[d].text=src.options[c].text;
				dest.options[d].value=src.options[c].value;
			}
			else
			{
				dest.options[d] = new Option(src.options[c].text);
				dest.options[d] = new Option(src.options[c].text,src.options[c].value);
			}
			d++;
		}
}

//==========================================================================================================
// Desc: atmazgatja az egyik listabol az elemeket egy masikba.Ha onlyselected erteke igaz , akkor csak
//		 azokat mozgatja at , melyek ki vannak jelolve.
//
//==========================================================================================================
function list_move(src,dest,onlyselected)
{
	list_copy(src,dest,onlyselected);

	for(c=src.length-1;c>=0;c--)
		if((!onlyselected) || (onlyselected && src.options[c].selected))
			src.options[c] = null;
}
