/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	masley.js - this file contains JavaScript  used by the www.masleyassociates.com
//	website. Functionality provided includes error handling, image rotation and testimonial
//	rotation.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Don't remove the next line. Used by WC3 html error checking modules.
/* <![CDATA[ */ 	

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	 Error handling function. Set to display Frist three errors encountered.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
window.onerror = function(msg, url, line)
{
	if (onerror.num++ < onerror.max)
	{
		alert("PSC JS Error: " + msg +"\n" + url + ":" + line);
		return true;
	}
}

// keep track of how many errors to display.
onerror.max = 3;
onerror.num = 0;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Image rotation
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Keeps track of next image to display
var nextImage = 1;
var imageTimer = 5 * 1000;

// Rotate the image every (imageTimer / 1000) seconds
function rotateImage()
{
	//get object id.
	var id = document.getElementById("rotatingImage");

	// return gracefully if objectId is not found.
	if (id == false)
	{
		return;
	}

	// start at begining of list if end has been reached.
	if (nextImage >= rotatingImageFiles.length)
	{
		nextImage = 0;
	}

	// set the new image.
	id.src = rotatingImageFiles[nextImage++].childNodes[0].nodeValue;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Testimonial rotation
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Keeps track of next testimonial to display
var nextTestimonial = 0;
var testimonialTimer = 5 * 1000;

// Rotate the image every (imageTimer / 1000) seconds
function rotateTestimonial()
{
	//get object id.
	var id = document.getElementById("rotatingTestimonial");
	// return gracefully if objectId is not found.
	if (id == false)
	{
		return;
	}

	// start at begining of list if end has been reached.
	if (nextTestimonial >= testimonialData.names.length)
	{
		nextTestimonial = 0;
	}

	// set the new image.
	var msg  = "<q><i><b>" + testimonialData.quotes[nextTestimonial].childNodes[0].nodeValue + "</b></i></q>";
	msg += "<br><br>-- " + testimonialData.names[nextTestimonial].childNodes[0].nodeValue;
				
	if (testimonialData.dates[nextTestimonial].childNodes[0])
	{
		if (testimonialData.dates[nextTestimonial].childNodes[0].nodeValue != "")
		{
			msg += ", " + testimonialData.dates[nextTestimonial].childNodes[0].nodeValue;
		}
	}

	id.innerHTML = msg;

	nextTestimonial++;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Load the xml doc.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function loadXMLDoc(docName)
{
	var xmlDoc = null;

	try //Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)  
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			xmlDoc = document.implementation.createDocument("","",null);
		}  
		catch(e)
		{
			alert(e.message)
			return(null);
		}  
	}

	try   
	{  
		xmlDoc.async=false;  
		xmlDoc.load(docName);  
		return(xmlDoc);  
	}
	catch(e) 
	{
		alert(e.message)
		return(null);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// load and parse testimonials
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
xmlDoc = loadXMLDoc("testimonials.xml");

var testimonialData = {};

if (xmlDoc != null)
{
	testimonialData.names = xmlDoc.getElementsByTagName("name");
	testimonialData.quotes = xmlDoc.getElementsByTagName("quote");
	testimonialData.dates = xmlDoc.getElementsByTagName("date");
}
else
{
	testimonialData.names = {};
	testimonialData.quotes = {};
	testimonialData.dates = {};
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// load and parse images
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var xmlImages = loadXMLDoc("images.xml");
var rotatingImageFiles={};

if (xmlImages != null)
{
	rotatingImageFiles =  xmlImages.getElementsByTagName("src");
}


/* ]]> */