// Define content structure
function DataSelect() {
}

// Title
DataSelect.prototype.optionDef;
// General vars
DataSelect.prototype.xmlRowName;
DataSelect.prototype.dataSource;
DataSelect.prototype.selectId;
DataSelect.prototype.showAllOption;
DataSelect.prototype.allOptionLabel;

// Functions
DataSelect.prototype.load = getData;
DataSelect.prototype.render = renderSelect;

// HTTP Request
DataSelect.prototype.http_request;

function getData() {
  try {	
    // Remove old options
	var selectIds = new Array();
	var selects = new Array();
	selectIds = this.selectId.split(";");
	for(var i = 0; i < selectIds.length; i++) {
		selects[i] = document.getElementById(selectIds[i]);
		if(selects[i]) {
			while (selects[i].length > 0)
			{
				selects[i].remove(selects[i].length - 1);
			}
		}
	}

	var PAGE_SUCCESS = 200;

	this.http_request = get_http_request();

	this.http_request.open("get", this.dataSource, false);
	this.http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	 
	this.http_request.send(null);	

	if(this.http_request.readyState == 4)		
	{				
		if (this.http_request.status == 200)
		{	
			this.render(this.http_request.responseXML);
		}
	}	

  } catch(err){
    alert(err);
  }
}

function renderSelect(xmlData) {
  try {
    var rowNum = xmlData.getElementsByTagName(this.xmlRowName).length;

    // Write html to document
	var selectIds = new Array();
	var selects = new Array();
	selectIds = this.selectId.split(";");
	for(var i = 0; i < selectIds.length; i++)
		selects[i] = document.getElementById(selectIds[i]);
	
	// Render options
	if(this.showAllOption) {
		for(var i = 0; i < selectIds.length; i++) {
			var allOption = document.createElement('option');
			allOption.value = "";
			allOption.text = htmlEntitiesToUtf(this.allOptionLabel);
			if(selects[i])
				selects[i].options.add(allOption, selects[i].length);
		}
	}
    for(var i = 0; i < rowNum; i++) {
        var rowData = xmlData.getElementsByTagName(this.xmlRowName).item(i);
		rowValue = rowData.getElementsByTagName(this.optionDef['value']).item(0).childNodes.item(0).data.replace(/^\s+|\s+$/g, '');
		rowText = rowData.getElementsByTagName(this.optionDef['label']).item(0).childNodes.item(0).data.replace(/^\s+|\s+$/g, '');

		for(var j = 0; j < selectIds.length; j++) {
	        var newOption = document.createElement('option');
			newOption.value = rowValue;
			newOption.text = rowText;
			if(!this.showAllOption && i == 0)
				newOption.selected = true;
			if(selects[j])
				selects[j].options.add(newOption, selects[j].length);
		}
    }
  } catch(err) {
      alert(err);
  }
}

function get_http_request()
{
	var http_request;
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{ // IE
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");

		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request)
	{
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	} else {
		return http_request;
	}
}

function htmlEntitiesToUtf(input) {
	try {
		input = input.replace(/&nbsp;/g, " ");
		var nextEntity = 0;
		var output = input;
		while(true) {
			nextEntity = input.indexOf("&#", nextEntity);
			if(nextEntity == -1)
				break;
			else {
				nextEntity += 2;
				var charCode = input.substring(nextEntity, input.indexOf(";", nextEntity));
				output = output.replace("&#"+charCode+";", String.fromCharCode(charCode));
			}
		}
		return output;
	}
	catch(err) {
		return input;
	}
}
