
// --------------------------------------------JavaScript SelectionList class-------------------------

function SelectionList(htmlContainerDivElement,sourceList, destinationList,sourceListTitle,destinationListTitle)
{			
	this._containerDivElementID=htmlContainerDivElement.id;
	this.Init(htmlContainerDivElement,sourceList,destinationList,sourceListTitle,destinationListTitle);		
}

SelectionList.prototype.ControlButton_Click = function (oEvent)
{
	var obj=window.event?oEvent.srcElement:this;				
	var objType=(GetActualObjectType(obj.id));
	
	var sourceList=GetElement(obj.id,'sourceList');
	
	var destinationList=GetElement(obj.id,'destinationList');
	
	var aux=new Array();
	var j=0;
	
	switch(objType)
	{				
		case "add":						
			for(var i=0; i<sourceList.options.length;i++)
			{
				if(sourceList.options[i].selected)
				{				
					destinationList.options.add(new Option(sourceList.options[i].text,sourceList.options[i].value));//sourceList.options[i]);															
					//destinationList.options[destinationList.options.length]=sourceList.options[i];					
					aux[aux.length]=destinationList.options[destinationList.options.length-1];
				}				
			}
			for(var i=0; i<sourceList.options.length;i++)
			{
				if(aux.length<=j) break;
				if(sourceList.options[i].value==aux[j].value)
				{										
					sourceList.options[i]=null;					
					i--;
					j++;
				}				
			}		
			obj.disabled=true;	
			GetElement(obj.id,'removeAll').disabled=false;
		break;
		case "addAll":
			for(var i=0; i<sourceList.options.length;i++)
			{			
				//destinationList.options[destinationList.options.length]=sourceList.options[i];	
				destinationList.options.add(new Option(sourceList.options[i].text,sourceList.options[i].value));
				//i--;		
			}
			while(sourceList.options.length>0)			
				sourceList.options[0]=null;			
			
			obj.disabled=true;
			GetElement(obj.id,'add').disabled=true;
			GetElement(obj.id,'removeAll').disabled=false;										
		break;
		case "remove":
			for(var i=0; i<destinationList.options.length;i++)
			{
				if(destinationList.options[i].selected)
				{				
					sourceList.options.add(new Option(destinationList.options[i].text,destinationList.options[i].value));									
					aux[aux.length]=sourceList.options[sourceList.options.length-1];
				}				
			}
			for(var i=0; i<destinationList.options.length;i++)
			{
				if(aux.length<=j) break;
				if(destinationList.options[i].value==aux[j].value)
				{										
					destinationList.options[i]=null;					
					i--;
					j++;
				}				
			}			
			obj.disabled=true;
			GetElement(obj.id,'addAll').disabled=false;										
		break;
		case "removeAll":
			for(var i=0; i<destinationList.options.length;i++)
			{			
				//destinationList.options[destinationList.options.length]=sourceList.options[i];	
				sourceList.options.add(new Option(destinationList.options[i].text,destinationList.options[i].value));
				//i--;		
			}
			while(destinationList.options.length>0)			
				destinationList.options[0]=null;	
				
			obj.disabled=true;			
			GetElement(obj.id,'remove').disabled=true;	
			GetElement(obj.id,'addAll').disabled=false;										
		break;	
	}
}

SelectionList.prototype.List_Click = function(oEvent)
{
	var obj=window.event?oEvent.srcElement:this;				
}

function GetActualObjectType(id)
{
	var idReverse=id.reverse();
	index=idReverse.indexOf('_');	
	index=idReverse.length-index;		
	var objType=id.substring(index);
	return objType;
}

function GetElement(id,elementUID)
{
	var index=0;
	var idReverse=id.reverse();
	index=idReverse.indexOf('_');	
	index=idReverse.length-index;		
	var newId=id.substring(0,index) + elementUID;		
	var el=document.getElementById(newId);
	return el;
}

SelectionList.prototype.List_Change = function(oEvent)
{
	var obj=window.event?oEvent.srcElement:this;		
	var actualList=obj.id.indexOf('_sourceList')>0?'source':'destination';
	if(actualList=='source')
	{
		if(obj.options.length==0)
		{
			//disable buttons
			GetElement(obj.id,'add').disabled=true;
			GetElement(obj.id,'addAll').disabled=true;
		}
		else
		{
			//disable buttons
			GetElement(obj.id,'add').disabled=false;
			GetElement(obj.id,'addAll').disabled=false;
		}		
	}	
	else
	{
		if(obj.options.length==0)
		{
			//disable buttons
			GetElement(obj.id,'remove').disabled=true;
			GetElement(obj.id,'removeAll').disabled=true;
		}
		else
		{
			//disable buttons
			GetElement(obj.id,'remove').disabled=false;
			GetElement(obj.id,'removeAll').disabled=false;
		}		
	}	
}


String.prototype.reverse = function() {
    var s = "";
    var i = this.length;
    while (i>0) {
        s += this.substring(i-1,i);
        i--;
    }
    return s;
}


SelectionList.prototype.Init = function(htmlContainerDivElement,sourceList, destinationList,sourceListTitle,destinationListTitle)
{		

	var table_tdS = document.createElement("TD");
	var table_tdD = document.createElement("TD");

	this._sourceList = document.createElement("select");	
	this._sourceList.size=7;	
	this._sourceList.style.width=150;			
	
	this.InitListBox(this._sourceList,htmlContainerDivElement.id + '_sourceList');					
	var auxSourceListText=document.createElement("input");
	auxSourceListText.type = "hidden";
	auxSourceListText.name = this._sourceList.name + '_lstValues';
	auxSourceListText.id = this._sourceList.id + '_lstValues';
	
	
	this.AddElementsToList(this._sourceList,sourceList);
	table_tdS.appendChild(this._sourceList);
	table_tdS.appendChild(auxSourceListText);
	var sourceListControl=new HTMLObject(this._sourceList);
	sourceListControl.AddEventListener('click',this.List_Click);
	sourceListControl.AddEventListener('change',this.List_Change);
	
	
	var table_trHeader=document.createElement("TR");
	var table_tdHeaderDestination = document.createElement("TD");
	table_tdHeaderDestination.align='center';
	table_tdHeaderDestination.innerHTML=destinationListTitle;
	
	var table_tdHeaderCenter = document.createElement("TD");
	table_tdHeaderCenter.align='center';
		
	var table_tdHeaderSource = document.createElement("TD");
	table_tdHeaderSource.align='center';
	table_tdHeaderSource.innerHTML=sourceListTitle;
	
	table_trHeader.appendChild(table_tdHeaderDestination);
	table_trHeader.appendChild(table_tdHeaderCenter);
	table_trHeader.appendChild(table_tdHeaderSource);
	
	
	
	
	this.AddOptionButtons(htmlContainerDivElement,table_tdS,table_tdD, table_trHeader);
		
	
	this._destinationList = document.createElement("select");	
	this._destinationList.size=7;		
	this._destinationList.style.width=150;	
	this.InitListBox(this._destinationList,htmlContainerDivElement.id + '_destinationList');	
	var auxDestinationListText=document.createElement("input");
	auxDestinationListText.type = "hidden";
	auxDestinationListText.name = this._destinationList.name + '_lstValues';
	auxDestinationListText.id = this._destinationList.id + '_lstValues';
					
	this.AddElementsToList(this._destinationList,destinationList);							
	table_tdD.appendChild(this._destinationList);	
	table_tdD.appendChild(auxDestinationListText);
	
	var destinationListControl=new HTMLObject(this._destinationList);
	destinationListControl.AddEventListener('click',this.List_Click);
	destinationListControl.AddEventListener('change',this.List_Change);
	

	if(GetElement(htmlContainerDivElement.id + '_sourceList','sourceList').options.length>0)
	{			
		if(navigator.appName!='Netscape') GetElement(htmlContainerDivElement.id + '_add','add').disabled=false;
		GetElement(htmlContainerDivElement.id + '_addAll','addAll').disabled=false;
	}
	if(GetElement(htmlContainerDivElement.id + '_destinationList','destinationList').options.length>0)
	{			
		if(navigator.appName!='Netscape') GetElement(htmlContainerDivElement.id + '_remove','remove').disabled=false;
		GetElement(htmlContainerDivElement.id + '_removeAll','removeAll').disabled=false;
	}

}

SelectionList.prototype.AddElementsToList = function(htmlListBox, list)
{
	for(var i=0; i<list.length;i++)
	{		
		htmlListBox.options[htmlListBox.options.length] = new Option(list[i][1], list[i][0]);
	}

}
SelectionList.prototype.InitListBox = function(htmlListBox,id)
{
	htmlListBox.name = id;
	htmlListBox.id = id;
	htmlListBox.multiple = true;
	
}

SelectionList.prototype.AddOptionButtons = function(htmlContainerDivElement, table_tdS, table_tdD, table_trHeader)
{
	var table = document.createElement("TABLE");
	if(navigator.appName=='Netscape')
		table.style.tableLayout='fixed';
	var tbody = document.createElement("TBODY");
	
	
	//var table_trHeader = document.createElement("TR");
	
	
	tbody.appendChild(table_trHeader);
	
	
	var table_tr1 = document.createElement("TR");
		
	table_tdS.rowSpan='4';
	table_tdS.align='center';
	table_tdD.rowSpan='4';
	table_tdD.align='center';
	
	var table_td1 = document.createElement("TD");
	table_td1.align='center';
			
	table_tr1.appendChild(table_tdS);	
	table_tr1.appendChild(table_td1);	
	table_tr1.appendChild(table_tdD);	
	tbody.appendChild(table_tr1);
	
	var table_tr2 = document.createElement("TR");
	var table_td21 = document.createElement("TD");
	table_td21.align='center';
	table_tr2.appendChild(table_td21);	
	tbody.appendChild(table_tr2);
	
	var table_tr3 = document.createElement("TR");
	var table_td31 = document.createElement("TD");
	table_td31.align='center';
	table_tr3.appendChild(table_td31);	
	tbody.appendChild(table_tr3);

	var table_tr4 = document.createElement("TR");
	var table_td41 = document.createElement("TD");
	table_td41.align='center';
	table_tr4.appendChild(table_td41);	
	tbody.appendChild(table_tr4);
			
	this.AddButton('add',table_td1,htmlContainerDivElement.id);
	this.AddButton('addAll',table_td21,htmlContainerDivElement.id);
	this.AddButton('remove',table_td31,htmlContainerDivElement.id);
	this.AddButton('removeAll',table_td41,htmlContainerDivElement.id);
	
	
	table.appendChild(tbody);	
	htmlContainerDivElement.appendChild(table);

}

SelectionList.prototype.AddButton = function(buttonType,containerElement,id)
{
	var button=document.createElement('input');
	button.type='button';
	switch(buttonType)
	{
		case "add":
			button.value=" > ";
			break;
		case "remove":
			button.value=" < ";
			break;
		case "addAll":
			button.value=">>";
			break;
		case "removeAll":
			button.value="<<";
			break;
		default:
			button.value="?";
			break;	
	}
	button.width=50;
	button.className='btn';	
	button.id=id + '_' + buttonType;
	button.disabled=true;
	containerElement.appendChild(button);
	
	var buttonControl=new HTMLObject(button);
	buttonControl.AddEventListener('click',this.ControlButton_Click);
	
	return button;
}

// --------------------------------------------JavaScript SelectionList class End-------------------------
