﻿var file = "/xml/testimonials.xml";
var testimonialArray = new Array();
var quotesArrayPos = 0;
var authorsArrayPos = 0;
var items = 0;

var quotes = [];
var authors = [];

function loadXML(file) {
if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = traverse;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) traverse()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load(file);        
}

function traverse() {

    var x = xmlDoc.getElementsByTagName('testimonial');
    
    items=x.length;
    
    for (i=0; i<x.length; i++)
    {
        for (j=0; j<x[i].childNodes.length; j++) {
            if (x[i].childNodes[j].nodeType != 1) continue;
            
            if (x[i].childNodes[j].nodeName=="quote") {
                quotes[quotesArrayPos] = (x[i].childNodes[j].firstChild.nodeValue);
                quotesArrayPos ++;
            }
            
            if (x[i].childNodes[j].nodeName=="author") {
                authors[authorsArrayPos] = (x[i].childNodes[j].firstChild.nodeValue);
                authorsArrayPos ++;
            }
        }
    }
    
    var randomNumber=Math.floor(Math.random()*(items));
    document.getElementById("testimonialQuote").innerHTML  = "\"" + quotes[randomNumber] + "\"";
    document.getElementById("testimonialAuthor").innerHTML  = authors[randomNumber];
}

function choose() {
    var items;
    loadXML(file);
}