﻿BenefulMap.ModalAddReview = BenefulMap.ModalAuthenticatedForm.extend({
	init: function(modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params, formElements) {
		this._super( modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params, formElements);
		this.openListener("addreview.open");
		this.closeListener("addreview.close");
		this.allowSubmission = true;
		if (jQuery.url.attr("file")) {
			this.spotId = jQuery.url.attr("file").replace(".aspx", "");
		} else {
			this.spotId = 0;
		}
		params = {
			id: this.getSpotId(),
			ph: 1
		};
		this.setParams(params);
		this.initialParams = params;
	},
	getSpotId: function() {
		return parseInt(this.spotId);	
	},
	setSpotId: function(newSpotId) {
		this.spotId = parseInt(newSpotId);
	},
	getAllowSubmission: function() {
	   return this.allowSubmission;
	},
	setAllowSubmission: function(allowSubmission) {
	   this.allowSubmission = allowSubmission;
	},
	addModalListeners: function() {
        this._super();
		$("#review-thank-you-see").click(function(event) {
			event.preventDefault();
			var href = window.location.href;
			href = href.split("#")[0] + "#0";
			window.location.href = href;
			location.reload();
		});
    },
	showThankYou: function() {
		this.setTitle("Thanks for the Review");
		var params = {};
		params.ph = 2;
		params.id = this.getSpotId();
		this.setParams(params);
		this.populate();
	},
	redisplayFormWithMessage: function(message) {
		var params = {};
		params.ph = 1;
		params.id = this.getSpotId();
        params.message = message;
        this.setParams(params);
        this.populate();
	},
	checkResult: function(pass, msg) {
        this.setAllowSubmission(true);
		if (pass == true) {
			if (msg == "promotion" || msg == " promotion") {
			    this.close();	
				$(document).trigger("promotion.open", [0, "review"]);
			} else {
				this.showThankYou();
			}
        } else {
            this.redisplayFormWithMessage(msg);
        }
	},
	openListener: function() {
		var self = this;
		$(document).bind("addreview.open", function(event, spotId) {
			self.setSpotId(spotId);
			params = {
				ph: 1,
				id: self.getSpotId()
			};
			self.setParams(params);
			self.open();
		});
	},
	serializeFormData: function() {
		var self = this;
		var serializedData = $(this.getFieldsToSerialize()).serializeArray();
		jQuery.each(serializedData, function() {
			self.setIndividualFormData(this.name, this.value);
		});
		$(this.getModalDiv() + " input[name=Rating]").each( function() {
			if (this.checked) {
				self.setIndividualFormData("Rating", $(this).val());
			}
		});
	},
	submitFormData: function() {
	        this._super();
	        // prevent hyperactive users from clicking the Send button over and over and submitting duplicate records.  Added per a defect
	        if (this.getAllowSubmission() == true) { 
	            this.setAllowSubmission(false);
		        var self = this;
		        var id = this.getSpotId();
		        var headline = this.getIndividualFormData('Headline');
		        var body = this.getIndividualFormData('Review');
		        var rating = parseInt(this.getIndividualFormData('Rating'));
		        console.log(this.getFormData());
		        WagWorld.Web.AjaxUtility.AddBusinessReview(id, headline, body, rating, function(result) {
			        self.checkResult(result.value.Pass, result.value.Reason);		    
		    });
		}
	}
});


