//-----------------------------------------------
// Populates a selectbox with options from the
// specified array. If the array is empty the
// selectbox is hidden.
//-----------------------------------------------
function populateSelectFromArray(selectId, arr)
{
	var select = $(selectId);
	if (select)
	{
		clearSelect(selectId);
		
		if (arr.length > 0)
		{
			select.show();
			for (var i = 0; i < arr.length; i++)
			{
				select.options[i] = new Option(arr[i][0], arr[i][1]);
			}
			select.selectedIndex = 0;
		}
		else
		{
			select.hide();
		}	
	}
}

//-----------------------------------------------
// Removes all options from a selectbox
//-----------------------------------------------
function clearSelect(selectId)
{
	var select = $(selectId);
	if (select)
	{
		for (var i = select.options.length-1; i >= 0; i--)
		{
			select.options[i] = null;
		}
		select.selectedIndex = -1;
	}
}

//-----------------------------------------------
// Swaps visibility of all elements in the supplied array
//-----------------------------------------------
function swapElementVisibility(arrElementIds)
{
	for (var i = 0; i < arrElementIds.length; i++)
	{
		$(arrElementIds[i]).toggle();		
	}
}

//-----------------------------------------------
// Pops up the "can I edit?"-window
//-----------------------------------------------
function openCanIEditPopup()
{
	window.open('../util/caniedit.php', 'caniedit', 'width=400,height=290,left=200,top=200,resizable=no');
}

//-----------------------------------------------
// Opens a window for editing the Wikipedia url for a team
//-----------------------------------------------
function openWikiUrlEdit(teamId)
{
    var width  = 500;
    var height = 280;

    // Center window on screen
    var left = (screen.width - width)  / 2;
    var top  = Math.max(((screen.height - height) / 2) - 50, 0);

	var win = window.open('editWikiUrl.php?teamId=' + teamId, 'editWikiUrl', 'width='+width+',height='+height+',left='+left+',top='+top+',resizable=no,status=yes');
	win.focus();
}

//-----------------------------------------------
// Opens a window for editing the Google maps url for a team
//-----------------------------------------------
function openGoogleMapsUrlEdit(teamId)
{
    var width  = 500;
    var height = 280;

    // Center window on screen
    var left = (screen.width - width)  / 2;
    var top  = Math.max(((screen.height - height) / 2) - 50, 0);

	var win = window.open('editGoogleMapsUrl.php?teamId=' + teamId, 'editGMapsUrl', 'width='+width+',height='+height+',left='+left+',top='+top+',resizable=no,status=yes');
	win.focus();
}

//-----------------------------------------------
// Opens the league compare window
//-----------------------------------------------
function openLeagueCompare(stageId, groupId)
{
	// Size the window slightly smaller than the screen
    var width  = (screen.width  * 0.85);
    var height = 680;

    // Center window on screen
    var left = (screen.width   - width)  / 2;
    var top  = Math.max(((screen.height - height) / 2) - 50, 0);

	var url = 'leagueCompare.php?stageId=' + stageId + '&groupId=' + groupId;
 	var win = window.open(url, 'leagueCompare', 'width='+width+',height='+height+',left='+left+',top='+top+',resizable=yes,scrollbars=yes,status=yes');
	win.focus();
}

//-----------------------------------------------
// Constructs an html-page for printing
//-----------------------------------------------
function printPage(siteRoot) 
{
	if (!window.print)
	{
		window.status = 'No print';
		return;
	}
	
	// The main content area
	var contentDiv = document.getElementById('contentdiv');		
		
	if(contentDiv) 
	{
		contentHtml = '<div style="padding: 8mm;">' + contentDiv.innerHTML + '</div>';
		
		var beginHtml = 
			'<html>' +
			'<head>' +
			'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'	+		
			'<link rel="stylesheet" type="text/css" href="' + siteRoot + 'styles/main.css">' +
			'<link rel="stylesheet" type="text/css" href="' + siteRoot + 'styles/print.css">' +
			'<title>SoccerDB</title>' +
			'</head>' +
			'<body>';
			
		var endHtml = "</body></html>";

		var printWin = window.open('about:blank','','width=705,height=540,scrollbars=yes,toolbar=yes');
		printWin.document.open();
		printWin.document.write(beginHtml + contentHtml + endHtml);
		printWin.document.close();
		
		printWin.print();
		printWin.close();
	}
}

//-----------------------------------------------
// Validate the form before submitting
//-----------------------------------------------
function validateEditMatchesForm(form)
{
	for (i = 0; i < form.elements.length; i++)
	{
		// Make sure goals are numeric
		if (((form.elements[i].name.substr(0,9) == 'homeGoals') ||
			(form.elements[i].name.substr(0,9) == 'awayGoals')) &&
			(!/^\d*$/.test(form.elements[i].value)))
		{
			alert('You can only enter numeric values for goals.');
			form.elements[i].focus();
			return false;
		}

		// Check the date. this check is not complete but the date format
		// is also checked in the php-page before it's stored in the db.
		if ((form.elements[i].name.substr(0,9) == 'matchDate') &&
			(!/^[1-2]\d\d\d\-[0-1]\d\-[0-3]\d$/.test(form.elements[i].value))) 
		{
			alert('Please enter a valid date on the format YYYY-MM-DD.');
			form.elements[i].focus();
			return false;
		}
	}

	return true;
}

//-----------------------------------------------
// Shows or hides rows in a table.
// Pass -1 for visibleRowCount to display all rows.
//-----------------------------------------------
function showTableRows(baseId, visibleRowCount)
{
	// Show or hide table rows
	var table = document.getElementById(baseId + 'Table');
	if (table)
	{
		var rows = table.getElementsByTagName('tr');
		for (var i = 0; i < rows.length; i++)
		{
			rows[i].style.display = 
				((visibleRowCount == -1) || 
				 (i < visibleRowCount) || 
				 (i == rows.length-1)) ? '' : 'none';
		}
	}
	
	// Show or hide More and Less links
	var moreLink = document.getElementById(baseId + 'MoreLink');
	var lessLink = document.getElementById(baseId + 'LessLink');
	if (moreLink && lessLink)
	{
		if (visibleRowCount == -1) // -1 = show all rows
		{
			moreLink.style.display = 'none';
			lessLink.style.display = 'inline';
		}
		else
		{
			moreLink.style.display = 'inline';
			lessLink.style.display = 'none';
		}
	}
}

//-----------------------------------------------
// Activates the league tab.
//-----------------------------------------------
function showLeagueTab(isActive)
{
	$('LeagueTabHeader').className	= 'LeftMenuTab';
	$('TeamTabHeader').className	= 'LeftMenuTab LeftMenuTabInactive';
	
	$('LeagueTab').show();
	$('TeamTab').hide();
	
	if (isActive)
	{
		if ($('ContextMenuDiv')) $('ContextMenuDiv').show();
	}
	else
	{
		if ($('ContextMenuDiv')) $('ContextMenuDiv').hide();	
	}
}

//-----------------------------------------------
// Activates the team tab.
//-----------------------------------------------
function showTeamTab(isActive)
{
	$('TeamTabHeader').className	= 'LeftMenuTab';
	$('LeagueTabHeader').className	= 'LeftMenuTab LeftMenuTabInactive';

	$('TeamTab').show();
	$('LeagueTab').hide();

	if (isActive)
	{
		if ($('ContextMenuDiv')) $('ContextMenuDiv').show();
	}
	else
	{
		if ($('ContextMenuDiv')) $('ContextMenuDiv').hide();	
	}
}

//-----------------------------------------------
// Toggles css on a tab when the cursor moves over or out
//-----------------------------------------------
function toggleHoverTab(tab)
{
	if ($(tab).hasClassName('LeftMenuTabInactive'))
	{
		$(tab).toggleClassName('LeftMenuTabHover');
	}
}


