// JavaScript Document
var xmlDoc2;

function loadTestimonialXML()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp2=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp2.open("GET","includes/testimonials.xml",false);
	xmlhttp2.send();
	xmlDoc2=xmlhttp2.responseXML;
}

Array.prototype.shuffle = function() { //shuffles array
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1));
	while (s.length) this.push(s.pop());
	return this;
}

function printtestimonials()  //populates a div called "reviews", designed for main body
{
	var testimonials = xmlDoc2.getElementsByTagName("review");
	var x = new Array();
	
	for(i=1;i<testimonials.length;i++)
		x[i-1] = i;
	x.shuffle();
	
	for(i=1;i<testimonials.length;i++){}
//	alert("Testimonial file has " + testimonials.length + " testimonials.");

	var html = "<table class='directory' cellspacing='5'><tr><th colspan='2'><h2>Testimonials and Success Stories</h2></th></tr>";
	html += "<tr><td colspan='2' align='left'>";
	html += "<blockquote style='font-size:16px;margin-top:0px;'>A+ is committed to every student's success! We carefully match students with the best tutors in the Delaware Valley, and follow up regularly to ensure quality service you can trust. Whether you are looking for ongoing academic support or to improve standardized test scores, we have tutors of all backgrounds to help! Our entire team values your feedback, and is proud to share these success stories. Contact us today to discuss your test prep and tutoring needs!</blockquote>"
	html += "</td></tr><tr><td colspan='2'><hr/></td></tr>";
	for (j=1;j<testimonials.length;j++) //do not include sample record
		{
		var i = x[j-1];
		var firstname = "";
		var lastname = "";
		var namestring = "";

		if(xmlDoc2.getElementsByTagName("studentfirstname")[i].hasChildNodes()==false) //generic testimonial, no pic
		{
//			alert("Record #" + i + " does not have a student name associated, processing html for generic testimonial");
			html += "<tr><td colspan='2' valign='top' align='left'>";			
			html += "<blockquote>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
			html += "<p style='margin-left:40px;margin-top:3px;'>-";
			if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
				{
					html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
					html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue;
				}
			html += " (" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")</p></blockquote></td></tr>"; 
		}
		else
		{
			if(xmlDoc2.getElementsByTagName("picname")[i].hasChildNodes()==true)
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " has pic associated.");
							
				var imagepath = "images/testimonial_pics/";		
				imagepath += "/" + xmlDoc2.getElementsByTagName("picname")[i].childNodes[0].nodeValue; 
//				alert("the imagepath is: " + imagepath);
				var imagetag = "<img width='100' src=\"" + imagepath + "\" alt='" + namestring + "' />";
	
				html += "<tr><td valign='top' align='center' width='120'><p style='margin:0px;'>";
				html += imagetag + "</p></td><td valign='top' align='left'>"; 			
				html += xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
				html += "<p style='margin-left:40px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + " (" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></td></tr>"; 
			}
			else //pic does not exist
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " does not have pic associated.");
				html += "<tr><td colspan='2' valign='top' align='left'>";			
				html += "<blockquote>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
				html += "<p style='margin-left:40px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + " (" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></blockquote></td></tr>"; 
			} //end if pic file exists
		}//end if student name exists
	if(j<testimonials.length-1)
		html += "<tr><td colspan='2'><hr/></td></tr>";			
} //end for
	html = html + "</table>";
	
	var reviewsdiv = document.getElementById("reviews");
	reviewsdiv.innerHTML = html;
//	reviewsdiv.style.zIndex = "5";

}

function outputrandom(index,div_id)  // populates a div titled "testimonial" with the selected record, formatted for sidebar
{
	var html = "<table width='200' align='center' valign='top' padding='0'><tr>";
	var i = index;
		
	var firstname = "";
	var lastname = "";
	var namestring = "";

		if(xmlDoc2.getElementsByTagName("studentfirstname")[i].hasChildNodes()==false) //generic testimonial, no pic
		{
//			alert("Record #" + i + " does not have a student name associated, processing html for generic testimonial");
			html += "<td width='100%' align='left'>";
			html += "<div style='padding:8px;'>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
			html += "<p style='margin-left:8px;margin-top:3px;'>-";
			if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
				{
					html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
					html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue;
				}	
			html += "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")</p></div></td></tr>"; 
		}
		else
		{
			if(xmlDoc2.getElementsByTagName("picname")[i].hasChildNodes()==true)
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " has pic associated.");
							
				var imagepath = "images/testimonial_pics/";		
				imagepath += "/" + xmlDoc2.getElementsByTagName("picname")[i].childNodes[0].nodeValue; 
//				alert("the imagepath is: " + imagepath);
				var imagetag = "<img width='60' style='float:left;padding:5px;width:60px;' src=\"" + imagepath + "\" alt='" + namestring + "' />";
	
				html += "<td valign='top' width='100%' align='left'><p style='margin:3px;'>";
				html += imagetag + " "; 			
				html += xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue + "</p>";
				if(lastname=="Pacheco")
				{
					html = html.replace("recommendations","recommen- dations");
				}
				html += "<p style='margin-left:8px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></td></tr>"; 
			}
			else //pic does not exist
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " does not have pic associated.");
				html += "<td valign='top' width='100%' align='left'>";			
				html += "<div style='padding:8px;'>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
				html += "<p style='margin-left:8px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></div></td></tr>"; 

			} //end if pic file exists
		}//end if student name exists
 	html = html + "</table>";
	
	var testimonialdiv = document.getElementById(div_id);
	testimonialdiv.innerHTML = html;

}

function randomQuote(div_id)  // randomly selects one "testimonial" 
{
//	alert("Random quote requested...");
	var testimonials = xmlDoc2.getElementsByTagName("review");
	var ks = specifiedQuote('Sullivan','Kerri');
	var i = ks;
	do
	  {
		i = Math.floor(Math.random()*(testimonials.length-1)) + 1;
	  }
	while (i == ks);

	outputrandom(i,div_id); // call output to sidebar
	
}

function specifiedQuote(last,first)  // randomly selects a Bucks County testimonial, returns record index
{
	var lastname = last;
	var firstname = first;
	var testimonials = xmlDoc2.getElementsByTagName("review");
	var i = 0;
	while((xmlDoc2.getElementsByTagName("studentlastname")[i].hasChildNodes()==false)||((xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue!=lastname)&&(xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue!=firstname)))
	{	
		i++;
	}
	return i;  
}

function randomCounty(countystring)  // randomly selects a Bucks County testimonial, returns record index
{
	var countyname = countystring;
	var testimonials = xmlDoc2.getElementsByTagName("review");
	var i = 0;
	while((xmlDoc2.getElementsByTagName("county")[i].hasChildNodes()==false)||(xmlDoc2.getElementsByTagName("county")[i].childNodes[0].nodeValue!=countyname))
	{	
		i = Math.floor(Math.random()*(testimonials.length-1)) + 1;
	}
	return i;  
}

function printCountyTestimonials(countystring)  //populates a div called "reviews", designed for main body
{
	var countyname = countystring;
	var testimonials = xmlDoc2.getElementsByTagName("review");
	var x = new Array();
	var j = 0;
	for(i=0;i<testimonials.length;i++)
		if(xmlDoc2.getElementsByTagName("county")[i].hasChildNodes()==true)
			if(xmlDoc2.getElementsByTagName("county")[i].childNodes[0].nodeValue==countyname)
				{
					x[j] = i;
					j++
				}

//	alert("The following array of indices for " + countyname + " was established: " + x);
	x.shuffle();
	var k;
	
	var html = "<table class='directory' cellspacing='5'><tr><th colspan='2'><h2>Success Stories from " + countyname + " County</h2></th></tr>";
	html += "<tr><td colspan='2'>";
	html += "<blockquote style='font-size:16px;margin-top:0px;'>A+ is committed to every student's success! We carefully match students with the best tutors in the Delaware Valley, and follow up regularly to ensure quality service you can trust. Whether you are looking for ongoing academic support or to improve standardized test scores, we have tutors of all backgrounds to help! Our entire team values your feedback, and is proud to share these success stories. Contact us today to discuss your test prep and tutoring needs!</blockquote>"
	html += "</td></tr><tr><td colspan='2'><hr/></td></tr>";
	for (k=0;k<j;k++) //do not include sample record
		{
		var firstname = "";
		var lastname = "";
		var namestring = "";
		var i = x[k];
//		alert("Array element #" + k +" is record index #" + i);
		if(xmlDoc2.getElementsByTagName("studentfirstname")[i].hasChildNodes()==false) //generic testimonial, no pic
		{
//			alert("Record #" + i + " does not have a student name associated, processing html for generic testimonial");
			html += "<tr><td colspan='2' valign='top'>";			
			html += "<blockquote>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
			html += "<p style='margin-left:40px;margin-top:3px;'>-";
			if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
				{
					html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
					html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue;
				}
			html += "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")</p></blockquote></td></tr>"; 
		}
		else
		{
			if(xmlDoc2.getElementsByTagName("picname")[i].hasChildNodes()==true)
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " has pic associated.");
							
				var imagepath = "images/testimonial_pics/";		
				imagepath += "/" + xmlDoc2.getElementsByTagName("picname")[i].childNodes[0].nodeValue; 
//				alert("the imagepath is: " + imagepath);
				var imagetag = "<img width='100' src=\"" + imagepath + "\" alt='" + namestring + "' />";
	
				html += "<tr><td valign='top' align='center' width='120'><p style='margin:0px;'>";
				html += imagetag + "</p></td><td valign='top'>"; 			
				html += xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
				html += "<p style='margin-left:40px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></td></tr>"; 
			}
			else //pic does not exist
			{
				firstname = xmlDoc2.getElementsByTagName("studentfirstname")[i].childNodes[0].nodeValue;
				lastname = xmlDoc2.getElementsByTagName("studentlastname")[i].childNodes[0].nodeValue;
				namestring = firstname + " " + lastname;
//				alert("Record #" + i + ", for " + namestring + " does not have pic associated.");
				html += "<tr><td colspan='2' valign='top'>";			
				html += "<blockquote>" + xmlDoc2.getElementsByTagName("quote")[i].childNodes[0].nodeValue;
				html += "<p style='margin-left:40px;margin-top:3px;'>-";
				if(xmlDoc2.getElementsByTagName("quoteby")[i].hasChildNodes()==true)
					{
						html += xmlDoc2.getElementsByTagName("quoteby")[i].childNodes[0].nodeValue;
						html += ", " + xmlDoc2.getElementsByTagName("relation")[i].childNodes[0].nodeValue + " of<br/>";
					}
				html += namestring + "<br/>(" + xmlDoc2.getElementsByTagName("school")[i].childNodes[0].nodeValue + ")<br/>";
				if(xmlDoc2.getElementsByTagName("scoreincrease")[i].hasChildNodes()==true)
					html += "<i><b>" + xmlDoc2.getElementsByTagName("scoreincrease")[i].childNodes[0].nodeValue + " Point Increase on the " + xmlDoc2.getElementsByTagName("test")[i].childNodes[0].nodeValue + "</b></i>";
				html += "</p></blockquote></td></tr>"; 
			} //end if pic file exists
		}//end if student name exists
	if(k<j-1)
		html += "<tr><td colspan='2'><hr/></td></tr>";			
	} //end for
	html = html + "</table>";
	
	var reviewsdiv = document.getElementById("reviews");
	reviewsdiv.innerHTML = html;
//	reviewsdiv.style.zIndex = "5";

}

