var VM=VM||{};VM.BusinessRegistration={storageKey:"VM_CreateBusiness",$form:null,$email:null,$fName:null,$lName:null,$phone:null,$emailArea:null,hasAccount:!1,$hasAccount:null,$noAccount:null,$loggedIn:null,isLoggedIn:!1,hasRegistered:!1,init:function(){this.$form=$("#onboarding-form").on("submit",this.onValidate);this.$email=$("#Email").on("blur",this.onEmailBlur);this.$fName=$("#FirstName");this.$lName=$("#LastName");this.$phone=$("#Phone");this.$emailArea=$("#onboarding-emailarea");this.$hasAccount=$("#onboarding-hasaccount");this.$noAccount=$("#onboarding-noaccount");this.$loggedIn=$("#onboarding-loggedin");this.$email[0].readOnly&&(VM.Login.fillEmail(this.$email.val()).prop("readonly",!0),VM.Register.fillEmail(this.$email.val()).prop("readonly",!0),$("#Register_FirstName").val(this.$fName.val()),$("#Register_LastName").val(this.$lName.val()),$("#Register_Username").val(this.$fName.val()+" "+this.$lName.val()));this.addReturnUrl();VM.Me.getUser(function(n){VM.BusinessRegistration.fillValuesFromStorage();n.id!==0&&(VM.BusinessRegistration.hasAccount=!0,VM.BusinessRegistration.isLoggedIn=!0);VM.BusinessRegistration.$email.val()==""||VM.BusinessRegistration.$emailArea.hasClass("hidden")||VM.BusinessRegistration.onEmailBlur.call(VM.BusinessRegistration.$email)});VM.AccountModal.$modal.on("hide.bs.modal",this.onAccountModalClosed)},fillValuesFromStorage:function(){try{var n=JSON.parse(localStorage.getItem(this.storageKey));this.fillValueFromStorage(n,"name",$("#BusinessName"));this.fillValueFromStorage(n,"category",$("#BusinessCategory"));this.isLoggedIn||(this.$email.prop("readonly")||this.fillValueFromStorage(n,"email",this.$email),this.fillValueFromStorage(n,"fName",this.$fName),this.fillValueFromStorage(n,"lName",this.$lName),this.fillValueFromStorage(n,"phone",this.$phone))}catch(t){}},fillValueFromStorage:function(n,t,i){if(i.length){var r=n[t];r&&r!==""&&i.val(r)}},onValidate:function(n){n.preventDefault();VM.BusinessRegistration.$form.validate();VM.BusinessRegistration.$form.valid()&&VM.BusinessRegistration.submit()},submit:function(){if(VM.Login.fillEmail(VM.BusinessRegistration.$email.val()),VM.Register.fillEmail(VM.BusinessRegistration.$email.val()),$("#Register_FirstName").val(VM.BusinessRegistration.$fName.val()),$("#Register_LastName").val(VM.BusinessRegistration.$lName.val()),VM.BusinessRegistration.hasAccount&&!VM.BusinessRegistration.isLoggedIn)return VM.AccountModal.setView(1),VM.AccountModal.$modal.modal("show"),!1;if(VM.BusinessRegistration.hasAccount||VM.BusinessRegistration.isLoggedIn)localStorage.removeItem(VM.BusinessRegistration.storageKey),VM.BusinessRegistration.$form.off("submit").submit();else return VM.AccountModal.setView(0),VM.AccountModal.$modal.modal("show"),!1},addReturnUrl:function(){var n=$("#hd-account-unlogged");if(n.length){var t="?returnUrl="+encodeURIComponent(window.location.pathname+window.location.search),i=n.find("a[href='/account/login']"),r=n.find("a[href='/account/register']");i.attr("href",i.attr("href")+t);r.attr("href",r.attr("href")+t)}},onEmailBlur:function(){var n,t;(VM.BusinessRegistration.$hasAccount.addClass("hidden"),VM.BusinessRegistration.$noAccount.addClass("hidden"),n=$(this),n.hasClass("input-validation-error"))||n.valid()&&(t={__RequestVerificationToken:VM.BusinessRegistration.$form.find("input[name = '__RequestVerificationToken']").val()},t[n.data("remote-param")]=n.val(),$.ajax({dataType:"json",method:"POST",data:t,url:n.data("remote")}).done(function(n){VM.BusinessRegistration.hasAccount=!n;VM.BusinessRegistration.$hasAccount.toggleClass("hidden",n);VM.BusinessRegistration.$noAccount.toggleClass("hidden",!n)}).fail(function(){}))},onAccountModalOpened:function(){VM.Login.fillEmail(VM.BusinessRegistration.$email.val())},onAccountModalClosed:function(){VM.BusinessRegistration.hasRegistered&&VM.BusinessRegistration.$form.submit()},onLoginSuccess:function(n){VM.BusinessRegistration.afterLoginOrRegistration(n);VM.Ajax.resetValidation(VM.BusinessRegistration.$form)},onRegisteredSuccess:function(n){VM.BusinessRegistration.afterLoginOrRegistration(n);VM.BusinessRegistration.hasRegistered=!0},afterLoginOrRegistration:function(n){this.$hasAccount.addClass("hidden");this.$noAccount.addClass("hidden");this.$loggedIn.removeClass("hidden");this.$loggedIn.find("b").html(n.username);this.$fName.val()==""&&this.$fName.val(n.firstName);this.$lName.val()==""&&this.$lName.val(n.lastName);this.$phone.val()==""&&this.$phone.val(n.phone);this.isLoggedIn=!0},onRegisteredPartialSuccess:function(n){var t=VM.BusinessRegistration,i=t.$form.data("after-partial-reg-endpoint"),r;if(i)$.ajax({method:"POST",url:i,data:{userId:n.user.id,user:n.userid,isOwner:$("input[name='Owner']").val()}}).done(function(){}).fail(function(){console.error("There was a problem with partial registration")});else{var u=$("#BusinessName").val(),f=$("#BusinessCategory").val(),e={name:u,category:f,email:t.$email.val(),fName:t.$fName.val(),lName:t.$lName.val(),phone:t.$phone.val()};window.localStorage.setItem(t.storageKey,JSON.stringify(e))}r=$("#onboarding-partialregistered").removeClass("hidden");VM.BusinessRegistration.$form.addClass("hidden");VM.scrollTo(r)}};$(function(){VM.BusinessRegistration.init()})