// JavaScript Document
function checkHotelReviewInput(e, submission)
{
	var overall = obj("overall");
	if (overall.value == 0) {
		alert("Please provide an overall rating for this resort/hotel.");
		return false;
	}

	// Check if at least one radio button is selected
	var approxDate = obj("approxDate");
	if (approxDate.value != "") {
		// Check for valid date
		if (!checkDate(approxDate.value)) {
			alert("Please enter in a valid approximate stay date (MM/DD/YYYY).");
			return false;
		}
	}
	
	var title = obj("title");
	var review = obj("review");
	
	if (title.value == "") {
		alert("Please enter in a title for your review.");
		return false;
	}
	
	if (review.value == "" || review.value == "Please write your review here.") {
		alert("Please enter in your review text.");
		return false;
	}

	// Submit or show preview window
	if (submission) {
		if(typeof document.forms[1].submit.click=="function")
			document.forms[1].submit.click();
		else
			document.forms[1].submit();
	
		return true;
	}
	else
		showPreview(e);
		
	return false;
}

function showPreview(e)
{
	// Get mouse position
   	var posx = 0;
   	var posy = 0;

	if (!e) var e = window.event;

	if (!isIE && MouseX) {
    	posx = MouseX;
      	posy = MouseY;
   	}
	else if (e.pageX || e.pageY){
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
	}
	
	var revPreview = obj("revPreview");
	revPreview.style.top = (posy - 500) + "px";
	revPreview.style.left = (posx + 25) + "px";
	revPreview.style.display = "block";
	
	var review = obj("review");
	var title = obj("title");
	var reviewCopy = obj("reviewCopy");
	reviewCopy.innerHTML = "<br /><br /><span style='font-weight: bold; font-size: 16px'>" + title.value + "</span><br /><br />";
	reviewCopy.innerHTML += "<div style='font-size: 12px; height: 250px; overflow: auto'>" + nl2br(htmlentities(review.value, 'ENT_QUOTES')) + "</div>";
	reviewCopy.innerHTML += "<div style='margin-top: 10px'><input type='button' value='Continue Editing' name='continueEdit' style='margin-right: 10px' onClick='closePrevWin()'/><input type='button' name='submitReview' value='Submit Review' onClick='submitReview(event)'/></div>";
	
	return false;
}

function TrackingMousePosition(e)
{
   e = e || window.event;
   if (e.pageX)
   {
	   MouseX = e.pageX;
	   MouseY = e.pageY;
   }
}
window.onmousemove = TrackingMousePosition;

function closePrevWin()
{
	// Clear out preview window and close
	var reviewCopy = obj("reviewCopy");
	var revPreview = obj("revPreview");
	
	reviewCopy.innerHTML = "";
	revPreview.style.display = 'none';
}

function submitReview(e)
{
	// Clear out preview window and close
	checkHotelReviewInput(e, true)
	return true;
}

function updateDestAttr()
{
	// Call ajax function to grab new state list based on country
	var dests = obj("dests");
	var params = "id=" + escape(dests.value);
	
	var url = "../ajax/ajx_getdestsight.php";
	xmlHttp = GetXmlHttpObject(buildAttrList);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.send(params);

	return true;
}

function buildAttrList()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

		if (xmlHttp.status == 200) {

			// Get xml document
			var sights = obj("sights");
			var sightList = xmlHttp.responseXML.documentElement;

			// Empty states list
			sights.options.length = 0;
			
			// Setup constant entries
			sights.options[sights.options.length] = new Option("Select an Attraction", "");
			
			for (i=0; i < sightList.childNodes.length; i++) {
				var newSightList = sightList.getElementsByTagName('sight')[i];
				var newSightName = URLDecode(newSightList.getElementsByTagName('name')[0].firstChild.nodeValue);
				var newSightID = URLDecode(newSightList.getElementsByTagName('id')[0].firstChild.nodeValue);
				
				// If there are no states returned just skip
				if (newSightName == "NONE")
					continue;

				sights.options[sights.options.length] = new Option(newSightName, newSightID);
			}
			
			var attRatingDiv = obj("attRatingDiv");
			attRatingDiv.innerHTML = "";
		}
		else {
                alert("ERROR: Could not retrieve attraction list - error=" + xmlHttp.status + ": " + xmlHttp.statusText);
		}
	}
}

function buildRestList()
{
	if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == "complete") {

		if (xmlHttp2.status == 200) {

			// Get xml document
			var rests = obj("rests");
			var restRating = obj("restRating");
			var restList = xmlHttp2.responseXML.documentElement;
	
			// Empty states list
			rests.options.length = 0;
			restRating.options[0].selected = true;


			// Setup constant entries
			rests.options[rests.options.length] = new Option("Select a Restaurant", "");
			
			for (var i=0; i < restList.childNodes.length; i++) {
				var newRestList = restList.getElementsByTagName('rest')[i];
				var newRestName = URLDecode(newRestList.getElementsByTagName('name')[0].firstChild.nodeValue);
				var newRestID = URLDecode(newRestList.getElementsByTagName('id')[0].firstChild.nodeValue);
				
				// If there are no states returned just skip
				if (newRestName == "NONE")
					continue;

				rests.options[rests.options.length] = new Option(newRestName, newRestID);
			}

		}
		else {
                alert("ERROR: Could not retrieve restaurant list - error=" + xmlHttp2.status + ": " + xmlHttp2.statusText);
		}
	}
}

// JavaScript Document
function checkDestReviewInput(e, submission)
{
	var dests = obj("dests");
	var newDest = obj("newDest");
	if (dests.value == "" && newDest.value == "") {
		alert("Please enter in a destination to review.");
		return false;
	}
	
	// Check if at least one radio button is selected
	var approxDate = obj("approxDate");
	if (approxDate.value != "") {
		// Check for valid date
		if (!checkDate(approxDate.value)) {
			alert("Please enter in a valid approximate stay date (MM/DD/YYYY).");
			return false;
		}
	}
	
	var title = obj("title");
	var review = obj("review");
	
	if (title.value == "") {
		alert("Please enter in a title for your review.");
		return false;
	}
	
	if (review.value == "" || review.value == "Please write your review here.") {
		alert("Please enter in your review text.");
		return false;
	}

	// Submit or show preview window
	if (submission) {
		document.destReview.submit();
		return false;
	}
	else {
		showDestPreview(e);
	}
	return false;
}

function showDestPreview(e)
{

	// Get mouse position
   	var posx = 0;
   	var posy = 0;

	if (!e) var e = window.event;

	if (!isIE && MouseX) {
    	posx = MouseX;
      	posy = MouseY;
   	}
	else if (e.pageX || e.pageY){
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY){
		posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
	}

	var revPreview = obj("revPreview");
	revPreview.style.top = (posy - 550) + "px";
	revPreview.style.left = (posx - 250) + "px";
	revPreview.style.display = "block";
	
	var review = obj("review");
	var title = obj("title");
	var reviewCopy = obj("reviewCopy");

	var divHTML = "<div style='font-size: 12px; height: 425px; overflow: auto'>";
	divHTML += "<br /><br /><span style='font-weight: bold; font-size: 16px'>Confirm Your Review:</span>";

	// Display email address 
	var displayEmail = "No";
	if (getSelectedRadioValue(document.destReview.display) == 1)
		displayEmail = "Yes";
	divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>Display Email:</span> " + displayEmail;	

	// Display destination
	var dests = obj("dests");
	var newDest = obj("newDest");
	if (newDest.value != "")
		divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>Destination:</span> " + newDest.value;	
	else
		divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>Destination:</span> " + dests[dests.selectedIndex].innerHTML;	
		
	// Display date of stay
	var approxDate = obj("approxDate");
	divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>Date of Stay:</span> " + approxDate.value;	
	
	// Children who travelled
	ageHTML = "";
	var tot = obj("ages0to2");
	var kids36 = obj("ages3to6");
	var kids79 = obj("ages7to9");
	var tween = obj("ages10to12");
	var teen = obj("ages13plus");

	if (tot.checked)
		ageHTML += "<br /><br /><span style='font-weight: bold; font-size: 11px'>Tots (0-2):</span> Yes";
	else
		ageHTML += "<br /><br /><span style='font-weight: bold; font-size: 11px'>Tots (0-2):</span> No";
		
	if (kids36.checked)
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (3-6):</span> Yes";
	else
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (3-6):</span> No";
		
	if (kids79.checked)
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (7-9):</span> Yes";
	else
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (7-9):</span> No";
		
	if (tween.checked)
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tweens (10-12):</span> Yes";
	else
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tweens (10-12):</span> No";
		
	if (teen.checked)
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Teens (13+):</span> Yes";
	else
		ageHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Teens (13)+:</span> No";
		
	if (ageHTML != "") {
		divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>Children Whom Traveled:</span>";
		divHTML += ageHTML;
	}
		
	// Display review
	divHTML += "<br /><br /><span style='font-weight: bold; font-size: 12px'>" + htmlentities(title.value, 'ENT_QUOTES') + "</span><br />";
	divHTML += nl2br(htmlentities(review.value, 'ENT_QUOTES'));

	var attractions = obj("sights");
	var newAtt = obj("newAtt");
	var newAttInfo = obj("newAttInfo");
	var newAttRating = obj("newAttRating");

	// Display Attraction info
	divHTML += "<br /><br /><br /><span style='font-weight: bold; font-size: 12px'>Attraction Info:</span>";	
	
	var attractionSelected = false;
	
	for (i = 1; i < attractions.options.length; i++) {

		if (attractions.options[i].selected) {
			attractionSelected = true;
			divHTML += "<br /><span style='font-weight: normal; font-size: 12px'>" + attractions.options[i].innerHTML;

			// Get ratings values and add them to preview window
			var rating = obj("rating_" + attractions.options[i].value);
			if (rating.value != 0)
				divHTML += " (Rating: " + rating.value + ")";
			else
				divHTML += " (Rating: No Rating)";

			divHTML += "</span>";	
			//divHTML += "<br /><br /><span style='font-weight: bold; font-size: 11px'>Attraction Rating: </span>" + attRatingText;	
		}
	}
	
	if (newAtt.value != "") {
		divHTML += "<br /><span style='font-weight: normal; font-size: 12px'>" + htmlentities(newAtt.value, 'ENT_QUOTES');

		if (newAttRating.value == 0)
			divHTML += " (Rating: None)";
		else
			divHTML += " (Rating: " + newAttRating.value + ")"; 
		divHTML += "</span>";	
		
		attractionSelected = true;
	}

	if (newAttInfo.value != "")
		divHTML += "<br /><br /><span style='font-weight: normal; font-size: 12px'>" +  nl2br(htmlentities(newAttInfo.value, 'ENT_QUOTES')) + "</span>";	

	if (!attractionSelected) {
		divHTML += "<br /><br /><span style='font-weight: normal; font-size: 12px'>None</span>";	
	}

	// Display restaurant info
	var restInfo = obj("restInfo");
	if (restInfo.value != "") {
		divHTML += "<br /><br /><br /><span style='font-weight: bold; font-size: 12px'>Restaurant Info:</span>";	
		divHTML += "<br /><br /><span style='font-weight: normal; font-size: 12px'>" + nl2br(htmlentities(restInfo.value, 'ENT_QUOTES')) + "</span>";	
	}
	
	// Display family ratings
	divHTML += "<br /><br /><br /><span style='font-weight: bold; font-size: 12px'>Family Ratings:</span>";	
	
	var parent = obj("parent");
	if (parent.value == 0)
		divHTML += "<br /><br /><span style='font-weight: bold; font-size: 11px'>Mom and Dad: </span> No Rating";
	else
		divHTML += "<br /><br /><span style='font-weight: bold; font-size: 11px'>Mom and Dad: </span>" + parent.value;	
	
	var totList = obj("totList");
	if (totList.value == 0)
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tots (0-2): </span> No Rating";
	else
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tots (0-2): </span>" + totList.value;	
	
	var kids36List = obj("kids36List");
	if (kids36List.value == 0)
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (3-6): </span> No Rating";
	else
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (3-6): </span>" + kids36List.value;	
	
	var kids79List = obj("kids79List");
	if (kids79List.value == 0)
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (7-9): </span> No Rating";
	else
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Kids (7-9): </span>" + kids79List.value;	
	
	var tweensList = obj("tweensList");
	if (tweensList.value == 0)
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tweens (10-12): </span> No Rating";
	else
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Tweens (10-12): </span>" + tweensList.value;	
	
	var teensList = obj("teensList");
	if (teensList.value == 0)
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Teens (10-12): </span> No Rating";
	else
		divHTML += "<br /><span style='font-weight: bold; font-size: 11px'>Teens (10-12): </span>" + teensList.value;	
	

	divHTML += "</div>";

	reviewCopy.innerHTML = divHTML;

	reviewCopy.innerHTML += "<div style='margin-top: 10px'><input type='button' value='Continue Editing' name='continueEdit' style='margin-right: 10px' onClick='closePrevWin()'/><input type='button' name='submitReview' value='Submit Review' onClick='submitDestReview(event)'/></div>";
	
	return false;
}

function editRating(checkbox, rating, ratingList)
{
	var ageChk = obj(checkbox);
	var rtg = obj(rating);
	var rtgList = obj(ratingList);
	
	if (ageChk.checked) {
		rtgList.options[0].selected = true;
		rtg.style.display = 'block';
	}
	else {
		rtg.style.display = 'none';
	}
	
	return true;
}

function submitDestReview(e)
{
	// Clear out preview window and close
	checkDestReviewInput(e, true)
	return true;
}

function updateRatingList()
{
	// Clear out rating div area
	var divHTML = "";
	var attRatingDiv = obj("attRatingDiv");
	attRatingDiv.innerHTML = "";
	
	
	// Loop through selected attraction list
	var sights = obj("sights");
	for (i=0; i < sights.length; i++) {
		
		if (i == 0)
			continue; 
			
		if (sights[i].selected) {
			
			divHTML += "<div><br />";
			divHTML += "<span style='font-weight: bold'>" + sights[i].innerHTML + " Rating:</span><br />";
			divHTML += "<select name='rating_" + sights[i].value + "' id='rating_" + sights[i].value + "' >";
			divHTML += "<option value='0'>Select a Rating</option>";
			divHTML += "<option value='1'>1</option>";
			divHTML += "<option value='2'>2</option>";
			divHTML += "<option value='3'>3</option>";
			divHTML += "<option value='4'>4</option>";
			divHTML += "<option value='5'>5</option>";
			divHTML += "</select>";
			divHTML += "</div>";
		}
	}
	
	attRatingDiv.innerHTML = divHTML;
	
	return true;
}







