//*** Prototypes and functional extensiom

Array.prototype.in_array = function(p_val) {
	for (var i = 0, l = this.length; i < l; i++) {
		if (this[i] == p_val) {
			return true;
		}
	}
	return false;
}

Array.prototype.array_key_exists = function(p_val) {
	if (typeof this[p_val] != "undefined") {
		return true;
	} else {
		return false;
	}
}

Array.prototype.array_merge = function(mArray) {
	for (var i=0; i<mArray.length; i++) {
		if (! this.in_array(mArray[i])) {
			this[this.length] = mArray[i];
		}
	}
	return this;
}

// date diff in days

Date.prototype.dateDiff = function(d) {
	return Math.round((d.valueOf() - this.valueOf()) / 86400000); 
}

// date adds

Date.prototype.add = function(n) {
	var d = new Date(this);
	d.setDate(d.getDate() + n);
	return d;
}

Date.prototype.addMonth = function(n) {
	var d = new Date(this);
	d.setMonth(d.getMonth() + n);
	return d;
}

Date.prototype.addYear = function(n) {
	var d = new Date(this);
	d.setFullYear(d.getFullYear() + n);
	return d;
}

//*** Implements function overloading
//		'name' is the base method name passed as a string: e.g., Foo.prototype.bar is passed as "bar"
//		'constructors' is an array of types for the arguments.  A method with a string and number arguments would be passed as [String,Number]
//		'method' is the function with its arguments: e.g., function(str,num)
// 		Peter Nederlof (http://blogger.xs4all.nl/peterned/)
Function.prototype.overload = function(name, constructors, method) {
	// get overloaded name based on arguments
	// becomes something like _barStringArray
	function _getName(args, name, instance) {
		var call = '_' + name, reg = /function ([^(]+)/;
		for(var obj,r,i=0; i<args.length; i++) {
			//*** Build the method name by inspecting the types (String, Array, etc.) of the arguments
			obj = instance ? args[i].constructor : args[i];
			call += ((r = reg.exec(obj.toString())) ? r[1] : 'Object');
		}
		return call;
	}

	// create the wrapper
	if(!this.prototype[name].overloaded) {
		var base = this.prototype[name];
		this.prototype[name] = function() {
			var call = this[_getName(arguments, name, true)] || base;
			call.apply(this, arguments);
		}
		this.prototype[name].overloaded = true;
	}
	
	// store prototype 
	this.prototype[_getName(constructors, name)] = method;
}

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};


