// getCurrentTip.js

var theTipRequest;
var theAllTipRequest;
var archiveFileNames;

function populateTip( fileName )
{
	// Leave notice
	var noticeDiv = document.getElementById( 'Problem' );
	noticeDiv.innerHTML = "Retrieving tip from the server. Please wait";
		

	// Native XMLHttpRequest object: FireFox, Safari, Mozilla, netscape
	if (window.XMLHttpRequest)
			theTipRequest = new XMLHttpRequest();
	// IE/Windows ActiveX version
	else if (window.ActiveXObject)      
			theTipRequest = new ActiveXObject("Microsoft.XMLHTTP");
	
	if (theTipRequest == null)
	{
		noticeDiv.innerHtml = "<p><span class=style4>Your browser does not support requests for data from a server. Please update the browser to a newer edition, \
								or change browsers to a more modern one. \n It may be that this is a temporary problem. If you think it is, try refreshing the page later.</span></p>";
		return;
	} 

	// Send the request
	try 
	{
		theTipRequest.onreadystatechange = processTip;
		theTipRequest.open("POST", "TipOfMonth/GetTip.php?where=file&fileName=" + fileName, true);	  
		theTipRequest.send(null);
	} catch(e) {
		document.getElementById( "Problem" ).innerHtml = "<p><span class=style4>Your browser could not send a request for data to the server; This is probably a temporary problem, try refreshing the page later.</span></p>";
	}
}


function processTip()
{
	// Check this one to see if it is "Done"
	if( theTipRequest.readyState == 4 || theTipRequest.readyState == "complete" )
	{
		if (theTipRequest.status == 200) 
		{
			var xmlDoc = theTipRequest.responseXML;
			
			// Get the tip elements
			var problem = '';
			var solution = '';
			var results = ''; 
			
			try
			{problem = xmlDoc.getElementsByTagName( "problem" )[0].childNodes[0].nodeValue;
			}catch(e)
			{}

			try
			{ solution = xmlDoc.getElementsByTagName( "solution" )[0].childNodes[0].nodeValue;
			}catch(e)
			{}

			try
			{ results = xmlDoc.getElementsByTagName( "results" )[0].childNodes[0].nodeValue;
			}catch(e)
			{}
			
			// Now check for bold and italic words
			problem = fixFormatting( problem );
			solution = fixFormatting( solution );
			results = fixFormatting( results );

			// Load the problem, solution and results
			document.getElementById( 'Problem' ).innerHTML = problem;
			document.getElementById( 'Solution' ).innerHTML = solution;
			document.getElementById( 'Results' ).innerHTML = results;
		}
	}
}

function fixFormatting ( text )
{
	// Replace every '~p' with <p> and every p~ with </p>
	text = text.replace( /~p/g, "<p>" );
	text = text.replace( /p~/g, "</p>" );

	// Replace every '~b' with <b> and every b~ with </b>
	text = text.replace( /~b/g, "<b>" );
	text = text.replace( /b~/g, "</b>" );

	// Replace every '~i' with <i> and every i~ with </i>
	text = text.replace( /~i/g, "<i>" );
	return text.replace( /i~/g, "</i>" );
}

function getArchivedTips()
{
	var noticeDiv = document.getElementById( 'Archive' );

	// Native XMLHttpRequest object: FireFox, Safari, Mozilla, netscape
	if (window.XMLHttpRequest)
			theAllTipRequest = new XMLHttpRequest();
	// IE/Windows ActiveX version
	else if (window.ActiveXObject)      
			theAllTipRequest = new ActiveXObject("Microsoft.XMLHTTP");
	
	if (theAllTipRequest == null)
	{
		noticeDiv.innerHtml = "<p><span class=style4>Your browser does not support requests for data from a server. Please update the browser to a newer edition, \
								or change browsers to a more modern one. \n It may be that this is a temporary problem. If you think it is, try refreshing the page later.</span></p>";
		return;
	} 

	// Send the request
	try 
	{
		theAllTipRequest.onreadystatechange = processArchiveResponse;
		theAllTipRequest.open("POST", "TipOfMonth/GetAllTipDates.php", true);	   
		theAllTipRequest.send(null);
	} catch(e) {
		document.getElementById( "archivearea" ).innerHtml = "<p><span class=style4>Your browser could not send a request for data to the server; This is probably a temporary problem, try refreshing the page later.</span></p>";
	}
}

function processArchiveResponse()
{
	// Check this one to see if it is "Done"
	if( theAllTipRequest.readyState == 4 || theAllTipRequest.readyState == "complete" )
	{
		if (theAllTipRequest.status == 200) 
		{
			var xmlDoc = theAllTipRequest.responseXML;
			if( xmlDoc == null )
			{
				document.getElementById('Archive').innerHTML = '<p><span class=style4>The network connection is poor. Try refreshing the page or try again later.</span></p>' ;
				return;
			}
			
			errors = xmlDoc.getElementsByTagName( "errorcode" );
			if( errors[0].childNodes[0].nodeValue == 0 )
			{
				listArchives( xmlDoc );	
			}
			else
			{
				var msg = xmlDoc.getElementsByTagName( "msg" );
				document.getElementById('Archive').innerHTML = '<p><span class=style4>Error saving:' + errors[0].childNodes[0].nodeValue + '; ' + msg[0].childNodes[0].nodeValue + '</span></p>' ;
				return;
			}
		}
	}
}

function listArchives( xmlDoc )
{
	// Get the list of archived fileNames
	archiveFileNames = xmlDoc.getElementsByTagName( "fileName" );	
	var last = archiveFileNames.length - 1;
	
	// skip Preview
	var text = '';
	for( var i=last; ; i-- )
	{
		var name = archiveFileNames[i].childNodes[0].nodeValue;
		if( name != 'Preview' )
			text += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a href='javascript:showTip(" + i + ")' >" + name + "</a>";

		if( i == 0 || i == (last-8) )
			break;
	}
	
	document.getElementById( 'Archive' ).innerHTML = text;	
}

function showTip( ctr )
{
	var name = archiveFileNames[ctr].childNodes[0].nodeValue;
	
	populateTip( name );
}


