var pollOldOnload = window.onload;

window.onload = function()
{
    if (typeof pollOldOnload == 'function')
    {
         pollOldOnload();
    }

    window.pollInstance = new poll();
    pollInstance.init();
}

poll = function ()
{
    this.form = null;
    this.container = null;
}


poll.prototype.init = function()
{

    this.form = document.getElementById('pollForm');
    if (!this.form)
    {
        return;
    }
    this.container = document.getElementById('pollBox');
    if (!this.container)
    {
        return;
    }

    var script = this;

    this.form.onsubmit = function()
    {
        script.submitPoll(this);
        return false;
    }
}

poll.prototype.submitPoll = function(form)
{
    var optionChecked = false;
    var inputs = form.getElementsByTagName('input');

    for (var i=0; i<inputs.length; i++)
    {
        var input = inputs[i];
        if (
            (input.type == 'radio')
            &&
            (input.checked)
        )
        {
            optionChecked = true;
        }
    }
    if (!optionChecked)
    {
        return;
    }


    Blocks.submitFormAsBlock(form, 'results', this.processPollResponse, this);
}

poll.prototype.processPollResponse = function( xmlhttp, script )
{
    script.container.innerHTML =  xmlhttp.responseText;
}
