summaryrefslogtreecommitdiff
path: root/static/js/home.js
blob: 910de023302187ff823c4ab640d795e24699ed6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function ifEnter(fn) {
    return function(e) {
        if (e.keyCode == 13) { fn(); }
    };
}

function initHome() {
	
	var defaults = {
		'#nickInput':      "username",
		"#passwordInput":  "not-a-real-password"
	}
	
	_.map(defaults, function(value, id){
	  
	  // set default values for the login form only if empty
	  if ($(id).val() == "") $(id).val(value);
	  
	  $(id).focus(function(){
	    if($(id).val() == value) $(id).val("")
	    else $(id).select()
	  })

	  $(id).blur(function(){
	    if($(id).val() == "") $(id).val(value);
	  })
	  
	})
	
    $('#passwordInput').keyup(ifEnter(login));
    $('#loginSubmit').click(login);
}

function login() {
    $('#passwordInput, #loginSubmit').blur();
    var nick = $('#nickInput').val();
    var password = $('#passwordInput').val();
    var hash = hex_sha1(nick + '$' + password + '$dumpfm');

    var onSuccess = function(json) {
        location.href = "/chat";
    };

    var onError = function(resp, textStatus, errorThrown) {
        alert("Error logging in!");
    };

    $.ajax({
        type: 'GET',
        timeout: 5000,
        url: 'login',
        data: {'nick': nick, ts: '', 'hash': hash },
        cache: false,
        dataType: 'json',
        success: onSuccess,
        error: onError
    });
};