﻿BenefulMap.ModalRegistrationForm = BenefulMap.ModalForm.extend({
    init: function(modalTitle, modalElements, modalOptions, snippetPath, params, formElements) {
        this._super(modalTitle, modalElements, modalOptions, snippetPath, params, formElements);
        this.openListener("registration.open");
        this.closeListener("registration.close");
    },
    addModalListeners: function() {
        this._super();
        this.increaseAjaxProTimeout();
        this.handleAjaxProTimeout();
		this.thankYouButtonListeners();
		this.loginButtonListener();
    },
    increaseAjaxProTimeout: function() {
    	AjaxPro.timeoutPeriod = 20 * 1000;	
    },
    handleAjaxProTimeout: function() {
    	var self = this;
    	AjaxPro.onTimeout = function() {
    		console.warn("Ajax pro method timed out.");
    		if (self) {
    			self.redisplayFormWithMessage(9);
    		}
    	}	
    },
	thankYouButtonListeners: function() {
		var self = this;
		$("#register-add-spot").click(function(event) {
			event.preventDefault();
			var href = window.location.href + "#add-spot";
			window.location.href = href;
			location.reload();
		});
	},
	loginButtonListener: function() {
		var self = this;
		$(this.getModalDiv() + " a.registration-login").click(function(event) {
			$(self.getModalDiv()).dialog('close');
			$(document).trigger("login.open");
			event.preventDefault();
		});
	},
	injectAdditionalDataIntoSerializedArray: function() {
		var self = this;
		var radioButtonIsChecked = false;
		$("#modal-registration input:radio").each(function() {
			if (this.checked) {
				self.setIndividualFormData("EmailCommunication", this.value);
				radioButtonIsChecked = true;
			}
		});
		if (!radioButtonIsChecked) {
			self.setIndividualFormData("EmailCommunication", "N");
		}
	},
	testAdditionalRequiredFields: function() {
		var radioButtonsStatus = false;
		$("#modal-registration input:radio").each(function() {
			console.log(this);
			if (this.checked) {
				radioButtonsStatus = true;
			}
		});
		
		var agreeCheckStatus = false;
		if ($("#modal-registration input:checkbox").length) {
			if ($("#modal-registration input:checkbox").get(0).checked) {
				console.log($("#modal-registration input:checkbox"));
				agreeCheckStatus = true;
			}
		}

		var additionalFieldStatus = false;
		if (radioButtonsStatus && agreeCheckStatus) {
			additionalFieldStatus = true;
		}
		console.log(additionalFieldStatus);
		return additionalFieldStatus;
	},
	showThankYou: function() {
		this.setTitle("Thanks for Joining!");
		var params = {};
		params.ph = 2;
		this.setParams(params);
		this.populate();
	},
	redisplayFormWithMessage: function(message) {
        var params = {};
		params.ph = 1;
		params.message = message;
		this.setParams(params);
		this.populate();
	},
	checkResult: function(pass, msg, goToLogin) {
		console.log(pass, msg);
		if (pass == true) {
			this.showThankYou();
        } else if (goToLogin == true) {
			$(this.getModalDiv()).dialog('close');
			$(document).trigger("login.open", [null, msg]);
        } else {
			this.redisplayFormWithMessage(msg);
		}
	},
    submitFormData: function() {
    	this._super();
        var self = this;
		// pull data out of serialized array
		var firstName = this.getIndividualFormData('FirstName');
		var lastName = this.getIndividualFormData('LastName');
		var nickName = this.getIndividualFormData('ScreenName');
		var dogName = this.getIndividualFormData('DogsName');
		var emailAddress = this.getIndividualFormData('EmailAddress');
		var password = this.getIndividualFormData("Password");
		var password2 = this.getIndividualFormData("PasswordConfirm");
		var emailOptIn = this.getIndividualFormData('EmailCommunication');
		// send data to server
        WagWorld.Web.AjaxUtility.RegisterUser(firstName, lastName, nickName, dogName, emailAddress, password, password2, emailOptIn, function(result) {
            console.log("Result: ", result.value);
            var pass = result.value.Pass;
            var reason = result.value.Reason;
			var goToLogin = false;
			if (result.value.AuxiliaryNumber == 1) {
				goToLogin = true;
			}
			self.checkResult(pass, reason, goToLogin);
        });
    },
	dialogCloseListener: function() {
		var self = this;
		$(this.getModalDiv()).bind('dialogclose', function(event, ui) {
			console.info(self.getModalTitle(), " now closing");
			if (self.getTitle() == "Thanks for Joining!") {
				location.reload();
			} else {
				self.revertToInitialState();
			}
		});
	},
	close: function() {
		console.log("closing");
		$(this.getModalDiv()).dialog('close');
	}
});



