// Importing XML document containing testimonials
var xmlDoc;

function importxml(XMLfile) {
	var moz = (typeof document.implementation != 'undefined') && 
		(typeof document.implementation.createDocument != 'undefined');
	var ie = (typeof window.ActiveXObject != 'undefined');

	if (moz) {
		xmlDoc = document.implementation.createDocument("","",null)
		xmlDoc.onload = readxml;
	}
	else if (ie)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function(){if (xmlDoc.readyState == 4) setTimeout(readxml,0);}		
	}
    xmlDoc.load(XMLfile);
}

var lCountTracker;
var rCountTracker;

function readxml() {

var lCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
var rCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
checksame(); checkrepeat();
var lImage = xmlDoc.getElementsByTagName('testimonial')[lCount].getAttribute('image');
var rImage = xmlDoc.getElementsByTagName('testimonial')[rCount].getAttribute('image');
var lName = xmlDoc.getElementsByTagName('testimonial')[lCount].getAttribute('name');
var rName = xmlDoc.getElementsByTagName('testimonial')[rCount].getAttribute('name');

var lWebsite = xmlDoc.getElementsByTagName('testimonial')[lCount].getAttribute('website');
var rWebsite = xmlDoc.getElementsByTagName('testimonial')[rCount].getAttribute('website');

var lQuote = xmlDoc.getElementsByTagName('testimonialshort')[lCount].firstChild.nodeValue;
var rQuote = xmlDoc.getElementsByTagName('testimonialshort')[rCount].firstChild.nodeValue;

	function checksame () {
		if (lCount == rCount) {
			lCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
			rCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
			checksame();
		}
	}

	function checkrepeat() {
		if (lCountTracker == lCount || rCountTracker == rCount){
			if (lCountTracker == lCount) {
				lCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
				checkrepeat();
			}
			else if (rCountTracker == rCount) {
				rCount = Math.floor(Math.random()*xmlDoc.getElementsByTagName('testimonial').length);
				checkrepeat();
			}
		}
	}
		
	function leftbox() {
		document.getElementById("limage").src = lImage;
		document.getElementById("lname").innerHTML = lwebsiteexists();
		document.getElementById("lquote").innerHTML = lQuote;
	}
	function rightbox() {
		document.getElementById("rimage").src = rImage;
		document.getElementById("rname").innerHTML = rwebsiteexists();
		document.getElementById("rquote").innerHTML = rQuote;
	}
	
	function lwebsiteexists() {
		if (lWebsite) {
			nameString = lName + " - " + "<a href='" + lWebsite + "' target='_blank'>" + lWebsite + "</a>";
			return nameString;
		} 
		else {
			return lName;
		}
	}
	function rwebsiteexists() {
		if (rWebsite) {
			nameString = rName + " - " + "<a href='" + rWebsite + "' target='_blank'>" + rWebsite + "</a>";
			return nameString;
		} 
		else {
			return rName;
		}
	}
leftbox();
rightbox();

lCountTracker = lCount;
rCountTracker = rCount;
}

function addloadevent(funct) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = funct;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      funct();
    }
  }
}

addloadevent(function() {importxml('testimonials.xml');})

setInterval("addloadevent(importxml('testimonials.xml'))", 15000); // rotation delay - higher is slower