//this is the number of surveys displayed per page
var defaultGroupSize = 3;

//user selected yes or no for a specific review
function updateRatings(helpful, count, email, type) {

	hideTRObject("allowReview" + count);
	showTRObject("updatingText" + count);
	var id = document.getElementById("surveyId" + count).firstChild.data;
	var url = "/tires/SurveyDisplayOnTires?updateRatings=true&helpful=" + helpful + "&id=" + id + "&email=" + email + "&count=" + count + "&type=" + type;

	var ajax = new AJAXInteraction(url, displayUpdatedRatings);
	ajax.doGet();
}


/*Formats and displays ratings info for a given survey on the tires.jsp comments page */
function displayUpdatedRatings(responseXML) {
	if(responseXML) {

		var surveyRatingUpdateInfo =  responseXML.getElementsByTagName("surveyRatingUpdateInfo");
		if (surveyRatingUpdateInfo.length > 0){
			var count = surveyRatingUpdateInfo[0].getElementsByTagName("count")[0].firstChild.data;

			hideTRObject("updatingText" + count);

			//change rating options to reflect selection
			document.getElementById("allowReview" + count).style.display = "none";
			document.getElementById("helpfulRatingNo" + count).style.display = "none";
			document.getElementById("helpfulRatingYes" + count).style.display = "none";
	
			if (surveyRatingUpdateInfo[0].getElementsByTagName("helpful")[0].firstChild.data.indexOf("Y") == -1) {
				document.getElementById("helpfulRatingNo" + count).style.display = "block";
			} else {
				document.getElementById("helpfulRatingYes" + count).style.display = "block";
			}
	
			//update rating totals
			document.getElementById("displayRatings" + count).firstChild.data = surveyRatingUpdateInfo[0].getElementsByTagName("helpfulRatings")[0].firstChild.data + " of " + surveyRatingUpdateInfo[0].getElementsByTagName("totalRatings")[0].firstChild.data + " people found the following review helpful:";
	
			document.getElementById("displayRatings" + count).style.display = "block";
	
		} else {
				//alert("incomplete");
			var sessionExpired =  responseXML.getElementsByTagName("sessionExpired");
			var error = "We're sorry. Your session has expired. Please reselect your vehicle or tire.";
			document.location.replace("/tires/reviews/MenuServlet?search=surveyComments&error=" + error);
			return;
		}


	}
}


function surveyInit() {

	//need tiremake, tiremodel and email.If tireInfo is null, we don't have what we need so just return
	if (document.tireInfo == null) {
		return;
	}
	var tireMake = document.tireInfo.tireMake.value;
	var tireModel = document.tireInfo.tireModel.value;

//	alert("tireMake: " + tireMake);

	document.getElementById("displaySurveys").style.display = "none";
	document.getElementById("noSurveys").style.display = "none";

	var url = "/tires/SurveyDisplayOnTires?tireMake=" + tireMake + "&tireModel=" + tireModel;
	var ajax = new AJAXInteraction(url, displayResults);
	ajax.doGet();

}


function displayResults(responseXML) {

	if(responseXML) {

		hideTRObject("loadingResults");

		if (responseXML.getElementsByTagName("noSurveys").length > 0) {
			//alert("we do NOT have surveys");
			document.getElementById("noSurveys").style.display = "block";
		} else {

			//clearSurveys(0);
			//clearFollowUps(0);

			//survey data and paging info
			var surveys =  responseXML.getElementsByTagName("survey");



	//alert("displayResults surveys count " + surveys.length);
	var hideStart = surveys.length;

		for(var i=0; i<surveys.length; i++) {

			document.getElementById("surveyVehicle" + i).firstChild.data = surveys[i].getElementsByTagName("surveyVehicle")[0].firstChild.data;

			//link surveyVehicle for SurveyComments.jsp
				var tmpVehicleLink = document.createElement('a');
				tmpVehicleLink.id = "vehicle link";
				tmpVehicleLink.href = "/survey/SurveyCommentsMMY.jsp?autoMake=" + surveys[i].getElementsByTagName("surveyAutoMake")[0].firstChild.data + "&autoModel=" + surveys[i].getElementsByTagName("surveyAutoModel")[0].firstChild.data + "&autoYear=" + surveys[i].getElementsByTagName("surveyAutoYear")[0].firstChild.data + "&commentStatus=P&additionalComments=Y&index=0";

				var tmpVehicleLinkText = document.createTextNode("More Tire Reviews for This Vehicle");
				tmpVehicleLink.appendChild(tmpVehicleLinkText);

				var tmpVehicleLinkChildren = document.getElementById("vehicleLink" + i).childNodes;
				for( var j=0; j < tmpVehicleLinkChildren.length; j++) {
					document.getElementById("vehicleLink" + i).removeChild(tmpVehicleLinkChildren.item(j));
				}

				document.getElementById("vehicleLink" + i).appendChild(tmpVehicleLink); 



			//survey tire
			var bold = document.createElement("b");
			bold.appendChild(document.createTextNode(surveys[i].getElementsByTagName("surveyTireMake")[0].firstChild.data + " " + surveys[i].getElementsByTagName("surveyTireModel")[0].firstChild.data));
			document.getElementById("surveyTire" + i).appendChild(bold); 


			//perf cat link
			var perfCatLink = document.createElement('a');
			perfCatLink.href = surveys[i].getElementsByTagName("surveyPerfURL")[0].firstChild.data;
			var perfText= document.createTextNode(surveys[i].getElementsByTagName("surveyTirePerfName")[0].firstChild.data);

  			perfCatLink.appendChild(perfText);

			//first remove child, if it exists
			var surveyPerfURLChildren = document.getElementById("surveyPerfURL" + i).childNodes;
			for( var j=0; j < surveyPerfURLChildren.length; j++) {
				document.getElementById("surveyPerfURL" + i).removeChild(surveyPerfURLChildren.item(j));
			}
			//then append a new child
			document.getElementById("surveyPerfURL" + i).appendChild(perfCatLink); 


			//Survey Rating in red. 
			var bold2 = document.createElement("b");
			bold2.appendChild(document.createTextNode(surveys[i].getElementsByTagName("surveyRating")[0].firstChild.data));
			if(surveys[i].getElementsByTagName("surveyRating")[0].firstChild.data == 'Not Rated') {
				bold2.className = "rednote12";
			} else {
				bold2.className = "redhead";
			}
			//first remove children if they exist
			var surveyRatingChildren = document.getElementById("surveyRating" + i).childNodes;
			for( var j=0; j < surveyRatingChildren.length; j++) {
				document.getElementById("surveyRating" + i).removeChild(surveyRatingChildren.item(j));
			}

			//then append a new child
			document.getElementById("surveyRating" + i).appendChild(bold2);


			document.getElementById("totalMiles" + i).firstChild.data = surveys[i].getElementsByTagName("totalMiles")[0].firstChild.data;
			document.getElementById("initalReviewMiles" + i).firstChild.data = surveys[i].getElementsByTagName("surveyMiles")[0].firstChild.data;
			if (surveys[i].getElementsByTagName("trEmployee")[0].firstChild.data != 'nope') {
				document.getElementById("trEmployee" + i).firstChild.data = surveys[i].getElementsByTagName("trEmployee")[0].firstChild.data;

			}
			document.getElementById("surveyDate" + i).firstChild.data = surveys[i].getElementsByTagName("surveyDate")[0].firstChild.data;
			document.getElementById("surveyId" + i).firstChild.data = surveys[i].getElementsByTagName("surveyId")[0].firstChild.data;
			document.getElementById("surveyLocation" + i).firstChild.data = surveys[i].getElementsByTagName("surveyLocation")[0].firstChild.data;
			document.getElementById("surveyDrivingCondition" + i).firstChild.data = surveys[i].getElementsByTagName("surveyDrivingCondition")[0].firstChild.data;
			document.getElementById("surveyDrivingStyle" + i).firstChild.data = surveys[i].getElementsByTagName("surveyDrivingStyle")[0].firstChild.data;
			var encodedComment = surveys[i].getElementsByTagName("additionalComments")[0].firstChild.data;
			var decodedComment = decodeURIComponent(encodedComment);
			decodedComment = decodedComment.replace(/\+/g, " ");
			document.getElementById("additionalComments" + i).firstChild.data = decodedComment.replace(/PLUS/g, "+");


			//Create SurveyFollowUps
			if(surveys[i].getElementsByTagName("surveyFollowUp").length > 0){


				var followUps = surveys[i].getElementsByTagName("surveyFollowUp");
				//var displayFollowUps = document.getElementById("displayFollowUps");
				var displayFollowUps = document.getElementById("displayFollowUps" + i);



				//First Clear Previous Settings
				var followUpChildren = document.getElementById("displayFollowUps" + i).childNodes;
				for( var j=0; j < followUps.length; j++) {

					for( var loop=0; loop < followUpChildren.length; loop++) {
						//alert("child: " + followUpChildren[loop].id);
						document.getElementById("displayFollowUps" + i).removeChild(followUpChildren[loop]);
					}
	
					//for reasons that might make sense to me later, followUpChildren still has 
					//values after first loop, so I'm running it again to keep extra stuff from appearing
					//on the page. 
					for( var loop=0; loop < followUpChildren.length; loop++) {
						//alert("2nd loop child: " + followUpChildren[loop].id);
						document.getElementById("displayFollowUps" + i).removeChild(followUpChildren[loop]);
					}
					//alert("followUpChildren.length after clearing: " + followUpChildren.length);

				}

				
				for( var j=0; j < followUps.length; j++) {

					var reviewDateBlock = document.createElement("p");
					//reviewDateBlock.className = "reviewdate";
					//reviewDateBlock.id = "reviewDateBlock";

					//review date paragraph
					var bold = document.createElement("strong");
					bold.appendChild(document.createTextNode("Follow-up Review"));
					reviewDateBlock.appendChild(bold);
					reviewDateBlock.appendChild(document.createTextNode(", " + followUps[j].getElementsByTagName("followUpMiles")[0].firstChild.data + " Miles on Tires " ));
					displayFollowUps.appendChild(reviewDateBlock);
					reviewDateBlock = document.createElement("p");
  				//reviewDateBlock.appendChild(document.createElement("br"));
					reviewDateBlock.appendChild(document.createTextNode(followUps[j].getElementsByTagName("followUpDate")[0].firstChild.data));
					displayFollowUps.appendChild(reviewDateBlock);
											
					//comment div
					var commentBlock = document.createElement("blockquote");
					//commentBlock.className = "reviewtext";
					//commentBlock.id = "commentBlock";

					var encodedFollowUpComment = followUps[j].getElementsByTagName("followUpComment")[0].firstChild.data; 
					var decodedFollowUpComment = decodeURIComponent(encodedFollowUpComment);
					decodedFollowUpComment = decodedFollowUpComment.replace(/\+/g, " ");
					decodedFollowUpComment = decodedFollowUpComment.replace(/PLUS/g, "+");


					commentBlock.appendChild(document.createTextNode(decodedFollowUpComment));
					displayFollowUps.appendChild(commentBlock);
					//displayFollowUps.appendChild(document.createElement("p"));


				}

				document.getElementById("displayFollowUps" + i).style.display = "block";
			}

			//ratings info
			if(surveys[i].getElementsByTagName("totalRatings")[0].firstChild.data.indexOf("0") != -1){
				document.getElementById("displayRatings" + i).style.display = "none";
			} else {
				document.getElementById("displayRatings" + i).firstChild.data = surveys[i].getElementsByTagName("helpfulRatings")[0].firstChild.data + " of " + surveys[i].getElementsByTagName("totalRatings")[0].firstChild.data + " people found the following review helpful:";
				document.getElementById("displayRatings" + i).style.display = "block";
			}

			//turn display off on all options
			document.getElementById("allowReview" + i).style.display = "none";
			document.getElementById("helpfulRatingNo" + i).style.display = "none";
			document.getElementById("helpfulRatingYes" + i).style.display = "none";
			document.getElementById("ownReview" + i).style.display = "none";
			document.getElementById("followUpLink" + i).style.display = "none";

			//is it user's review and have they not yet rated it?
			if (surveys[i].getElementsByTagName("ownReview")[0].firstChild.data.indexOf("false") != -1 &&
				surveys[i].getElementsByTagName("alreadyRated")[0].firstChild.data.indexOf("false") != -1) {
				//if no to both, allow user to rate it.
				document.getElementById("allowReview" + i).style.display = "block";

			} else if (surveys[i].getElementsByTagName("alreadyRated")[0].firstChild.data.indexOf("true") != -1) {

				if (surveys[i].getElementsByTagName("helpfulRating")[0].firstChild.data.indexOf("Y") != -1) {
					//if already rated, did user find review helpful?
					document.getElementById("helpfulRatingYes" + i).style.display = "block";
				} else {
					document.getElementById("helpfulRatingNo" + i).style.display = "block";
				}
			} else if (surveys[i].getElementsByTagName("ownReview")[0].firstChild.data.indexOf("true") != -1) {
				document.getElementById("ownReview" + i).style.display = "block";

				if(surveys[i].getElementsByTagName("surveyFollowUpPending")[0].firstChild.data.indexOf("false") != -1) {
					var tmpLink = document.createElement('a');
					var tmpElement = document.createElement('li');
					tmpLink.id = "tmpLink" + i;
					tmpLink.href = "/account/ReviewUpdate.jsp?surveyID=" + surveys[i].getElementsByTagName("surveyId")[0].firstChild.data;
					var linkText= document.createTextNode("Submit a Follow-up Survey");
					
					tmpLink.appendChild(linkText);

					tmpElement.appendChild(tmpLink);

					var followUpLinkChildren = document.getElementById("followUpLink" + i).childNodes;
					for( var j=0; j < followUpLinkChildren.length; j++) {
						document.getElementById("followUpLink" + i).removeChild(followUpLinkChildren.item(j));
					}
					document.getElementById("followUpLink" + i).appendChild(tmpElement); 
					document.getElementById("followUpLink" + i).style.display = "block";


				}
			}

			document.getElementById("surveyBody" + i).style.display = "block";

	}

	    //remove any remainder surveys if current page size isn't the default size 
		//so extras are not displayed
		if (hideStart < defaultGroupSize) {
		     clearSurveys(hideStart);
		 }



			document.getElementById("displaySurveys").style.display = "block";

		} //end of noSurveys.length

	} //end of if responseXML

} //end of displayResults function



function clearSurveys(start) {
	for(start; start<defaultGroupSize; start++) {
		document.getElementById("surveyBody" + start).style.display = "none";
	}
}
