LibManager.require('frame.prototype.ajax'); LibManager.require('frame.prototype.form'); LibManager.require('frame.scriptaculous.effects'); var HolidayReviewRatingController = { ajaxRequest: null, ajaxTimeout: null, ajaxReqTime: null, ajaxMaxReqTime: 10000, ajaxCheckInterval: 500, ajaxBaseUrl: '/HolidayReview/AddRatingToReview', attach: function() { for (i = 0; i <= document.forms.length - 1; i++) { if (document.forms[i].className == 'reviewRating') { Event.observe(document.forms[i], 'submit', this.validate.bindAsEventListener(this)) } } }, validate: function(e) { var form = Event.element(e); var ratingResponse = Form.getInputs(form, "radio", "rating"); var reviewId = Form.getInputs(form, "hidden", "reviewId")[0].value; var rating = 0; var wrapper = $("ratingWrapper_" + reviewId); var msgBox = $("ratingMsg_" + reviewId); if (ratingResponse[0].checked || ratingResponse[1].checked) { if (ratingResponse[0].checked) { rating = ratingResponse[0].value } else { rating = ratingResponse[1].value } this.rateReview(reviewId, rating, msgBox, wrapper) } else { msgBox.firstChild.nodeValue = "You have not selected whether the review was helpful or not helpful."; this.doHighlight(wrapper); this.styleMsgBox(msgBox) } Event.stop(e) }, callInProgress: function() { switch (this.ajaxRequest.transport.readyState) { case 1: case 2: case 3: return true; break; default: return false; break } }, rateReview: function(reviewId, rating, msgBox, wrapper) { var errMsg = 'Error: no data received. Please try again.'; var requestVars = { content: 'rateHolidayReviewAjax', rating: rating, reviewId: reviewId, reviewType: "holiday" }; this.styleMsgBox(msgBox); this.createAjaxUpdater(wrapper, msgBox, requestVars, errMsg) }, checkResponse: function() { var thisObj = this; this.ajaxReqTime += this.ajaxCheckInterval; if (this.callInProgress() && (this.ajaxReqTime >= this.ajaxMaxReqTime)) { this.ajaxRequest.transport.abort(); if (this.ajaxRequest.options['onFailure']) { this.ajaxRequest.options['onFailure']() } } else { this.ajaxTimeout = window.setTimeout(function() { thisObj.checkResponse() }, this.ajaxCheckInterval) } }, doHighlight: function(wrapper) { new Effect.Highlight(wrapper, { startcolor: '#ffffff', endcolor: '#ffffcc', duration: 0.5 }) }, styleMsgBox: function(msgBox) { msgBox.style.padding = "1em 0 1em 0" }, createAjaxUpdater: function(wrapper, container, reqVars, errorMsg) { var thisObj = this; this.ajaxReqTime = 0; this.ajaxTimeout = window.setTimeout(function() { thisObj.checkResponse() }, thisObj.ajaxCheckInterval); this.ajaxRequest = new Ajax.Updater(container, thisObj.ajaxBaseUrl, { method: 'get', parameters: $H(reqVars).toQueryString(), onComplete: function(request) { window.clearTimeout(thisObj.ajaxTimeout); if (!request.responseText.length || request.status != 200) { alert(errorMsg) } else { thisObj.doHighlight(wrapper) } }, onFailure: function(request) { window.clearTimeout(thisObj.ajaxTimeout); alert('Error: Cannot contact database. Please try again.') } }) } }; $j(function() { HolidayReviewRatingController.attach() });