Source

Mobile POS Front End / web / loginTemplate.html


<!DOCTYPE html>
<html>

    <head>


        <meta charset="utf-8">
        <title id="systemName">Mobile POS</title>

        <!-- Le styles -->
        <link href="css/bootstrap.css" rel="stylesheet">
        <style type="text/css">

        </style>
    </head>

    <body>
        <div class="container">
            <img id ="loginBanner" src=""/>
        </div>
        <br><br>
        <div class="container">
            <div class="span6 offset2">
                <form name="loginForm" class="form-horizontal"> 
                    <div class="control-group">
                        <label class="control-label" for="userId">User Name</label>
                        <div class="controls" id="lolol">
                            <input id="userId" type="text" name="userId" placeholder="User Name"  class="input-xlarge">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="userPassword">Password</label>
                        <div class="controls">
                            <input id="password" name="userPassword" type="password" placeholder="Password"  class="input-xlarge">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="userBranch">Branch</label>
                        <div class="controls">
                            <select id="listForBranchDetails" class="span6">

                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="controls">
                            <label class="checkbox">
                                <input type="checkbox" id="rememberCheckBox"> Remember me
                            </label>
                            <button id="loginBtn" type="button" class="btn btn-large btn-block btn-primary">Login</button>
                            <p id="errorMessage" class="text-error"> </p>
                            <div class="progress progress-striped active" id="progressBarDiv" style="display:none">
                                <div class="bar" id="progressBar" style="width: 40%; display:none"></div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>  
        <!-- jQuery via Google + local fallback, see h5bp.com -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

        <!-- Bootstrap JS: compiled and minified -->
        <script src="js/bootstrap.min.js"></script>
        <script src="js/loginTemplate.js"></script>

        <script>
   
            $(document).ready(function(){
                document.title = pageTitleName;
                var d = document.getElementById("loginBanner")
                d.setAttribute("src",bannerURL); 
                
                
                if(readCookie("rememberLogin")=="true")
                {
                    window.location = "indexTemplate.html";
                }
                
                loadBranchDetails();
                
            });
            
            function loadBranchDetails() {
                
                var divInPlace =  document.getElementById("listForBranchDetails");
                
                $.getJSON("http://lowe.jschua.com/DinoPos/api/branches/", function (data) {
                
                    // On success, 'data' contains a list of branch.
                    $.each(data, function (key, val) {

                        // Format the text to display.
                        var branchOption = document.createElement("option");
                        branchOption.innerHTML = val.Name;
                        branchOption.id = val.Branch_ID;
                        branchOption.className = val.Tax_Percentage;
                        divInPlace.appendChild(branchOption);

                        console.log(val.Name);
                    
                    });
                })
                .fail(
                function (jqXHR, textStatus, err) {
                    $('#allbranch').text('Error: ' + err);
                });
            }
           
            $('#loginBtn').click(function() {
                console.log("came login!!");
                
                numberOfLoop=0;
		
                if(validateField())
                {
                    checkValidUserAndRedirect();
                    tid = setInterval(showProgress, 750);
                }
            });
            
            function validateField()
            {
                //get the email and password
                var userEmail = document.loginForm.userId.value;
                var userPassword = document.loginForm.userPassword.value;
                
                if(userEmail.length==0)
                {
                    alert("Please fill in your userEmail");
                    return false;
                }
                if(userPassword.length==0)
                {
                    alert("Please key in your password");
                    return false;
                }
                
                return true;
            }
            
            //global variable for the progress bars.
            var numberOfLoop = 0;
            var tid;
        
            function showProgress()
            {
                console.log("came into show progress");
                numberOfLoop++;
                if(numberOfLoop ==6)
                {    
                    clearTimeout(tid);
                    return;
                }
                var percentage = Number(numberOfLoop*20);
                $('#progressBarDiv').attr('style', "");
                $('#progressBar').attr('style',"width:"+percentage+"%");
                
              
            }
            
          
            
            function checkValidUserAndRedirect()
            {
                
                var userName = document.loginForm.userId.value;
                var userPassword = document.loginForm.userPassword.value;
                
                console.log("username =="+userName);
                console.log("userPassword=="+userPassword);
                
                
                var userInfo = { "username": userName, "password": userPassword };
                $.ajax({
                    type: "POST",
                    url: "http://lowe.jschua.com/DinoPos/api/users",
                    async: true,
                    data: JSON.stringify(userInfo),
                    contentType: "application/json; charset=utf-8",
                    statusCode: {
                        202: function () {
                            //$('#result').text("202 (Accepted)");

                        },
                        406: function () {
                            //$('#result').text("406 (Not Acceptable)");
                            $('#errorMessage').text("Invalid login or password. Please try again.");
                            clearTimeout(tid);
                            $('#progressBar').attr('style',"width:100%");
                          
                        }
                    },
                    success: function (data) {
                        
                        var x = document.getElementById("listForBranchDetails").selectedIndex;
                        var branchId = document.getElementsByTagName("option")[x].id;
                        var taxPercentage = document.getElementsByTagName("option")[x].className;
                        
                        var daysForCookie = 0;
                        
                        if($('input[id=rememberCheckBox]').is(':checked'))
                        {
                            daysForCookie = 3;
                            createCookie('rememberLogin','true',3);
                           
                        }
                        
                        createCookie('employeeId',data,daysForCookie);
                        createCookie('branchId',branchId,daysForCookie);
                        createCookie('taxPercentage',taxPercentage,daysForCookie);
                        
                        window.location = "indexTemplate.html";
                    },
                    dataType: "text",
                    error: function (jqXHR, textStatus, errorThrown) {
                        // $('#result').text("The following error occured: " + textStatus, errorThrown);
                    }
                });
               
            }
            
            function createCookie(name,value,days) {
                if (days) {
                    var date = new Date();
                    date.setTime(date.getTime()+(days*24*60*60*1000));
                    var expires = "; expires="+date.toGMTString();
                }
                else var expires = "";
                document.cookie = name+"="+value+expires+"; path=/";
            }
            
            function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                    var c = ca[i];
                    while (c.charAt(0)==' ') c = c.substring(1,c.length);
                    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
                }
                return null;
            }

            
        </script>

    </body>

</html>