~cjsmo/laudio/laudio

« back to all changes in this revision

Viewing changes to laudio/tpl/javascript/main.js

  • Committer: Bernhard Posselt
  • Date: 2012-07-21 21:46:45 UTC
  • Revision ID: git-v1:65b8b97f72df4a9245bd564e3344b3e897472143
django 1.4 changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Laudio - A webbased musicplayer
3
 
 *
4
 
 * Copyright (C) 2010 Bernhard Posselt, bernhard.posselt@gmx.at
5
 
 *
6
 
 * Laudio is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 3 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * Laudio is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
 *
20
 
 */
21
 
 
22
 
 
23
 
/**
24
 
 * Enable jquery for csrf protection
25
 
 */
26
 
$(document).ajaxSend(function(event, xhr, settings) {
27
 
    function getCookie(name) {
28
 
        var cookieValue = null;
29
 
        if (document.cookie && document.cookie != '') {
30
 
            var cookies = document.cookie.split(';');
31
 
            for (var i = 0; i < cookies.length; i++) {
32
 
                var cookie = jQuery.trim(cookies[i]);
33
 
                // Does this cookie string begin with the name we want?
34
 
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
35
 
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
36
 
                    break;
37
 
                }
38
 
            }
39
 
        }
40
 
        return cookieValue;
41
 
    }
42
 
    function sameOrigin(url) {
43
 
        // url could be relative or scheme relative or absolute
44
 
        var host = document.location.host; // host + port
45
 
        var protocol = document.location.protocol;
46
 
        var sr_origin = '//' + host;
47
 
        var origin = protocol + sr_origin;
48
 
        // Allow absolute or scheme relative URLs to same origin
49
 
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
50
 
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
51
 
            // or any other URL that isn't scheme relative or absolute i.e relative.
52
 
            !(/^(\/\/|http:|https:).*/.test(url));
53
 
    }
54
 
    function safeMethod(method) {
55
 
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
56
 
    }
57
 
 
58
 
    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
59
 
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
60
 
    }
61
 
});
62
 
 
63
 
// js actions which are available in the whole applicatin
64
 
$(document).ready(function () {
65
 
    
66
 
    // password check
67
 
    $('#id_password1').keyup(function(){
68
 
        validate_password($(this), $('#id_password2'))
69
 
    });
70
 
    $('#id_password2').keyup(function(){
71
 
        validate_password($(this), $('#id_password1'))
72
 
    });
73
 
 
74
 
});
75
 
 
76
 
 
77
 
/**
78
 
 * Function which adds or removes the correct classes on 2 fields
79
 
 *
80
 
 * @param $input1: The first jquery input element
81
 
 * @param $input2: The second jquery input element
82
 
 */
83
 
function validate_password($input1, $input2){
84
 
    if($input1.val() === $input2.val()){
85
 
        $input1.removeClass('incorrect');
86
 
        $input2.removeClass('incorrect');
87
 
        $input1.addClass('correct');
88
 
        $input2.addClass('correct');
89
 
    } else {
90
 
        $input1.removeClass('correct');
91
 
        $input2.removeClass('correct');
92
 
        $input1.addClass('incorrect');
93
 
        $input2.addClass('incorrect');
94
 
    }
95
 
}