//factoid.js
//Michelle Flynn
//stateoftomorrow.com 2009

var numberOfFactoids = 25; //must enter total number of factoids for next and previous to work.
var randomnumber=Math.floor(Math.random()*numberOfFactoids);
var previous=randomnumber-1;
var next=randomnumber+1;

function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	  }
		return xmlHttp;
	}

function parseXML(randomnumber)
			{
			try //Internet Explorer
			  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  }
			catch(e)
			  {
			  try //Firefox, Mozilla, Opera, etc.
				{
				xmlDoc=document.implementation.createDocument("","",null);
				}
			  catch(e)
				{
				alert(e.message);
				return;
				}
			  }
			xmlDoc.async=false;
			xmlDoc.load("includes/xml/factoids.xml");
			
			//generate a random number as a selector for the factoids with max of one minus number of total factoids
			document.getElementById("factoid").innerHTML=
			xmlDoc.getElementsByTagName("quote")[randomnumber].childNodes[0].nodeValue;
			}

function newFactoid(q)
	{
		try //Internet Explorer
			  {
			  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			  }
			catch(e)
			  {
			  try //Firefox, Mozilla, Opera, etc.
				{
				xmlDoc=document.implementation.createDocument("","",null);
				}
			  catch(e)
				{
				alert(e.message);
				return;
				}
			  }
			xmlDoc.async=false;
			xmlDoc.load("includes/xml/factoids.xml");
			if (q>numberOfFactoids) {
				q=0; }
			document.getElementById("factoid").innerHTML=
			xmlDoc.getElementsByTagName("quote")[q].childNodes[0].nodeValue;
			previous=q-1;
			next=q+1;
		}
	
function changeFactoid(q) 
	{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp===null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		}
		else
		{
		xmlHttp.onreadystatechange=newFactoid(q);
		}
	}