<commit_before>/*
    This file is part of agora-election.
    Copyright (C) 2014 <NAME> <edulix AT agoravoting DOT com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function(){

    var AE = this.AE = {}; // AE  means "Agora Election"
    var app = this.app = {};
    app.current_view = null;
    app.modal_dialog = null;

    var Checker = {};
    Checker.email = function(v) {
        var RFC822;
        RFC822 = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/;
        return RFC822.test(v);
    };

    Checker.tlf = function (tlf) {
        if (!tlf) {
            return null;
        }
        tlf = tlf.trim();
        tlf = tlf.replace(/ /g, '');
        tlf = tlf.replace(/^0034/, "+34");
        tlf = tlf.replace(/^34/, "+34");
        if (!/^\+34/.test(tlf)) {
            tlf = "+34" + tlf;
        }
        var regExp = new RegExp(app_data.tlf_no_rx);
        if (!regExp.test(tlf)) {
            return null;
        }
        return tlf;
    };

    Checker.dni = function(v) {
        var regex;
        regex = /^[0-9]{8}[A-Za-z]{1}$/;

        if(!regex.test(v)) {
            return false;
        }

        var dni = parseInt(v);

        var mod_letters = '<KEY>';
        var digits = v.substring(0, 8);
        var letter = v.substring(8, 9).toUpperCase();

        var expected = mod_letters.charAt(dni % 23);

        return letter == expected;
    };

    /**
     *  Main function, creates the router and starts the routing processing
     */
    var main = function() {
        app.modal_view = new AE.ModalDialogView();

        // process app_data
        var rx = /^(https?:\/\/[^/]+)\/[^/]+\/[^/]+\/election\//
        app_data.election.base_url = rx.exec(app_data.url)[1];

        // Initiate the router
        app.router = new AE.Router();

        // Start Backbone history a necessary step for bookmarkable URL's
        Backbone.history.start();
    };

    /**
     * Url router, launches the appropiate views depending on the current
     * #hashbang
     */
    AE.Router = Backbone.Router.extend({
        routes: {
            "": "home",
            "identify": "identify",
            "verify-sms": "verify_sms",
            "contact": "contact",
            'mail-sent': 'mail_sent',
            'verify-vote': 'security_center',
            'verify-tally': 'security_center',
            "page/:name": 'page',
            'results-table': 'results_table'

        },

        /**
         * removes the current view and initializes the base template if it's
         * the first time
         */
        removeCurrentView: function() {
            if(app.current_view) {
                app.current_view.remove();
                $("#main").prepend('<div id="render-view-canvas">');
            } else {
                var templ = _.template($("#template-base-view").html());
                $("#renderall").html(templ(app_data));
            }
        },

        home: function() {
            this.removeCurrentView();
            app.current_view = new AE.HomeView();
        },

        identify: function() {
            this.removeCurrentView();
            app.current_view = new AE.IdentifyView();
        },

        verify_sms: function() {
            this.removeCurrentView();
            app.current_view = new AE.VerifySMSView();
        },

        contact: function() {
            this.removeCurrentView();
            app.current_view = new AE.ContactView();
        },

        mail_sent: function() {
            this.removeCurrentView();
            app.current_view = new AE.MailSentView();
        },

        security_center: function() {
            this.removeCurrentView();
            if (!app.current_view ||
                app.current_view.class_name != "security-center")
            {
                app.current_view = new AE.SecurityCenterView();
            }
        },

        page: function(name) {
            this.removeCurrentView();
            app.current_view = new AE.PageView({name: name});
        },

        results_table: function() {
            this.removeCurrentView();
            app.current_view = new AE.ResultsTableView();
        }
    });

    AE.getCandidateCount = function(name, results) {
        for(var i = 0; i < results.length; i++) {
            if (results[i].value == name) {
                return results[i];
            }
        }
        console.log("name = " + name + ", not found");
        return null;
    };

    AE.getYoutubeEmbedUrl = function(urls) {
        var baseUrl = AE.findUrlByTitle(urls, "Youtube").url;
        var rx = /[^?]\?(.+\&)?v=([a-zA-Z0-9]+)(&.+)?/;
        if (!rx.test()) {
            return;
        }
        var urlCode = rx.exec(baseUrl)[1];
        return "//www.youtube.com/embed/" + urlCode + "?autoplay=1&referrer=" + app_data.parent_site.name;
    };

    /**
     * Home view - just renders the home page template with the app_data
     */
    AE.HomeView = Backbone.View.extend({
        el: "#render-view-canvas",

        events: {
            'click a.details': 'showDetails',
        },

        initialize: function() {
            this.template = _.template($("#template-home-view").html());
            if (app_data.election.tally_released_at_date == null) {
                this.tmplCandidates = _.template($("#template-candidates-list-view").html());
            } else if (app_data.election.questions[0].tally_type == "APPROVAL") {
                this.tmplCandidates = _.template($("#template-candidates-approval-double-results-view").html());
            } else if (app_data.election.questions[0].tally_type == "MEEK-STV") {
                this.tmplCandidates = _.template($("#template-candidates-stv-results-view").html());
            }
            this.tmplCandModalBody = _.template($("#template-candidate-modal-body").html());
            this.render();
        },

        render: function() {
            if (app_data.election.tally_released_at_date == null &&
                app_data.election.questions[0].randomize_answer_order)<commit_msg>allowing short youtube url videos<commit_after>/*
    This file is part of agora-election.
    Copyright (C) 2014 <NAME> <edulix AT agoravoting DOT com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function(){

    var AE = this.AE = {}; // AE  means "Agora Election"
    var app = this.app = {};
    app.current_view = null;
    app.modal_dialog = null;

    var Checker = {};
    Checker.email = function(v) {
        var RFC822;
        RFC822 = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/;
        return RFC822.test(v);
    };

    Checker.tlf = function (tlf) {
        if (!tlf) {
            return null;
        }
        tlf = tlf.trim();
        tlf = tlf.replace(/ /g, '');
        tlf = tlf.replace(/^0034/, "+34");
        tlf = tlf.replace(/^34/, "+34");
        if (!/^\+34/.test(tlf)) {
            tlf = "+34" + tlf;
        }
        var regExp = new RegExp(app_data.tlf_no_rx);
        if (!regExp.test(tlf)) {
            return null;
        }
        return tlf;
    };

    Checker.dni = function(v) {
        var regex;
        regex = /^[0-9]{8}[A-Za-z]{1}$/;

        if(!regex.test(v)) {
            return false;
        }

        var dni = parseInt(v);

        var mod_letters = 'TRWAGMYFPDXBNJZSQVHLCKE';
        var digits = v.substring(0, 8);
        var letter = v.substring(8, 9).toUpperCase();

        var expected = mod_letters.charAt(dni % 23);

        return letter == expected;
    };

    /**
     *  Main function, creates the router and starts the routing processing
     */
    var main = function() {
        app.modal_view = new AE.ModalDialogView();

        // process app_data
        var rx = /^(https?:\/\/[^/]+)\/[^/]+\/[^/]+\/election\//
        app_data.election.base_url = rx.exec(app_data.url)[1];

        // Initiate the router
        app.router = new AE.Router();

        // Start Backbone history a necessary step for bookmarkable URL's
        Backbone.history.start();
    };

    /**
     * Url router, launches the appropiate views depending on the current
     * #hashbang
     */
    AE.Router = Backbone.Router.extend({
        routes: {
            "": "home",
            "identify": "identify",
            "verify-sms": "verify_sms",
            "contact": "contact",
            'mail-sent': 'mail_sent',
            'verify-vote': 'security_center',
            'verify-tally': 'security_center',
            "page/:name": 'page',
            'results-table': 'results_table'

        },

        /**
         * removes the current view and initializes the base template if it's
         * the first time
         */
        removeCurrentView: function() {
            if(app.current_view) {
                app.current_view.remove();
                $("#main").prepend('<div id="render-view-canvas">');
            } else {
                var templ = _.template($("#template-base-view").html());
                $("#renderall").html(templ(app_data));
            }
        },

        home: function() {
            this.removeCurrentView();
            app.current_view = new AE.HomeView();
        },

        identify: function() {
            this.removeCurrentView();
            app.current_view = new AE.IdentifyView();
        },

        verify_sms: function() {
            this.removeCurrentView();
            app.current_view = new AE.VerifySMSView();
        },

        contact: function() {
            this.removeCurrentView();
            app.current_view = new AE.ContactView();
        },

        mail_sent: function() {
            this.removeCurrentView();
            app.current_view = new AE.MailSentView();
        },

        security_center: function() {
            this.removeCurrentView();
            if (!app.current_view ||
                app.current_view.class_name != "security-center")
            {
                app.current_view = new AE.SecurityCenterView();
            }
        },

        page: function(name) {
            this.removeCurrentView();
            app.current_view = new AE.PageView({name: name});
        },

        results_table: function() {
            this.removeCurrentView();
            app.current_view = new AE.ResultsTableView();
        }
    });

    AE.getCandidateCount = function(name, results) {
        for(var i = 0; i < results.length; i++) {
            if (results[i].value == name) {
                return results[i];
            }
        }
        console.log("name = " + name + ", not found");
        return null;
    };

    AE.getYoutubeEmbedUrl = function(urls) {
        var baseUrl = AE.findUrlByTitle(urls, "Youtube").url;
        var rx = /[^?]\?(.+\&)?v=([a-zA-Z0-9]+)(&.+)?/;
        if (baseUrl.indexof("youtu.be/") != 1) {
            var index = baseUrl.indexof("youtu.be/") + 9;
            var urlCode = baseUrl.substr(index);
        } else if (rx.test()) {
            var urlCode = rx.exec(baseUrl)[1];
        } else {
            return;
        }

        return "//www.youtube.com/embed/" + urlCode + "?autoplay=1&referrer=" + app_data.parent_site.name;
    };

    /**
     * Home view - just renders the home page template with the app_data
     */
    AE.HomeView = Backbone.View.extend({
        el: "#render-view-canvas",

        events: {
            'click a.details': 'showDetails',
        },

        initialize: function() {
            this.template = _.template($("#template-home-view").html());
            if (app_data.election.tally_released_at_date == null) {
                this.tmplCandidates = _.template($("#template-candidates-list-view").html());
            } else if (app_data.election.questions[0].tally_type == "APPROVAL") {
                this.tmplCandidates = _.template($("#template-candidates-approval-double-results-view").html());
            } else if (app_data.election.questions[0].tally_type == "MEEK-STV") {
                this.tmplCandidates = _.template($("#template-candidates-stv-results-view").html());
            }
            this.tmplCandModalBody = _.template($("#template-candidate-modal-body").html());
            this.render();
        },

        render: function() {
            if (app_data.election.tally_released_at_date == null &&
                app_data.election.questions[0].randomize_answer_order)
{'fraction_non_alphanumeric': 0.15831953642384106, 'fraction_numerical': 0.02511037527593819, 'mean_word_length': 1.8769597142290138, 'pattern_counts': {'":': 10, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2340768', 'n_tokens_mistral': 4986, 'n_tokens_neox': 4676, 'n_words': 901}