//var srcRowsListID;
//var dstRowsListID;
//var dstListMaxLength;
//var dstListMaxLength_Message;


function checkListParams()
{
	if( typeof(srcRowsListID) !== 'string' ) return false;
	if( typeof(dstRowsListID) !== 'string' ) return false;
	if( typeof(hiddenRowsListID) !== 'string' ) return false;
	if( typeof(dstListMaxLength) !== 'number' ) return false;
	
	return true;
}



function dstListMaxLength_ShowMessage()
{
	if( typeof(dstListMaxLength_Message) !== 'string' ) return false;
	if( dstListMaxLength_Message.length < 1 ) return false;
	
	alert(dstListMaxLength_Message + ' ' + dstListMaxLength);
	return true;
}



function prepareHiddenList(sList)
{
    var str = "";
    var next = false;
    
    for(i = 0; i < sList.options.length; i++)
    {
        if( next ) str += ":"; else next = true;
        str += sList.options[i].value;
    }
    
    return str;
}


function showInfoList()
{
    //var currEvent = window.event
    //alert("test");
    //alert(currEvent.type);
}

function showInfoList_clear()
{
    nd();
}


function initSrcList()
{
	if( ! checkListParams() ) return;
	
	var srcList = document.getElementById(srcRowsListID);
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);
	
	
	if( srcList === null || dstList === null || hiddenList === null ) return;
	if( dstList.options.length == 0 ) 
	{
	    hiddenList.value = "";
	    return;
	}
	
	for(i = 0; i < dstList.options.length; i++)
	{
		for(j = 0; j < srcList.options.length; j++)
		{		
			if( dstList.options[i].value !== srcList.options[j].value ) continue;
			srcList.options[j].className = "RowSelect";
			break;
		}
	}
	
	
	
	hiddenList.value = prepareHiddenList(dstList);
}



function addSelectedRow()
{
	if( ! checkListParams() ) return;
	
	var srcList = document.getElementById(srcRowsListID);
	var dstList = document.getElementById(dstRowsListID);
    var hiddenList = document.getElementById(hiddenRowsListID);
	
	if( srcList.options.selectedIndex < 0 || srcList.options.length < 1 )
	{
		return;
	}
	
	if( dstListMaxLength > 0 && dstList.options.length >= dstListMaxLength ) 
	{
		dstListMaxLength_ShowMessage();
		return;
	}
	
	
	var srcSelectOption = srcList.options[ srcList.options.selectedIndex ];
	if( srcSelectOption.value < 1 ) return;
	
	for(i = 0; i < dstList.options.length; i++ )
	{
		if( srcSelectOption.value == dstList.options[i].value ) return;
	}
	
	var opt = new Option();
	opt.value = srcSelectOption.value;
	opt.text  = srcSelectOption.text;
	
	srcList.options[ srcList.options.selectedIndex ].className = "RowSelect";
	dstList.options.add(opt);
	
	hiddenList.value = prepareHiddenList(dstList);
}



function addAllRows()
{
	if( ! checkListParams() ) return;
	
	var srcList = document.getElementById(srcRowsListID);
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);

	if( srcList.options.length < 1 )
	{
		return;
	}
	
	
	for(i = 0; i < srcList.options.length; i++ )
	{
		if( dstListMaxLength > 0 && dstList.options.length == dstListMaxLength ) break;
		
		if( srcList.options[i].value <  1 ) continue;
		if( srcList.options[i].className ===  "RowSelect" ) continue;
		
		var opt = new Option();
		opt.value = srcList.options[i].value;
		opt.text  = srcList.options[i].text;
		
		srcList.options[i].className = "RowSelect";
		dstList.options.add(opt);
	}
	
	hiddenList.value = prepareHiddenList(dstList);

}



function removeSelectedRow()
{
	if( ! checkListParams() ) return;
	
	var srcList = document.getElementById(srcRowsListID);
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);
	
	if( dstList.options.selectedIndex < 0 || dstList.options.length < 1 )
	{
		return;
	}
	
	
	for(i = 0; i < srcList.options.length; i++ )
	{
		if( srcList.options[i].value != dstList.options[ dstList.options.selectedIndex ].value ) continue;
		srcList.options[i].className = "";
	}
	
	var index = dstList.options.selectedIndex;
	dstList.options[ dstList.options.selectedIndex ] = null;
	
	if( dstList.options.length == 0 ) return;
	
	if( index >= dstList.options.length ) index = dstList.options.length - 1;
	dstList.options.selectedIndex = index;
	
	hiddenList.value = prepareHiddenList(dstList);
}



function removeAllRows()
{
	if( ! checkListParams() ) return;
	
	var srcList = document.getElementById(srcRowsListID);
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);
	
	for(i = dstList.options.length - 1; i >= 0; i--)
	{
		dstList.options[i] = null;
	}
	
	for(i = srcList.options.length - 1; i >= 0; i--)
	{
		srcList.options[i].className = "";
	}
    
    hiddenList.value = "";
}


function moveUpRow()
{
	if( ! checkListParams() ) return;
	
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);
	
	if( dstList.options.selectedIndex < 1 || dstList.options.length < 2 )
	{
		return;
	}
	
	var tmpValue = dstList.options[ dstList.options.selectedIndex - 1 ].value;
	var tmpText = dstList.options[ dstList.options.selectedIndex - 1].text;
	
	dstList.options[ dstList.options.selectedIndex - 1 ].value = dstList.options[ dstList.options.selectedIndex ].value;
	dstList.options[ dstList.options.selectedIndex - 1 ].text = dstList.options[ dstList.options.selectedIndex ].text;
	
	dstList.options[ dstList.options.selectedIndex ].value = tmpValue
	dstList.options[ dstList.options.selectedIndex ].text = tmpText

	dstList.options.selectedIndex--;
	hiddenList.value = prepareHiddenList(dstList);
}



function moveDownRow()
{
	if( ! checkListParams() ) return;
	
	var dstList = document.getElementById(dstRowsListID);
	var hiddenList = document.getElementById(hiddenRowsListID);
	
	if( dstList.options.selectedIndex < 0 || dstList.options.length < 1 )
	{
		hiddenList.value = "";
		return;
	}
	
	if( (dstList.options.selectedIndex + 1) == dstList.options.length )
	{
		return;
	}
	

	var tmpValue = dstList.options[ dstList.options.selectedIndex + 1 ].value;
	var tmpText = dstList.options[ dstList.options.selectedIndex + 1].text;
	
	dstList.options[ dstList.options.selectedIndex + 1 ].value = dstList.options[ dstList.options.selectedIndex ].value;
	dstList.options[ dstList.options.selectedIndex + 1 ].text = dstList.options[ dstList.options.selectedIndex ].text;
	
	dstList.options[ dstList.options.selectedIndex ].value = tmpValue
	dstList.options[ dstList.options.selectedIndex ].text = tmpText
	
	dstList.options.selectedIndex++;
	hiddenList.value = prepareHiddenList(dstList);
}



