/**
* <script>
*
* EasyGUI Enhanced Combobox Script
*
* @author 		Gaetan Lauff	<glauff@plansoft.de>
* @copyright	Plan Software GmbH, 2001 - 2004
* @package 		Base
*
* This is not free software.
*/

/**
* Constructor
*/
function enhancedcombobox(id) {
	this.id = id;
	this.selectedIndex = 0;
	this.selectedId = null;
	this.value 	= '';
	this.text	= '';
	this.textcolor 	= 'black';
	this.bgcolor	= 'white';
	this.highlightcolor = '#316ac5';
	this.highlighttextcolor = 'white';
	this.combobuttonsrc = 'gfx/combobut_xp.gif';
	this.combobuttonhlsrc = 'gfx/combobut_xp_hl.gif';
	this.active	= false;
	this.disabled = false;
	this.opts	= new Array();
	
	this.Button = '';
	this.Text	= '';
	this.Items	= '';
	this.Rows	= '';
	this.Head	= '';
}

/**
* Combo Entry Class
*/
function comboEntry(value, texts) {
	this.value = value;
	this.texts = texts;
}

function renderEnhancedCombobox(id) {
	var combobox 	= document.isycat[id];
	
	if ((displaycol = combobox.getAttribute('displaycol')) == null) {
		displaycol = 1;
	}
		
	var comboContainer 			= document.createElement('DIV');
		
	comboContainer.id 			= id + '_CONTAINER';
	comboContainer.style.left 	= combobox.style.left;
	comboContainer.style.top	= combobox.style.top;
	comboContainer.style.position = 'absolute';
			
	var comboHead				= document.createElement('TABLE');
	
	comboHead.id				= id + '_HEAD';
	comboHead.cellSpacing		= 0;
	comboHead.cellPadding		= 0;
	comboHead.border			= 0;
	comboHead.style.position	= 'relative';
	comboHead.style.tableLayout = 'fixed';
	comboHead.style.width		= combobox.style.width;
	
	if (combobox.className == null || combobox.className == '') {
		if (is_winxp) {
			comboHead.className = 'easygui-combobox-head-xp';
		} else {
			comboHead.className = 'easygui-combobox-head';
		}
	} else {
		comboHead.className = 	combobox.className;
	}
		
	var comboHeadRow = comboHead.insertRow();
	var comboHeadRowCell = comboHeadRow.insertCell();
		
	comboHeadRowCell.style.height 	= '22px';
	comboHeadRowCell.style.border 	= '1px solid #7f9db9';
	comboHeadRowCell.style.borderRight = 'none';
				
	var comboText 			= document.createElement('INPUT');
			
	comboText.type 		= 'text';
	comboText.name 		= id + '_TEXT';
	comboText.id		= id + '_TEXT';
	comboText.readonly 	= 'readonly';
	comboText.value 	= '';
			
	comboText.setAttribute('comboboxId', id);
	comboText.style.width 	 = combobox.style.width;
	comboText.style.border 	 = 'none';
	comboText.style.height 	 = '17px';
	comboText.style.position = 'relative';
	
	if ((textStyle = combobox.getAttribute('textstyle')) != null) {
		comboText.className = textStyle;
	}
	
	comboHeadRowCell.appendChild(comboText);
	
	var comboButton			= document.createElement('IMG');
	comboButton.style.position = 'relative';
		
	comboButton.id		= id + '_BUTTON';
	
	comboButton.setAttribute('comboboxId', id);	
	comboButton.src = combobox.getAttribute('combobuttonsrc');
	comboButton.setAttribute('mouseoverimage', combobox.getAttribute('combobuttonhlsrc'));
	
	if (is_winxp) {
		addEvent(comboButton, "mouseover", flipImage, false);
		addEvent(comboButton, "mouseout", flipImage, false);
	} else {
		addEvent(comboButton, "mousedown", flipImage, false);
		addEvent(comboButton, "mouseup", flipImage, false);
	}
		
	addEvent(comboButton, "click", openCombo, false);
	addEvent(comboText, "click", openCombo, false);
	
	searchable = null;
	editable = null;
	
	if ((editable = combobox.getAttribute('editable')) == null) {
		addEvent(comboText, "focus", blurElement, false);
	} else {
		addEvent(comboText, "change", setSubmitValue, false);
		addEvent(comboText, "blur", closeAllCombos, false);
	}

	var comboHeadRowCell				= comboHeadRow.insertCell();
	
	comboHeadRowCell.style.border 		= '1px solid #7f9db9';
	comboHeadRowCell.style.borderLeft 	= 'none';
	comboHeadRowCell.style.height 		= '22px';
	comboHeadRowCell.style.width		= '18px';
	comboHeadRowCell.style.textAlign 	= 'center'
	comboHeadRowCell.style.padding		= '1px';
	
	comboHeadRowCell.appendChild(comboButton);
		
	var comboItems	= document.createElement('DIV');
			
	comboItems.id 				= id + "_ITEMS";
	comboItems.style.overflow 	= 'auto';
	comboItems.style.visibility = 'hidden';
	1
	if ((itemStyle = combobox.getAttribute('itemstyle')) == null) {
		comboItems.className = 'easygui-combobox-items';
	} else {
		comboItems.className = itemStyle;
	}
			
	var comboRows				= document.createElement('TABLE');
		
	comboRows.id			= id + '_ROWS';
	comboRows.cellPadding 	= 0;
	comboRows.cellSpacing 	= 0;
	
	if ((rowStyle = combobox.getAttribute('rowstyle')) == null) {
		comboRows.className = 'easygui-combobox-rows';
	} else {
		comboRows.className = rowStyle;
	}

	comboItems.appendChild(comboRows);
	comboContainer.appendChild(comboHead);
	comboContainer.appendChild(comboItems);
	comboItems.style.position = 'relative';	
	combobox.parentNode.appendChild(comboContainer);
	comboText.style.width = parseInt(comboText.style.width) - 20;
	
	if ((items = combobox.getAttribute('items')) != null) {
		var optionList;
		var itemList = items.split("|");
		var selectedIndex = 0;
		
		eval (id + " = new Array();");
		
		var comboArray = eval(id);
		
		for (var i=0; i < itemList.length; i++) {	
			optionList = itemList[i].split("=");
			
			optionValue = optionList[0];
			optionTexts = optionList[1].split("#");
			
			optionText = new Array();
			
			for (var k=0; k < optionTexts.length; k++) {
				myText = optionTexts[k].replace(/&equal;/g, '=');
				optionText[optionText.length] = myText;
			}
			
			comboArray[i] = new comboEntry(optionValue, optionText);
		}
	}
}

function initEnhancedCombobox(id) {	
	enhancedCombosInit=null;
	combopresent = getCombobox(id);
	
	var combobox = document.getElementById(id);
	
	combobox.style.top = '-1000px';
	
	setEnhancedComboboxItems(id);
	
	var combo = new enhancedcombobox(id);
	
	combo.Button 	= document.getElementById(id + '_BUTTON');
	combo.Text		= document.getElementById(id + '_TEXT');
	combo.Items		= document.getElementById(id + '_ITEMS');
	combo.Rows		= document.getElementById(id + '_ROWS');
	combo.Head		= document.getElementById(id + '_HEAD');
	combo.options	= eval(id);
	
	if ((droppedWidth = combobox.getAttribute('droppedwidth')) == null) {
		if (parseInt(combo.Rows.offsetWidth) < parseInt(combo.Head.style.width)) {
			combo.Items.style.width = combo.Head.style.width;
		}
	} else {
		combo.Items.style.width = droppedWidth;
	}
	
	if ((droppedHeight = combobox.getAttribute('droppedheight')) != null) {
		combo.Items.style.height = droppedHeight;
	} else {
		combo.Items.style.height = '100';
	}
	
	if (!is_winxp) {
		combo.highlightcolor = '#0a246a';
	}
	
	if (parseInt(combo.Rows.offsetHeight) < parseInt(combo.Items.offsetHeight)) {
		combo.Items.style.height = parseInt(combo.Rows.offsetHeight) + 2;
	}
		
	combobox.setAttribute('isytype', 'combobox');
	
	if ((selectedIdx = combobox.getAttribute('selected')) != null && selectedIdx != '') {
		combo.selectedId = null;
		
		for (i=0;  i < combo.Rows.rows.length; i++) {
			if (combo.Rows.rows[i].value == selectedIdx) {
				colIdx = parseInt(combobox.getAttribute('displaycol')) - 1;
				combo.Text.value = combo.Rows.rows[i].cells[colIdx].text;
				document.isycat[combobox.id].value = selectedIdx;
				combo.selectedId = combo.Rows.rows[i].id;
			}
		}
	}

	if ((onchanged = String(combobox.getAttribute('onchange'))) != 'null') {
		var mystart = parseInt(onchanged.indexOf('{')) + 1;
		var myend = onchanged.lastIndexOf('(');
		var onchangedHandler = onchanged.substring(mystart, myend);
		addEvent(combo.Rows, "click", eval(onchangedHandler), false);
	}
	
	enhancedCombos[enhancedCombos.length] = combo;
		
	if (combo.selectedId != null) {
		itemSelect(combo.selectedId);
	}
}

function setEnhancedComboboxItems(id) {
	var 	combobox = document.getElementById(id);
	var 	comboItems = document.getElementById(id + '_ITEMS');
	
	comboItems.innerHTML = '';
	
	var comboRows				= document.createElement('TABLE');
		
	comboRows.id			= id + '_ROWS';
	comboRows.cellPadding 	= 0;
	comboRows.cellSpacing 	= 0;
	
	if ((rowStyle = combobox.getAttribute('rowstyle')) == null) {
		comboRows.className = 'easygui-combobox-rows';
	} else {
		comboRows.className = rowStyle;
	}
	
	comboArray 	= eval(id + ";");
	
	for (i=0; i < comboArray.length; i++) {
		var comboEntry 		= comboArray[i];
		var comboRows_TR 	= comboRows.insertRow();
		
		comboRows_TR.id  = id + "_ROW_" + i;
		comboRows_TR.setAttribute('value', comboEntry.value);
		
		for (var j=0; j < comboEntry.texts.length; j++) {
			var comboRows_TR_TD = comboRows_TR.insertCell();
			comboRows_TR_TD.id = id + "_CELL_" + i + "_" + j;
			comboRows_TR_TD.innerHTML = comboEntry.texts[j];
			comboRows_TR_TD.setAttribute('text', comboEntry.texts[j]);
		}
	}
	
	for (i=0; i < comboRows.rows.length; i++) {
		addEvent(comboRows.rows[i], "mouseover", itemOver, false);
		addEvent(comboRows.rows[i], "mouseout", itemOut, false);
		addEvent(comboRows.rows[i], "click", itemSelect, false);
		comboRows.rows[i].setAttribute('comboboxId', id);
	}

	comboItems.appendChild(comboRows);
	combo = getCombobox(id);
}


function searchValue(evt) {
	var element = getElement(evt);
	var val = element.value;
	
	var combo = getCombobox(element.comboboxId);
	var comboArray = eval(element.comboboxId);
}

function setSearchableSubmitValue(evt) {
	var element = getElement(evt);
	var val = element.value;
	var combo = getCombobox(element.comboboxId);
}

function setComboboxItems(id) {
	var combobox 	= document.isycat[id];
	
	if ((selected = combobox.getAttribute('selected')) == null) {
		var selected = '';
	}
	
	if (combobox.getAttribute('enhanced') != null) {
		comboArray 	= eval(id + ";");
		
		for (var i=0; i < comboArray.length; i++) {
			var comboEntry = comboArray[i];
			
			if (comboEntry.value == selected) {
				var newOption  = new Option(comboEntry.texts[0], comboEntry.value, false, true);
			} else {
				var newOption  = new Option(comboEntry.texts[0], comboEntry.value, false, false);
			}			
			
			combobox.options[i] = newOption;
		}
		
		enhCombo = getCombobox(id);
		enhCombo.options = comboArray;
		
	} else {
		if ((itemList = combobox.getAttribute('items')) != null) {		
			var optionList;
			var itemList;
			var selectedIndex = 0;
				
			itemList = itemList.split("|");
			
			combobox.options.length = itemList.length;
					
			for (var i=0; i < itemList.length; i++) {
				optionList = itemList[i].split("=");
				myText = '';
				
				if (optionList[1] != null) {
					var myText = optionList[1].replace(/&equal;/g, '=');
				}
				
				var newOption  = new Option(myText, optionList[0], false, false);
				combobox.options[i] = newOption;
	
				if (optionList[0] == selected) {
					selectedIndex = i;
				}
			}
			
			combobox.selectedIndex = selectedIndex;
		}
	}
}

function setSubmitValue(evt) {
	var element = getElement(evt);
	var combo = document.isycat[element.comboboxId];
	combo.options[0].value = element.value;
	combo.options[0].selected = true;
}
	
function setComboboxZIndexes(comboboxId) {
	var Table = document.getElementById(comboboxId+'_ROWS');
	
	if(comboboxId != '') {
		Table.style.visibility='visible';
		Table.style.zIndex=10000;
		
		for(var i=0; i < Table.rows.length; i++) {
			Table.rows[i].style.zIndex=10000;
			Table.rows[i].style.backgroundColor='white';
		}

		document.getElementById(String(comboboxId)+"_CONTAINER").style.zIndex = 10000;	
	}

	for (i=0; i < enhancedCombos.length; i++) {
		if (String(enhancedCombos[i].id) != String(comboboxId)) {
			var id=String(enhancedCombos[i].id);
			var Table=document.getElementById(id+'_ROWS');
			
			Table.style.visibility = 'hidden';
			Table.style.zIndex = -1000;
			
			for(var n = 0; n < Table.rows.length; n++) {
				Table.rows[n].style.zIndex = -1000;
			}
			
			document.getElementById(id+"_CONTAINER").style.zIndex = -1000;	
		} 
	}
}

function openCombo(evt) {
	var element = getElement(evt);
	var comboboxId = element.getAttribute('comboboxId');

	combo = getCombobox(comboboxId);
	
	if (combo.selectedId != null && (highlightRow = document.getElementById(combo.selectedId)) != null) {
		for (var j=0; j < highlightRow.cells.length; j++) {
			highlightRow.cells[j].style.backgroundColor = combobox.highlightcolor;
			highlightRow.cells[j].style.color			= combobox.highlighttextcolor;
		}
	}
				
	if (combo.active) {
		combo.Items.style.zIndex = -1000;
		combo.Items.style.visibility = 'hidden';
		combo.active = false;
		setComboboxZIndexes('');
		showNormalSelects();
	} else {
		combo.Items.style.zIndex = 10000;
		combo.Items.style.visibility = 'visible';
		closeActiveCombo();
		setActiveCombo(comboboxId);
		setComboboxZIndexes(comboboxId);
		hideNormalSelects(combo.Text);
	}
	
	cancelEventBubble();
}

var hiddenSelects = new Array();

function hideNormalSelects(items) {
	var container = getElementFromElementByTagName(items, 'DIV');
	var ctnTopOffset = parseInt(container.offsetTop);
	var ctnLeftOffset = parseInt(container.offsetLeft)
	var ctnReachTop = parseInt(container.offsetHeight) + ctnTopOffset;
	var ctnReachLeft = parseInt(container.offsetWidth) + ctnLeftOffset;
	
	var allSelects = document.getElementsByTagName('SELECT');

	for (var i = 0; i < allSelects.length; i++) {
		var selectTop = parseInt(allSelects[i].offsetTop);
		var selectLeft = parseInt(allSelects[i].offsetLeft);

		var selectHeight = parseInt(allSelects[i].offsetHeight);
		var selectWidth = parseInt(allSelects[i].offsetWidth);
		
		var selectRight		= selectLeft + selectWidth;
		var selectBottom	= selectTop + selectHeight;
		
		if ( 
			((selectTop <= ctnReachTop && selectTop >= ctnTopOffset)
		|| (selectBottom <= ctnReachTop && selectBottom >= ctnTopOffset))
		&& 
		((selectLeft >= ctnLeftOffset && selectLeft <= ctnReachLeft)
		|| (selectRight >= ctnLeftOffset && selectRight <= ctnReachLeft)))
		{
			if (allSelects[i].style.visibility == 'visible' || allSelects[i].style.visibility == '') {
				allSelects[i].style.visibility = 'hidden';
				hiddenSelects[hiddenSelects.length] = allSelects[i];
			}
		}
	}	
}

function showNormalSelects() {
	for (var i=0; i < hiddenSelects.length; i++) {
		hiddenSelects[i].style.visibility = 'visible';
	}
	hiddenSelects.length = 0;
}

function setActiveCombo(id) {
	combo = getCombobox(id);
	combo.active = true;
}

function closeActiveCombo() {
	for (i=0; i < enhancedCombos.length; i++) {
		if (enhancedCombos[i].active) {
			enhancedCombos[i].Items.style.visibility = 'hidden';				
		}
	}
	
	setComboboxZIndexes('');
	showNormalSelects();
}

function getCombobox(id) {
	for (i=0; i < enhancedCombos.length; i++) {
		if (enhancedCombos[i].id == id) {
			return enhancedCombos[i];
		}
	}
	
	return false;
}

function itemOver(evt) {
	var element 	= getElement(evt);
	var itemRow 	= getElementFromElementByTagName(element, 'TR');
	var combobox 	= getCombobox(itemRow.getAttribute('comboboxId'));

	if (combo.selectedId != null && (highlightRow = document.getElementById(combo.selectedId)) != null) {
		var highlightRow = document.getElementById(combobox.selectedId);
		
		for (var i=0; i < highlightRow.cells.length; i++) {
			highlightRow.cells[i].style.backgroundColor = combobox.bgcolor;
			highlightRow.cells[i].style.color			= combobox.textcolor;
		}
	}
		
	for (var i=0; i < itemRow.cells.length; i++) {
		itemRow.cells[i].style.backgroundColor 		= combobox.highlightcolor;
		itemRow.cells[i].style.color				= combobox.highlighttextcolor;
	}
	
	cancelEventBubble();
}

function itemOut(evt) {
	var element 	= getElement(evt);
	var itemRow 	= getElementFromElementByTagName(element, 'TR');
	var combobox 	= getCombobox(itemRow.getAttribute('comboboxId'));
	
	for (var i=0; i < itemRow.cells.length; i++) {
		itemRow.cells[i].style.backgroundColor 	= combobox.bgcolor;
		itemRow.cells[i].style.color			= combobox.textcolor;
	}
		
	cancelEventBubble();	
}

function itemSelect(evt) {
	var element = getElement(evt);
	itemRow 	= getElementFromElementByTagName(element, 'TR');
	combobox 	= getCombobox(itemRow.getAttribute('comboboxId'));
	
	combobox.selectedId = itemRow.id;
	comboboxId 	= itemRow.getAttribute('comboboxId');
	
	document.isycat[comboboxId].options[0].value = itemRow.value;
	document.isycat[comboboxId].options[0].selected = true;

	trgtCombo 	= document.getElementById(comboboxId);
	
	colIdx 		= parseInt(trgtCombo.getAttribute('displaycol')) - 1;
	textField 	= document.getElementById(comboboxId + "_TEXT");
	
	if( enhancedCombosInit == false && textField.value != itemRow.cells[colIdx].text ) {
		var onchanged=String(textField.getAttribute('onchange'));
		var sucherg=onchanged.search(/ekSubmit/);
		
		if ( sucherg != -1 ) {
			textField.value = itemRow.cells[colIdx].text;
			closeActiveCombo();
			ekSubmit(trgtCombo);
			return;
		}
	}
	
	textField.value = itemRow.cells[colIdx].text;
	closeActiveCombo();
}

function closeAllCombos(evt) {
	var element = getElement(evt);

	for (i=0; i < enhancedCombos.length; i++) {
		enhancedCombos[i].Items.style.visibility = 'hidden';
		enhancedCombos[i].Button.src = enhancedCombos[i].combobuttonsrc;
		enhancedCombos[i].Button.setAttribute('mouseoverimage', enhancedCombos[i].combobuttonhlsrc);
		enhancedCombos[i].active = false;
	}
	
	showNormalSelects();
	
	setComboboxZIndexes('');
}

function disableEnhancedCombobox(id) {
	var combo = getCombobox(id);

	removeEvent(combo.Button, "click", openCombo, false);
	removeEvent(combo.Button, "mousedown", flipImage, false);
	removeEvent(combo.Button, "mouseup", flipImage, false);
	
	combo.Text.disabled = true;
}

function comboboxEnable(id) {
	combo = getCombobox(id);
	combo.disabled = false;
}