/**
* <script>
*
* EasyGUI Multiple Column Combobox Script
*
* @author 		Gaetan Lauff	<glauff@plansoft.de>
* @copyright	Plan Software GmbH, 2001 - 2004
* @package 		Base
*
* This is not free software.
*/

function getTargetList(id) {
	;
}

function setMultipleItemList(id) {
	var combobox 	= document.getElementById(id);
	
	for (var i=0; i < document.isycat.length; i++) {
		if (document.isycat[i].name == id + '[]') {
			targetlist = document.isycat[i];
		}
	}
	
	for (var j = targetlist.options.length; j >= 0; j--) {
		targetlist.options[j] = null;
	}
	
	
	if ((itemList = combobox.getAttribute('selected')) != null) {
		itemList = itemList.split(',');
		
		for (i=0; i < itemList.length; i++) {
			for (j=0; j < combobox.length; j++) {
				if (itemList[i] == combobox.options[j].value) {
					targetlist.options[i] = new Option(combobox.options[j].text, combobox.options[j].value, false, false);
				}
			}
		}
	}
}

function transferSourceItems(evt) {
	var element = getElement(evt);
	var id 		= element.getAttribute('listId');
	var sourcelist 	= document.isycat[id + '_SOURCELIST'];
	
	for (var i=0; i < document.isycat.length; i++) {
		if (document.isycat[i].name == id + '[]') {
			targetlist = document.isycat[i];
		}
	}
	
	for ( var i = 0; i < sourcelist.length; i++ ) {
		if ( sourcelist.options[i].selected ) {	
			idx = targetlist.length;
			isInList = false;
			compare = sourcelist.options[i].value;
			
			for (var j=0; j < targetlist.length; j++) {
				if (compare == targetlist.options[j].value) {
					isInList = true;
				}
				
				targetlist.options[j].selected = true;
			}
			
			if (!isInList) {
				targetlist.options[idx] = new Option(sourcelist.options[i].text, sourcelist.options[i].value, false, false);
				targetlist.options[idx].selected = true;
			}			
		}
	}
	
	sourcelist.selectedIndex = null;
}

function transferTargetItems(evt) {
	var element		= getElement(evt);
	var id 			= element.getAttribute('listId');
	var j			= 0;

	for (var i=0; i < document.isycat.length; i++) {
		if (document.isycat[i].name == id + '[]') {
			targetlist = document.isycat[i];
		}
	}
	
	for (i = targetlist.length - 1; i >= 0; i--) {
		if (targetlist.options[i].selected) {
			targetlist.options[i] = null;
			i = targetlist.length;
		}
	}
}

function selectAll(id) {
	for (var i=0; i < document.isycat.length; i++) {
		if (document.isycat[i].name == id + '[]') {
			targetlist = document.isycat[i];
		}
	}
		
	for (var i = 0; i < targetlist.length; i++) {
		targetlist.options[i].selected = true;
	}
	
	return true;
}

function disableMultipleSelectionList(id) {
	componentHide(id + "_TARGET_BUTTON");
	componentHide(id + "_SOURCE_BUTTON");
}

function enableMultipleSelectionList(id) {
	componentShow(id + "_TARGET_BUTTON");
	componentShow(id + "_SOURCE_BUTTON");
}

function renderMultipleList(id) {
	combobox = document.isycat[id];
	combobox.id = id + '_SOURCELIST';
	combobox.name = id + '_SOURCELIST';
	
	combobox = document.isycat[id + '_SOURCELIST'];

	combobox.style.width = parseInt(combobox.style.width) / 2 - 25;
		
	var targetlist = document.createElement('SELECT');
	
	targetlist.id 			= id;
	targetlist.name			= id + "[]";
	
	targetlist.style.position = 'absolute';
	targetlist.style.left 	= parseInt(combobox.style.left) + parseInt(combobox.style.width) + 50;
	targetlist.style.top  	= parseInt(combobox.style.top);
	targetlist.style.width	= parseInt(combobox.style.width);
	targetlist.style.height = parseInt(combobox.style.height);
	targetlist.multiple   	= 'multiple';
	targetlist.setAttribute('multicol', true);
	
	var transferButton1 = document.createElement('BUTTON');
	var transferButton2	= document.createElement('BUTTON');
	
	transferButton1.id 				= combobox.id + "_TARGET_BUTTON";
	transferButton1.name			= combobox.id + "_TARGET_BUTTON";
	buttonLabel1 					= document.createTextNode(">>");
	transferButton1.appendChild(buttonLabel1);
	transferButton1.style.width 	= '23px';
	transferButton1.style.height 	= '23px';
	transferButton1.style.position	= 'absolute';
	transferButton1.style.top		= parseInt(combobox.style.top) + (parseInt(combobox.style.height) / 2) - 12;
	transferButton1.style.left 		= parseInt(combobox.style.left) + parseInt(combobox.style.width) + 12;
	transferButton1.setAttribute('listId', id);
	
	transferButton2.id 				= combobox.id + "_SOURCE_BUTTON";
	transferButton2.name			= combobox.id + "_SOURCE_BUTTON";
	buttonLabel2 					= document.createTextNode("<<");
	transferButton2.appendChild(buttonLabel2);
	transferButton2.style.width 	= '23px';
	transferButton2.style.height 	= '23px';
	transferButton2.style.position	= 'absolute';
	transferButton2.style.top		= parseInt(combobox.style.top) + (parseInt(combobox.style.height) / 2) + 12;
	transferButton2.style.left 		= parseInt(combobox.style.left) + parseInt(combobox.style.width) + 12;
	transferButton2.setAttribute('listId', id);
	
	addEvent(transferButton1, "click", transferSourceItems, false);
	addEvent(transferButton2, "click", transferTargetItems, false);
	
	combobox.parentNode.appendChild(targetlist);
	combobox.parentNode.appendChild(transferButton1);
	combobox.parentNode.appendChild(transferButton2);
	
	if ((itemList = combobox.getAttribute('selected')) != null) {
		itemList = itemList.split(',');
		
		for (i=0; i < itemList.length; i++) {
			for (j=0; j < combobox.length; j++) {
				if (itemList[i] == combobox.options[j].value) {
					targetlist.options[i] = new Option(combobox.options[j].text, combobox.options[j].value, false, false);
				}
			}
		}
	}
	
	multipleCombos.push(targetlist.id);
}

function setSelectedItemsInMultipleCombobox(id) {
	var combobox = document.isycat[id];

	if ((selectedItems = combobox.getAttribute('selected')) != null) {
		selectedItems = selectedItems.split('|');
	
		for (var i=0; i < combobox.length; i++) {
			if (inArray(combobox.options[i].value, selectedItems)) {
				combobox.options[i].selected = true;
			} else {
				combobox.options[i].selected = false;
			}
		}
	}
	else {
		for (var i=0; i < combobox.length; i++) {
			combobox.options[i].selected = false;
		}
	}
}

function selectAllInMultipleComboboxes() {
	for (var i=0; i < multipleCombos.length; i++) {
		for (var k=0; k < document.isycat.length; k++) {
			if (document.isycat[k].name == multipleCombos[i] + '[]') {		
				combo = document.isycat[k];
			}
		}

		for (var j=0; j < combo.length; j++) {
			combo.options[j].selected = true;
		}
	}
}