﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
function focusInput(me, id, myClass) {
    jQuery('.' + myClass + '[type=text]').val('');
    jQuery('.' + myClass + '[type=text]').attr('disabled', 'disabled');
    jQuery('.' + myClass + '[type=text]').addClass('nonActiveInput')
    jQuery(me).siblings().removeClass('nonActiveInput');
    jQuery('#' + id).removeAttr('disabled');
    jQuery('#' + id).removeClass('nonActiveInput');
    jQuery('#' + id).addClass('activeInput');
}

function focusInputRadio(inputsClass) {
    jQuery('.' + inputsClass + '[type=text]').val('');
    jQuery('.' + inputsClass + '[type=text]').attr('disabled', 'disabled');
    jQuery('.' + inputsClass + '[type=text]').removeClass('activeInput');
    jQuery('.' + inputsClass + '[type=text]').addClass('nonActiveInput');
}

function vote(id_sondy, nazwa_sondy, tytul, url, userName, typ) {
    var i = 1;
    var array = new Array();
    var j = 0;

    jQuery('.' + nazwa_sondy + '_sondaOdpTr td').each(function() {
        if (i % 2 == 0) {
            var element = jQuery.trim(jQuery(this).html());
            var ex = document.createElement('span');
            ex.innerHTML = element;
            var fake = ex.childNodes[0].getAttribute('id').substring(0, 4);
            if (fake == 'fake') {
                var x = jQuery('.' + nazwa_sondy + '_sondaOdpTr td').eq(i - 2);
                var el = x.children('input');
                array[j] = el.attr('type') + "~" + el.attr('id').substring(4, el.attr('id').length) + ":" + el.val();
            }
            else {
                var type = ex.childNodes[0].getAttribute('type');
                if (type == 'text') {
                    array[j] = ex.childNodes[0].getAttribute('type') + "~" + ex.childNodes[0].getAttribute('id') + ":" + jQuery('#' + ex.childNodes[0].getAttribute('id')).val();
                }
                else {
                    if (jQuery('#' + ex.childNodes[0].getAttribute('id')).attr('checked') == true) {
                        array[j] = ex.childNodes[0].getAttribute('type') + "~" + ex.childNodes[0].getAttribute('id') + ':true';
                    }
                    else {
                        array[j] = ex.childNodes[0].getAttribute('type') + "~" + ex.childNodes[0].getAttribute('id') + ':false';
                    }
                }
            }
            j = parseInt(j) + parseInt(1);
        }
        i = parseInt(i) + parseInt(1);
    });

    for (var i = 0; i < array.length; i++) {
        if (i > 0 && i < array.length - 1) {
            array[i] = "||" + array[i] + "||";
        }
        else {
            if (i == 0) {
                array[i] = array[i] + "||";
            }
            if (i == array.length - 1) {
                array[i] = "||" + array[i];
            }
        }
    }
    
    return array;
}

function goVoting(id_sondy, nazwa_sondy, tytul, url, userName, typ, timer) {
    var tablica = vote(id_sondy, nazwa_sondy, tytul, url, userName, typ);
    jQuery('.passingContainer').show();
    var parametry = { glosy: tablica, id: id_sondy, uzytkownik: userName };
    jQuery.ajax({
        type: "POST",
        url: "/2009/Vote.aspx",
        data: jQuery.param(parametry),
        success: function(dane) {
            changeStyles(nazwa_sondy);
            if (dane == 'OK') {
                //w zależności od typu - jeśli sonda - wyświetlane wyniki
                //jeśli ankieta lub test

                if (typ == 'sonda') {
                    getPage(id_sondy, url, nazwa_sondy, tytul);
                }
                else if (typ == 'test') {
                    jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html('<span>Dziękujemy!</span>');
                    //usuniecie funkcji ktora mierzy czas

                    if (timer == '1') {
                        clearTimeout(t);
                    }
                }
                else if (typ == 'ankieta') {
                    jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html('<span>Dziękujemy!</span>');

                }
               jQuery('.' + nazwa_sondy + '_sondaContainerClass').remove();
            }
        },
        complete: function() {

        }
    });
}

function endTime(id_sondy, nazwa_sondy, tytul, url, userName, typ) {
    var tablica = vote(id_sondy, nazwa_sondy, tytul, url, userName, typ);

    var parametry = { glosy: tablica, id: id_sondy, uzytkownik: userName };
    jQuery.ajax({
        type: "POST",
        url: "/2009/Vote.aspx",
        data: jQuery.param(parametry),
        success: function(dane) {
            changeStyles(nazwa_sondy);
            if (dane == 'OK') {
                jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html('<span>Twój czas się skończył! Dziękujemy!</span>');
                jQuery('.' + nazwa_sondy + '_sondaContainerClass').remove();

            }
        },
        complete: function() {

        }
    });
}

function getPage(id_sondy, url, nazwa_sondy, tytul) {
    var parametry = { id_sondy: id_sondy, url: url, nazwa_sondy: nazwa_sondy, tytul: jQuery.trim(tytul) };

    jQuery.ajax({
        type: "POST",
        url: "/2009/VotesRepeater.aspx",
        data: jQuery.param(parametry),
        success: function(dane) {
            var el = jQuery(dane);
            jQuery('.passingContainer').hide();
            jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html(el);
        },
        complete: function() {

        }
    });
}

function getPageSmall(id_sondy, url, nazwa_sondy, czy_bez_glosowania_na_www, tytul) {
    var parametry = { id_sondy: id_sondy, url: url, nazwa_sondy: nazwa_sondy, bez_www: czy_bez_glosowania_na_www, tytul: jQuery.trim(tytul) };

    jQuery.ajax({
        type: "POST",
        url: "/2009/VotesRepeater.aspx",
        data: jQuery.param(parametry),
        success: function(dane) {
            var el = jQuery(dane);
            jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html(el);
        },
        complete: function() {

        }
    });
}

function getXmlPage(id_sondy, url, nazwa_sondy, czy_bez_glosowania_na_www, tytul) {
    var parametry = { id_sondy: id_sondy, url: url, nazwa_sondy: nazwa_sondy, bez_www: czy_bez_glosowania_na_www, tytul: jQuery.trim(tytul), xml:'true' };

    jQuery.ajax({
        type: "POST",
        url: "/2009/VotesRepeater.aspx",
        data: jQuery.param(parametry),
        success: function(dane) {
            var el = jQuery(dane);
            jQuery('.' + nazwa_sondy + '_sondaPodziekowania').html(el);
        },
        complete: function() {

        }
    });
}

function testTimer(seconds, id_sondy, nazwa_sondy, tytul, url, userName, typ) {
    var minutes = Math.round(parseInt(seconds) / 60);
    var hours = Math.round(parseInt(minutes) / 60);
    minutes = Math.round(parseInt(seconds) / 60) - (hours * 60);
    if (hours < 10) {
        hours = '0' + hours;
    }
    if (minutes < 10) {
        minutes = '0' + minutes;
    }
    timerStart = 0;
    var thick = 1;
    jQuery('.' + nazwa_sondy + '_tytulSondyTr td').append('<br/><span class="'+nazwa_sondy+'_timer">Czas do zakończenia testu: ' + hours + ':' + minutes + ':00</span>');
    inter = setInterval('timeoutCheck("' + seconds + '","' + nazwa_sondy + '","' + thick + '");', 1000);
    var miliseconds = seconds * 1000;
    t = setTimeout('endTime("' + id_sondy + '","' + nazwa_sondy + '","' + tytul + '","' + url + "','" + userName + "','" + typ + '")', miliseconds);
}

function timeoutCheck(seconds, nazwa_sondy, thick) {
    timerStart = parseInt(timerStart) + parseInt(thick);
    seconds = seconds - timerStart;
    jQuery('.timer').html('');
    var minutes = Math.floor(parseInt(seconds) / 60);
    var getThick = Math.floor(timerStart / 60);
    var minutyKoncowe = minutes - getThick;
    var godzKoncowe = Math.floor(minutes / 60);
    minuty = minutes - (godzKoncowe * 60);
    if (minuty < 10) {
        minuty = '0' + minuty.toString();
    }
    if (godzKoncowe < 10) {
        godzKoncowe = '0' + godzKoncowe.toString();
    }
    var sec = 60 - timerStart;
    if (sec < 0) {
        var pomoc = Math.floor(timerStart / 60);

        sec = 60 - (timerStart - (pomoc * 60));
        if (sec == 60) {
            sec = 0;
        }
    }
    if (sec < 10) {
        sec = '0' + sec;
    }
    if (godzKoncowe == 0 && minuty == 0 && sec == 0) {
        clearInterval(inter);
    }
    else {

        jQuery('.'+nazwa_sondy+'_timer').html('Czas do zakończenia testu: '+godzKoncowe + ':' + minuty + ':' + sec);
    }
}


function changeStyles(nazwa_sondy) {
    jQuery('.' + nazwa_sondy + '_sondaContainerClass').css('display', 'none');
    jQuery('.' + nazwa_sondy + '_tblPytania').css('display', 'none');
    jQuery('.' + nazwa_sondy + '_sondaPodziekowania').css('display', 'block');
    //jQuery('.' + nazwa_sondy + '_sondaPodziekowania').css('vertical-align', 'middle');
}


