Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. HA.user = (function(w,d,$,sb,api,pub){
  2.  
  3. var events = { };
  4.  
  5. var jsonModel = typeof w.jsonCtrl == 'function' ? jsonCtrl().model : { };
  6.  
  7. var init = function() {
  8. api.retrieveKnownModelValuesFromJSONModel();
  9. sb.bind('loginForm',HA.models.loginForm);
  10. sb.bind('forgotPasswordForm',HA.models.loginForm);
  11. sb.bind('registrationForm',HA.models.registrationForm);
  12. sb.bind('addressForm',HA.models.addressForm);
  13. sb.bind('billingForm',HA.models.billingForm);
  14. sb.bind('settings',{ customerSupportNum: jsonCtrl().model.conciergeSupportNumber });
  15. };
  16.  
  17. $(d).ready(init);
  18.  
  19. var isValid = function(key,loadable) {
  20. var isValid = HA.models.validate(key);
  21. if (!isValid) {
  22. sb.bind(key, HA.models[key]);
  23. loadable.setState('initial validation');
  24. return false;
  25. }
  26. return true;
  27. };
  28.  
  29. events.loginUser = function(evt,loginForm) {
  30. evt.preventDefault();
  31. var loadable = HA.ui.loadable('premiumLogin');
  32. if (!isValid('loginForm',loadable)) return false;
  33. loadable.setState('loading');
  34. api.loginUser(loginForm,function(err,resp){
  35. if(err) {
  36. loadable.setState('initial error');
  37. } else {
  38. jsonModel.entityId = resp.USERID;
  39. HA.models.addressForm.entityId = resp.USERID;
  40. page.redirect(HA.config.routes.address.path + '?id=' + HA.status.selectedPlan.id);
  41. }
  42. });
  43. };
  44.  
  45. events.forgotPasswordSubmit = function(evt,forgotPasswordForm) {
  46. evt.preventDefault();
  47. var loadable = HA.ui.loadable('premiumForgotPassword');
  48. if(!isValid('forgotPasswordForm',loadable)) return false;
  49. loadable.setState('loading');
  50. api.forgotPassword(forgotPasswordForm,function(err,resp){
  51. if(err) {
  52. loadable.setState('initial error');
  53. } else {
  54. HA.models.loginForm.username = forgotPasswordForm.email;
  55. sb.bind('loginForm',HA.models.loginForm);
  56. page(HA.config.routes.passwordSent.path);
  57. }
  58. });
  59. };
  60.  
  61. events.activateAccount = function(evt) {
  62. evt.preventDefault();
  63. var loadable = HA.ui.loadable('premiumActivate');
  64. if(typeof jsonCtrl().model.consumer.email == 'undefined') return loadable.setState('initial error');
  65. loadable.setState('loading');
  66. api.forgotPassword({email: jsonCtrl().model.consumer.email},function(err,resp){
  67. if(err) {
  68. loadable.setState('initial error');
  69. } else {
  70. page.redirect(HA.config.routes.passwordSent.path + '?id=' + simpleBind.getState().boundObjects.selectedPlan.id);
  71. }
  72. });
  73. };
  74.  
  75. events.registerConsumer = function(evt,registrationForm) {
  76. evt.preventDefault();
  77. var loadable = HA.ui.loadable('premiumRegister');
  78. if (!isValid('registrationForm',loadable)) return false;
  79. loadable.setState('loading');
  80. api.createNewUser(registrationForm,function(err,resp){
  81. if(err) {
  82. if(err.statusId == 409) {
  83. HA.models.loginForm.username = registrationForm.username;
  84. sb.bind('loginForm',HA.models.loginForm);
  85. page(HA.config.routes.login.path + '?id=' + HA.status.selectedPlan.id);
  86. } else {
  87. loadable.setState('initial error');
  88. }
  89. } else {
  90. jsonModel.entityId = resp.entityId;
  91. jsonModel.consumer.phone = registrationForm.homePhone;
  92. jsonModel.consumer.address.zip = registrationForm.primaryZip;
  93. jsonModel.consumer.address.city = resp.primaryCity;
  94. jsonModel.consumer.address.state = resp.primaryState;
  95. api.retrieveKnownModelValuesFromJSONModel();
  96. HA.models.addressForm.entityId = resp.entityId;
  97. sb.bind('addressForm',HA.models.addressForm);
  98. page(HA.config.routes.address.path + '?id=' + HA.status.selectedPlan.id);
  99. api.loginUser({username: registrationForm.username, password: registrationForm.password});
  100. }
  101. });
  102. };
  103.  
  104. events.collectAddress = function(evt,addressForm) {
  105. evt.preventDefault();
  106. var loadable = HA.ui.loadable('premiumAddress')
  107. if (!isValid('addressForm',loadable)) return false;
  108. loadable.setState('loading');
  109. api.setUserAddress(addressForm,function(err,resp){
  110. if(err) {
  111. loadable.setState('initial error');
  112. } else {
  113. jsonModel.consumer.address.id = resp.addressId;
  114. page(HA.config.routes.billing.path + '?id=' + HA.status.selectedPlan.id);
  115. }
  116. });
  117. };
  118.  
  119. events.collectBilling = function(evt,billingForm) {
  120. evt.preventDefault();
  121. var loadable = HA.ui.loadable('premiumBilling');
  122. if (!isValid('billingForm',loadable)) {
  123. HA.models.billingForm.forceShowAddr = true;
  124. isValid('billingForm',loadable); // recall this to show any address-related billing validation
  125. return false;
  126. }
  127. loadable.setState('loading');
  128. SM.util.Payment.submitWithToken = function(data) {
  129. api.setBillingInfo({
  130. accountToken: data.token,
  131. duplicateToken: data.statusCode == 409,
  132. accountNumberLastFour: billingForm.cardNum.substring(billingForm.cardNum.length-4),
  133. securityCode: billingForm.secCode,
  134. creditCardType: SM.util.Payment.determineCardType(billingForm.cardNum),
  135. expirationMonth: billingForm.expMonth,
  136. expirationYear: billingForm.expYear,
  137. cardholderName: billingForm.cardholderName,
  138. billingAddress1: billingForm.address1,
  139. billingAddress2: billingForm.address2,
  140. billingCity: billingForm.city,
  141. billingState: billingForm.state,
  142. billingZip: billingForm.zip,
  143. catalogItemId: 18,
  144. feePlanId: 160,
  145. consumerId: jsonModel.entityId
  146. },function(err,resp){
  147. if(err) {
  148. s_sm.fireSimpleEvent('event175','HA Premium: Account Created');
  149. loadable.setState('initial error');
  150. } else {
  151. s_sm.fireSimpleEvent('event174','HA Premium: Account Created');
  152. w.location.href = '/myhomeadvisor/premium';
  153. }
  154. })
  155. };
  156. SM.util.Payment.profileTokenBadRequest = function() { // = SM.util.Payment.profileTokenServerFail = SM.util.Payment.requiresRefresh = SM.util.Payment.requiresSecure = function() {
  157. loadable.setState('initial error');
  158. s_sm.fireSimpleEvent('event175','HA Premium: Account Created');
  159. };
  160. // get the token, response will be fed to submitWithToken callback
  161. SM.util.Payment.requestPaymentToken(
  162. jsonModel.tokenURL,
  163. jsonModel.merchantKey,
  164. billingForm.cardNum,
  165. billingForm.expMonth,
  166. billingForm.expYear
  167. );
  168. };
  169.  
  170. events.billingAddrToggle = function(evt,billingForm) {
  171. if(this.checked) {
  172. billingForm.address1 = HA.models.addressForm.address1;
  173. billingForm.address2 = HA.models.addressForm.address2;
  174. billingForm.city = HA.models.addressForm.city;
  175. billingForm.state = HA.models.addressForm.state;
  176. billingForm.zip = HA.models.addressForm.zip;
  177. } else {
  178. billingForm.address1 = '';
  179. billingForm.address2 = '';
  180. billingForm.city = '';
  181. }
  182. sb.bind('billingForm',billingForm);
  183. };
  184.  
  185. sb.registerEvent('loginConsumer',events.loginUser);
  186. sb.registerEvent('forgotPasswordSubmit',events.forgotPasswordSubmit);
  187. sb.registerEvent('activateAccount',events.activateAccount);
  188. sb.registerEvent('registerConsumer',events.registerConsumer);
  189. sb.registerEvent('collectAddress',events.collectAddress);
  190. sb.registerEvent('collectBilling',events.collectBilling);
  191. sb.registerEvent('billingAddrToggle',events.billingAddrToggle);
  192.  
  193. pub.getBillingInfo = function(ctx,next) {
  194. if(jsonModel.entityId) {
  195. HA.models.billingForm.cardholderName = HA.models.registrationForm.firstName + ' ' + HA.models.registrationForm.lastName;
  196. HA.models.billingForm.billingAddrToggle = true;
  197. sb.bind('billingForm',HA.models.billingForm);
  198. pub.triggerEvent('billingAddrToggle', d.getElementById('billingAddrToggle'), HA.models.billingForm);
  199. next();
  200. } else {
  201. page.redirect(HA.config.routes.login.path + '?id=' + sb.getState().boundObjects.selectedPlan.id);
  202. }
  203. };
  204.  
  205. /**
  206. * param1: eventName
  207. * param2: element on which to trigger event, 'this' context
  208. * param3: object to be passed to eventHandler
  209. *
  210. * example usage:
  211. *
  212. * pub.triggerEvent('billingAddrToggle', d.getElementById('billingAddrToggle'), HA.models.billingForm);
  213. *
  214. */
  215. pub.triggerEvent = function() {
  216. var args = Array.prototype.slice.call(arguments)
  217. , eventName = args.shift()
  218. , el = args.shift();
  219. args.unshift(null);
  220. if(typeof events[eventName] == 'function') {
  221. events[eventName].apply(el,args);
  222. }
  223. };
  224.  
  225. return pub;
  226. })(window,document,HA.dom,simpleBind,HA.services,new EmitterFactory(HA.user));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement