/**
* quoteview-mem.js - loads a random quote from an XML document and refreshes a page element.
* author - Karl Martino
* license - do whatever you want with this...
*/

// set the following varibles to configure quoteview
var refreshSeconds = 60; // set to -1 to disable auto-refresh, enter delay in seconds
var quoteViewEndPoint = '/includes/testimonialsXML.xml'; // where to find the xml doc

// prepare the request object and set refresh period
try {   // The following "try" blocks get the XMLHTTP object for various browsers…
  XMLRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    XMLRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
  // This block handles Mozilla/Firefox browsers...
    try {
	  XMLRequestObject = new XMLHttpRequest();
    } catch (e3) {
    XMLRequestObject = false;
	}
  }
}
 

if (refreshSeconds > -1) {
  window.setInterval("update_timer()", refreshSeconds*1000); // update the data
}

/**
* Updates when XMLHttpRequest readystate changes.
*/
function reqChange() {
  // if data received correctly...
  if (XMLRequestObject.readyState==4) {
    var quotes = new Array();   
    var node = XMLRequestObject.responseXML.documentElement;   
	var items = node.getElementsByTagName('testimonialType');
	var count = 0;
    for (var n=0; n < items.length; n++) {
	  if (items[n].getAttribute("type") == "Education") {
        var subitems = items[n].getElementsByTagName('testimonial');
		for (var x=0; x < subitems.length; x++) {
		  var itemQuote = subitems[x].getElementsByTagName('quote').item(0).firstChild.data;
		  var itemSource = subitems[x].getElementsByTagName('source').item(0).firstChild.data;  
		  if (itemQuote.length > 1 && itemQuote.length < 255) {  
			quotes[count] = {source: itemSource, quote: itemQuote};
			count++;
		  }
		  
		}
	  }
    }	
    
    // show a quote!
    changeQuote(quotes);
  }
	
}

/**
* Changes the quote that is displayed in the quoteview page element
*/
function changeQuote(quotes) {  

  var ranNum = Math.round(Math.random()*quotes.length);
  //document.getElementById("quoteview").innerHTML = "Length = " + quotes.length + "<br />ranNum = " + ranNum;
  var quote = quotes[ranNum];
  var content = '<img src="/images/quote/lq.gif" alt="quote" style="display:inline" />&nbsp;'+quote.quote+'<br />&mdash; <em>'+quote.source+'</em>';

  document.getElementById("quoteview").innerHTML = content;     
}

/**
* Starts the AJAX reader
*/
function quoteViewXmlRequest() {
  // let folks know we are doing something
  //document.getElementById("quoteview").innerHTML = '<img src="/images/quote/quote-loader.gif" alt="Loading" style="padding-left:115px" />';

  XMLRequestObject.open("GET", quoteViewEndPoint , true);

  // set the onreadystatechange function
  XMLRequestObject.onreadystatechange = reqChange;

  // send
  XMLRequestObject.send(null); 
}

/*
* Runs at configured time
*/
function update_timer() {
  quoteViewXmlRequest();
}
