function mysqlTimeStampToDate(timestamp) {
	//function parses mysql datetime string and returns javascript Date object
	//input has to be in this format: 2007-06-05 15:26:02
	var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
	var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
	return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}

function mysqlDateToDate(mysqlDate) {
	//function parses mysql date string and returns javascript Date object
	//input has to be in this format: 2007-06-05
	var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])$/;
	var parts=mysqlDate.replace(regex,"$1 $2 $3").split(' ');
	return new Date(parts[0],parts[1]-1,parts[2],0,0,0);
}

function checkTime(i) {
	if (i<10) {
		i="0" + i;
	}
	return i;
}

function getAmPmTime(date) {
	var curr_hour = date.getHours();
	var a_p = "";
	
	if (curr_hour < 12) {
		a_p = "AM";
	} else {
		a_p = "PM";
	}
	if (curr_hour == 0) {
		curr_hour = 12;
	}
	
	if (curr_hour > 12) {
	   curr_hour = curr_hour - 12;
	}
	
	return curr_hour + ':' + checkTime(date.getMinutes()) + ' ' + a_p;
}

function nl2br (str, is_xhtml) {
    // Converts newlines to HTML line breaks
    // 
    // version: 1003.2411
    // discuss at: http://phpjs.org/functions/nl2br

    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function formatDate(date1) {
  return date1.getFullYear() + '-' +
    (date1.getMonth() < 9 ? '0' : '') + (date1.getMonth()+1) + '-' +
    (date1.getDate() < 10 ? '0' : '') + date1.getDate();
}

function userFriendlyDate(date) {
	var today = new Date();
	var tomorrow = new Date();
	tomorrow.setDate(today.getDate() + 1);
	var yesterday = new Date();
	yesterday.setDate(today.getDate() - 1);
	if (date.toDateString() == today.toDateString())
		return 'Today';
	else if (date.toDateString() == tomorrow.toDateString())
		return 'Tomorrow';
	else if (date.toDateString() == yesterday.toDateString())
		return 'Yesterday';
	else {
		var sDays = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
		var sMonths = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
		return sDays[date.getDay()] + ', ' + sMonths[date.getMonth()] + ' ' + date.getDate();
	}
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1)
		{ 
			c_start = c_start + c_name.length+1 ;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end));
		} 
	}
	return "";
}

function setCookie(c_name,value,expiredays)	{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString());
}

function urlFormat(str) {
  var newstr = str.replace(/^\s+/g, "");
  newstr = newstr.replace(/\s+$/g, "");
  newstr = newstr.replace(/\r\n/g, " ");
  newstr = newstr.replace(/\n/g, " ");
  return encodeURIComponent(newstr);
}

// Add a leading '0' if string is only 1 char
function stringPad(str) {
  var newStr = "" + str;
  if (newStr.length == 1) {
    newStr = "0" + newStr;
  }
  return newStr;
}

// Converts the given time into UTC, returns this in a string
function getUTCDateString(timeObj) {
  var dateStr = "" + timeObj.getUTCFullYear();
  dateStr += stringPad(timeObj.getUTCMonth()+1);
  dateStr += stringPad(timeObj.getUTCDate());
  dateStr += "T" + stringPad(timeObj.getUTCHours());
  dateStr += stringPad(timeObj.getUTCMinutes()) + "00Z";
  return dateStr;
}

Date.prototype.addHours = function (n) {this.setHours (this.getHours () + n)};
