// Existing object hacks
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");};
String.prototype.ltrim = function() {return this.replace(/^\s+/g,"");};
String.prototype.rtrim = function() {return this.replace(/\s+$/g,"");};
String.prototype.supplant = function (o) {return this.replace(/{([^{}]*)}/g,function (a, b) {var r = o[b];return typeof r === 'string' ? r : a;});};
// Gets total number of days in a given month
Date.prototype.daysInMonth = function () {return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate()};
// Gets the day of the week (numeric) that a particular date falls on
Date.prototype.dayOfTheMonth = function() {return new Date(this.getFullYear(), this.getMonth(), 1).getDay()};
// Core jlp JS object
jlp = {};

// Utility branch
jlp.util = {};

jlp.util.walkTheDOM = function(node, func) {
	func(node);
	node = node.firstChild;
	while(node) {
		jlp.util.walkTheDOM(node,func);
		node = node.nextSibling;
	}
};

jlp.util.getElementsByClassName = function(className){
	var results = [];
	jlp.util.walkTheDOM(document.body, function(node) {
		var a, c = node.className, i;
		if (c) {
			a = c.split(' ');
			for (i = 0; i < a.length; i += 1) {
				if (a[i] === className) {
					results.push(node);
					break;
				}	
			}
		}				   
	});
	return results;
};


// Replaces document.getElementById()
jlp.util.fetch = function() {
	if(arguments.length) {
		var i, e, a = [];
		for (i=0; i < arguments.length; i++) {
			e = arguments[i];
			if (typeof arguments[i] === 'string') {
				e = document.getElementById(e);	
			}
			if (arguments.length === 1) {
				return e;	
			}
			a.push(e);
		}
		return a;
	}	
};

jlp.util.clone = function(o) {
	var F = function() {};
	F.prototype=o;
	return new F();
};

jlp.util.addEvent = function(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() {
			obj["e"+type+fn]( window.event );
		}
		obj.attachEvent("on"+type, obj[type+fn]);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
};

jlp.util.purge = function (d) {
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i += 1) {
			n = a[i].name;
			if (typeof d[n] === 'function') {
				d[n] = null;
			}
		}
	}
	a = d.childNodes;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i += 1) {
			jlp.util.purge(d.childNodes[i]);
		}
	}
};

