//	SelectBox v1.0 by Bjørn Rosell
//	Find more scripts here: www.prototypeDHTML.net

function SelectBox(formID,selectboxName) {
//	this.elm = document.forms[formID][selectboxName]
	this.id = SelectBox.count
	SelectBox.count++

	this.formID = formID
	this.selectboxName = selectboxName
	this.options = []
	this.waiting = []
	this.created = false
}
SelectBox.prototype.getSelectBoxElm = function() {
	if (!this.elmSelectbox) this.elmSelectbox = document.forms[this.formID][this.selectboxName]
	return this.elmSelectbox;
}
SelectBox.prototype.getDivElm = function() {
	if (!this.elmDiv) this.elmDiv = API.getElm('selectboxElm'+this.id)
	return this.elmDiv
}
SelectBox.prototype.testIfWritingNeeded=function() {
	if (!document.createElement) return true
	elmSB = this.getSelectBoxElm()
	if (!elmSB) return true
	if (!elmSB.options.add) return true;
	var elm = document.createElement("OPTION")
	if (!elm) return true
	return false
}
SelectBox.prototype.needWriting = function() {
	if (this.bNeedsWriting==null) this.bNeedsWriting = this.testIfWritingNeeded()
	return this.bNeedsWriting
}
SelectBox.prototype.add = function(text,value) {
	if (this.created) {
		this.waiting[this.waiting.length] = {cmd:'_add', params:[text,value]}
	}
	else {
		this.options[this.options.length] = [text,value]
	}
}
SelectBox.prototype._add = function(params) {
	this.options[this.options.length] = params
	if (this.needWriting()) return false
	var elm = document.createElement("OPTION")
	if (!elm) return false		
	elm.text = params[0]
	elm.value = params[1]
	this.getSelectBoxElm().options.add(elm)
	return true
}
SelectBox.prototype.clear = function(bUpdate) {
	if (this.created) {
		this.waiting[this.waiting.length] = {cmd:'_clear', params:[]}
	}
	else {
		this.options = []
	}
}
SelectBox.prototype._clear = function(params) {
	this.options = []
	if (this.needWriting()) return false
	elm = this.getSelectBoxElm()
	l = elm.options.length
	for (var i=0; i<l; i++) {
		elm.remove(l-i-1)	
	}
	return true
}
SelectBox.prototype.select = function(value) {
	if (this.created) {
		this._select(value)
	}
	else {
		this.waiting[this.waiting.length] = {cmd:'_select', params:[value]}
	}
}
SelectBox.prototype._select = function(value) {
	elm = this.getSelectBoxElm()
	var i=0
	for (var i=0; i<elm.options.length;i++) {
		if (elm.options[i].value==value) {
			elm.selectedIndex=i
			return
		}
	}
}
SelectBox.prototype.getHTML = function() {
	var s=''
	s+='<select name="'+this.selectboxName+'">'
	for (var i=0; i<this.options.length; i++) {
		s+='<option value="'+this.options[i][1]+'">'+this.options[i][0]+'</option>'
	}
	s+='</select>'
	return s
}
SelectBox.prototype.update = function() {
	if (!this.created) return	
	var needWriting=false
	for (var i=0; i<this.waiting.length; i++) {
		if (!this[this.waiting[i].cmd](this.waiting[i].params)) needWriting=true
	}
	this.waiting = []
	if (needWriting) API.writeHTML(this.getDivElm(),this.getHTML())
}
SelectBox.prototype.writeHTML = function() {
	this.created=true
	document.write('<div style="display:inline;" id="selectboxElm'+this.id+'">'+this.getHTML()+'</div>')
}
SelectBox.count = 0

