
	function resetInputBuffer(srcRef) {
		
		srcRef.combobox.inputBuffer ='';
					
	}
	
	function toggleMainStyle(e) {
			
		srcRef = eventElement(e);
		
		if (srcRef.combobox) {
		
			divRef = srcRef.combobox.selectBody;
			valuesRef = srcRef.combobox.valuesDiv;
			styleName = srcRef.combobox.styleName;
			
			if (divRef.className == styleName+'_body') {
			
				divRef.className = styleName+'_body_over';
				
			} else if (valuesRef.className != styleName+'_values_show') {
			
				divRef.className = styleName+'_body';
				
			}				
						
			resetInputBuffer(srcRef);
			
			cancelBubble(e);
			
		}
		
	}
	
	function highlightValuesItem(e) {		
	
		srcRef = eventElement(e);
		
		if (srcRef.combobox) {
		
			tableRef = srcRef.combobox.valuesTable;		
							
			for (i=0; i<tableRef.rows.length; i++) {
				
				if (tableRef.rows[i].cells[0].getAttribute("value") != srcRef.getAttribute("value")) {
					tableRef.rows[i].cells[0].className = '';
				} else {
					tableRef.rows[i].cells[0].className = 'over';
				}
			
			}
			
		}
	
	}
	
	function selectValue(e) {
				
		srcRef = eventElement(e);
		
		if (srcRef.combobox) {
		
			tableRefValues = srcRef.valuesTable;		
			
			hidden = srcRef.combobox.inputElement;
			hidden.value = srcRef.getAttribute("value");
			
			valueInput = srcRef.combobox.valueInput;		
	//		valueInput.setAttribute("value", srcRef.combobox.valuesArray[srcRef.getAttribute("value")]);	
			valueInput.innerHTML = srcRef.combobox.valuesArray[srcRef.getAttribute("value")];
	
			resetInputBuffer(srcRef);
			
		}	
			
	}
	
	function inputValueKeyPress(e) {
		
		srcRef = eventElement(e);	
		
		if (srcRef.combobox) {
			
			chr = String.fromCharCode(e.keyCode);
			
	//		alert(window.event.keyCode);
			
			if (srcRef.combobox.inputBuffer.length == 0)
				window.setTimeout("if (srcRef) srcRef.combobox.inputBuffer='';", 2000);
	
			srcRef.combobox.inputBuffer += chr.toLowerCase();
			for (var i in srcRef.combobox.valuesArray) {
				
				tempStr = new String(srcRef.combobox.valuesArray[i].substr(0, srcRef.combobox.inputBuffer.length));
				if (tempStr.toLowerCase() == srcRef.combobox.inputBuffer) {
					
	//				alert(srcRef.combobox.valuesArray[i]);
					
					srcRef.combobox.inputElement.value = i;	
					srcRef.combobox.valueInput.innerHTML = srcRef.combobox.valuesArray[i];
//					srcRef.combobox.valueInput.value = srcRef.combobox.valuesArray[i];	
					
					break;
					
				}
				
			}
			
		}
		
	}
		
	// событие для закрытия окон ComboBox
	
	function valuesDivToggle(e) {
		
		cb = document.getElementsByTagName("div");	
		srcRef = eventElement(e);	
								
		for (i=0; i<cb.length; i++) {		
		
			if (cb[i].id) {
			
				idStr = cb[i].id.toString();
				
				if (idStr.indexOf('combobox_values')>-1 && !srcRef.combobox) {

					styleName = cb[i].combobox.styleName;
					newStyleName = styleName+'_values';
					
					if (cb[i].className!=newStyleName) {
						cb[i].className = newStyleName;					
						divRef = cb[i].combobox.selectBody;
						divRef.className = styleName+'_body';
					}
					
//					alert(1);
										
				} else if (srcRef.combobox) {
									
					valuesRef = srcRef.combobox.valuesDiv;
					tableRef = srcRef.combobox.valuesTable;
					divRef = srcRef.combobox.selectBody;
					styleName = srcRef.combobox.styleName;
				
					if (valuesRef.className == styleName+'_values') {
																					
						valuesRef.className = styleName+'_values_show';		
						divRef.className = styleName+'_body_over';											
						
						heightLimit = document.body.clientHeight/2;
									
						valuesRef.style.height = (
								valuesRef.scrollHeight < heightLimit ? valuesRef.scrollHeight : heightLimit
							) + 2 + 'px';

												
					} else {
					
						valuesRef.className = styleName+'_values';	
						divRef.className = styleName+'_body';
						
					}
											
					for (i=0; i<tableRef.rows.length; i++) {
					
						if (tableRef.rows[i].cells[0].value == srcRef.combobox.inputElement.value && tableRef.rows[i].cells[0].className != 'over')
							tableRef.rows[i].cells[0].className = 'over';
						else if (tableRef.rows[i].cells[0].className != '')
							tableRef.rows[i].cells[0].className = ''
							
					}
						
					return false;
										
				}
				
			}
			
		}
		
	}
	
	bindEvent(document, "click", valuesDivToggle);
		
	// создание объекта

	ComboBox.prototype.instanceCount = 0;		
	
	ComboBox.prototype.createMyElement = function(elementName) {
	
		tempObj = document.createElement(elementName);
		tempObj.combobox = this;
		
		return tempObj;
		
	}
	
	ComboBox.prototype.setValue = function(value) {						
						
		if (this.valuesArray[value]) {
								
			this.inputElement.setAttribute("value", value);
//			this.valueInput.setAttribute("value", this.valuesArray[value]);
			this.valueInput.innerHTML = this.valuesArray[value];
			
			return true;
				
		} else {
			
			for (var i in this.valuesArray) {
				
//				this.valueInput.setAttribute("value", this.valuesArray[i]);					
				this.valueInput.innerHTML = this.valuesArray[i];
				break;
			}
			
			return false;
			
		}
		
	}
	
	function ComboBox(comboName, valuesArray, defaultValue, width, styleName) {		
		
		this.comboName = comboName;
		this.valuesArray = valuesArray;
		this.defaultValue = defaultValue;
		this.width = width;
		this.styleName = styleName?styleName:'combobox';
		this.inputBuffer = '';

		if (hasDOM) {			

			mainNodeId = comboName;		
			ComboBox.prototype.instanceCount++;
			valuesRefId = "combobox_values"+this.instanceCount;
	
			document.write("<div id='"+mainNodeId+"'></div>");
			
			// поле хранения значения
			
			this.inputElement = this.createMyElement("input");
			this.inputElement.setAttribute("name", comboName);
			this.inputElement.setAttribute("type", "hidden");
			this.inputElement.className = this.styleName;
//			this.inputElement.setAttribute("value", defaultValue);
	
			// поле вывода combobox
			
			this.selectBody = this.createMyElement("div");
			this.selectBody.setAttribute("id", "combobox_body");
			this.selectBody.className = this.styleName+"_body";
	
			// таблица для отображения текущего значения			
						
			this.bodyTable = this.createMyElement("table");
			this.bodyTable.setAttribute("border", "0");
			this.bodyTable.cellPadding = "0";
			this.bodyTable.cellSpacing = "0";
			this.bodyTable.setAttribute("width", "100%");
	
			tr = this.bodyTable.insertRow(0);
			
			// текущее значение
	
			outputTd = tr.insertCell(0);
			outputTd.combobox = this;
			outputTd.className = "content";			
			
			this.valueInput = this.createMyElement("div");
//			this.valueInput.setAttribute("type", "hidden");			
//			this.valueInput.setAttribute("readOnly", true);			
			this.valueInput.style.width = '100%';			
			this.valueInput.style.background = 'none';
			this.valueInput.style.border = '0px';
			this.valueInput.style.padding = '0px';			
						
			this.setValue(defaultValue);				
						
			outputTd.appendChild(this.valueInput);			
	
			// иконка стрелки
						
			buttonTd = tr.insertCell(1);
			buttonTd.combobox = this;
			buttonTd.className = 'arrow';			
				
			filler = this.createMyElement("table");
			filler.setAttribute("width", "100%");
			filler.insertRow(0);
			fillerCell = filler.rows[0].insertCell(0);
			fillerCell.combobox = this;
			
			buttonTd.appendChild(filler);

			
			this.selectBody.appendChild(this.bodyTable);
			
			// таблица значений
			
			this.valuesDiv = this.createMyElement("div");
			this.valuesDiv.className = this.styleName+"_values";
			this.valuesDiv.id = valuesRefId;						
			if (width) this.valuesDiv.style.width = width;												
			
			this.valuesTable = this.createMyElement("table");
			this.valuesTable.setAttribute("border", "0");
			this.valuesTable.cellPadding = "0";
			this.valuesTable.cellSpacing = "0";	
			this.valuesTable.style.width = "100%";		
					
			i = 0; this.currentRow = -1;
			count = 0;
			
			for (var property in valuesArray) {
						
				count++;
				
				newTr = this.valuesTable.insertRow(i++);
				
				valueCell = newTr.insertCell(0);	
				valueCell.combobox = this;
				valueCell.setAttribute("name", this.styleName+"_valueItem");
				valueCell.setAttribute("value", property);
								
				/* --------------------------------------------------------------- */
				
//				valueCell.appendChild(document.createTextNode(valuesArray[property]));
				valueCell.innerHTML = valuesArray[property];
				
				/* --------------------------------------------------------------- */

				if (valuesArray[defaultValue] && property == defaultValue)
					this.currentRow = i-1;

				bindEvent(valueCell, "mouseover", highlightValuesItem);				
				bindEvent(valueCell, "click", selectValue);
								
			}
			
//			this.valuesDiv.style.height = "100px";
			
			if (this.currentRow<0) this.currentRow = 0;
									
			this.valuesDiv.appendChild(this.valuesTable);
						
	//		сборка конечного элемента		
	
			this.selectElement = document.getElementById(mainNodeId);	
			if (width) this.selectElement.style.width = width;			
	
			this.selectElement.appendChild(this.selectBody);					
			this.selectElement.appendChild(this.inputElement);		
			this.selectElement.appendChild(this.valuesDiv);
			
			bindEvent(this.selectBody, "keypress", inputValueKeyPress);
			
			bindEvent(this.selectBody, "mouseover", toggleMainStyle);
			bindEvent(this.selectBody, "mouseout", toggleMainStyle);			
										
		}
	
	}