


var stsConsumer= new function(){}




function stsC_onLogin(){
    var userName = Ext.get('username');
    var password = Ext.get('password');
    var accountId = Ext.get('accountId');
    var phone = Ext.get('phone');
    
    if((userName.dom.value=='' || password.dom.value=='') && accountId.dom.value!='' && phone.dom.value!=''){
        //accountID login
         this.wait=Ext.Msg.wait('Processing Login...',"Please Wait");
        Ext.Ajax.request({
            url:"/findMembership"
            ,params:{phone:phone.dom.value,accountId:accountId.dom.value}
            ,method:'GET'
            ,scope:this
            ,success:function(res){
                
                var status = res.responseText;
                if(status=='USER_NOT_REGISTERED'){
                    this.wait.hide();
                    new stsC_registerBox();
                }
                if (status=='USER_NOT_FOUND'){
                    this.wait.hide();
                    stsC_invalidLogin();
                }
                
                //user found and already registered. Cookie should already be set so just reload the page.
                if (status=='USER_IS_REGISTERED' || status == 'EMAIL_NOT_VALIDATED' || status=='USER_IS_LEGACY'){
                    this.wait.updateText("Login Successful! Loading...");
                    window.location='/';
                }
            }
            ,failure:function(res){
                this.wait.hide();
                Ext.Msg.alert('Error',"An error ocurred while trying to log you in! This usually only happens if you try to put somthing other than just numbers as your account ID or phone number. Please check your login credentials and try again.");
            }
        })
        
      //  window.console.log('accountID login');
    } else if (userName.dom.value!='' && password.dom.value!='') {
        this.wait=Ext.Msg.wait('Processing Login...',"Please Wait");
        Ext.Ajax.request({
            url:"/loginAjax"
            ,scope:this
            ,params:{username:userName.dom.value,password:password.dom.value}
            ,method:'POST'
            ,success:function(res){
                    this.wait.updateText("Login Successful! Loading...");
                    window.location='/';
            }
            ,failure:function(){
                    this.wait.hide();
                Ext.Msg.alert('Incorrect Login','The username and password you have entered do not match any registered users.<br>Please try again.<br>If you continue to have this problem, you may also try to log in with your account ID and phone number instead.');
            }
        })
    } else {
        Ext.Msg.alert('Incorrect Login','You must enter either both email address and password, or account ID and phone number to login. Please try agian.');
        //not all fields are entered
        //window.console.log('login incomplete');
    }
    
    return false;
}

function stsC_invalidLogin(){
    Ext.Msg.alert("Invalid Login","The login you entered does not appear to be valid. Please try again.");
}


function stsC_registerBox(){

    this.accountId = Ext.get('accountId').dom.value;
    this.phone = Ext.get('phone').dom.value;
    this.window=new Ext.Window({
        items:[{
           // form as the only item in window
            xtype:'form'
           ,labelWidth:60
           ,frame:true
           
           ,items:[
                {
                    xtype:'label'
                   ,html:"Enter your email address and set a password<br>to make logging in easier next time!<br><br>"
                }
                ,{
                   // textfield
                    fieldLabel:'Email'
                    ,id:'setEmail'
                   ,xtype:'textfield'
                   //,anchor:'-18'
                }
                ,{
                   fieldLabel:'Password'
                   ,id:'setPassword'
                   ,xtype:'textfield'
                   
                }
                
            ]
        }]
        ,buttons:[{
                    xtype:'button'
                    ,text:"SAVE</span><img  style='margin-left:3px;margin-right:-3px;' w  src='/images/arrow-red.gif'>"
                    ,scope:this
                    ,listeners:{
                        click: function(){
                            this.wait=Ext.Msg.wait('Registering...',"Please Wait");
                             Ext.Ajax.request({
                                url:"/services/registerUser"
                                ,params:{email:Ext.get('setEmail').dom.value,password:Ext.get('setPassword').dom.value}
                                ,method:'POST'
                                ,scope:this
                                ,success:function(res){
                                    this.wait.updateText("Registration Successful! Logging in...");
                                    window.location='/';
                                }
                                ,failure:function(res){
                                    this.wait.hide();
                                    Ext.Msg.alert('Error',res.responseText,function(){window.location='/';});
                                }
                            })
                        }
                    }
                }]
        ,listeners:{
            close:function(){
                Ext.Msg.wait("Loading...","Please Wait");
                window.location='/';
            }
        }
        ,width: 250
        ,labelWidth:60
        
        ,modal:true
        ,resizeable:false
        ,hideBorders:true
            
        });
    this.window.show();
}




function loginHelp(){
	var body="<div class='whyCantILogIn' style='padding:5px 5px 5px 5px;'>If you are having trouble logging in, please check that you have done the following:<ul style='margin-left:7px;list-style:circle inside none;'>"+
		"<li>Make sure you have already <a href='/registration'>registered</a> in the site.</li>"+
		"<li>Make sure you have verified your email address by clicking on the link in your welcome email after registering.</li>"+
		"<li>If you didn't receive an email, you can resend your welcome email through the  <a href='/registration'>registration center</a>.</li>"+
		"<li>You can also try resetting your password in the <a href='/registration'>registration center</a>.</li>"+
		"</ul></div>";
	stsConsumer.loginHelpWin=	new Ext.Window({
		 title:"Why can't I log in?"
		,html:	body
		,width:"700"
		,buttons:[
			{	 text:"Close"
				,scope:stsConsumer
				,handler:function(){stsConsumer.loginHelpWin.close()}
			}
		]
	});
		stsConsumer.loginHelpWin.show();
}

function stsC_showRegistrationIstructions(){
    var accountId = Ext.get('accountId');
    var phone = Ext.get('phone');
    accountId.highlight();
    phone.highlight();
    
    Ext.Msg.alert("Registration no longer required","Good news! You don't need to register anymore to access your benefits!<br>Just login with your account ID and password in the box on the right!",function(){
            var accountId = Ext.get('accountId');
            var phone = Ext.get('phone');
            accountId.highlight();
            phone.highlight();
            accountId.focus();
            //accountId.dom.focus();
        
        });
}
