/**
 * Basic object extentions for the Rumble toolkit
 * 
 * Paul Serby - paul.serby@clock.co.uk
 * Clock.
 * 22 April 2007 
 */
window.clearSelection = function() {
	try {
		window.getSelection().removeAllRanges();
	} catch(e) {};
	try {
		document.selection.clear();
	} catch(e) {};	
};

Object.extend(String.prototype, {
  trim: function() {
	  return this.replace(/^\s*|\s*$/g, "");
	}}
);

Object.extend(Date.prototype, {
  setFromTimestamp: function(timestamp) {
  	if (matches = /(\d\d\d\d)\-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d).*/.exec(timestamp)) {
			this.setDate(parseInt(matches[3], 10));
			this.setMonth(parseInt(matches[2], 10) - 1);
			this.setYear(parseInt(matches[1], 10));	
			this.setHours(parseInt(matches[4], 10));
			this.setMinutes(parseInt(matches[5], 10));
			this.setSeconds(parseInt(matches[6], 10));
			this.setMilliseconds(0);
		} else if (matches = /(\d\d\d\d)\-(\d\d)-(\d\d) (\d\d):(\d\d).*/.exec(timestamp)) {
			this.setDate(parseInt(matches[3], 10));
			this.setMonth(parseInt(matches[2], 10) - 1);
			this.setYear(parseInt(matches[1], 10));
			this.setHours(parseInt(matches[4], 10));
			this.setMinutes(parseInt(matches[5], 10));
			this.setSeconds(0);
			this.setMilliseconds(0);
		}  else if (matches = /(\d\d\d\d)\-(\d\d)-(\d\d).*/.exec(timestamp)) {
			this.setDate(parseInt(matches[3], 10));
			this.setMonth(parseInt(matches[2], 10) - 1);
			this.setYear(parseInt(matches[1], 10));
			this.setHours(0);
			this.setMinutes(0);
			this.setSeconds(0);
			this.setMilliseconds(0);
		} else {
			this.setTime(timestamp);
		}
	}}
);

Object.extend(Date.prototype, {
  toString: function() {
		return this.getFullYear().toPaddedString(4) + "-" + (this.getMonth() + 1).toPaddedString(2) + "-" + this.getDate().toPaddedString(2) + " " + this.getHours().toPaddedString(2) + ":" + this.getMinutes().toPaddedString(2) + ":" + this.getSeconds().toPaddedString(2);
	}}
);
