/*
 * General functions
 */
function $$(className)
{
	return document.getElementsByClassName(className);
}
function StrToTitle(title)
{

	var arrName = title.split(" ");
	_title = "";
	for(i=0; i<arrName.length; ++i)
	{
		if(arrName[i] != "for" && arrName[i] != "and" && arrName[i] != "with")
		_title += arrName[i].substring(0,1).toUpperCase()+""+arrName[i].substring(1) + " ";
		else _title += arrName[i] + " ";
	}
	_title = _title;
	return _title;
}
/*
 * An implementation of a combo-box class
 */
function ComboBox(id)
{
	this.popupItemHeight = 13;
	this.popupItemsMaxShown = 4;
	this.elements = new Array();
	this.text = "[Undefined]";
	this.name = id;
	this.invalidate = ComboBox_Invalidate;
	this.add = ComboBox_Add;
	this.remove = ComboBox_Remove;
	this.getText = ComboBox_GetText;
	this.getValue = ComboBox_GetValue;
	this.hidePopup = ComboBox_HidePopup;
	this.setPopupItemsVisible = ComboBox_SetPopupItemsVisible;

	dropDownList = $(this.name);
	text = dropDownList.childNodes.item(0).nodeValue;
	width = eval(parseInt(dropDownList.style.width));
	theId = dropDownList.id;
	var table = document.createElement("table");
	table.cellPadding = 0;
	table.cellSpacing = 0;
	html  = "<tr><td class='DropDownList_Component'><div id='"+theId+"_TextField' onClick='ComboBox_onClick($(\""+theId+"\"))'>";
	html += "<span style='color:#666666;' class='DropDownList_TextField'>" + text + "</span></div><div id='"+theId+"_Popup' ";
	html += "class='Popup' style='width:"+(width-6)+"px;display: none;'>";
	html += "<div class='PopupItemOut' onMouseOver='ComboPopupOver(this);' onMouseOut";
	html += "='ComboPopupOut(this);'>Sample</div><div class='PopupItemOut'"
	html += "onMouseOver='ComboPopupOver(this);' onMouseOut='ComboPopupOut(this);'>Text</div>";
	html += "</div></td>";
	html += "<td class='Button DropDownList_Button' onClick='ComboBox_onClick($(\""+theId+"\"))'>&nbsp;&nbsp;</td></tr>";
	table.innerHTML = html;
	table.width = width;
	table.className = dropDownList.className;
	var parent = dropDownList.parentNode;
	parent.replaceChild(table, dropDownList);
	table.id = theId;
	
	return this;
}
function ComboBox_Invalidate()
{
	var component = $(this.name);
	var popup = $(this.name+"_Popup");
	var html = "";
	for(i=0; i<this.elements.length; ++i)
	{
		html += "<div ";
		html += "class='PopupItemOut' ";
		html += "onMouseOver='this.className=\"PopupItemOver\"' ";
		html += "onMouseOut='this.className=\"PopupItemOut\"' ";
		html += "onClick='ComboBox_OnClickPopup($(\""+this.name+"\"), this, \""+this.elements[i][1]+"\")'>";
		html += this.elements[i][0];
		html += "</div>";
	}
	popup.innerHTML = html;
	if(this.popupItemsMaxShown<this.elements.length)
	{
		popup.style.height = eval(this.popupItemsMaxShown*this.popupItemHeight) + "px";
		popup.style.overflow = "auto";
	}
	else popup.style.height = eval(this.elements.length*this.popupItemHeight) + "px";
}
function ComboBox_Add(name, value)
{
	this.elements[this.elements.length] = new Array(StrToTitle(name), value);
	this.invalidate();
}
function ComboBox_Remove(value)
{
	var _elements = new Array();
	for(i=0; i<this.elements.length; ++i)
	{
		if(this.elements[i][1]!=value)
		  _elements[_elements.length] = this.elements[i];
	}
	this.elements = _elements;
	this.invalidate();
}
function ComboBox_GetText()
{
	this.text = $(this.name+"_TextField").innerText;
	return this.text;
}
function ComboBox_OnClickPopup(combo, source, value)
{
	comboTextField = $(combo.id+"_TextField");
	comboPopup = $(combo.id+"_Popup");
	html  = "<input type='hidden' id='"+combo.id+"_Text' value='"+value+"' />";
	html += source.childNodes.item(0).nodeValue;
	comboTextField.innerHTML = html;
	comboPopup.style.display = "none";
}
function ComboBox_onClick(source)
{
	//source.childNodes.item(0).childNodes.item(0).childNodes.item(1).className
	var combo = $(source.id);
	var popup = $(source.id+"_Popup");
	combo.className = (popup.style.display=="") ? "DropDownList" : "DropDownList DropDownList_OnGainFocus";
	popup.className = (popup.style.display=="") ? "Popup" : "Popup Popup_OnGainFocus";
	popup.style.display = (popup.style.display=="") ? "none" : "";
	//window.alert();
}
function ComboPopupOver(source)
{
	source.className = "PopupItemOver";
}
function ComboPopupOut(source)
{
	source.className = "PopupItemOut";
}
function ComboBox_HidePopup()
{
	var popup = $(this.name+"_Popup");
	popup.style.display = "none";
	this.invalidate();
}
function ComboBox_GetValue()
{
	return $(this.name+"_Text").value;
}
function ComboBox_SetPopupItemsVisible(height)
{
	this.popupItemsMaxShown = height;
	this.invalidate();
}