var suggesturl = 'http://new.studylight.org/cgi-bin/default/autosuggest.cgi?l=';

// pre-load images:
var image = new Array(4);

image[0] = new Image(), image[1] = new Image(),
image[2] = new Image(), image[3] = new Image();

image[0].src = "/images/arrow-down.gif", image[1].src = "/images/arrow-down-d.gif";
image[2].src = "/images/arrow-up.gif",   image[3].src = "/images/arrow-up-d.gif"; 

function actb(id)
{
	// Public Variables:
	this.actb_timeOut    = -1;                  // Autocomplete Timeout in ms (-1: autocomplete never time out)
	this.actb_lim        = 5;                   // Number of elements autocomplete can show (-1: no limit)
	this.actb_firstText  = true;                // should the auto complete be limited to the beginning of keyword?
	this.actb_mouse      = true;                // Enable Mouse Support
	this.actb_delimiter  = new Array(';', ','); // Delimiter for multiple autocomplete. Set it to empty array for single autocomplete
	this.actb_startcheck = 0;                   // Show widget only after this number of characters is typed in.

	// Styles:
	this.actb_arColor   = '#656291';  // background color for the "arrows"
	this.actb_bgColor   = '#FFFFFF';
	this.actb_textColor = '#000000';
	this.actb_hColor    = '#D6D7E7';
	this.actb_fFamily   = 'verdana,arial,helvetica';
	this.actb_arrowSize = "7px";
	this.actb_fSize     = '10px';
	this.actb_hStyle    = 'font-weight:800;font-family:verdana,arial,helvetica;color:#9C004A';

	// "Private" Variables:
	this.actb_keywords   = [];
	this.actb_delimwords = [];
	this.actb_cdelimword = 0;
	this.actb_delimchar  = [];
	this.actb_display    = false;

	this.actb_pos    = 0;
	this.actb_total  = 0;
	this.actb_rangeu = 0;
	this.actb_ranged = 0;
	this.actb_bool   = [];
	this.actb_pre    = 0;
	this.actb_toid   = 0;
	this.actb_tomake = false;

	this.actb_mouse_on_list = 1;
	this.actb_caretmove     = false;

	this.actb_id   = id;
	this.actb_curr = document.getElementById(id);

	return this.construct();
};

actb.prototype = {

	callLater: function(func, obj)
	{ return function() { func.call(obj) }; },

	construct: function()
	{
		this.actb_curr.actb = this;

		// pre-create event functions
		this.funcClick = this.actb_mouseclick;
		this.funcCheck = this.actb_checkkey;

		this.funcHighlight = this.actb_table_highlight;
		
		this.funcClear = this.callLater(this.actb_clear,    this);
		this.funcPress = this.callLater(this.actb_keypress, this);

		this.funcUp   = this.callLater(this.actb_goup,   this);
		this.funcDown = this.callLater(this.actb_godown, this);

		this.funcFocus   = this.callLater(this.actb_table_focus,   this);
		this.funcUnfocus = this.callLater(this.actb_table_unfocus, this);

		addEvent(this.actb_curr, "focus", this.callLater(this.actb_setup, this));

		return this;
	},

	actb_setup: function()
	{
		addEvent(document,       "keydown",  this.funcCheck);
		addEvent(this.actb_curr, "blur",     this.funcClear);
		addEvent(document,       "keypress", this.funcPress);
	},

	actb_clear: function()
	{
		removeEvent(document,       "keydown",  this.funcCheck);
		removeEvent(this.actb_curr, "blur",     this.funcClear);
		removeEvent(document,       "keypress", this.funcPress);

		this.actb_removedisp();
	},

	actb_parse: function(n)
	{
	    var t, plen;

		if(this.actb_delimiter.length > 0)
		{
			   t = this.actb_delimwords[this.actb_cdelimword].trim().addslashes();
			plen = this.actb_delimwords[this.actb_cdelimword].trim().length;
		}
		else
		{
			   t = this.actb_curr.value.addslashes();
			plen = this.actb_curr.value.length;
		}

		var tobuild = '';

		var re = this.actb_firstText ? new RegExp("^" + t, "i") : new RegExp(t, "i");
		var p  = n.search(re);

		tobuild = n.substr(0, p);

		tobuild += "<font style='" + (this.actb_hStyle) + "'>";

		tobuild += n.substring(p, plen + p);

		tobuild += "</font>";

		tobuild += n.substring(plen + p, n.length);

		return tobuild;
	},

	actb_generate: function()
	{
		var table_id = 'tat_table' + this.actb_id;
		if(document.getElementById(table_id))
		{
		  this.actb_display = false;
		  document.body.removeChild(document.getElementById(table_id));
		}

		if(this.actb_total == 0)
		{
			this.actb_display = false;
			return;
		}

		var table = document.createElement('table');
		table.id  = table_id;

		table.cellSpacing ='1px';
		table.cellPadding ='2px';

		table.style.position = 'absolute';
		table.style.border   = '#000000 solid 1px';

		table.style.top   = eval(curTop(this.actb_curr) + this.actb_curr.offsetHeight) + "px";
		table.style.left  = curLeft(this.actb_curr) + 1 + "px";
		table.style.width = this.actb_curr.offsetWidth + "px";

		table.style.backgroundColor = this.actb_bgColor;
		document.body.appendChild(table);

		var first = true, j = 1;

		if(this.actb_mouse)
		{
			table.onmouseout = this.funcUnfocus;
			table.onmouseover = this.funcFocus;
		}

		var counter = 0;
		for(var i = 0; i < this.actb_keywords.length; i++)
		{
			if((this.actb_keywords.length > this.actb_lim) && (this.actb_total > this.actb_lim) && !i)
			{
				var row = table.insertRow(-1);
				row.style.backgroundColor = this.actb_arColor;

				var cell = row.insertCell(-1);
				cell.style.color = this.actb_textColor;
				cell.style.fontFamily = 'arial narrow';
				cell.style.fontSize   = this.actb_arrowSize;
				cell.style.cursor     = 'default';
				cell.align = 'center';

				replaceHTML(cell, image[3]);
				addEvent(cell, "click", this.funcUp);
			}

			if(this.actb_bool[i] && (counter < this.actb_lim))
			{
				counter++;
				var row = table.insertRow(-1);
				if(first && !this.actb_tomake)
				{
					row.style.backgroundColor = this.actb_hColor;
					first = false;
					this.actb_pos = counter;
				}
				else if(this.actb_pre == i)
				{
					row.style.backgroundColor = this.actb_hColor;
					first = false;
					this.actb_pos = counter;
				}
				else
				{
					row.style.backgroundColor = this.actb_bgColor;
				}

				row.id = 'tat_tr' + this.actb_id + j;

				var cell = row.insertCell(-1);
				cell.style.color = this.actb_textColor;
				cell.style.fontFamily = this.actb_fFamily;
				cell.style.fontSize = this.actb_fSize;
				cell.innerHTML = this.actb_parse(this.actb_keywords[i]);
				cell.id   = 'tat_td' + this.actb_id + j;
				cell.actb = this; 
				cell.setAttribute('pos', j);

				if(this.actb_mouse)
				{
					cell.style.cursor = 'pointer';
					addEvent(cell, "click", this.funcClick);
					cell.onmouseover = this.funcHighlight;
				}

				j++;
			}
		}

		if(this.actb_total > this.actb_lim)
		{
			var row = table.insertRow(-1);
			row.style.backgroundColor = this.actb_arColor;

			var cell = row.insertCell(-1);
			cell.style.color = this.actb_textColor;
			cell.style.fontFamily = 'arial narrow';
			cell.style.fontSize = this.actb_arrowSize;
			cell.style.cursor = "pointer";
			cell.align = 'center';
			replaceHTML(cell, image[0]);

			addEvent(cell, "click", this.funcDown);
		}

		this.actb_rangeu = 1;
		this.actb_ranged = j - 1;
		this.actb_display = true;

		if(this.actb_pos <= 0) this.actb_pos = 1;
	},

	actb_remake: function()
	{
		var table = document.getElementById('tat_table' + this.actb_id);

		if(this.actb_mouse)
		{
			table.onmouseout  = this.funcUnfocus;
			table.onmouseover = this.funcFocus;
		}

		var i, k = 0;
		var first = true;
		var j = 1;

		if(this.actb_total > this.actb_lim)
		{
		    var allow_up_arrow = (this.actb_rangeu > 1);

			var row = table.rows[k++];
			row.style.backgroundColor = this.actb_arColor;

			var cell = row.firstChild;
			cell.style.color = this.actb_textColor;
			cell.style.fontFamily = 'arial narrow';
			cell.style.fontSize   = this.actb_arrowSize;
			cell.style.cursor     = 'default';
			cell.align = 'center';

			replaceHTML(cell, allow_up_arrow ? image[2] : image[3]);

			if(allow_up_arrow)
			{
				addEvent(cell, "click", this.funcUp);
				cell.style.cursor = 'pointer';
			}
			else
			{
				cell.style.cursor = 'default';
			}
		}

		for(var i = 0; i < this.actb_keywords.length; i++)
		{
			if(this.actb_bool[i])
			{
				if(j >= this.actb_rangeu && j <= this.actb_ranged)
				{
					var row = table.rows[k++];
					row.style.backgroundColor = this.actb_bgColor;
					row.id = 'tat_tr' + this.actb_id + j;

					var cell = row.firstChild;
					cell.style.color      = this.actb_textColor;
					cell.style.fontFamily = this.actb_fFamily;
					cell.style.fontSize   = this.actb_fSize;
					cell.innerHTML        = this.actb_parse(this.actb_keywords[i]);
					cell.id = 'tat_td' + this.actb_id + j;
					cell.setAttribute('pos', j);
					j++;
				}
				else
				{
					j++;
				}
			}

			if(j > this.actb_ranged) break;
		}

		if(this.actb_keywords.length > this.actb_lim)
		{
			var allow_down_arrow = ((j - 1) < this.actb_total);

			var row = table.rows[k];
			row.style.backgroundColor = this.actb_arColor;

			var cell = row.firstChild;
			cell.style.color = this.actb_textColor;
			cell.style.fontFamily = 'arial narrow';
			cell.style.fontSize = this.actb_arrowSize;
			cell.style.cursor = 'default';
			cell.align='center';

			replaceHTML(cell, allow_down_arrow ? image[0] : image[1]);

			if(allow_down_arrow)
			{
				addEvent(cell, "click", this.funcDown);
				cell.style.cursor = 'pointer';
			}
			else
			{
				cell.style.cursor = 'default';
			}
		}
	},
 
	actb_goup: function()
	{
		this.actb_curr.focus(); 

		if(!this.actb_display) return;
		if(this.actb_pos == 1) return;

		var row = document.getElementById('tat_tr' + this.actb_id + this.actb_pos);
		if(row && row.style) row.style.backgroundColor = this.actb_bgColor;

		this.actb_pos--;
		if(this.actb_pos < this.actb_rangeu)
		{
			this.actb_rangeu--;
			this.actb_ranged--;
			this.actb_remake();
		}

		row = document.getElementById('tat_tr' + this.actb_id + this.actb_pos);
		if(row && row.style) row.style.backgroundColor = this.actb_hColor;

		if(this.actb_toid)
		{
			clearTimeout(this.actb_toid);
			this.actb_toid = 0;
		}

		if(this.actb_timeOut > 0)
		  this.actb_toid = setTimeout(function() { this.actb_mouse_on_list = 1; this.actb_removedisp(); }, this.actb_timeOut);

		this.actb_curr.focus(); 
	},

	actb_godown: function()
	{
		this.actb_curr.focus(); 

		if(!this.actb_display) return;
		if(this.actb_pos == this.actb_total) return;

		var row = document.getElementById('tat_tr' + this.actb_id + this.actb_pos);
		if(row && row.style) row.style.backgroundColor = this.actb_bgColor;

		this.actb_pos++;
		if(this.actb_pos > this.actb_ranged)
		{
			this.actb_rangeu++;
			this.actb_ranged++;
			this.actb_remake();
		}

		row = document.getElementById('tat_tr' + this.actb_id + this.actb_pos);
		if(row && row.style) row.style.backgroundColor = this.actb_hColor;

		if(this.actb_toid)
		{
			clearTimeout(this.actb_toid);
			this.actb_toid = 0;
		}

		if(this.actb_timeOut > 0)
		  this.actb_toid = setTimeout(function() { this.actb_mouse_on_list = 1; this.actb_removedisp(); }, this.actb_timeOut);

		this.actb_curr.focus(); 
	},

	actb_mouseclick: function(event)
	{
		var elem = getTargetElement(event);
		if(!elem.id) elem = elem.parentNode;

		var obj = elem.actb; 
		if(!obj.actb_display) return;

		obj.actb_mouse_on_list = 0;
		obj.actb_pos = elem.getAttribute('pos');
		obj.actb_penter();
	},

	actb_table_focus: function()
	{ this.actb_mouse_on_list = 1; },

	actb_table_unfocus: function()
	{
		this.actb_mouse_on_list = 0;

		if(this.actb_toid)
		{
			clearTimeout(this.actb_toid);
			this.actb_toid = 0;
		}

		if(this.actb_timeOut > 0)
			this.actb_toid = setTimeout(function() { obj.actb_mouse_on_list = 0; this.actb_removedisp(); }, this.actb_timeOut);
	},

	actb_table_highlight: function(event)
	{
		var elem = getTargetElement(event);

		var obj = elem.actb;
		if(!obj) return;

		obj.actb_mouse_on_list = 1;

		var row = document.getElementById('tat_tr' + obj.actb_id + obj.actb_pos);
		if(row && row.style) row.style.backgroundColor = obj.actb_bgColor;

		obj.actb_pos = elem.getAttribute('pos');

		row = document.getElementById('tat_tr' + obj.actb_id + obj.actb_pos);
		if(row && row.style)
			row.style.backgroundColor = obj.actb_hColor;

		if(obj.actb_toid)
		{
			clearTimeout(obj.actb_toid);
			obj.actb_toid = 0;
		}

		if(obj.actb_timeOut > 0)
			obj.actb_toid = setTimeout(function(){ obj.actb_mouse_on_list = 0; obj.actb_removedisp(); }, obj.actb_timeOut);
	},
 
	actb_insertword: function(a)
	{
		if(this.actb_delimiter.length > 0)
		{
			var str = '';

			for(i = 0; i < this.actb_delimwords.length; i++)
			{
				if(this.actb_cdelimword == i)
				{
					prespace = postspace = '';
					gotbreak = false;
					for(j = 0; j < this.actb_delimwords[i].length; ++j)
					{
						if(this.actb_delimwords[i].charAt(j) != ' ')
						{
							gotbreak = true;
							break;
						}

						prespace += ' ';
					}

					for(j = this.actb_delimwords[i].length - 1; j >= 0; --j)
					{
						if(this.actb_delimwords[i].charAt(j) != ' ') break;
						postspace += ' ';
					}

					str += prespace;
					str += a;

					if(gotbreak) str += postspace;
				}
				else
				{
					str += this.actb_delimwords[i];
				}

				if(i != this.actb_delimwords.length - 1)
					str += this.actb_delimchar[i];
			}

			this.actb_curr.value = str;
			setCaret(this.actb_curr, this.actb_curr.value.length);
		}
		else
		{
			this.actb_curr.value = a;
		}

		this.actb_mouse_on_list = 0;
		this.actb_removedisp();
	},

	actb_penter: function()
	{
		if(!this.actb_display) return;

		this.actb_display = false;
		var word = '';
		var c = 0;

		for(var i = 0; i <= this.actb_keywords.length; i++)
		{
			if(this.actb_bool[i]) c++;
			if(c == this.actb_pos)
			{
				word = this.actb_keywords[i];
				break;
			}
		}

		this.actb_insertword(word);
	},

	actb_removedisp: function()
	{
		if(this.actb_mouse_on_list == 0)
		{
			this.actb_display = 0;

			var table = document.getElementById('tat_table' + this.actb_id);
			if(table)
			  document.body.removeChild(table);

			if(this.actb_toid)
			{
			  clearTimeout(this.actb_toid);
			  this.actb_toid = 0;
			}
		}
	},

	actb_keypress: function(e)
	{
		if(this.actb_caretmove) stopEvent(e);
		return !this.actb_caretmove;
	},

	actb_checkkey: function(event)
	{
		event = event || window.event;

		var code = event.keyCode;
		var obj = getTargetElement(event).actb; 
		obj.actb_caretmove = 0;

		var term = "";

		if(obj.actb_toid)
		{
			clearTimeout(obj.actb_toid);
			obj.actb_toid = 0;
		}

		switch(code)
		{
			case 37:
			case 39:
				return true;
				break;

			case 38:
				obj.actb_goup();
				obj.actb_caretmove = 1;
				return false;
				break;

			case 40:
				obj.actb_godown();
				obj.actb_caretmove = 1;
				return false;
				break;

			case 27:
				term = obj.actb_curr.value;

				obj.actb_mouse_on_list = 0;
				obj.actb_removedisp();
				break;

			case 13:
				if(obj.actb_display)
				{
					obj.actb_caretmove = 1;
					obj.actb_penter();
					return false;
				}
				break;

			case 9:
				if(obj.actb_display || obj.actb_toid)
				{
					obj.actb_caretmove = 1;
					obj.actb_penter();

					setTimeout(function() { obj.actb_curr.focus(); }, 25);
					return false;
				}
				break;

			default:
				obj.actb_toid = setTimeout(function() { obj.actb_tocomplete.call(obj, code) }, 200);
				break;
		}

		if(term.length) setTimeout(function() { obj.actb_curr.value = term; }, 25);
		return true;
	},

	actb_tocomplete: function(kc)
	{
		if(this.actb_toid)
		{
			clearTimeout(this.actb_toid);
			this.actb_toid = 0;
		}
		else
		{
			return;
		}

		if(kc == 38 || kc == 40 || kc == 13) return;

		if(this.actb_display)
		{ 
			var word = 0;
			var c = 0;

			for(var i = 0; i <= this.actb_keywords.length; i++)
			{
				if(this.actb_bool[i]) c++;

				if(c == this.actb_pos)
				{
					word = i;
					break;
				}
			}
			
			this.actb_pre = word;
		}
		else
		{
			this.actb_pre = -1;
		}
		
		if(this.actb_curr.value == '')
		{
			this.actb_mouse_on_list = 0;
			this.actb_removedisp();
		}

		var ot, t;

		if(this.actb_delimiter.length > 0)
		{
			var caret_pos_end = this.actb_curr.value.length;

			var delim_split = '';
			for(var i = 0; i < this.actb_delimiter.length; i++)
				delim_split += this.actb_delimiter[i];

		    delim_split = delim_split.addslashes();
			var delim_split_rx = new RegExp("([" + delim_split + "])");
			c = 0;
			this.actb_delimwords = [];
			this.actb_delimwords[0] = '';

			for(var i = 0, j = this.actb_curr.value.length; i < this.actb_curr.value.length; i++, j--)
			{
				if(this.actb_curr.value.substr(i, j).search(delim_split_rx) == 0)
				{
					ma = this.actb_curr.value.substr(i,j).match(delim_split_rx);
					this.actb_delimchar[c] = ma[1];
					c++;
					this.actb_delimwords[c] = '';
				}
				else
				{
					this.actb_delimwords[c] += this.actb_curr.value.charAt(i);
				}
			}

			var l = 0;
			this.actb_cdelimword = -1;
			for(i = 0; i < this.actb_delimwords.length; i++)
			{
				if((caret_pos_end >= l) && (caret_pos_end <= l + this.actb_delimwords[i].length))
					this.actb_cdelimword = i;

				l += this.actb_delimwords[i].length + 1;
			}

			ot = this.actb_delimwords[this.actb_cdelimword].trim(); 
			 t = this.actb_delimwords[this.actb_cdelimword].addslashes().trim();
		}
		else
		{
			ot = this.actb_curr.value;
			 t = this.actb_curr.value.addslashes();
		}

		if(ot.length == 0)
		{
			this.actb_mouse_on_list = 0;
			this.actb_removedisp();
		}
		else if( (ot.length == 1) ||
		       (((ot.length > 1)  && !this.actb_keywords.length)) ||
		        ((ot.length > 1)  && (this.actb_keywords[0].substr(0, 1).toLowerCase() != ot.substr(0, 1).toLowerCase())))
		{
			var ot_ = (ot.length > 1) ? ot.substr(0, 1) : ot;
			var res = document.getElementById('abbrev').value;

			var obj = this;

			// For local debugging in Mozilla/Firefox only!
			/*try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			} catch (e) { }*/ 

			function on_load()
			{
			    var xmlDocument = null, tmpinfo = null;

				try
				{
					xmlDocument = this.req.responseXML;
					tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
				}
				catch(e)
				{
					try
					{
						xmlDocument = (new DOMParser()).parseFromString(this.req.responseText, "text/xml");
						tmpinfo = xmlDocument.getElementsByTagName('listdata').item(0).firstChild.data;
					}
					catch(ee) {}
				}

				if(tmpinfo.indexOf('No entries') != -1)
				{
					alert('No entries were found starting with \'' + ot_ + '\'');

					active_textbox_obj.value = '';
					active_textbox_obj.focus();
				}
				else
				{
					obj.actb_keywords = tmpinfo.split('|');
					obj.done.call(obj, ot_, t);
				}
			}

			// create xmlhttprequest object:
			var http = new net.ContentLoader(suggesturl + ot_ + '&r=' + res, on_load); 
		}
		else
		{
			this.done(ot, t);
		}
	},

	done: function(ot, t)
	{
		if(ot.length < this.actb_startcheck) return;

		var re = new RegExp("^" + t, "i");

		this.actb_total = 0;
		this.actb_tomake = false;

		var al = this.actb_keywords.length;

		for(var i = 0; i < al; i++)
		{
			this.actb_bool[i] = false;
			if(re.test(this.actb_keywords[i]))
			{
				this.actb_total++;
				this.actb_bool[i] = true;

				if(this.actb_pre == i) this.actb_tomake = true;
			}
		}

		if(!this.actb_firstText)
		{
			var tmp = [];

			for(i = 0; i < al; i++)
			{
				if(this.actb_bool[i])
					tmp[tmp.length] = this.actb_keywords[i];
			}

			re = new RegExp(t, "i");

			for(i = 0; i < al; i++)
			{
				if(re.test(this.actb_keywords[i]) && !this.actb_bool[i])
				{
					this.actb_total++;
					this.actb_bool[i] = true;

					if(this.actb_pre == i) this.actb_tomake = true;

					tmp[tmp.length] = this.actb_keywords[i];
				}
			}

			for(i = 0; i < al; i++)
			{
				if(!this.actb_bool[i])
					tmp[tmp.length] = this.actb_keywords[i];
			}

			for(i = 0; i < al; i++)
				this.actb_keywords[i] = tmp[i];

			for(i = 0; i < al; i++)
				this.actb_bool[i] = (i < this.actb_total) ? true : false;
		}

		if(this.actb_timeOut > 0)
		  this.actb_toid = setTimeout(function(){ this.actb_mouse_on_list = 0; this.actb_removedisp(); }, this.actb_timeOut);

		this.actb_generate();
	}
}

// Supplementary functions

// Add an event to the obj given
// event_name refers to the event trigger, without the "on", like click or mouseover
// func_name refers to the function callback when event is triggered
function addEvent(obj, event_name, func_ref)
{
	if(obj.addEventListener && !window.opera)
	{
		obj.addEventListener(event_name, func_ref, true);
	}
	else
	{
		obj["on" + event_name] = func_ref;
	}
}

// Removes an event from the object
function removeEvent(obj, event_name, func_ref)
{
	if(obj.removeEventListener && !window.opera)
	{
		obj.removeEventListener(event_name, func_ref, true);
	}
	else
	{
		obj["on" + event_name] = null;
	}
}

// Stop an event from bubbling up the event DOM
function stopEvent(event)
{
	event = event || window.event;
	
	if(event.stopPropagation) event.stopPropagation();
	if(event.preventDefault) event.preventDefault();

	if(typeof event.cancelBubble != "undefined")
	{
		event.cancelBubble = true;
		event.returnValue = false;
	}

	return false;
}

// Get the obj that triggers off the event
function getTargetElement(event)
{
	event = event || window.event;
	return event.srcElement || event.target;
}
   
// Sets the caret position to l in the object
function setCaret(obj, l)
{
	obj.focus();

	if(obj.setSelectionRange)
	{
		obj.setSelectionRange(l, l);
	}
	else if(obj.createTextRange)
	{
		m = obj.createTextRange();		
		m.moveStart('character', l);
		m.collapse();
		m.select();
	}
}

// String functions
String.prototype.addslashes = function() { return this.replace(/(["\\\.\|\[\]\^\*\+\?\$\(\)])/g, '\\$1'); }

String.prototype.trim = function () { return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); };

// Offset position from top of the screen
function curTop(obj)
{
	var toreturn = 0;
	while(obj)
	{
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}

	return toreturn;
}

// Offset position from left of the screen
function curLeft(obj)
{
	var toreturn = 0;
	while(obj)
	{
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}

	return toreturn;
}

// Image installation
function replaceHTML(obj, oImg)
{
	var el = obj.childNodes[0];
	while(el)
	{
		obj.removeChild(el);
		el = obj.childNodes[0];
	}

	obj.appendChild(oImg);
}
