var comboBoxes=new Array();

function ComboBox_draw(element) {
	var html='<table id="comboTable'+this.id+'" cellpadding="0" cellspacing="0"><tr><td><img src="'+this.leftImage+'"/></td>';
	html   +='<td style="cursor:hand" id="comboVoice'+this.id+'" background="'+this.centerImage+'" width="'+this.width+'" height="'+this.height+'">&nbsp;</td>';
	html   +='<td><img style="cursor:hand" id="comboArrow'+this.id+'" src="'+this.rightImage+'"/></td></tr></table>';
	html   +='<div id="comboListPosDiv'+this.id+'" style="position:relative;left:0px;top:0px;width:1px;height:1px"><img src="'+this.spacerImage+'"/ width="1" height="1"></div>';
	html   +='<div id="comboListDiv'+this.id+'" style="position:absolute;left:0px;top:0px;width:'+this.globalWidth+'px;height:1px;z-index:10000"><span id="comboList'+this.id+'" >&nbsp;</span></div>';
	
	element.innerHTML=html;
	
	var comboArrow=document.getElementById('comboArrow'+this.id);
	comboArrow.combo=this;
	comboArrow.onclick=new Function("this.combo.listToggle()");
	var comboVoice=document.getElementById('comboVoice'+this.id);
	comboVoice.combo=this;
	comboVoice.onclick=new Function("this.combo.listToggle()");
	this.container=element;
	this.container.value=this.value;
	this.container.description=this.description;
	document.getElementById('comboVoice'+this.id).innerHTML=this.initValue;
	
}

function ComboBox_open() {
	
	var refDiv=document.getElementById('comboListPosDiv'+this.id);
	var div=document.getElementById('comboListDiv'+this.id);
	div.style.pixelLeft=refDiv.offsetLeft;
	div.style.pixelTop=refDiv.offsetTop;
	this.opened=true;
	var comboList=document.getElementById('comboList'+this.id);
	var html='<table  bgcolor="white" width="'+(this.globalWidth-18)+'">';
	for(var description in this.comboList) {
		html+='<tr><td id="comboListVoice'+this.id+description+'" width="'+(this.globalWidth-18)+'" style="cursor:hand">'+description+'</td></tr>';
	}
	html+='</table>';
	comboList.innerHTML=html;
	div.style.height='100px';
	div.style.backgroundColor='white';
	div.style.border='black solid 1px';
	div.style.clip='rect(0 '+this.globalWidth+'px 100px 0)';
	div.style.overflowY='scroll';
	for(var description in this.comboList) {
		var voice=document.getElementById('comboListVoice'+this.id+description);
		voice.combo=this;
		voice.onmouseover=new Function('this.combo.overItem(this)');
		voice.onmouseout=new Function('this.combo.outItem(this)');
		voice.onclick=new Function('this.combo.setVoice(\''+description+'\')');
	}
}

function ComboBox_close() {
	var div=document.getElementById('comboListDiv'+this.id);
	if(div) {
		div.style.height='1px';
		div.style.backgroundColor='';
		div.style.border='';
		div.style.overflowY='hidden';
	}
	this.opened=false;
	var comboList=document.getElementById('comboList'+this.id);
	if(comboList)
		comboList.innerHTML='<img src="'+this.spacerImage+'" width="1" height="1"/>';
}

function ComboBox_listToggle() {
	
	if(this.opened)
		this.close();
	else
		this.open();
}

function ComboBox_setVoice(description) {
	
	if(document.getElementById('comboVoice'+this.id))
		document.getElementById('comboVoice'+this.id).innerHTML=description;
	this.description=description;
	this.value=this.comboList[description];
	if(this.container) {
		this.container.value=this.value;
		this.container.description=this.description;
		if(this.onSelect)
			eval(this.onSelect+'(\''+this.value+'\')');
	}
	this.close();
}

function ComboBox_addItem(description,item) {
	this.comboList[description]=item;
	if(this.empty) {
		this.empty=false;
		this.setVoice(description);
		this.initValue=description;
	}
}

function ComboBox_overItem(item) {
	item.style.color=this.overColor;
	item.style.backgroundColor=this.overBackgroundColor;
}

function ComboBox_outItem(item) {
	item.style.color=this.defaultColor;
	item.style.backgroundColor=this.defaultBackgroundColor;
}

function ComboBox_init() {
	var found=false;
	for(var description in this.comboList) {
		if(!found) {
			found=true;
			this.value=this.comboList[description];
			this.description=description;
		}
	}
}

function ComboBox_clear() {
	this.close();
	this.comboList=new Array();
	this.empty=true;
}

function ComboBox_setWidth(width) {
	this.width=width;
	this.globalWidth=width+19;
}

function ComboBox(id) {
	this.id=id;
	this.height=21;
	this.width=200;
	this.globalWidth=219;
	this.leftImage='immagini/combo_left.gif';
	this.centerImage='immagini/combo_center.gif';
	this.rightImage='immagini/combo_right.gif';
	this.spacerImage='immagini/spacer.gif';
	comboBoxes[id]=this;
	this.opened=false;
	this.comboList=new Array();
	this.container=null;
	this.empty=true;
	this.value=null;
	this.description=null;
	this.defaultColor='black';
	this.defaultBackgroundColor='white';
	this.overColor='white';
	this.overBackgroundColor='black';
	this.onSelect=null;
}

ComboBox.prototype.draw=ComboBox_draw;
ComboBox.prototype.open=ComboBox_open;
ComboBox.prototype.close=ComboBox_close;
ComboBox.prototype.listToggle=ComboBox_listToggle;
ComboBox.prototype.clear=ComboBox_clear;
ComboBox.prototype.addItem=ComboBox_addItem;
ComboBox.prototype.setVoice=ComboBox_setVoice;
ComboBox.prototype.overItem=ComboBox_overItem;
ComboBox.prototype.outItem=ComboBox_outItem;
ComboBox.prototype.init=ComboBox_init;
ComboBox.prototype.setWidth=ComboBox_setWidth;

