// $Id: prototypes.js,v 1.1.1.1 2009/04/02 00:08:57 cmanley Exp $

// Extend some core classes.
if (!String.prototype.toHtml) {
	String.prototype.toHtml = function() {
		return this.replace(/[&<>]/g, function(c) { if (c == '&') return '&amp;'; if (c == '<') return '&lt;'; if (c == '>') return '&gt;'; });
	}
}
if (!String.prototype.trim) {
	String.prototype.trim = function() {
		return this.replace(/(^\s+|\s$)/g, '');
	}
}
if (!String.prototype.nl2br) {
	String.prototype.nl2br = function() {
		return this.replace(/\r?\n/g, '<br/>');
	}
}
if (!Date.prototype.getUnixTime) {
	Date.prototype.getUnixTime = function() {
		return Math.floor(this.valueOf() / 1000);
	}
}
String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; }
String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; }
Number.prototype.zf = function(l) { return this.toString().zf(l); }
Date.prototype.format = function(f) {
	if (!this.valueOf()) {
		return ' ';
	}
	var d = this;
	return f.replace(/(yyyy|mm|dd|hh|nn|ss)/gi,
		function($1) {
			switch ($1.toLowerCase()) {
				case 'yyyy': return d.getFullYear();
				//case 'mmmm': return gsMonthNames[d.getMonth()];
				//case 'mmm':  return gsMonthNames[d.getMonth()].substr(0,3);
				case 'mm':   return (d.getMonth() + 1).zf(2);
				//case 'dddd': return gsDayNames[d.getDay()];
				//case 'ddd':  return gsDayNames[d.getDay()].substr(0, 3);
				case 'dd':   return d.getDate().zf(2);
				case 'hh':   return ((h = d.getHours() % 12) ? h : 12).zf(2);
				case 'nn':   return d.getMinutes().zf(2);
				case 'ss':   return d.getSeconds().zf(2);
			}
		}
	);
}
Date.prototype.isWeekDay = function() {
	var d = this.getDay();
	return (d != 0) && (d != 6);
}


// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
if (!Array.prototype.map) {
	Array.prototype.map = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function") {
			throw new TypeError();
		}
		var res = new Array(len);
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) {
				res[i] = fun.call(thisp, this[i], i, this);
			}
		}
		return res;
	};
}


if (!Array.prototype.grep) {
	Array.prototype.grep = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function") { // TODO: add support for RegExp too.
			throw new TypeError();
		}
		var res = new Array();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) {
				if (fun.call(thisp, this[i])) {
					res.push(this[i]);
				}
			}
		}
		return res;
	};
}


// http://www.schuerig.de/michael/javascript/
if (!Array.prototype.equals) {
	Array.prototype.equals = function(other) {
		if (!other) {
			return false;
		}
		var len = this.length;
		if (len != other.length) {
			return false;
		}
		for (var i = 0; i < len; i++) {
			if (this[i] != other[i]) {
				return false;
			}
		}
		return true;
	};
}

// From http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
			from += len;
		for (; from < len; from++) {
			if (from in this &&	this[from] === elt)
				return from;
		}
		return -1;
	};
}

// Based on http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf
if (!Array.prototype.indexOfNoCase) {
	Array.prototype.indexOfNoCase = function(elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		if (typeof(elt) != 'string') {
			return this.indexOf(elt, from);
		}
		elt = elt.toLowerCase();
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0) {
			from += len;
		}
		for (; from < len; from++) {
			if (!(from in this)) {
				continue;
			}
			var x = typeof(this[from]) == 'string' ? this[from].toLowerCase() : this[from];
			if (x == elt) {
				return from;
			}
		}
		return -1;
	};
}


// http://4umi.com/web/javascript/array.php#unique
// Array.unique( strict ) - Remove duplicate values
if (!Array.prototype.unique) {
	Array.prototype.unique = function(b) {
		var a = [], i, l = this.length;
		for (i=0; i<l; i++) {
			if (a.indexOf(this[i], 0, b) < 0) {
				a.push(this[i]);
			}
		}
		return a;
	}
};


if (!Array.prototype.uniqueNoCase) {
	Array.prototype.uniqueNoCase = function(b) {
		var a = [], i, l = this.length;
		for (i=0; i<l; i++) {
			if (a.indexOfNoCase(this[i], 0, b) < 0) {
				a.push(this[i]);
			}
		}
		return a;
	}
};


if (!Array.prototype.isUnique) {
	Array.prototype.isUnique = function() {
		return this.unique().equals(this);
	}
};


if (!Array.prototype.isUniqueNoCase) {
	Array.prototype.isUniqueNoCase = function() {
		var lc = function(s) { return typeof(s) == 'string' ? s.toLowerCase() : s; };
		var a = this.map(lc);
		var b = a.slice();
		return a.unique().length == b.length;
	}
};

