if (!window.Ninemsn) window.Ninemsn = {};

/********* UTILITY FUNCTIONS **********/

Ninemsn.Utility = new function () {

	this.GetDateObjFromStringDate = function (serviceDate) {
		//2010-07-20T17:57:01.0000000
		var year = serviceDate.substr(0, 4);
		var month = serviceDate.substr(5, 2);
		if (month.substr(0, 1) == '0') {
			month = parseInt(month.substr(1, 1));
		}

		var day = serviceDate.substr(8, 2);
		if (day.substr(0, 1) == '0') {
			day = parseInt(day.substr(1, 1));
		}
		var hours = serviceDate.substr(11, 2);
		if (hours.substr(0, 1) == '0') {
			hours = parseInt(hours.substr(1, 1));
		}
		var minutes = serviceDate.substr(14, 2);
		if (minutes.substr(0, 1) == '0') {
			minutes = parseInt(minutes.substr(1, 1));
		}
		var seconds = serviceDate.substr(17, 2);
		if (seconds.substr(0, 1) == '0') {
			seconds = parseInt(seconds.substr(1, 1));
		}

		return new Date(year, month - 1, day, hours, minutes, seconds, 0);
	};

	this.GetDateDifferenceString = function (date, format) {
		var dateStr = '';

		if (date) {
			var now = new Date();
			var nowS = Math.floor(now.getTime() / 1000);
			var createdDateS = Math.floor(date.getTime() / 1000);
			var diff = nowS - createdDateS;

			if (diff < 60) {
				dateStr = diff + ' seconds ago';
			}
			else if (diff < 3600) {
				var diffMinutes = Math.floor(diff / 60);

				dateStr = diffMinutes + ' minutes ago';
			}
			else if (diff < 86400) {
				var diffHours = Math.floor(diff / 3600);
				var diffMins = Math.floor((diff % 3600) / 60);
				dateStr = '~ ' + diffHours + ' hours ago';
			}
			else {
				if (typeof (date.format) == 'function') {
					dateStr = date.format(format);
				}
			}
		}

		return dateStr;
	};

	this.GetQueryValue = function (key, url) {
		var param = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + key + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(url);
		if (results === null) {
			return '';
		}
		else {
			return results[1];
		}
	};

	this.AddCommas = function (nStr) {
		nStr += '';
		var x = nStr.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	};

	this.RightTrim = function (str) {
		return str.replace(/\s+$/, "");
	}

};

