//
// Parse response from server in XML format.
//
var response;
var finalQuoteString = '';
var quoteArray = null;
var nameArray;
var citeArray;
var bioArray; 
var extLink1;
var extLink2;
	    
function dspAllQuotes() {
  
  if( isStateReady() ) {
	  
    var x = 0;
	
    finalQuoteString = '<h1>Testimonials</h1>';
	  
	for( x=0; x<quoteArray.length; x++ ) {
	  extLink1 = "";
	  extLink2 = "";
	  finalQuoteString += '<blockquote';
	  if( citeArray[x].childNodes.length > 0 ) {
	    finalQuoteString += ' cite="' + citeArray[x].firstChild.nodeValue +'"';
		extLink1 = '<a href="' + citeArray[x].firstChild.nodeValue + '" target="_blank">';
		extLink2 = '</a>';
	  }
	  finalQuoteString += '>';
		
	  finalQuoteString += '<p>' + quoteArray[x].firstChild.nodeValue + '</p>';
		
	  finalQuoteString += '<p class="source">--' + extLink1 + nameArray[x].firstChild.nodeValue + extLink2 + ',<br />';
	  finalQuoteString += bioArray[x].firstChild.nodeValue + '</p></blockquote>';
	  
	} // end for()
	  
	document.getElementById( 'testimonials' ).innerHTML = finalQuoteString;
	 
  }
  
} // end function dspAllQuotes()

function dspRandomQuote() {
 
  if( isStateReady() ) {
    var randomNumber=Math.floor(Math.random()*(quoteArray.length));
	  
	extLink1 = "";
	extLink2 = "";
	finalQuoteString = '<blockquote';
	if( citeArray[randomNumber].childNodes.length > 0 ) {
	  finalQuoteString += ' cite="' + citeArray[randomNumber].firstChild.nodeValue +'"';
      extLink1 = '<a href="' + citeArray[randomNumber].firstChild.nodeValue + '" target="_blank">';
	  extLink2 = '</a>';
	}
	 
	finalQuoteString += '>';
		
	finalQuoteString += '<p>' + quoteArray[randomNumber].firstChild.nodeValue + '</p>';
		
	finalQuoteString += '<p class="source">-- ' + extLink1 + nameArray[randomNumber].firstChild.nodeValue+ extLink2 + ',<br />';
	finalQuoteString += bioArray[randomNumber].firstChild.nodeValue + '</p><p class="more"><a href="testimonials.htm">more...</a></p></blockquote>';
	  
	document.getElementById( 'leftside' ).innerHTML = finalQuoteString;
	  
  }
  
} // end function dspRandomQuote()

function isStateReady() {

  var state = false;
  
  // If the arrays are empty, try to fill them.  If they are full, skip this process and return 
  // with a completed state in order to use the data already retrieved.
  if( !quoteArray ) {
  
    // Is the readyState = 4?
    if( request.readyState == 4 ) {
	  // Is the status = 200?
	  if( request.status == 200 ) {
	    // Grab the responseText from the XMLHttpRequest.
	  
	    // Here we could just receive the text as a string, if it it coming from 
	    // an unformatted text file using 'responseTest' and the parse it out
	    // with our own routine,
	    // Or we can receive the vales as XML, if the source is in XML format, using 
	    // responseXML.  Then we can use the methods getElementById() and getElementsByTagName()
	    // to parse the data.
	    // Use xmlObject.getElementsByTagName( tagName ) to get a list of the tag values.
	    // Use document.getElementById( elementId ).innerHTML to set the desination of the values.
	    response = request.responseXML;
	    quoteArray = response.getElementsByTagName( 'QUOTE' );
	    nameArray = response.getElementsByTagName( 'NAME' );
	    citeArray = response.getElementsByTagName( 'CITE' );
	    bioArray = response.getElementsByTagName( 'BIO' );
	  
	    state = true;
	  
	  } else {
	    alert( 'There was a problem retrieving the data: \n' + request.statusText );
	    request = null;
	  }
    }	  
  
  } else {
    state = true;
  } // end if( quoteArray.length == 0)
  
  return state;
  
} // end function isStateReady()