// Sidenav Menu & Submenu Toggle
function toggle_sidebar_menu(menu) {
	document.getElementById('sidebar_menu_' + menu).style.display = (document.getElementById('sidebar_menu_' + menu).style.display == 'none' ? '' : 'none');
	return false;
};

function toggle_sidebar_submenu(menu) {
	document.getElementById('sidebar_submenu_' + menu).style.display = (document.getElementById('sidebar_submenu_' + menu).style.display == 'none' ? '' : 'none');
	return false;
};

// Member List Player Details Toggle
function toggle_player_details(objid, hasbutton) {
	document.getElementById(objid).style.display = (document.getElementById(objid).style.display == 'none' ? '' : 'none');

	if(hasbutton)
	{
		document.getElementById(objid+'button').innerHTML = (document.getElementById(objid+'button').innerHTML == '+' ? '-' : '+');
	}

	return false;
};

// Calculate Bar Width
function calc_bar_width(current,total) {
	current = current + "";
	total = total + "";
	current = current.replace(",","");
	total = total.replace(",","");

	if (total <= 0)
	{
		total = 1;
	}

	var barwidth = (current / total)*100;

	if (barwidth > 100)
	{
		barwidth = 100;
	}

	return barwidth + '%';
};

// Select All Form Check Boxes
function form_check_all(form_name) {
	checked = document.getElementById(form_name + '_checkall_toggle').checked;

	for (var i = 0; i < document.forms[form_name].elements.length; i++) 
	{
		document.forms[form_name].elements[i].checked=checked;
	}

	return;
};

// Time Till functions
function calc_timetill(time, nowtoggle) {
	if (nowtoggle == null)
	{
		nowtoggle = true;
	}

	var processedtime = '';

	if (time <= 1 && nowtoggle)
	{
		//Lets setup a default
		processedtime = 'Now';
	}
	else if (time < 60) // Lets see how many seconds we have
	{
		processedtime = time + "s";
	}
	else if (time < 3600) //Lets see how many minutes we have
	{
		var minutes = Math.floor(time/60);
		processedtime =  minutes + 'm ' + calc_timetill(time%60, false);
	}
	else if (time < 86400) // Lets see how many hours we have
	{
		var hours = Math.floor(time/3600); 
		processedtime = hours + 'h ' + calc_timetill(time%3600, false);
	}
	return processedtime; 
};

function calc_timetill_alt(time, nowtoggle) {
	if (nowtoggle == null)
	{
		nowtoggle = true;
	}

	var processedtime = '';

	if (time <= 1 && nowtoggle)
	{
		//Lets setup a default
		processedtime = 'Now';
	}
	else if (time < 10) // Lets see how many seconds we have
	{
		processedtime = "0" + time;
	}
	else if (time < 60) // Lets see how many seconds we have
	{
		processedtime = time;
	}
	else if (time < 3600) //Lets see how many minutes we have
	{
		var minutes = Math.floor(time/60);
		processedtime =  minutes + ':' + calc_timetill_alt(time%60, false);
	}
	else if (time < 86400) // Lets see how many hours we have
	{
		var hours = Math.floor(time/3600); 
		processedtime = hours + ':' + calc_timetill_alt(time%3600, false);
	}
	return processedtime; 
};

// Correct for timezone differences
Date.prototype.setTimeZone = function(offset) {
	if (offset == null)
	{
		offset = 5;
	}

	var temp = new Date(0);
	dst = (temp.getTimezoneOffset() - this.getTimezoneOffset())/60;

	this.setHours(this.getUTCHours() - offset + dst);
};

// Format Date & Time - Simulates PHP's date function
Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};

Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Supported"; },
	// Week
	W: function() { return "Not Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Supported"; },
	// Year
	L: function() { return (((this.getFullYear()%4==0)&&(this.getFullYear()%100 != 0)) || (this.getFullYear()%400==0)) ? '1' : '0'; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Supported"; },
	g: function() { return this.getHours() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':' + (Math.abs(this.getTimezoneOffset() % 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() % 60)); },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return this.format("Y-m-d") + "T" + this.format("H:i:sP"); },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
};