Subversion Repositories SmartDukaan

Rev

Rev 20253 | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function(){
        $('#frmRegister').submit(function(){
                trackEventWithGA('Account', 'New User Register', $('#frmRegister #email').val());
                trackPageWithGA("/register"); // For registration goal.
        });
        
        $('#frmLogin').validate({
                loginEmail: {
                        required: true,
                        email: true
                },
                loginPassword: {
                        required: true,
                        minlength: 6,
                        maxlength: 20
                }
        });
        
        $("#frmRegister").validate({
                rules: {
                        email: {
                                required: true,
                                email: true
                        },
                        txtPassword: {
                                required: true,
                                minlength: 6,
                                maxlength: 20
                        }
                }
        });
        
        $('.loginregister').live('click', function(){
                if($('input[name=loginregister]:checked').val() == "register") {
                        $('#signin').hide();
                        $('#signup').show();
                } else {
                        $('#signin').show();
                        $('#signup').hide();
                }
        });
        
});

/*function loginToSaholicByFacebook(userDetails, authResponse){
        var longLivedToken = null;
        
        if(authResponse!==null) {
                $.ajax({
                        type    : "GET",
                        url             : "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id="+
                                           '${in.shop2020.appId}' + "&client_secret="+'${in.shop2020.appSecret}'+
                                          "&fb_exchange_token="+authResponse.accessToken,
        
                        success : function(response) {
                                longLivedToken = response.substring(0,response.indexOf("&expires")).split("=")[1];
                        },
                        
                        complete: function() {
                                $.ajax({
                                        type : "POST",
                                        url : "/login/?email="+userDetails.email+"&name="+userDetails.name+"&gender="+userDetails.gender+"&isFacebookUser=True&accessToken="+longLivedToken+"&facebookId="+authResponse.userID,
                                        success : function(response) {
                                                window.location = "/";
                                        }
                                });
                        }
                        
                });
        } else {
                $.ajax({
                        type : "POST",
                        url : "/login/?email="+userDetails.email+"&name="+userDetails.name+"&gender="+userDetails.gender+"&isFacebookUser=True",
                        success : function(response) {
                                window.location = "/";
                        }
                });
        }
}*/


var LoginNamespace={};
var authDetails=null;
var userDetails=null;
var sourceShip=false;
$(document).ready(function() {
          $.ajaxSetup({ cache: true });
          $.getScript('//connect.facebook.net/en_US/sdk.js', function(){
            FB.init({
              appId: '{'+'${in.shop2020.appId}'+'}',
              version: 'v2.7' // or v2.0, v2.1, v2.2, v2.3
            });     
            $('#loginbutton,#feedbutton').removeAttr('disabled');

          });

        (function(d, s, id) {
          var js, fjs = d.getElementsByTagName(s)[0];
          if (d.getElementById(id)) return;
          js = d.createElement(s); js.id = id;
          js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7&appId="+'${in.shop2020.appId}';
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
        });

var mode = '';
  function checkAndCallFacebookLogin() {
          mode = $('#loginText').html();
         
          $('#errormessage').hide();
          $('#errormessage').html('');
                //window.fbAsyncInit();
            FB.getLoginStatus(function(response) {
                  // Check login status on load, and if the user is
                  // already logged in, go directly to the welcome message.
                authDetails = response;
                  if (authDetails.status == 'connected') {
                          
                        getCurrentUserInfo();
                  } else {
                    // Otherwise, show Login dialog first.
                    FB.login(function(response) {
                        authDetails=response;
                        getCurrentUserInfo();
                    }, {scope: 'user_friends, email'});
                  }
                });
  }
  
  function loginregister(src){
          mode = $('#loginText').html();
          $('#errormessage').hide();
          $('#errormessage').html('');
          var email;
          var password;
          if(src&&src!=undefined){
                  email = $('#lemail').val();;
                  password = $('#lpassword').val();
          }else{
                  email = $('#email').val();;
                  password = $('#password').val();
          }
          if(mode =='Login'){
                 try{ trackEventWithGA('Account', 'Login from '+window.location.pathname, $('#email').val());}catch(e){}
                  $.ajax({
                                type : "POST",
                                url : "/login?email="+email+"&password="+password,
                                success : function(response) {
                                        response = JSON.parse(response);
                                        if(response.status=='success'){
                                                if(sourceShip)
                                                        window.location.href='/shipping';
                                                else
                                                        window.location.reload();
                                        }else{
                                                $('#errormessage').html(response.message);
                                        $('#errormessage').show();
                                        }
                                        
                                }
                        });
          }else{
                  try{trackEventWithGA('Account', 'Register from '+window.location.pathname, $('#email').val());}catch(e){}
                  $.ajax({
                                type : "POST",
                                url : "/register?redirectUrl=/&email="+email+"&password="+password,
                                success : function(response) {
                                        response = JSON.parse(response);
                                        if(response.status=='success'){
                                                if(sourceShip)
                                                        window.location.href='/shipping';
                                                else
                                                        window.location.reload();
                                        }else{
                                                $('#errormessage').html(response.message);
                                        $('#errormessage').show();
                                        }
                                }
                        });
          }
  }
  
        $('#poplogin').click(function(){
                loginsignup('ship');
        });
  
  
  
 
        function getCurrentUserInfo() {
              FB.api('/me?fields=id,name,email,permissions', function(userInfo) {
                  userDetails = userInfo;
                  if(authDetails && authDetails.status=='connected' && userDetails && userDetails.email){
                                if(mode=='Login'){
                                  $.ajax({
                                        type : "POST",
                                        url : "/login!verifyFbUser?accessToken="+authDetails.authResponse.accessToken+"&email="+userDetails.email,
                                        success : function(response) {
                                                response = JSON.parse(response);
                                                if(response.status=='success'){
                                                        if(sourceShip)
                                                                window.location.href='/shipping';
                                                        else
                                                                window.location.reload();
                                                        
                                                }else{
                                                        $('#errormessage').html(response.message);
                                                        $('#errormessage').show();
                                                }
                                        }
                                });
                                }else{
                                        $.ajax({
                                                type : "POST",
                                                url : "/login!signupFacebookUser?accessToken="+authDetails.authResponse.accessToken+"&email="+userDetails.email,
                                                success : function(response) {
                                                        response = JSON.parse(response);
                                                        if(response.status=='success'){
                                                                if(sourceShip)
                                                                        window.location.href='/shipping';
                                                                else
                                                                        window.location.reload();
                                                        }else{
                                                                $('#errormessage').html(response.message);
                                                                $('#errormessage').show();
                                                        }
                                                }
                                                
                                        });
                                }
                                
                                
                  }else{
                          //retry
                          //login unsuccessful
                  }
              });
            }
  

        
                function openpopup(){
                        $('#register-login-popup').show();
                        toggleLoginRegisterView('login');
                }

                function closepopup(){
                $('#register-login-popup').hide();
                toggleLoginRegisterView('login');
                }

                function toggleLoginRegisterView(view){
                        $('#errormessage').hide();
                        $('#email').html('');
                        $('#password').html('');
                        $('#errormessage').html('');
                        
                if(view=='register'){
                $('#registerlogin').html('REGISTER');
                $('#loginText').html('Register');
                $('#fbloginbtn').html('Register with Facebook');
                $('#loginregistertext').html('Already a member? <a href="javascript: void(0);" style="color:#069" onclick="toggleLoginRegisterView(\'login\')">Login</a></div>');
                $('#forgotPassword').hide();
                }else{
                $('#registerlogin').html('LOGIN');
                $('#loginText').html('Login');
                $('#fbloginbtn').html('Login with Facebook');
                $('#loginregistertext').html('Do not have an account?<a href="javascript: void(0);" style="color:#069" onclick="toggleLoginRegisterView(\'register\')">Register</a></div>');
                $('#forgotPassword').show();
                }
                }
        
        function loginsignup(source){
                if(source) 
                        sourceShip=true;
                else 
                        sourceShip = false;
                if($('#register-login-popup').length==0){
                        $('<html><style>#register-login-popup{width:100%;height:100%;position:fixed;top:0;left:0;background:rgba(102,102,102,.55)}.login-container{margin:0 auto;width:740px;z-index:10;height:auto;top:40px;position:relative;background:0 0}.login-inner-container{z-index:95;width:660px;min-height:400px;left:0;right:0;top:0;margin:auto;height:auto;background:#fff;position:relative}div.wrap{width:250px;margin-left:45px}div.wrap div{position:relative;margin:20px 0}.inactivelabel{position:absolute;top:0;font-size:16px;margin:21px 25px 25px -5px;color:grey;padding:0 10px;background-color:#fff;-webkit-transition:top .2s ease-in-out,font-size .2s ease-in-out;transition:top .2s ease-in-out,font-size .2s ease-in-out}.activelabel{top:-30px;font-size:20px;color:#069}div.wrap input{width:100%;margin:20px 20px 5px 5px;border:none;border-bottom:1px solid grey;font-size:16px;background-color:transparent;color:grey;position:relative;outline:0}div.wrap input:focus{border-color:#069;font-size:16px;-webkit-transition:border-color .15s ease-in-out 0s,box-shadow .15s ease-in-out 0s;transition:border-color .15s ease-in-out 0s,box-shadow .15s ease-in-out 0s}#loginBtn{cursor:pointer;border-radius:5px;width:250px;background-color:#069;padding:5 0;border:2px solid #069;text-align:center;margin-top:51px}#loginBtn:hover{border-color:grey;-webkit-transition:border-color .15s ease-in-out 0s,box-shadow .15s ease-in-out 0s;transition:border-color .15s ease-in-out 0s,box-shadow .15s ease-in-out 0s}#loginText{font-size:24px;color:#fff;margin:10px 0}</style><script src="http://code.jquery.com/jquery-2.1.1.min.js" ></script><div id="register-login-popup" style="display:none"> <div id="iframeContainer" class="login-container"> <div class="login-inner-container"> <div onclick="closepopup()" id="closepopup" style="background-color: #333232;color: #fff;font-size: 24px;padding: 8px;position: absolute;right: -33px;cursor: pointer;">X</div><div style="height:10px;width:100%;margin:0 auto;padding:5px"></div><div id="registerlogin" style="width:100%;margin:0 auto;text-align: center;color:#069;float:left;padding-bottom:5px;font-size:26px;"> LOGIN </div><div style="width:100%;margin:0 auto;text-align: center;float: left;margin-top: 2px;"> <div style="width: 50%;height:auto;background-color: white;float:left;"> <div id="loginCreate" class="register-login" > <div id="errormessage" style="width:100%;text-align:center;font-size:12px;color:red;display:none"> email/password is wrong</div><div class="wrap"> <div> <label class="inactivelabel" >Email</label> <input id="email" name="email" type="text" autocomplete = "off" class="cool" value=""/> </div><div> <label class="inactivelabel" >Password</label> <input id="password" name="password" autocomplete="off" type="password" class="cool" value=""/> </div><div id="forgotPassword" style="margin: 0px;margin-top: -19px;margin-right: -162px;font-size: 12px;font-style: italic;"> <a href="/forgot-password" style="color:#069">forgot password?</a></div><div class="loginBtn" id="loginBtn" onclick="loginregister()"> <div id="loginText" > Login </div></div><div id="loginregistertext" style="width:100%;text-align:center;font-size:14px;font-weight:bold;"> Do not have an account? <a href="javascript: void(0);" style="color:#069" onclick="toggleLoginRegisterView(\'register\')">Register</a></div></div></div></div><div style="width:1px;height:300px;background-color: white;float: left;border-left: 1px solid grey;margin-top: 7px;margin-left: 3px;"></div><div style="width: 48%;height:300px;background-color: white;float:right;text-align:center;"><div style="width: 100%;font-size: 23px;color: #069;margin-top: 17px;"> Or Try </div><div onclick="checkAndCallFacebookLogin()" style="text-align:right;background: url(\'/unversioned/images/facebookconnect.png\');cursor:pointer;background-repeat: no-repeat;height: 42px;background-size: 250px 43px;margin-top: 162px;border-radius: 5px;width: 250px;margin-left: 29px;"><div id="fbloginbtn" style="color:white;font-weight:bold;font-size: 16px;padding: 8px;padding-top: 12px;">Login with Facebook</div></div></div></div></div></div></div></div></html>').insertAfter('#footer');
                        $(document).ready(function(){
                                $('input').focusin( function() {
                                  $(this).parent().find('label').addClass('activelabel');
                                 
                                });

                                $('input').focusout( function() {
                                  if (!this.value) {
                                    $(this).parent().find('label').removeClass('activelabel');
                                  }
                                });
                                
                                });
                }
                openpopup();
        }
        
        $(document).ready(function(){
                $('input').focusin( function() {
                  $(this).parent().find('label').addClass('activelabel');
                 
                });

                $('input').focusout( function() {
                  if (!this.value) {
                    $(this).parent().find('label').removeClass('activelabel');
                  }
                });
                
                });