// JavaScript Document

var req;

//load the xml document
function loadXMLDoc(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

// test document
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            getXmlData();
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

// this script will pick 1 random quote from testimonials.xml for displaying on the site
function getXmlData() {
	var xmldata = req.responseXML.documentElement;
	var quoteList = xmldata.getElementsByTagName("quote");
	var q = quoteList.length;	
	var rq=Math.floor(Math.random()*q);
	
	var quoteArray = new Array();
	quoteArray = quoteList[rq].childNodes;
	var data = new Array();
	
	var output = "";
	
	for (i=0 ; i < quoteArray.length ; i++) {
		if (quoteArray[i].nodeType == 1) {
			data[data.length] = quoteArray[i].firstChild.nodeValue;
		}		
	}	
	
	if (data[0]) { output += "<p>" + data[0] + "</p>"; }
	if (data[1]) { output += "<p style='text-align: right;'><strong>~ " + data[1] + "</strong>"; }
	if (data[2]) { output += "<br />" + data[2]; }
	if (data[0]) { output += "</p>"; }
	
		
	document.getElementById("testimonial").innerHTML = output;
}

//window.onload = loadXMLDoc("http://www.thepilatesbarr.ca/_xml/testimonials.xml");
