/** Orbis Prototype Extensions and Classes
 *  
 * All Prototype Framework extensions and classes should be placed here.
 *
 * Requires that the Prototype JavaScript framework [http://prototype.conio.net/] 
 * has been already included.
 */

/**
 * 
 */
Object.extend(String.prototype, {
	// Returns a copy of this String with leading and trailing whitespace removed. 
	trim: function() {
		return this.replace(/^\s+|\s+$/, '');
	},
  
  	// Returns true if this String starts with the given prefix.
	startsWith: function(prefix) {
  		return this.indexOf(prefix) == 0;
	},
	
	// Returns true if this String ends with the given suffix.
	endsWith: function(suffix) {
		return this.lastIndexOf(suffix) == this.length - suffix.length;
	}
});



/**
 * Returns an Array of all elements in the document. 
   See [http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html] 
   for a complete list.
 */
document.getAllElements = function() {
	var recurseForElements = function(node, nodeList) {
		if ( nodeList == undefined ) {
			nodeList = new Array();
		}
		
		nodeList.push(node);
		
		if (node.childNodes != undefined ) {
			for (var i=0; i < node.childNodes.length; i++) {
				var curNode = node.childNodes[i];				
				recurseForElements(curNode, nodeList);
			}			
		}
		
		return nodeList;
	}
	
	return recurseForElements(document.documentElement);
}

/**
 * Returns List of elements whose IDs start with the prefix.
 */
document.getElementsByIdStartingWith = function(prefix) {
	var matchedElms = new Array();
	document.getAllElements().each( function(elm) {
		if ( elm.id != undefined && elm.id.length != undefined && elm.id.startsWith(prefix) ) {
			matchedElms.push(elm);
		}
	});	
	return matchedElms;
}











