// JavaScript Document
/*
Title : doorsopen.js
Author : Balu Kiri, City of Toronto - Web Competency Centre
URL : http://www.toronto.ca/

Description : script containing functions to load xml file, populate appropriate data on webpage.

Created : 31/Mar/2009
Modified : 
*/



function loadxmlfile()
{
	/*
	This function is trigerred on htm page body onLoad event
	to detect the browser and load xml file
	*/
	if(typeof window.ActiveXObject != 'undefined') //If browser is IE
		{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); //create xmlDoc object
			xmlDoc.async = false;
			xmlDoc.load("xml/quotes.xml"); //loading xml file
			
			}
	
	else if(window.XMLHttpRequest)//If browser is FF,Opera,Safari
		{
			try 
				{
					xmlDoc = new XMLHttpRequest();
					//xmlDoc.async=false;
					xmlDoc.open("GET", "xml/quotes.xml", false);
					//xmlDoc.onreadystatechange = handleResponse;
					xmlDoc.send(null);
				} 
			catch(e) 
				{
					xmlDoc= false;
				}
		}
	else if(document.implementation && document.implementation.createDocument) 	//if browser is FF / Opera
		{
			xmlDoc=document.implementation.createDocument("","",null); //creates xmlDoc document
		}
	else
		{
		alert("Your browser does not support this script");
		return;
		}
		
		
		var quoteguy = new Array();
		var quotetxt = new Array();
		var newno;
		var divsection = document.getElementById("quotespace");// a div tag is defined on htm page with id='divLinks'. This is to display the links
		divsection.innerHTML='';
		
	
		if(typeof window.ActiveXObject != 'undefined')
		{
			//alert();
			//alert(xmlDoc.getElementsByTagName("quotes").length);
			for(var i=0; i<xmlDoc.getElementsByTagName("quotes").length; i++)
					{
						quoteguy[i] = xmlDoc.getElementsByTagName("quotes")[i].getAttribute("said");
						quotetxt[i] = xmlDoc.getElementsByTagName("quotes")[i].firstChild.nodeValue;
						newno = xmlDoc.getElementsByTagName("quotes").length;
					}
		}
		else if(window.XMLHttpRequest)
		{
			
			//alert(xmlDoc.responseXML.getElementsByTagName("quotes").length);
			for(var i=0; i<xmlDoc.responseXML.getElementsByTagName("quotes").length; i++)
					{
						quoteguy[i] = xmlDoc.responseXML.getElementsByTagName("quotes")[i].getAttribute("said");
						quotetxt[i] = xmlDoc.responseXML.getElementsByTagName("quotes")[i].firstChild.nodeValue;
						newno = xmlDoc.responseXML.getElementsByTagName("quotes").length;
					}
		}
		else
		{
			alert("Your browser does not support this script");
			return;
		}
		
		var htmlText = '';
		var finalno;
		finalno = Math.floor(Math.random()*newno)
		htmlText = htmlText + "<p>" + quotetxt[finalno] + "</p>"  + quoteguy[finalno];
		divsection.innerHTML= htmlText;
	
	
	}

function handleResponse() 
	{
    // only if req shows "loaded"
		//alert(xmlDoc.readyState);
		if (xmlDoc.readyState == 4) 
			{
			// only if "OK"
				alert(xmlDoc.readyState);
				alert(xmlDoc.status);
				if (xmlDoc.status == 200) 
					{
						//clearTopicList();
						//buildTopicList();
					}
				else 
					{
						
						alert("There was a problem retrieving the XML data:\n" + xmlDoc.statusText);
				 	}
			}
	}


