function spcOptionList()
{
	this.debug = false;
	this.initialized = false;
	this.editmode = false;
	
	this.id = '';
	this.size = 2;
	this.optionMultiple = false;
	
	this.options = [];
	this.selected = [];
	this.database = new spcDatabase;
	this.query = '';
	this.queryResult = null;
	this.callbackOnDone = null;
	this.filter = null;
	
	this.panelHTML = '';
	
}

spcOptionList.prototype.add = function(optionText,optionSelected)
{
	this.options.push({   text: 	optionText
						, selected: optionSelected
						});
}

spcOptionList.prototype.addNotSelected = function(optionText)
{
	var optionValue = '';
	//console.log('foreachObject.callback(' + optionText + ',' + optionValue + ')');
	this.options.push({   text: 	optionText
						, selected: false
						});
}


spcOptionList.prototype.size = function(size)
{
	this.size = size;
}

spcOptionList.prototype.length = function()
{
	var count = 0;
	var selObj = document.getElementById(this.id);
	if (selObj) {
	  for (var i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			count++;
		}
	  }	
	}
	return count;
}

spcOptionList.prototype.onQueryDone = function(queryResult)
{
	this.options = queryResult;
	this.queryResult  = queryResult;
							
	
	if (console.group) {
		console.group();
		console.log(this.options);
		console.groupEnd();
	
	}
	else {
		console.log(JSON.stringify(this.options));
	}
	
	console.log('Sample:');
	console.log('Length: ' + this.options.data.length);
	console.log('Length[]: ' + this.options.data[0].length);
	console.log(JSON.stringify(this.options.data[0][0]));
						
	if (this.callbackOnDone) {
		this.callbackOnDone(this.options);
	}
}

spcOptionList.prototype.create = function(id,size,optionMultiple,query,callbackOnDone,filter)
{
	var self = this;
	this.id = id;
	this.size = size;
	this.optionMultiple = optionMultiple;
	this.query = query;
	this.callbackOnDone = callbackOnDone;
	this.filter = filter;

	if (!query) {
	
	}
	else {
		
		console.log('Called spcOptionList.prototype.create with query: ' + query);
		
		this.database.fetchQuery(query,function(queryResult) {self.onQueryDone(queryResult);});
		return true;
	}
	
	console.log('Done spcOptionList.prototype.create');
		
	return false;
}

spcOptionList.prototype.htmlOption = function(optionText,optionValue,optionSelected)
{
	var sSelected = ' selected';
	if (!optionSelected) {
		sSelected = '';
	}

	if (this.filter) {
		if (optionText==this.filter) {
			this.panelHTML += '  <option' + sSelected +'>' + optionValue + '</option>';
		}
	}
	else {
		this.panelHTML += '  <option' + sSelected +'>' + optionValue + '</option>';
	}
}

spcOptionList.prototype.populateDOM = function()
{
	//var obj = document.getElementById(this.id);
	//if (obj) {
	//	obj.innerHTML = this.innerHTML();
	//}
	
	console.log('spcOptionList.prototype.populate()');
	
	var self = this;
	var idx = 0;
	
	id = this.id;
	size = this.size;

	this.panelHTML = '';							
	
	
	self.clearOptionsDOM();

	var proc = new spcObject();
	proc.foreachObjectPair({ data:		this.options
							,callback:	function(optionValue,optionText) {self.addOptionDOM(optionValue,optionText);}
							});
	
	console.log('Done spcOptionList.prototype.populate');

}

spcOptionList.prototype.clearOptionsDOM = function()
{

	var x=document.getElementById(this.id);
	if (x) {
		var length = x.length;
		for (i=0;i<length;i++) {
			
			x.remove(0);

		}
	}
}

spcOptionList.prototype.addOptionDOM = function(item,value)
{

	var x=document.getElementById(this.id);
	var option=document.createElement("option");
	if (option) {
		option.text = value;
		if (x) {
			option.value = x.length;
			
			if (this.filter) {
				if (item!=this.filter) {
					return;
			  }
			}
			
			try
			{
				// for IE earlier than version 8
				x.add(option,x.options[null]);
			}
			catch (e)
			{
				x.add(option,null);
			}
		}
	}
}


spcOptionList.prototype.innerHTML = function()
{
	var self = this;
	var idx = 0;
	
	id = this.id;
	size = this.size;

	
	this.panelHTML = '';							
	
	var proc = new spcObject();
	proc.foreachObject({ data:		this.options
						,callback:	function(optionText,optionValue) {self.htmlOption(optionText,optionValue,self.selected[idx++]);}
						});
							
	//console.log(JSON.stringify(this.options));


	console.log('Done spcOptionList.prototype.innerHTML');
	
	return this.panelHTML;
}

spcOptionList.prototype.htmlContent = function()
{
	var self = this;
	var idx = 0;
	
	id = this.id;
	size = this.size;
	optionMultiple = this.optionMultiple;
	
	sMultiple = ' multiple="multiple"';	
	if (!optionMultiple) {
		sMultiple = '';
	}
	
	this.panelHTML = '<select id="' + this.id + '"' + sMultiple +  " size=" + this.size + '">';							
	
	/*
	var proc = new spcObject();
	proc.foreachObject({ data:		this.options
						,callback:	function(optionText,optionValue) {self.htmlOption(optionText,optionValue,self.selected[idx++]);}
						});
	*/
	
	//console.log(JSON.stringify(this.options));

	this.panelHTML += '</select>';

	console.log('Done spcOptionList.prototype.draw');
	
	return this.panelHTML;
}

spcOptionList.prototype.getFilter = function()
{
	return this.filter;
}

spcOptionList.prototype.getSelectionList = function(prefix,separator,suffix)
{

var SelectionList = null;
var selObj = document.getElementById(this.id);
var i;
var count = 0;

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			if (!SelectionList) {
				SelectionList = selObj.options[i].text;
			}
			else {
				SelectionList += separator + selObj.options[i].text;
			}
		  count++;
		}
	  }
	}
	return prefix + SelectionList + suffix;
}

spcOptionList.prototype.getAssignmentList = function(prefix,separator,suffix)
{

var SelectionList = null;
var selObj = document.getElementById(this.id);
var i;
var count = 0;
var sresult = '';

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
			sresult = selObj.options[i].text.replace('\r','');
			sresult = sresult.replace('\n','');
			if (!SelectionList) {
				SelectionList = this.filter + " LIKE '" + sresult + "'";
			}
			else {
				SelectionList += separator + this.filter + " LIKE '" + sresult + "'";
			}
		  count++;
		}
	  }
	}
	return prefix + SelectionList + suffix;
}

spcOptionList.prototype.selectAll = function()
{

var selObj = document.getElementById(this.id);
var i;
var count = 0;

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		selObj.options[i].selected = true;
		
	  }
	}
	
	for (i=0; i<this.options.length; i++) {
		this.selected[i] = true;
	}
	
	return;
}

spcOptionList.prototype.deselectAll = function()
{

var selObj = document.getElementById(this.id);
var i;
var count = 0;

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		selObj.options[i].selected = false;
		
	  }
	}
	
	for (i=0; i<this.options.length; i++) {
		this.selected[i] = false;
	}
	
	return;
}

spcOptionList.prototype.select = function(optionText)
{
var selObj = document.getElementById(this.id);
var i;
var count = 0;

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		
		if (selObj.options[i].text==optionText || i==optionText) {
			selObj.options[i].selected = true;
			this.selected[i] = true;
			return true;
		}
	  }
	}
	
	return false;
}

spcOptionList.prototype.deselect = function(optionText)
{
var selObj = document.getElementById(this.id);
var i;
var count = 0;

	if (selObj) {
	  for (i=0; i<selObj.options.length; i++) {
		
		if (selObj.options[i].text==optionText || i==optionText) {
			selObj.options[i].selected = false;
			this.selected[i] = false;
			return true;
		}
	  }
	}
	
	return false;
}

