﻿BenefulMap.ModalToyPromotion = BenefulMap.ModalForm.extend({
	init: function(modalTitle, modalElements, modalOptions, snippetPath, params, formElements) {
		this._super(modalTitle, modalElements, modalOptions, snippetPath, params, formElements);
		this.openListener("promotion.open");
		this.allowSubmission = true;
	},
	getCheckbox: function() {
		return this.formElements.checkbox;
	},
	toggleAddressForm: function() {
		this.addressFormEnabled = !(this.addressFormEnabled);
		var checked = $(this.getCheckbox()).get(0).checked;
		$(this.getFieldsToSerialize()).each( function() {
			this.disabled = !(checked);
		});
		if (checked) {
			var validForm = this.checkFormIsValid();
			this.setFormStatus(validForm);
		}
	},
	checkboxListener: function() {
		var self = this;
		$(this.getCheckbox()).click( function(event) {
			self.toggleAddressForm();
		});
	},
	addModalListeners: function() {
		this._super();
		this.checkboxListener();
		$("#rules-link").toggle(
        function (e) {
            e.preventDefault();
            $("#rules-content").css({"display":"inline"});
            $("#rules-link").html("Close");
        },
        function (e) {
            e.preventDefault();
            $("#rules-content").css({"display":"none"});
            $("#rules-link").html("Read more");
        });
	},
	submitFormData: function() {
		if (this.allowSubmission == true) { // prevent hyperactive users from clicking the Send button over and over and submitting duplicate records.  Added per a defect
			this.allowSubmission = false;
			this._super();
			var self = this;
			WagWorld.Web.AjaxUtility.PromotionThankYouSubmit(this.getIndividualFormData('Address1'), this.getIndividualFormData('Address2'), this.getIndividualFormData('City'), this.getIndividualFormData('State'), this.getIndividualFormData('Zip'), function(result) {
				self.checkResult(result.value.Pass, result.value.Reason, result.value.Confirmation);
				console.log(result);
			});
		}
	},
	checkResult: function(pass, msg, confirmNum) {
		console.log(pass, msg, confirmNum);
		if (pass == true) {
			console.log("pass is true");
			this.displayConfirmationPage(confirmNum);
		} else {
			console.log("pass is false");
			var params = {};
	        params.context = this.context;
			params.confirmationId = this.confirmationId;
			params.message = msg;
			console.log("params to redisplay form with message", params);
			this.setParams(params);
			this.populate();
		}
	},
	displayConfirmationPage: function(confirmNum) {
		var params = {
			'confirmationId': confirmNum
		};
		console.log("params to show confirm panels", params);
		this.setParams(params);
		this.populate();
	},
	openListener: function(eventName) {
		var self = this;
		$(document).bind(eventName, function(event, confirmationId, context) {
			self.confirmationId = confirmationId;
			self.context = context;
			var params = {
				'confirmationId': confirmationId,
				'context': context
			};
			self.setParams(params);
			self.open();
		});
	},
	addModalTargetListener: function() {
		// this is a stub to override inherited function. this modal can not be fired from a link
	}
});




