/*
* This file reads the story.xml file and displays its contents.
* author:V.Sendling-Ortiz, vivortiz@yahoo.com
*/

////////////////////////
//Declarations
////////////////////////
var isFireFox = (!document.layers) && (navigator.userAgent.indexOf('Firefox')!=-1)?true:false;
var xmlFile = "xml/story.xml";
var xmlstoryDoc;

//Is IE or Firefox?
if (isFireFox){	
	//Create an instance of the XML parser in Mozilla browsers
	//namespace, root, file
	xmlstoryDoc=document.implementation.createDocument("","",null);
	xmlstoryDoc.load(xmlFile);
}else{
	xmlstoryDoc = new ActiveXObject("Microsoft.XMLDOM");
}

////////////////////////
//Functions
////////////////////////

//Loads XML file
function loadstoryXML(inXmlFile, inRetText, isDebugging)
{
  xmlFile = inXmlFile;
  if (isFireFox){	
	xmlstoryDoc.async="false";
	xmlstoryDoc.onreadystatechange=verify;	
	xmlstoryObj=xmlstoryDoc.documentElement; //root element 
  }else{
	xmlstoryDoc.async="false";
	xmlstoryDoc.onreadystatechange=verify;
	xmlstoryDoc.load(xmlFile);
	xmlstoryObj=xmlstoryDoc.documentElement; //root element 
  }
  try{ 	
	if (inRetText){
		//alert("inRetText = " + inRetText);
		//alert(xmlstoryDoc);
	}else{
		displaystoryXML();
	}
  }
  catch(e){    
	if (isDebugging){
		document.write("e is: " + e + "<br>")
		document.write("e.number is: " + (e.number & 0xFFFF) + "<br>");
		document.write("e.description is: " + e.description + "<br>");
		document.write("e.name is: " + e.name + "<br>");
		document.write("e.message is: " + e.message + "<br>");
	}else{
		document.write ("<BR><font color=white>This month's story coming soon!</font><BR> " +e.message +"<HR>")
	}	
  }  
}

//Verifies
function verify()
{
  // 0 Object is not initialized
  // 1 Loading object is loading data
  // 2 Loaded object has loaded data
  // 3 Data from object can be worked with
  // 4 Object completely initialized
  if (xmlstoryDoc.readyState != 4)
  {
		return false;
  }else{
		//alert(xmlstoryDoc.readyState);
  }
}

//Displays XML file contents
function displaystoryXML(){ 	
    var storyCount = 1; //xmlstoryObj.childNodes.length; //giving 3?
	var x;
	var strstoryNodeValue;
	
	//will loop thru from top top bottom XML file.
	for (i=0;i<storyCount;i++)
	{			
		//get story text
		var x=xmlstoryDoc.getElementsByTagName('text');		
		var strstoryNodeValue = x[i].childNodes[0].nodeValue;		
		//split at pipe and create paragraphs
		var arrstoryText = strstoryNodeValue.split("|");
		for (arrCounter=0; arrCounter<arrstoryText.length; arrCounter++){
			document.write("<h3>" + arrstoryText[arrCounter] + "</h3>");			
		}		
	}		
}

//Only return text without formating
function returnText(){ 		
    var storyCount = 1; //xmlStoryObj.childNodes.length;	
	var arrCounter;
	
	try{
		//will loop thru from top top bottom XML file.
		for (i=0;i<storyCount;i++)
		{			
			//get story text
			var x=xmlstoryDoc.getElementsByTagName('text');
			var strStoryNodeValue = x[i].childNodes[0].nodeValue;		
			//split at pipe and create paragraphs
			var arrStoryText = strStoryNodeValue.split("|");
		}	
		return arrStoryText;
	}
	 catch(e){
		alert("error");
		return e.message;
	 }
	
}