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
ts_secret_ani = null; // cb_secret_check animation
ts_country = 1; // country_list (1==collapsed)

// 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);
            p_country_restore();
            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
    });
}

function p_secret_check() {
    if (!ts_secret_ani) {
        var id = "p_secret_label";
        var color1 = dojo.style(id, "color");
        var color2 = "#f52558";
        ts_secret_ani = dojo.animateProperty({
            node: id, duration: 500,
            properties: {
                color: { start: color1, end: color2}
            },
            onEnd: function() {
                dojo.animateProperty({
                    node: id, 
                    duration: 500,
                    delay: 500,
                    properties: {
                        color: { start: color2, end: color1}
                    }
                }).play();
            }
        });
    }
    if (dojo.byId("p_secret").checked) {
        return true;
    } else {
        ts_secret_ani.play();
        return false;
    }
}

function p_country_restore() {
    var node = dojo.byId("country_list");
    if (!node) return false;
    var s = node.style;
    var arrow = dojo.byId("country_switch_arrow");
    if (ts_country) { //hidden
        s.height = "1px";
        s.display = "none";
        if (arrow) dojo.removeClass(arrow, "country_switch_arrow_up");
    } else {
        s.height = "auto";
        s.display = "";
        if (arrow) dojo.addClass(arrow, "country_switch_arrow_up");
    }
}

function p_country_switch() {
    var node = dojo.byId("country_list");
    if (!node) return false;
    
    if (ts_country) {
        var s = node.style;
        var o;
        var anim = dojo.animateProperty({
            node: node,
            duration: 200,
            properties: {
                height: {
                    // wrapped in functions so we wait till the last second to query (in case value has changed)
                    start: function(){
                        // start at current [computed] height, but use 1px rather than 0
                        // because 0 causes IE to display the whole panel
                        o = s.overflow;
                        s.overflow="hidden";
                        if(s.visibility=="hidden"||s.display=="none"){
                            s.height="1px";
                            s.display="";
                            s.visibility="";
                            return 1;
                        }else{
                            var height = dojo.style(node, "height");
                            return Math.max(height, 1);
                        }
                    },
                    end: function(){
                        return node.scrollHeight;
                    }
                }
            }
        });
        dojo.connect(anim, "onEnd", function(){ 
            s.height = "auto";
            s.overflow = o;
        });
        ts_country = 0;
    } else {
        var s = node.style;
        var o;
        var anim = dojo.animateProperty({
            node: node,
            duration: 200,
            properties: {
                height: {end: 1}
            }
        });
        dojo.connect(anim, "beforeBegin", function(){
            o = s.overflow;
            s.overflow = "hidden";
            s.display = "";
        });
        dojo.connect(anim, "onEnd", function(){
            s.overflow = o;
            s.height = "auto";
            s.display = "none";
        });
        ts_country = 1;
    }
    anim.play();
    var arrow = dojo.byId("country_switch_arrow");
    if (arrow) {
        if (ts_country) {
            dojo.removeClass(arrow, "country_switch_arrow_up");
        } else {
            dojo.addClass(arrow, "country_switch_arrow_up");
        }
    }
}

