var Reviews = {
	now: function() {
		var d = new Date();
		return (d.getMonth()+1) + "/" + (d.getDate()) + "/" + (d.getFullYear());
	},

	get_booking: function() {
		return {
			id: $("#bid").val(),
			custname: [$("#first").val(), $("#last").val()],
			location: [$("#city").val(), $("#state").val(), $("#country").val()],
			arrival: $("#arrival").val()
		}
	},
	
	get_prefs: function() {
		// var rec = $("#rec :checked").val();
		// var fmt = $("#fmt :checked").val();
		// var from = $("#from :checked").val();
		var rec = $("#rec :checked").length ? $("#rec :checked").val() : 1;
		var fmt = $("#fmt :selected").val();
		var from = $("#from :selected").val();
		return { rec: rec, fmt: fmt, from: from };
	},

	get_ratings: function() {
		return {
			cleanliness: Reviews.rating[$("#r_cleanliness :selected").val()] || "",
			service: Reviews.rating[$("#r_service :selected").val()] || "",
			value: Reviews.rating[$("#r_value :selected").val()] || ""
		};
	},
	
	get_ratings_html: function() {
		var r = Reviews.get_ratings();
		
		if (!r.cleanliness && !r.service && !r.value)
			return "";
		
		var h = "<table class=\"review-ratings\">";
		h += "<tr><th colspan=\"2\">By The Numbers</th></tr>";
		if (r.cleanliness)
			h += "<tr><td>Cleanliness:</td><td>" + r.cleanliness + "</td></tr>";
		if (r.service)
			h += "<tr><td>Service:</td><td>" + r.service + "</td></tr>";
		if (r.value)
			h += "<tr><td>Value:</td><td>" + r.value + "</td></tr>";
		h += "</table>";
		return h;
	},

	fmt_location: function() {
		var b = Reviews.get_booking();
		if (b.location[1] == '--' || b.location[1] == '')
			return b.location[0] + ", " + b.location[2];
		else
			return b.location.join(", ");
	},

	fmt_text: function(t) {
		return t.replace(/\r/g, "").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br/>");
	},

	init: function() {
		$("#post-form select,#post-form input").change(Reviews.onchange);
		$("#post-form textarea").keyup(Reviews.onchange);
		Reviews.onchange();
		$("#arrival").datepicker();
	},

	onchange: function() {
		Reviews.update_preview();
	},
	
	rating: {
		"5": "Excellent",
		"4": "Good",
		"3": "Just OK",
		"2": "Bad",
		"1": "Very bad"
	},
	
	submit: function() {
		var b = Reviews.get_booking();
		var f = $("#post-form-f").serializeArray();
		f.b = f.bid;
		f.save = 1;
		Fed.Popover.show("Submitting", "<p>" + Fed.Html.loading + "</p><p>We are submitting your review to our servers, please wait..</p>");
		
		// check global
		if (typeof(reviews_submit_url) == "undefined")
			// 20080716 tlack: changed because doesnt work sometimes 
			reviews_submit_url = "/review.php";
			//reviews_submit_url = "/reviews_post.phtml";
			
		$.post(reviews_submit_url, f, Reviews.submit_done);
		return false;
	},
	
	submit_done: function(data) {
		if (typeof(reviews_post_url) == "undefined")
			reviews_post_url = "/";
		if (Fed.trim(data) != "")
			var h = "<p><b>Error in submission:</b></p>" + data;
		else {
			var h = "<p>Thank you. Your information will be useful for other travelers. We'll review your recommendation and publish it shortly.</p><p><a target=\"_top\" href=\"" + reviews_post_url + "\">Click here to return to our homepage</a></p>";
			$("#post-form-f button").attr("disabled", "disabled");
		}
		Fed.Popover.update(h);
	},
	
	update_preview: function() {
		var b = Reviews.get_booking();
		var p = Reviews.get_prefs();
		var at = "";
		
		at = "<div class=\"review\">";
		at += Reviews.get_ratings_html();
		at += "<div class=\"review-from\">";
		
		if (p.rec == "1")
			at += "<img class=\"thumb\" src=\"/images/thumbsup.gif\" />Recommended. ";
		else
			at += "<img class=\"thumb\" src=\"/images/thumbsdown.gif\" />Not recommended. ";
		
		if (p.fmt == "full")
			at += b.custname.join(" ");
		if (p.fmt == "first")
			at += b.custname[0];
		if (p.fmt == "anon")
			at += "Anonymous";
			
		if (p.from == "all")
			at += " from " + Reviews.fmt_location();
		if (p.from == "country")
			at += " from " + b.location[2];
		
		if (b.arrival)
			at += " was a guest on " + b.arrival;
		
		at += "</div>";
		at += "<div class=\"review-body\">";
		at += Reviews.fmt_text($("#comments").val());
		at += "</div><div class=clr></div></div>";
		$("#appear-content").html(at);
	}
};

$(document).ready(Reviews.init);
