/*******************************************************************************
Javascript Utilities v1.0
A collection of various javascript functions that make life easier.
*******************************************************************************/

/*******************************************************************************
Events
*******************************************************************************/

function addListener(el, strEvent, handler) //{{{===============================
{
	if (el.addEventListener) return el.addEventListener(strEvent, handler, false);
	else if (el.attachEvent) return el.attachEvent('on' + strEvent, handler);
	throw("addEventListener: Can't add event to the given element.");
	return false;  
}
//}}}

/*******************************************************************************
General
*******************************************************************************/

function getDivHeight(div) //{{{================================================
{
	var d;
	var divHeight = 0;
	
	d = div;
	if (typeof(div) == "string")
		d = document.getElementById(div);
	
	if (!d)
	{
		throw("The given element or element id does not exist.");
		return;
	}

	if (d.offsetHeight)				return d.offsetHeight;
	else if (d.style.pixelHeight)	return d.style.pixelHeight;
	return false;
}
//}}}

/*******************************************************************************
Strings
*******************************************************************************/

// Trims whitespace off the beginning and end
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); }

// Splits the string by given separator and returns an array with trimmed items.
String.prototype.splitrim = function(t) { return this.trim().split(new RegExp('\\s*'+t+'\\s*')) }

// Escapes HTML in the string:
String.prototype.escHtml = function(){ var i,e={'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t }

// Unescapes HTML in the string:
String.prototype.unescHtml = function(){ var i,e={'&lt;':'<','&gt;':'>','&amp;':'&','&quot;':'"'},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t }

// URL encodes the string:
String.prototype.urlEncode = function(){ return encodeURIComponent(this); }

// Checks if the string is a valid email address:
String.prototype.isEmail = function () { var rx = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }

// Checks if the string is a valid URL address:
String.prototype.isURL = function () { var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?"); var matches = rx.exec(this); return (matches != null && this == matches[0]); }

// Checks if the string contains the passed as parameter value:
String.prototype.contains = function(t) { return this.indexOf(t) >= 0 ? true : false; }

// Checks if the string begins with the passed as parameter value. The second parameter is for ignore case:
String.prototype.beginsWith = function(t, i) { if (i==false) { return (t == this.substring(0, t.length)); } else { return (t.toLowerCase() == this.substring(0, t.length).toLowerCase()); } }

// Checks if the string ends with the passed as parameter value. The second parameter is for ignore case:
String.prototype.endsWith = function(t, i) { if (i==false) { return (t == this.substring(this.length - t.length)); } else { return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase()); } }

