/*
 * Zipwhip Web 2.0 Messaging Product 1.0.
 * Copyright(c) 2008-2011, Zipwhip.
 * 
 * 
 * This product is not currently available for public release. Only Zipwhip employees and authorized Carriers, and their customers are permitted to access this product during the authorized beta.
 */


Zw.namespace('Zw.sprint.features.login');Zw.sprint.features.login.SprintWelcomeWindow=Ext.extend(Zw.controls.Window,{cls:'zw-window zw-window-welcome sprint',title:'Sign On',maximizable:false,code:'window',width:610,height:374,resizable:false,securityMode:false,layout:'fit',REGULAR_TEXT:'<div class="header">Get Texting</div> To begin texting you must first sign on with your Sprint login and password.',SECURITY_TEXT:'<div>Thanks for using the Sprint Web Texter</div><br>To log back in please visit www.sprint.com',actionButton:new Zw.controls.ActionButton({iconCls:'zw-button-icon-login',style:'margin-top: 10px;',text:'Sign On To My Sprint',code:'button',handler:function(sender,e){if(!this.window)
this.window=this.findParentBy(function(f){return f.code=='window'});this.window.onSignOn.call(this.window.scope||this.window,this,e);}}),setSecurityMode:function(value){},getItems:function(){return[{xtype:'div',defaultType:'div',items:[{cls:'left side',html:'Welcome to the Sprint Web Texter! This new texting utility has some powerful features.  Get ready to experience the next generation of texting!<ul><li>Send texts to all major U.S. mobile carriers</li><li>Send and receive text messages <strong>(2-way)</strong></li><li>Smart Forwarding to your mobile</li></ul>'},{cls:'right side',code:'rightside',items:[{xtype:'div',html:this.securityMode?this.SECURITY_TEXT:this.REGULAR_TEXT},{xtype:'div',cls:'signInWrap',items:[this.actionButton]},{xtype:'div',html:'Having trouble Logging in?  Click Here.',cls:'loginTroubleLink',code:'loginTrouble',scope:this,handler:this.openSecureLoginPage}]}]}];},openSecureLoginPage:function(sender,e){document.location="https://sprint.zipwhip.com";},openRegistrationWizard:function(sender,e){if(this.context){Zw.console('this.context.publish -- ',this.context);this.context.publish('/desktop/user/register');}else{Zw.Application.Context.publish('/desktop/user/register');}}});Zw.sprint.features.login.SprintIFrameWindow=Ext.extend(Zw.controls.Window,{cls:'zw-window',title:'Sign On via Sprint: loading...',DEV_url:'http://dlsa0499.dev.sprint.com:20780/sso/redirect.do?deeplink=zipwhip',LIVE_url:'https://sso.sprintpcs.com/sso/redirect.do?deeplink=zipwhip',maximizable:false,code:'window',width:700,height:450,resizable:true,layout:'fit',getItems:function(){this.url=this.LIVE_url;this.title="Sign On via Sprint: "+this.url;return[{id:'iframe',xtype:'iframepanel',layout:'fit',defaultSrc:this.url,defaultType:'div'}];},onIFramePathChanged:function(sender){}});

Zw.namespace('Zw.sprint.features.login');Zw.sprint.features.login.SprintSSOFeature=Ext.extend(Zw.Feature,{monitorGuestState:true,onInit:function(){this.SECURITY_TIMEOUT=this.ONE_HOUR;},publishSecurityMessage:function(){this.context.publish('/desktop/error/show',{title:Zw.localize('sprint.window.title.sso'),msg:Zw.localize('sprint.window.login.message'),scope:this,onClose:this.onAlertClose,handler:this.bringWindowToFront});},clearSecurityCredential:function(){var cp=window.cookieProvider;cp.clear('timeout');Ext.destroy(cp);cp=null;Zw.inSecurityMode=false;Zw.securityTimeout=null;},getSecurityCredential:function(){Zw.console("Discovering security situation");if(Zw.inSecurityMode){Zw.console("We are in a security lockout. It is over at ",Zw.securityTimeout);var attemptDate=new Date();if((attemptDate>Zw.securityTimeout)){this.clearSecurityCredential();return false;}}
Zw.console("We didn't find any security information, lets look in the cookie");var cp=new Ext.state.CookieProvider({path:"/",expires:new Date(new Date().getTime()+(this.SECURITY_TIMEOUT)),domain:"zipwhip.com"});var value=cp.get('timeout');Zw.securityTimeout=cp.get('securityTimeout');Zw.console("We checked the cookie and found",Zw.securityTimeout);Ext.destroy(cp);cp=null;var result=(Zw.securityTimeout>new Date());Zw.console("We are in securityMode: ",result);return result;},onStart:function(){Zw.Application.Context.on('login',this.onLogin,this);},onLogin:function(){this.showWelcomeScreen();},onAuthenticatedAsGuest:function(){Zw.inSecurityMode=this.getSecurityCredential();if(!Zw.capabilities.ALLOW_GUEST){this.onLogin();}},onAuthenticatedAsUser:function(){if(this.window){if(this.iframe)
this.iframe.destroy();this.window.destroy();delete this.iframe;delete this.window;}
this._checkWebTexterPlusEnrollment();},_checkWebTexterPlusEnrollment:function(){if(Zw.portal=='sprint'||Zw.portal=='webtexter'){this.context.publish('/net/ajax/request',{url:'/sprint/texterplus/enrolled',session:true,success:function(sender,result){if(result.response){window.location.reload();}},failure:Ext.emptyFn,scope:this});}},bringWindowToFront:function(sender){sender.toFront();},onAlertClose:function(){this.preventWindow=false;this.onLogin();},showWelcomeScreen:function(){if(!this.window){this.window=this.context.Desktop.registerWindow(new Zw.sprint.features.login.SprintWelcomeWindow({closable:false,scope:this,onSignOn:this.onSignOn}));}
this.window.show();this.window.setSecurityMode(Zw.inSecurityMode);},showIFrame:function(){this.iframe=this.context.Desktop.registerWindow(new Zw.sprint.features.login.SprintIFrameWindow({}));this.addHandlers();this.iframe.show();},onIntegratedLogin:function(sender,uuid){if(sender==this.context){this.iframe.close();this.removeHandlers();}},onSignOn:function(sender,e){if(e.ctrlKey){this.showIFrame();this.window.close();}else{window.location='https://mysprint.sprint.com/mysprint/pages/sl/global/login.jsp?targetPage=http://zipwhip.sprint.com/sprint/sso/v2/bridge';}},removeHandlers:function(){Zw.Application.un('integrated-login',this.onIntegratedLogin,this);},addHandlers:function(){Zw.Application.on('integrated-login',this.onIntegratedLogin,this);},onDestroy:function(){Ext.destroy(this.iframe);delete this.iframe;Ext.destroy(this.window);delete this.window;this.removeHandlers();}});Zw.features.Factory.replace('Zw.features.login.LoginFeature',Zw.sprint.features.login.SprintSSOFeature);

Zw.namespace('Zw.sprint.features.login');Zw.sprint.features.login.SprintLoginFeature=Ext.extend(Zw.features.login.LoginFeature,{m_package:'login.sprint',m_loginWindow:'Zw.sprint.features.login.SprintLoginWindow',m_extraWindowArgs:{xtype:'Zw.sprint.features.login.SprintLoginWindow',closable:Zw.capabilities.ALLOW_GUEST},getWindowArgs:function(){return({xtype:'Zw.sprint.features.login.SprintLoginWindow',onToolbarClick:this.login_onToolbarClick,scope:this,onSubmit:this.login_onSubmit.createDelegate(this)});},login_onSubmit:function(args){this.carrierLogin(args);},onUserDetails:function(){Zw.sprint.features.login.SprintLoginFeature.superclass.onUserDetails.call(this);var isGuest=this.context.Account.isGuest(true);if(!isGuest){if(Zw.portal=='sprint'||Zw.portal=='webtexter'){this.context.publish('/net/ajax/request',{url:'/sprint/texterplus/enrolled',session:true,success:function(sender,result){if(result.response){window.location.reload();}},failure:Ext.emptyFn,scope:this});}}},login_onSuccess:function(sender,args){var jsonResult=Ext.util.JSON.decode(sender.responseText);var serverResponse=jsonResult.response;if(serverResponse){if(this.window)
this.window.close();var cookieProvider=this.getCookieProvider();cookieProvider.set('sessions',[serverResponse.key]);Zw.Application.createContext({account:{session:serverResponse.key,validated:true},active:true});}},carrierLogin:function(args){if(args){var username=args.mobileNumber;var rememberMe=args.remember;Ext.Ajax.request({url:'/sprint/carrier_login',params:{userId:username,password:args.password,rememberMe:rememberMe,portal:Zw.portal},scope:this,success:this.onSuccess,failure:this.onFailure});}},onFailure:function(response){var msg=response||Zw.localize('generic.alert.tryAgain');Ext.Msg.alert(Zw.localize('generic.alert.networkFailure.title'),msg);},onSuccess:function(response){var jsonResult=Ext.util.JSON.decode(response.responseText);var serverResponse=jsonResult.response;if(serverResponse){if(jsonResult.success){var cookieProvider=this.getCookieProvider();cookieProvider.set('sessions',serverResponse.key);Zw.Application.createContext({account:{session:serverResponse.key,validated:true},active:true});}
else{var msg="";var ec=serverResponse.errorCode;if(ec){if(ec=='-130'){msg=Zw.localize('generic.alert.loginFailed.text1');}
else if(ec=='-131'){msg=Zw.localize('generic.alert.subscriberNotFound');}
else if(ec=='-132'){msg=Zw.localize('generic.alert.creatingAccountFailed');}
else{msg=Zw.localize('generic.alert.retryLogin');}}
Ext.Msg.alert(Zw.localize('generic.alert.loginFailed.title'),msg);if(this.window){this.window.clearValue('pw');}
Zw.console("Login Failed. \n Error: ",serverResponse.errorCode," Message: ",serverResponse.message);}}
else{Ext.Msg.alert(Zw.localize('generic.alert.loginFailed.text3'));}},getCookieProvider:function(){var cookieProvider=new Ext.state.CookieProvider({path:'/',domain:Zw.cookieDomain||'zipwhip.com'});return cookieProvider;}});if(Ext.isSecure){Zw.features.Factory.replace('Zw.sprint.features.login.SprintSSOFeature','Zw.sprint.features.login.SprintLoginFeature');}

Zw.features.app.SprintOmnitureTagsFeature=Ext.extend(Zw.Feature,{onInit:function(){var c=this.context;c.subscribe('/desktop/corkboard/show',this.onCorkboard,this);c.subscribe('/desktop/contacts/show',this.onContacts,this);c.subscribe('/account/message/send',this.onOutgoingMessage,this);c.subscribe('/account/message/receive',this.onIncomingMessage,this);if(Analytics.MetricData)
Analytics.MetricData.zwSessionId=this.getSessionKey();},reportException:function(method,er){try{this.context.publish('/app/error',{name:'Sprint Omniture '+method,data:'',err:er});}catch(err){}},onContacts:function(){try{var key='contactsWindowOpenHandler';if(Analytics.DigLouZW){this.callMethod(Analytics.DigLouZW,key);}}catch(er){this.reportException(key,er);}},onOutgoingMessage:function(){try{var key='sendButtonHandler';if(Analytics.DigLouZW){this.callMethod(Analytics.DigLouZW,key);}}catch(er){this.reportException(key,er);}},onIncomingMessage:function(){try{var key='receivedWindowHandler';if(Analytics.DigLouZW){this.callMethod(Analytics.DigLouZW,key);}}catch(er){this.reportException(key,er);}},onCorkboard:function(){try{var key='corkboardButtonHandler';if(Analytics.DigLouZW){this.callMethod(Analytics.DigLouZW,key);}}catch(er){this.reportException(key,er);}},callMethod:function(scope,method){if(scope){method=scope[method];if(method){method.call(scope,[this.getSessionKey()]);}}},getSessionKey:function(){return this.context.Account.getSessionKey();}});if(Zw.portal=='Sprint'){Zw.features.Factory.register('Zw.features.app.SprintOmnitureTagsFeature');}

Zw.namespace('Zw.sprint.features.feedback');Zw.sprint.features.feedback.SprintSurveyFeature=Ext.extend(Zw.Feature,{surveyWindow:undefined,onInit:function(){var context=this.context;if(context){context.subscribe('/account/message/sendComplete',this.askForSurvey,this);}},askForSurvey:function(){var context=this.context;if(context){var websiteDeviceId=Zw.data.get(this.context.Account.identity,'websiteDeviceId');Ext.Ajax.request({url:'/SprintSurvey/survey/query',params:{deviceId:websiteDeviceId},success:this.askForSurveyComplete,scope:this});}},askForSurveyComplete:function(response){var result=Ext.util.JSON.decode(response.responseText);if(result.showSurvey){this.showSurveyWindow(result.surveyUrl);}
if(result.stopRequesting){var context=this.context;if(context){context.unsubscribe('/account/message/sendComplete',this.askForSurvey,this);}}},showSurveyWindow:function(baseUrl){var context=this.context;if(context){var surveyWindow=this.buildSurveyWindow(baseUrl);context.subscribe('/desktop/survey/complete',this.onSurveyComplete,this);surveyWindow.show();this.surveyWindow=surveyWindow;}},onSurveyComplete:function(){this.surveyWindow.close();var context=this.context;if(context){var websiteDeviceId=Zw.data.get(this.context.Account.identity,'websiteDeviceId');Ext.Ajax.request({url:'/SprintSurvey/survey/complete',params:{deviceId:websiteDeviceId}});}},buildSurveyWindow:function(baseUrl){var identity=this.context.Account.identity;var extRef=Zw.data.get(identity,'mobileNumber');var userName=(Zw.data.get(identity,'firstName')+' '+Zw.data.get(identity,'lastName')).replace(' ','_');var url=baseUrl;if(baseUrl.indexOf('?')===-1){url+='?';}else{url+='&';}
url+='ext_ref='+extRef+'&user_name='+userName;return new Zw.controls.Window({title:Zw.localize('sprint.window.title.survey'),renderTo:Ext.getBody(),cls:'zw-window zw-window-sprintSurvey',width:467,height:460,layout:'fit',items:{xtype:'iframepanel',defaultSrc:url,border:false}});},onDestroy:function(){var context=this.context;if(context){context.unsubscribe('/account/message/sendComplete',this.askForSurvey,this);}}});Zw.features.Factory.register('Zw.sprint.features.feedback.SprintSurveyFeature');

Zw.namespace("Zw.sprint.features.registration");Zw.sprint.features.registration.SprintRegistrationFeature=Ext.extend(Zw.features.registration.RegistrationFeature,{packages:['wizard.all','registration.all','registration.sprint'],createWizardController:function(){return new Zw.features.registration.ZipwhipRegistrationWizardControllerPlugin({plugins:['Zw.features.registration.EnterMobilePlugin','Zw.sprint.features.registration.SprintCarrierLookupPlugin','Zw.features.registration.SendVerificationCodePlugin','Zw.features.registration.EnterVerificationCodePlugin','Zw.features.registration.PersonalDetailsPlugin','Zw.features.registration.PickPhonePlugin','Zw.features.registration.GuessPhonePlugin','Zw.features.registration.MissingPhonePlugin','Zw.features.registration.GoodbyeForNowPlugin','Zw.sprint.features.registration.SprintTermsOfServicePlugin','Zw.features.registration.CompletePlugin'],context:this.context,modal:!Zw.capabilities.ALLOW_GUEST});}});Zw.features.Factory.replace('Zw.features.registration.RegistrationFeature',Zw.sprint.features.registration.SprintRegistrationFeature);

