
/***********************************************
 *                                             *
 *  Copyright © BW Systems 2008.               *
 *                                             *
 *  Az oldal fejlesztoi joga a BW Systems Bt   *
 *  tulajdona. A szoftver felhasznalasa csak   *
 *  a BW Systems Bt. irasos engedelye birto-   *
 *  kaban, vagy a BW Systems Bt-vel kotott     *
 *  szerzodes alapjan lehetseges.              *
 *                                             *
 *  A szoftver engedely nelkuli felhasznalasa, *
 *  masolasa, modositasa, terjesztese vagy at- *
 *  adasa polgari es buntetojogi kovetkezme-   *
 *  nyekkel jarhat!                            *
 *                                             *
 *  BW Systems Bt. 2011 Budakalasz,            *
 *  Vasut sor 42.                              *
 *                                             *
 *  Cg.13-06-050782                            *
 *                                             *
 ***********************************************/

function parseVote(xmldata) {
    var opts = new Array();
    var maxvote = 0;
    var surveyid = xmldata.getElementsByTagName('votes')[0].getAttribute('surveyid');
    var target = getElement('survey_' + surveyid);
    var container = target.parentNode;
    
    var options = xmldata.getElementsByTagName('vote');

    for (var i = 0; i < options.length; i++) {
        var o = new Array();
        o['id'] = parseInt(options[i].getAttribute('id'));
        o['votes'] = parseInt(options[i].getAttribute('votes'));
        o['text'] = options[i].firstChild.nodeValue;
        if (o['votes'] > maxvote) maxvote = o['votes'];
        opts.push(o);
    }

    var cnt = document.createElement('div');
    cnt.id = 'survey_res_' + surveyid;

    var tbl = document.createElement('div');
    tbl.setAttribute('class','tbl');
    tbl.setAttribute('className','tbl');
    tbl.style.marginBottom = '10px';

    for (var i = 0 ; i < opts.length; i++) {
        var w = Math.round(125 * parseFloat(opts[i].votes) / maxvote);

        var trow1 = document.createElement('div');
        trow1.setAttribute('class','trow');
        trow1.setAttribute('className','trow');
        
        var tcell1 = document.createElement('div');
        tcell1.setAttribute('class','tcell');
        tcell1.setAttribute('className','tcell');
        
        var tcell1in = document.createElement('div');
        tcell1in.style.padding = '2px 5px';
        tcell1in.innerHTML = opts[i]['text'] + ' (' + opts[i]['votes']+ ' db)';

        var trow2 = document.createElement('div');
        trow2.setAttribute('class','trow');
        trow2.setAttribute('className','trow');

        var tcell2 = document.createElement('div');
        tcell2.setAttribute('class','tcell');
        tcell2.setAttribute('className','tcell');
        
        var tcell2in = document.createElement('div');
        tcell2in.setAttribute('class','optionbar');
        tcell2in.setAttribute('className','optionbar');
        tcell2in.style.width = w + 'px';
        tcell2in.innerHTML = '&nbsp;';

        tcell1.appendChild(tcell1in);
        trow1.appendChild(tcell1);
        tcell2.appendChild(tcell2in);
        trow2.appendChild(tcell2);

        tbl.appendChild(trow1);
        tbl.appendChild(trow2);
    }
    var cent1 = document.createElement('div');
    cent1.setAttribute('class','centered1');
    cent1.setAttribute('className','centered1');

    var anch1 = document.createElement('a');
    anch1.setAttribute('href','/modules/Surveys');
    anch1.innerHTML = 'További szavazások';

    cent1.appendChild(anch1);
    cnt.appendChild(tbl);
    cnt.appendChild(cent1);

    if (target) container.replaceChild(cnt,target);
    else container.appendChild(cnt);
}

function vote(src) {
    var voted = false;
    var surveyid = src.id.replace('vote_','');
    var qs = 'survey=' + surveyid;
    var options = document.getElementsByName('option_' + surveyid);
    for (var i = 0; i < options.length; i++) if (options[i].checked) {
        qs += '&vote=' + options[i].id.replace('option_' + surveyid + '_','');
        voted = true;
    }

    if (voted) {
        src.disabled = true;
        var req = new BWS_XMLRequest();
        req.setUrl('/xml/AddVote');
        req.setMethod('POST');
        req.setContentType('application/x-www-form-urlencoded');
        req.setQueryString(qs);
        req.setProcessResponse(parseVote);
        req.go();
    }
}

