<!--User List-->

<?xjs
	
	// Uncomment/comment properties below to enable/disable list columns
	var columns = {
//		number : { name : "#", type : "number" },
		alias : { name : "Alias", type : "string" },
//		name : { name : "Name", type : "string" },
//		age : { name : "Age", type : "number" },
//		gender : { name : "Sex", type : "string" },
		location : { name : "Location", type : "string" },
		laston_date : { name : "Last On", type : "date" },
		connection : { name : "Via", type : "string" },
//		firston_date : { name : "First On", type : "date" },
//		total_logons : { name : "Calls", type : "number" },
//		total_posts : { name : "Posts", type : "number" }
	};

	// Most people won't need to edit below this line
	load("sbbsdefs.js");

	var users = [];
	var url = format(
		"http://%s%s",
		http_request.host,
		http_request.request_string.split("&")[0]
	);
	
	var sortUsers;
	var sortBy;
	var sortOrder;

	if(	typeof http_request.query.sortby == "undefined"
		||
		typeof http_request.query.sortorder == "undefined"
		||
		columns[http_request.query.sortby[0].toLowerCase()] == undefined
		||
		columns[http_request.query.sortby[0].toLowerCase()].type == undefined
	) {
		sortUsers = function(a,b) { return 0; }
	}
	else {
		sortBy = http_request.query.sortby[0].toLowerCase()
		sortOrder = http_request.query.sortorder[0];
		switch(columns[sortBy].type) {
			case 'string':
				if(sortOrder == "ascending")
					sortUsers = function(a,b) {
						if(a[sortBy].toLowerCase() < b[sortBy].toLowerCase())
							return -1;
						if(a[sortBy].toLowerCase() > b[sortBy].toLowerCase())
							return 1;
						return 0;
					};
				else
					sortUsers = function(a,b) {
						if(a[sortBy].toLowerCase() > b[sortBy].toLowerCase())
							return -1;
						if(a[sortBy].toLowerCase() < b[sortBy].toLowerCase())
							return 1;
						return 0;
					};
				break;
			case 'date':
			case 'number':
				if(sortOrder == "ascending")
					sortUsers = function(a,b) { return a[sortBy]-b[sortBy]; };
				else
					sortUsers = function(a,b) { return b[sortBy]-a[sortBy]; };
				break;
			default:
				sortUsers = function(a,b) { return 0; }
		}
	}
	
	var makeSortURLs = function(field, order) {
		return format(
			'<a class="ulLink" href="%s&sortby=%s&sortorder=ascending">'
			+ '<img src="./icons/up-arrow.png">'
			+ '</a>'
			+ '<a class="ulLink" href="%s&sortby=%s&sortorder=descending&">'
			+ '<img src="./icons/down-arrow.png">'
			+ '</a>',
			url,
			field,
			url,
			field
		);
	}
	
	var copyProperties = function(source, dest) {
		for(var property in columns) {
			if(source[property] == undefined)
				continue;
			if(typeof source[property] == "string" || typeof source[property] == "number")
				dest[property] = source[property];
		}
		return dest;
	}

	var usr = new User(1);
	for(var u = 1; u < system.lastuser; u++) {
		usr.number = u;
		if(usr.settings&USER_DELETED || usr.security.restrictions&UFLAG_Q)
			continue;
		var usr2 = copyProperties(usr, {});
		users.push(copyProperties(usr.stats, usr2));
	}
	users.sort(sortUsers);
	
?>

<span class="title">User List</span>
<table class="font" style="clear: both; margin: 0;">
<tr>
<?xjs
	for(var c in columns)
		print(format("<td>%s %s</td>", columns[c].name, makeSortURLs(c)));
?>
</tr>

<?xjs

for(var u in users) {
	print(format("<tr class='%s'>", (u % 2 == 0) ? "row1" : "row2"));
	for(var c in columns) {
		print(
			format(
				"<td>%s</td>",
				(columns[c].type == "date") ? system.timestr(users[u][c]) : users[u][c].toString()
			)
		);
	}
	print("</tr>");
}

?>

</table>

<?xjs

?>
