ts_timer = null;
ts_i_timer = 'NA';
ts_ra = null;
ts_ra_qn = '';
ts_ra_backup = null;
ts_problem = 0;
ts_problem_delay = -1; // no message

// timer function
function r_timer() {
    if (ts_i_timer == 'END'){
        if (ts_timer) {
            clearInterval(ts_timer);
            ts_timer = null;
        }
        dojo.byId("r_form").submit();
    } else if (ts_i_timer != 'NA'){
        ts_i_timer--;
        var m = Math.floor(ts_i_timer / 60);
        if (m < 10) m = '0'+m;
        var s = ts_i_timer % 60;
        if (s < 10) s = '0'+s;
        dojo.byId("i_timer").innerHTML = m+':'+s;
        if (ts_i_timer <= 0) {
            ts_i_timer = 'END';
        }
    }
}

// current test info + ra - ajax only
function r_info() {
    dojo.xhrGet({
        url: "/test/xhr_info",
        load: function(r, ioArgs){
            ts_ra = r.ra ? unescape(r.ra) : null;
            ts_ra_qn = r.ra_qn;
            dojo.byId("i_num").innerHTML = r.i_num;
            dojo.byId("i_count").innerHTML = r.i_count;
            if (ts_timer) {
                clearInterval(ts_timer);
                ts_timer = null;
            }
            ts_i_timer = r.i_timer;
            if (ts_i_timer == 'END'){
                dojo.byId("r_form").submit();
            } else if (ts_i_timer != 'NA'){
                ts_timer = setInterval("r_timer();", 1000);
            }
            return r;
        },
        error: function(r, ioArgs){
            return r;
        },
        handleAs: "json",
        preventCache: true
    });
}

// send question result - ajax + submit
function r_next(c) {
    if (ts_ra && (ts_ra_qn == dojo.byId("r_qn").value) ) {
        if(c){c.ra=1;}else{c={ra:1};}
    }
    dojo.xhrPost({
        url: dojo.byId("r_form").action+"&xhr=1",
        load: function(r, ioArgs){
            if ( c && c.ra && (r.indexOf("___RA_OK___") != -1) ) {
                dojo.forEach(dojo.query(".r_control"), function(i){i.disabled=false});
            } else {
                dojo.byId("xhr").innerHTML = r;
            }
            ts_ra = null;
            ts_ra_qn = '';
            ts_ra_backup = null;
            r_info();
            return r;
        },
        error: function(r, ioArgs){
            if (ts_ra_backup) {
                dojo.byId("xhr").innerHTML = ts_ra_backup;
                ts_ra_backup = null;
            }
            var f = dojo.byId("r_form");
            if(c){for(var p in c){if(p != "ra"){
                var i = document.createElement("input");
                i.type="hidden";
                i.name=p;
                i.value=c[p];
                f.appendChild(i);
            }}}
            dojo.byId("r_form").submit();
            return r;
        },
        form: "r_form",
        content: c
    });
    if (ts_ra && (ts_ra_qn == dojo.byId("r_qn").value) ) {
        ts_ra_backup = dojo.byId("xhr").innerHTML;
        dojo.byId("xhr").innerHTML = ts_ra;
        ts_ra = null;
        ts_ra_qn = '';
    }
    dojo.forEach(dojo.query(".r_control"), function(i){i.disabled=true});
}


// show message for problem countries
function problem_show(delay) {
    ts_problem_delay = delay;
    if (delay == -1) return 0;
    if (dojo.byId("problem")) {
        if (ts_problem || !delay) {
            problem_show_ok();
        } else {
            setTimeout("problem_show_ok();", delay*1000);
        }
    }
}

// executed by timer
function problem_show_ok() {
    var problem = dojo.byId("problem");
    if (problem) problem.style.display="block";
    ts_problem = 1;
}

// payment request - ajax + submit
function p_next(o) {
    dojo.forEach(dojo.query(".r_control"), function(i){i.disabled=true});
    dojo.xhrGet({
        url: o?o.href:dojo.byId("p_form").action,
        load: function(r, ioArgs){
            dojo.forEach(dojo.query(".r_control"), function(i){i.disabled=false});
            dojo.byId("xhr").innerHTML = r;
            problem_show(ts_problem_delay);
            return r;
        },
        error: function(r, ioArgs){
            if (o) {
                document.location = o.href;
            } else {
                dojo.byId("p_form").submit();
            }
            return r;
        },
        content: {xhr: 1},
        form: o?null:"p_form",
        preventCache: true
    });
}