// JavaScript Document
function XMLComboBox(pControlId, pValueAttributeName, pTextAttributeName, pXMLNode, pAutoLoadOptions, pEmptyOptionText) {
	control = document.getElementById(pControlId);
	if (control == null) { 
		alert("XMLCombo: The " + pControlId + " control doesn't exist.") ;
		return false;
	}
	
	//Attributes
	control.xmlNode = pXMLNode;
	control.valueAttributeName = pValueAttributeName;
	control.textAttributeName = pTextAttributeName;
	control.emptyOptionText = pEmptyOptionText;
	//Methods
	control.loadOptions = XMLComboBox_loadOptions;
	control.filter = XMLComboBox_filter;
	control.filtered = false;

	if (control.xmlNode.hasChildNodes) {
		var row = control.xmlNode.childNodes[0];
		var value = row.getAttribute(control.valueAttributeName);
		if (value == null) {
			alert("XMLCombo: " + control.id + ", the " + control.valueAttributeName + " field doesn't exist in the XML provided." );
			return false;
		}
		var text = row.getAttribute(control.textAttributeName);
		if (text == null) {
			alert("XMLCombo: " + control.id + ", the " + control.textAttributeName + " field doesn't exist in the XML provided." );
			return false;
		} 
	}

	if (pAutoLoadOptions) { 
		control.loadOptions(); 
	}
	
	return true;	
}

function XMLComboBox_loadOptions() {
  this.innerHTML = ""; //Cleaning Options
	
	/* Adding empty option if the text was provided. */
	if (this.emptyOptionText != null) {
		var option = document.createElement("option");
		option.value = "";		
		option.text = this.emptyOptionText;		
		this.options.add(option);		
	}

  for (var i=0; i < this.xmlNode.childNodes.length; i++) {
		var row = this.xmlNode.childNodes[i];
		var option = document.createElement("option");
		option.value = row.getAttribute(this.valueAttributeName);
		option.text = row.getAttribute(this.textAttributeName);
		option.row = row;
		this.options.add(option);
	}
	
  this.selectedIndex = -1;
}

function XMLComboBox_filter(pXPath, pShowAllIfFilterReturnsNoRecords) {
	if (pXPath == null) {
		this.loadOptions();		
		control.filtered = false;		
	} else {
		filterdRows = this.xmlNode.selectNodes(pXPath);
		if ( (filterdRows.length == 0) && (pShowAllIfFilterReturnsNoRecords) ) {
			if (this.filtered) {
				this.loadOptions();		
				control.filtered = false;
			}
		} else {
			/* Erasing all the combo options */
			this.innerHTML = ""; 
			/* Adding empty option if the text was provided. */
			if (this.emptyOptionText != null) {
				var option = document.createElement("option");
				option.value = "";		
				option.text = this.emptyOptionText;		
				this.options.add(option);		
			}
			/* Filling the filtered options */
			for (var i=0; i < filterdRows.length; i++) {
				var row = filterdRows[i];
				var option = document.createElement("option");
				option.value = row.getAttribute(this.valueAttributeName);
				option.text = row.getAttribute(this.textAttributeName);		
				option.row = row;				
				this.options.add(option);
			}		
			control.filtered = true;
		}
	}
	return true;	
}

//function XMLComboBox_setSelectedValue(pValue) {
//	if (pValue == null) {
//		this.selectedIndex = -1;
//		return true;
//	}
//	
//	for (var i=0; i < this.options.length; i++) {
//		if (this.options[i].value == pValue) {
//			this.value = pValue;
//			return true;
//		}
//	}
//	return false;
//}


function XMLCombo(pComponentName, pXMLDef, pXMLDataSet) {
	this.optionsNode = pXMLDef.selectSingleNode("OPTIONS");
  //Public Properties
	this.dataSet = pXMLDataSet;
	this.name = pComponentName;

	this.optionValueField = this.optionsNode.getAttribute("optionvaluefield");
	this.optionTextField = this.optionsNode.getAttribute("optiontextfield");
	if (this.dataSet.findField(this.optionValueField) == null) {
		alert("XMLCombo: " + this.name + ", the " + this.optionValueField + " field doesn't exist." );
		return null;
	}
	if (this.dataSet.findField(this.optionTextField) == null) {
		alert("XMLCombo: " + this.name + ", the " + this.optionTextField + " field doesn't exist." );
		return null;
	}

	this.control = document.getElementById(this.optionsNode.getAttribute("control"));
	if (this.control == null) { 
		alert("XMLCombo: " + this.name + " control " + this.optionsNode.getAttribute("control") + "doesn't exist") 
		return null;
	}
  this.control.dataSet = this.dataSet;
	this.control.xmlCombo = this;
	this.control.attachEvent("onchange", XMLCombo_ControlOnChangeEvent);	
	
	//Methods
	this.refresh = XMLCombo_refresh;
  this.setValue = XMLCombo_setValue;
	this.getValue = XMLCombo_getValue;
	this.getText = XMLCombo_getText;
	this.setFilter = XMLCombo_setFilter;
	
	//Event Handlers
	this.onGetOptionText = function(pSender, pDataSet) { return pDataSet.fieldByName(pSender.optionTextField); }
	this.onGetOptionValue = function(pSender, pDataSet) { return pDataSet.fieldByName(pSender.optionValueField); }
  this.onValueChange = null;
	
  this.setOptionAttribute = function(pOption, pDataSet) {return true;}//ST19273
	this.getOptionAttribute = function(pAttributeName) {return null;}//ST19273	
	this.isSelected = XMLCombo_isSelected;//ST19273
	this.refresh();
	return this;
}

function XMLCombo_refresh(){
//	while (this.control.options.length > 0) { 
//		this.control.options.remove(0);
//	}
  this.control.innerHTML = "";

	this.dataSet.first();	
	while (!this.dataSet.eof) {
		var option = document.createElement("option");
		option.text = this.onGetOptionText(this, this.dataSet);
		option.value = this.onGetOptionValue(this, this.dataSet);
		this.setOptionAttribute(option, this.dataSet);//ST19273
		this.control.options.add(option);
		this.dataSet.next();
	}
	
  this.control.selectedIndex = -1
	this.dataSet.go(-1);
}

function XMLCombo_ControlOnChangeEvent() {
	event.srcElement.dataSet.go(event.srcElement.selectedIndex);
	if (event.srcElement.xmlCombo.onValueChange != null)
		event.srcElement.xmlCombo.onValueChange(event.srcElement.xmlCombo, event.srcElement.dataSet);
}

function XMLCombo_setValue(pValue) {
	if (this.control.value != pValue) {
		this.control.value = pValue;
		this.dataSet.go(this.control.selectedIndex);
		if (this.onValueChange != null) 
			this.onValueChange(this, this.dataSet);
	}
}

function XMLCombo_getValue() {
	return this.control.value;
}

function XMLCombo_getText() {
	var result = "";
	if (this.control.selectedIndex != -1) {
		result = this.control.options[this.control.selectedIndex].innerText;
	}
	return result;
}

function XMLCombo_setFilter(pFuncRef) {
  if (pFuncRef == null) {
		if (this.dataSet.filter != null) {
			this.dataSet.filtered(false);
			this.refresh();
		}
	}
	else {
		this.dataSet.filtered(false);
		this.dataSet.filter = pFuncRef;
		this.dataSet.filtered(true);
		if (this.dataSet.recordCount == 0) {
			this.dataSet.filtered(false);
		}
		this.refresh();
	}
}

function XMLCombo_isSelected(){//ST19273
	return (this.control.selectedIndex != -1);	
}

/*****************************************************************************/
/*XMLCustomCombo-------------------------------------------------------------*/
/*****************************************************************************/
function XMLCustomCombo(pComponentName, pXMLDef, pXMLDataSet, pSetOptAttFunction) {//ST19273
	this.optionsNode = pXMLDef.selectSingleNode("OPTIONS");
  //Public Properties
	this.dataSet = pXMLDataSet;
	this.name = pComponentName;

	this.optionValueField = this.optionsNode.getAttribute("optionvaluefield");
	this.optionTextField = this.optionsNode.getAttribute("optiontextfield");
	if (this.dataSet.findField(this.optionValueField) == null) {
		alert("XMLCombo: " + this.name + ", the " + this.optionValueField + " field doesn't exist." );
		return null;
	}
	if (this.dataSet.findField(this.optionTextField) == null) {
		alert("XMLCombo: " + this.name + ", the " + this.optionTextField + " field doesn't exist." );
		return null;
	}

	this.control = document.getElementById(this.optionsNode.getAttribute("control"));
	if (this.control == null) { 
		alert("XMLCombo: " + this.name + " control " + this.optionsNode.getAttribute("control") + "doesn't exist") 
		return null;
	}
  this.control.dataSet = this.dataSet;
	this.control.xmlCombo = this;
	this.control.attachEvent("onchange", XMLCombo_ControlOnChangeEvent);	
	
	//Methods
	this.refresh = XMLCombo_refresh;
  this.setValue = XMLCombo_setValue;
	this.getValue = XMLCombo_getValue;
	this.getText = XMLCombo_getText;
	this.setFilter = XMLCombo_setFilter;
	
	//Event Handlers
	this.onGetOptionText = function(pSender, pDataSet) { return pDataSet.fieldByName(pSender.optionTextField); }
	this.onGetOptionValue = function(pSender, pDataSet) { return pDataSet.fieldByName(pSender.optionValueField); }
  this.onValueChange = null;
 
  this.setOptionAttribute = pSetOptAttFunction;
	this.getOptionAttribute = XMLCustomCombo_getOptionAttribute;
	this.isSelected = XMLCombo_isSelected;
			
	this.refresh();
	return this;
}

function XMLCustomCombo_getOptionAttribute(pAttributeName){//ST19273
	var result = "";
	if (this.control.selectedIndex != -1) {
		result = this.control.options[this.control.selectedIndex].getAttribute(pAttributeName);
	}
	return result;	
}