/** Orbis JavaScript Utils
 *
 *  Collect all simple loose javascript functions in here.
 */
 
function printDebugMessage(msg) 
{
	// open new, or get handle to existing window
	var w = window.open("", "debugWindow", "");		
	w.document.writeln(msg + "<hr>");
}

//create an AJAX request object
function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}

/** Orbis Js API */
var Orbis = {};

/** Orbis Form Utils */
Orbis.Forms = {};
// Adapted from [http://www.mredkj.com/tutorials/tutorial005.html]
// Removes the currently selected HTMLOptionElement from the HTMLSelectElement selectElm
Orbis.Forms.removeOptionSelected = function(selectElm) {
  var i;
  for (i = selectElm.length - 1; i>=0; i--) {
    if (selectElm.options[i].selected) {
      selectElm.remove(i);
    }
  }
}

// Sorts an HTMLSelectElement by option texts.
Orbis.Forms.sortSelectList = function(selectListElm) {
	var optLabels = new Array();			
	var optValuesMap = {};
	
	for (var i=0; i < selectListElm.length; i++)  {
		var theLabel = selectListElm.options[i].text;
		optLabels[i] = theLabel;
	  	optValuesMap[theLabel] = selectListElm.options[i].value;
	}
	
	optLabels.sort();
	
	for (var i=0; i < selectListElm.length; i++)  {
	  	selectListElm.options[i].text = optLabels[i];
	  	selectListElm.options[i].value = optValuesMap[optLabels[i]];
	}
}

// Adapted from [http://www.mredkj.com/tutorials/tutorial005.html]
// Appends an new HTMLOptionElement to the HTMLSelectElement selectElm.
// The new option will have a text value of aText and the value aValue.
Orbis.Forms.appendOption = function(selectElm, aText, aValue)	{
  var elOptNew = document.createElement('option');
  elOptNew.text = aText;
  elOptNew.value = aValue

  try {
    selectElm.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    selectElm.add(elOptNew); // IE only
  }
}

// Returns the currently selected option from the HTML Select List
Orbis.Forms.getOptionSelected = function(selectElm) {
  for (var i = selectElm.length - 1; i>=0; i--) {
    if (selectElm.options[i].selected) {
      return selectElm.options[i];
    }
  }
}