// MyTeamCaptain Application Functions

// Putting text in the status bar (works in IE only)
function LinkMessage(text)
{window.status=text;}

function KillMessage()
{window.status=" ";}

// simple show css function, passing the ID
function showID (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.display = "inline";
		}
	}
}
function showIDblock (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.display = "block";
		}
	}
}
function fadeInBlock (elementID,maxOpacity)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			if (/msie/i.test(navigator.userAgent)) // IE is slow so bypass the fade in
			{
				showIDblock(elementID);
			}
			else
			{
				maxOpacity = parseFloat(maxOpacity);
				if ((maxOpacity < 0.1) || (isNaN(maxOpacity)))
				{
					maxOpacity = 1;
				}
				document.getElementById(elementID).style.zoom = '1';
				document.getElementById(elementID).style.opacity = '0';
				document.getElementById(elementID).style.filter = 'alpha(opacity = 0)';
				document.getElementById(elementID).style.display = "block";
				fadeInBlock2 (elementID,maxOpacity);
			}
		}
	}
}
function fadeInBlock2 (elementID,maxOpacity)
{
	var currentOpacity = parseFloat(document.getElementById(elementID).style.opacity);
	
	// alert (currentOpacity + '-' + maxOpacity);
	
	if (currentOpacity < maxOpacity)
	{
		currentOpacity += 0.1;
		var alpha = currentOpacity * 100;
		document.getElementById(elementID).style.opacity = currentOpacity;
		document.getElementById(elementID).style.filter = 'alpha(opacity = ' + alpha + ')';
		setTimeout ('fadeInBlock2(\'' + elementID + '\',' + maxOpacity + ')',1);
	}
}
function fadeOutBlock (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.zoom = '1';

			var currentOpacity = parseFloat(document.getElementById(elementID).style.opacity);

			if (currentOpacity > 0)
			{
				currentOpacity -= 0.1;
				var alpha = currentOpacity * 100;
				document.getElementById(elementID).style.opacity = currentOpacity;
				document.getElementById(elementID).style.filter = 'alpha(opacity = ' + alpha + ')';
				setTimeout ('fadeOutBlock(\'' + elementID + '\')',1);
			}
			else
			{
				document.getElementById(elementID).style.display = "none"; // hide the element
				document.getElementById(elementID).style.opacity = '1'; // reset opacity 
				document.getElementById(elementID).style.filter = 'alpha(opacity = 100)';
			}
		}
	}
}
function fadeOutBlock2 (elementID)
{
	
	// alert (currentOpacity);
	
}

// simple hide css function, passing the ID
function hideID (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.display = "none";
		}
	}
}

function toggleVisible (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			if (document.getElementById(elementID).style.display == 'none')
			{
				fadeInBlock(elementID);
			}
			else
			{
				fadeOutBlock(elementID);
			}
		}
	}
}

// makeVisibleID and makeInvisibleID use the visible css element so we don't lose form elements
function makeVisibleID (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.visibility = "visible";
		}
	}
}
function makeInvisibleID (elementID)
{
	if (document.getElementById)
	{
		if (document.getElementById(elementID))
		{
			document.getElementById(elementID).style.visibility = "hidden";
		}
	}
}

// window popup function
// options are like 'status=yes,resizable=yes,scrollbars=yes,width=400,height=250'
function popUpWindow(address,windowName,options)
{
	popup=window.open(address,windowName,options)
	popup.focus()
}


// prevents the user from accidentally submitting a form by pressing enter when in an <input type=text> field
function preventEnterFromSubmitting()
{
	return !(window.event && window.event.keyCode == 13);
}

// replace ' with /' in javascript calls, usage is var = cleanApostrophes(var)
function cleanData (stuff)
{
	var type = typeof stuff;
	if ((type == 'undefined') || (stuff == null))
	{
		return '';
	}
	else
	{
		regex = /\\\\/g;
		stuff = stuff.replace(regex,'');

		regex = /'/g;
		stuff = stuff.replace(regex,"\\\\'");

		regex = /&/g;
		stuff = stuff.replace(regex,'//and//');

		regex = /;/g;
		stuff = stuff.replace(regex,'//semicolon//');

		regex = /\+/g;
		stuff = stuff.replace(regex,'//plus//');

		return stuff;
	}
}

// this function replaces the content of spanID with newdata - used most often with the ajax function return call
function rebuildID (spanID,newdata)
{
	if (document.getElementById(spanID))
	{
		document.getElementById(spanID).innerHTML = newdata;
	}
}

// similar to the above function, it will replace the outerHTML with the ajax-retrieved data
function rebuildIDouter (spanID,newdata)
{
	document.getElementById(spanID).outerHTML = newdata;
}


function forceCursor (spanID,cursorStyle)
{
	if (document.getElementById(spanID))
	{
		document.getElementById(spanID).style.cursor = cursorStyle;
	}
}


// shows help layer
function showHelp (helpDiv,helpTopic,teamPortalID)
{
	var postdata = 'HelpDiv=' + helpDiv + '&HelpTopic=' + helpTopic + '&PortalID=' + teamPortalID;
		
	// call the ajax function to update the attendance database and the players list
	postDataReturnText('ajaxHelp.pl', postdata, helpDiv, rebuildID, 'wait');

	fadeInBlock (helpDiv);
}


// shows quicklink layer
function showQuickMenu (acctDB,seasonID,teamName,seasonName,sourceMode,scheduleName,playersName)
{
	if ((document.getElementById('quickMenu' + acctDB + '_' + seasonID).style.display == 'none') || (document.getElementById('quickMenu' + acctDB + '_' + seasonID).style.display == ''))
	{
		// hide all the other quick menus
		for (i=0; i<document.getElementsByTagName('div').length; i++)
		{
			if (document.getElementsByTagName('div')[i].className == 'quickmenu')
			{
				document.getElementsByTagName('div')[i].style.display = 'none';
			}
		}
		for (i=0; i<document.getElementsByTagName('img').length; i++)
		{
			if (document.getElementsByTagName('img')[i].className == 'quickMenuStar')
			{
				document.getElementsByTagName('img')[i].src = '/images/header_icon_star.gif';
			}
		}
		
		
		fadeInBlock ('quickMenu' + acctDB + '_' + seasonID);
	
		var quickMenuHTML = 'Go to:';
		quickMenuHTML += '<span style="position: absolute; top: 1px; right: 1px; font-weight: bold; background-color: #999; color: darkred; border: 1px solid #000; display: inline; text-decoration: none; padding: 2px;"><a onClick="hideQuickMenu(\'' + acctDB + '\',\'' + seasonID + '\');" style="text-decoration: none; color: darkred;" title="Close Quick Navigation">X</a></span>';

		if (sourceMode != '')  // schedule screen won't show this
		{
			quickMenuHTML += '<div id="quickMenuRowSchedule' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
			quickMenuHTML += '<a href="schedule.pl?' + acctDB + '&' + seasonID + '" title="View ' + scheduleName + ' for ' + teamName + ', ' + seasonName + '">' + scheduleName + '</a>';
			quickMenuHTML += '</div>';
		}
		if (sourceMode != 'players')
		{
			quickMenuHTML += '<div id="quickMenuRowPlayers' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
			quickMenuHTML += '<a href="players.pl?' + acctDB + '&' + seasonID + '" title="View ' + playersName + ' for ' + teamName + ', ' + seasonName + '" >Contacts</a>';
			quickMenuHTML += '</div>';
		}

		quickMenuHTML += '<div id="quickMenuRowForum' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
		quickMenuHTML += '<a href="forum.pl?' + acctDB + '&' + seasonID + '" title="View Forum for ' + teamName + ', ' + seasonName + '">Forum</a>';
		quickMenuHTML += '</div>';

		quickMenuHTML += '<div id="quickMenuRowProjects' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
		quickMenuHTML += '<a href="forum.pl?' + acctDB + '&' + seasonID + '&1" title="View Projects for ' + teamName + ', ' + seasonName + '">Projects</a>';
		quickMenuHTML += '</div>';
		
		if (sourceMode != 'forum')
		{
			quickMenuHTML += '<div id="quickMenuRowSurveys' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
			quickMenuHTML += '<a href="forum.pl?' + acctDB + '&' + seasonID + '&&1" title="View surveys for ' + teamName + ', ' + seasonName + '" >Surveys</a>';
			quickMenuHTML += '</div>';
		}

		if (sourceMode != 'media')
		{
			quickMenuHTML += '<div id="quickMenuRowMedia' + acctDB + '_' + seasonID + '" class="quickmenuRow">';
			quickMenuHTML += '<a href="media.pl?' + acctDB + '&' + seasonID + '" title="View Media for ' + teamName + ', ' + seasonName + '">Media</a>';
			quickMenuHTML += '</div>';
		}

		// quickMenuHTML += '<div class="quickmenuRow">';
		// quickMenuHTML += '<a onClick="hideQuickMenu(\'' + acctDB + '\',\'' + seasonID + '\');">Close</a>';
		// quickMenuHTML += '</div>';

		document.getElementById('quickMenu' + acctDB + '_' + seasonID).innerHTML = quickMenuHTML;
		
		document.getElementById('quickMenuStar' + acctDB + '_' + seasonID).src = '/images/header_icon_star_purple.gif';
	}
}
function hideQuickMenu (acctDB,seasonID)
{
	document.getElementById('quickMenuStar' + acctDB + '_' + seasonID).src = '/images/header_icon_star.gif';
	fadeOutBlock('quickMenu' + acctDB + '_' + seasonID);
}

function showRoadblock (page)
{
	fadeInBlock('roadblock',0.7);
	showIDblock('roadblockContent');
	
	var postdata = 'Page=' + page;
		
	// call the ajax function to update the attendance database and the players list
	postDataReturnText('ajaxRoadblock.pl', postdata, 'roadblockContent', rebuildID, 'wait');
}
function closeRoadblock ()
{
	document.getElementById('roadblockContent').innerHTML = '';
	hideID('roadblock');
	hideID('roadblockContent');
}

// this ajax function uses POST method to send data to a server and then gets text back
// it calls the function 'callback' with the returned text
// Example: onClick="postDataReturnText('http://www.myteamcaptain.com/script.pl','parameter=5',displayfunc);"
// with this function in the <head> section
// function displayfunc(text)
// { document.getElementById('someDivOrSpanID').innerHTML = text; }

function postDataReturnText(url, data, spanID, callback, cursorHourglass)
{
	if (document.getElementById(spanID))
	{
		document.getElementById(spanID).style.cursor = cursorHourglass;
	}

	var XMLHttpRequestObject = false;

	if (window.XMLHttpRequest)
	{
		XMLHttpRequestObject = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (XMLHttpRequestObject)
	{
		XMLHttpRequestObject.open("POST", url);
		XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
			{
				callback(spanID, XMLHttpRequestObject.responseText);
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;

				if (document.getElementById)
				{
					if (document.getElementById(spanID))
					{
						document.getElementById(spanID).style.cursor = "auto";
					}
				}				
			}
		}
		XMLHttpRequestObject.send(data);
	}
}
