function loadCoachDetails(id)
{
	document.getElementById('coach-details-row-' + id).className = '';
	var coachDetails = document.getElementById('coach-details-' + id);
	coachDetails.innerHTML = 'H&auml;mtar detaljer...';
	addAjaxLoader('coach-details-' + id);
	ajaxLoadContent('coach-details-' + id, 'c admin_coach_details.html?id=' + id);
	return false;
}

function closeCoachDetails(id)
{
	document.getElementById('coach-details-row-' + id).className = 'hidden';
	return false;
}

function selectTeamToAdd(teamId, competitionId)
{
	document.getElementById('id').value = teamId;
	document.getElementById('competition.id').value = competitionId;
	return true;
}
function getExtraPath()
{
	return typeof(extraPath) == "undefined" ? '' : extraPath;
}

function addAjaxLoader(targetId)
{
	var ajaxLoader = document.createElement('div');
	ajaxLoader.className = 'ajaxloader';
	var target = document.getElementById(targetId);
	target.appendChild(ajaxLoader);
}

function ajaxLoadContent(targetId, url)
{
	var target = document.getElementById(targetId);

	url = getExtraPath() + url;
	url += url.indexOf('?') != -1 ? '&' : '?';
	url += 'ts=' + new Date().getTime();
	url += "&contentID=" + targetId;

	var xmlHttp = null;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			try
			{
				if (xmlHttp.status == 200)
				{
					target.innerHTML = xmlHttp.responseText;
				}
			}
			catch (e)
			{
				target = "H&auml;mtningen misslyckades, f&ouml;rs&ouml;k igen.";
			}
		}
	};
	
	xmlHttp.open("get", url);
	xmlHttp.send(null);
}
function coachenStrip(value)
{
    return value.replace(/^\s+/, '').replace(/\s+$/, '');
}

function coachenHasClassName(element, className)
{
	var elementClassName = element.className;
	return (elementClassName.length > 0 && (elementClassName == className || new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
}

function coachenAddClassName(element, className)
{
	if (!coachenHasClassName(element, className))
	{
		element.className += (element.className ? ' ' : '') + className;
	}
}

function coachenRemoveClassName(element, className)
{
	element.className = coachenStrip(element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' '));
}
function showComments(blogEntryId)
{
	document.getElementById('comments-' + blogEntryId).style.display = 'block';
	return false;
}

function hideSpamFields(id)
{
	document.getElementById('name-' + id).style.display = 'none';
	document.getElementById('email-' + id).style.display = 'none';
	document.getElementById('body-' + id).style.display = 'none';
}

function checkCommentFields(id)
{
	var formOk = document.getElementById('title-' + id).value.length > 0 && document.getElementById('message-' + id).value.length > 0 && document.getElementById('postername-' + id).value.length > 0 && document.getElementById('posteremail-' + id).value.length > 0;
	if (!formOk)
	{
		alert('Fyll i rubrik, kommentar, ditt namn och din e-postadress.');
	}
	return formOk;
}
function coachenCountDown(diffTime, year, month, day, hour, min, place)
{
	var stopTime = new Date(year, month, day, hour, min)
	var now = new Date(new Date().getTime() - diffTime);
	if (now < stopTime)
	{
		var iDays = Math.floor((stopTime - now) / (1000 * 60 * 60 * 24));
		var iHours = Math.floor((stopTime - now - iDays * 1000 * 60 * 60 * 24) / (1000 * 60 * 60));
		var iMins = Math.floor((stopTime - now - iDays * 1000 * 60 * 60 * 24 - iHours * 1000 * 60 * 60) / (1000 * 60));
		var iSecs = Math.floor((stopTime - now - iDays * 1000 * 60 * 60 * 24 - iHours * 1000 * 60 * 60 - iMins * 1000 * 60) / (1000));
		document.getElementById(place).innerHTML = (iDays > 0 ? iDays + " " + getSingularOrPlural(iDays, 'dag', 'dagar') + ", " : "") + (iDays > 0 || iHours > 0 ? iHours + " " + getSingularOrPlural(iHours, 'timme', 'timmar') + ", " : "") + (iDays > 0 || iHours > 0 || iMins > 0 ? iMins + " " + getSingularOrPlural(iMins, 'minut', 'minuter') + ", " : "") + iSecs + " " + getSingularOrPlural(iSecs, 'sekund', 'sekunder');
		setTimeout("coachenCountDown(" + diffTime + "," + year + "," + month + "," + day + "," + hour + ", " + min + ", '" + place + "')", 100);
	}
	else
	{
		document.getElementById(place).style.display = 'none';
	}
}

function getSingularOrPlural(value, singular, plural)
{
	return value == 1 ? singular : plural;
}
function toogleFaq(link)
{
	var faq = link.parentNode.parentNode.getElementsByTagName("div")[0];
	faq.className = faq.className == 'hidden' ? '' : 'hidden';
	return false;
}
function showLogin()
{
	document.getElementById('coachen-login').style.display = 'block';
	return false;
}

function hideLogin()
{
	document.getElementById('coachen-login').style.display = 'none';
	return false;
}
function loadPlayersByTeam(teamId)
{
	var playersListDiv = document.getElementById('players-in-team');
	playersListDiv.innerHTML = 'H&auml;mtar spelare...';
	addAjaxLoader('players-in-team');
	window.scrollTo(0,0);
	ajaxLoadContent('players-in-team', 'c player_list.html?id=' + teamId);
	return false;
}
function loadPlayersByCoachTypeIdAndTeamIdAndPositionId(coachTypeId, teamId, positionId)
{
	var playersListDiv = document.getElementById('players-in-team');
	playersListDiv.innerHTML = 'H&auml;mtar spelare...';
	addAjaxLoader('players-in-team');
	window.scrollTo(0,0);
	ajaxLoadContent('players-in-team', 'c team_trade_playerlist.html?id=' + teamId + "&positionId=" + positionId + "&coachTypeId=" + coachTypeId);
	return false;
}

function tradePlayer(id)
{
	document.getElementById('trade-progress').style.display = 'block';
	document.getElementById('inPlayer.id').value = id;

	var tradeForm = document.getElementById('trade-form');
	tradeForm.style.display = 'none';
	tradeForm.submit();
	return false;
}
