module.exports = {
    poll: function(target, room, user) {
        if (!this.can('broadcast', null, room)) return;
        if (Poll[room.id].question) return this.sendReply('There is currently a poll going on already.');
        if (!this.canTalk()) return;

        var options = Poll.splint(target);
        if (options.length < 3) return this.sendReply('/poll [question], [option 1], [option 2]... - Create a poll where users can vote on an option.');

        var question = options.shift();

        options = options.join(',').toLowerCase().split(',');

        Poll[room.id].question = question;
        Poll[room.id].optionList = options;

        var pollOptions = '';
        var start = 0;
        while (start < Poll[room.id].optionList.length) {
            pollOptions += '<button name="send" value="/vote ' + Tools.escapeHTML(Poll[room.id].optionList[start]) + '">' + Tools.escapeHTML(Poll[room.id].optionList[start]) + '</button>&nbsp;';
            start++;
        }
        Poll[room.id].display = '<h2>' + Tools.escapeHTML(Poll[room.id].question) + '&nbsp;&nbsp;<font size="1" color="#AAAAAA">/vote OPTION</font><br><font size="1" color="#AAAAAA">Poll started by <em>' + user.name + '</em></font><br><hr>&nbsp;&nbsp;&nbsp;&nbsp;' + pollOptions;
        room.add('|raw|<div class="infobox">' + Poll[room.id].display + '</div>');
    },   

    vote: function(target, room, user) {
        if (!Poll[room.id].question) return this.sendReply('There is no poll currently going on in this room.');
        if (!this.canTalk()) return;
        if (!target) return this.sendReply('/vote [option] - Vote for an option in the poll.');
        if (Poll[room.id].optionList.indexOf(target.toLowerCase()) === -1) return this.sendReply('\'' + target + '\' is not an option for the current poll.');

        var ips = JSON.stringify(user.ips);
        Poll[room.id].options[ips] = target.toLowerCase();

        return this.sendReply('You are now voting for ' + target + '.');
    },

    pr: 'pollremind',
    pollremind: function(target, room, user) {
        if (!Poll[room.id].question) return this.sendReply('There is no poll currently going on in this room.');
        if (!this.canBroadcast()) return;
        this.sendReplyBox(Poll[room.id].display);
    },

    votes: function(target, room, user) {
        if (!this.canBroadcast()) return;
        if (!Poll[room.id].question) return this.sendReply('There is no poll currently going on in this room.');
        this.sendReply('NUMBER OF VOTES: ' + Object.keys(Poll[room.id].options).length);
    },

    endpoll: function(target, room, user) {
        if (!this.can('broadcast', null, room)) return;
        if (!Poll[room.id].question) return this.sendReply('There is no poll to end in this room.');

        var votes = Object.keys(Poll[room.id].options).length;

        if (votes === 0) {
            Poll.reset(room.id);
            return room.add('|raw|<h3>The poll was canceled because of lack of voters.</h3>');
        }

        var options = {};

        for (var l in Poll[room.id].optionList) {
            options[Poll[room.id].optionList[l]] = 0;
        }

        for (var o in Poll[room.id].options) {
            options[Poll[room.id].options[o]]++;
        }

        var data = [];
        for (var i in options) {
            data.push([i, options[i]]);
        }
        data.sort(function(a, b) {
            return a[1] - b[1];
        });

        var results = '';
        var len = data.length;
        var topOption = data[len - 1][0];
        while (len--) {
            if (data[len][1] > 0) {
                results += '&bull; ' + data[len][0] + ' - ' + Math.floor(data[len][1] / votes * 100) + '% (' + data[len][1] + ')<br>';
            }
        }
        room.add('|raw|<div class="infobox"><h2>Results to "' + Poll[room.id].question + '"</h2><font size="1" color="#AAAAAA"><strong>Poll ended by <em>' + user.name + '</em></font><br><hr>' + results + '</strong></div>');
        Poll.reset(room.id);
        Poll[room.id].topOption = topOption;
    }
};
@@ -101,32 +101,81 @@
  room)) return;%0A
+        if (!Poll%5Broom.id%5D) Poll.reset(room.id);%0A
         if (Poll
@@ -260,24 +260,24 @@
 already.');%0A
-
         if (
@@ -1435,32 +1435,81 @@
 , room, user) %7B%0A
+        if (!Poll%5Broom.id%5D) Poll.reset(room.id);%0A
         if (!Pol
@@ -2144,32 +2144,81 @@
 , room, user) %7B%0A
+        if (!Poll%5Broom.id%5D) Poll.reset(room.id);%0A
         if (!Pol
@@ -2490,32 +2490,81 @@
 cast()) return;%0A
+        if (!Poll%5Broom.id%5D) Poll.reset(room.id);%0A
         if (!Pol
@@ -2793,32 +2793,32 @@
 , room, user) %7B%0A
-
         if (!thi
@@ -2849,32 +2849,81 @@
  room)) return;%0A
+        if (!Poll%5Broom.id%5D) Poll.reset(room.id);%0A
         if (!Pol

Reset poll for newly created rooms