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_cbox_ani = null; // checkbox animation (store original color)
ts_country = 1; // country_list (1==collapsed)
ts_ss_submit = null; // flag for disabling submit button && lock for ss_next executing twice

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

// callback for lite version (iframe)
function lite_api_cb() {
    if (typeof(lite_api) != "undefined" && lite_api) lite_api.callback();
}

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

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

// subscription form type (phone/passw)
function ss_type(f) {
    if (!f) f = dojo.byId("ss_form");
    if (!f) return false;
    var t = null;
    if (f.phone) {
        t = "phone";
    } else if (f.passw) {
        t = "passw";
    }
    return t;
}

// enabling/disabling submit button
function ss_submit(enable) {
    var b = dojo.byId("ss_button");
    if (ts_ss_submit == null) {
        if (b) {
            b.style.display="block";
            lite_api_cb();
        }
    } else if (!enable == !ts_ss_submit) {
        return false;
    }
    ts_ss_submit = enable;
    if (!b) return false;
    
    var f = dojo.byId("ss_form");
    if (!f) return false;
    var t = null;
    if (f.phone) {
        t = "phone";
    } else if (f.passw) {
        t = "passw";
    }
    if (!t) return false;
    if (f.posix) {
        t = "posix_" + t;
    }
    
    if (ts_ss_submit) {
        dojo.removeClass(b, "button_"+t+"_disabled");
    } else {
        dojo.addClass(b, "button_"+t+"_disabled");
    }
}

// validating phone/passw input, executing ss_submit
function ss_validate() {
    var f = dojo.byId("ss_form");
    if (!f) return false;
    var t = ss_type(f);
    var v = f[t].value;
    if (!v) {
        ss_submit(0);
        return false;
    }
    var vn = new String('');
    var changed = 0;
    var digits = [];
    for (i=0; i<v.length; i++) {
        var ic = v.charCodeAt(i);
        // 32 40 41 43 45 48..59
        if (ic==32||ic==40||ic==41||ic==43||ic==45||(ic>47&&ic<60)) {
            vn += v.charAt(i);
            if (ic>47&&ic<60) digits.push(v.charAt(i));
        } else {
            changed = 1;
        }
    }
    if (changed) f[t].value = vn;
    if (t == "phone") {
        if (digits.length<10) { // less than 10
            ss_submit(0);
        } else if (digits[0]==7||digits[0]==8) { // start from 7/8
            ss_submit((digits.length==11)?1:0);
        } else if (digits[0]==9) { // w/o code
            ss_submit((digits.length==10)?1:0);
        } else {
            ss_submit(1);
        }
    } else if (t == "passw") {
        ss_submit((digits.length==6)?1:0);
    } else { // impossible
        return false;
    }
}


// subscription payment request - ajax + submit
function ss_next(cc) {
    if (!cc || cc == '1') { // 
        if (!ts_ss_submit) return false; // button disabled
        if (cc && !p_cbox_check()) return false; // have checkbox, not checked
        ss_submit(0);
        cc = null;
    }
    dojo.xhrGet({
        url: cc?cc:dojo.byId("ss_form").action,
        load: function(r, ioArgs){
            dojo.byId("xhr").innerHTML = r;
            var b = dojo.byId("ss_button");
            if (b) b.style.display="block";
            ts_ss_submit = null; // forcing
            ss_validate(); // checking phone/passw
            lite_api_cb();
            return r;
        },
        error: function(r, ioArgs){
            if (cc) {
                document.location = cc;
            } else {
                dojo.byId("ss_form").submit();
            }
            return r;
        },
        content: {xhr: 1},
        form: cc?null:"ss_form",
        preventCache: true
    });
}

function p_cbox_check() {
    var id = "p_cbox_label";
    var color1 = ts_cbox_ani?ts_cbox_ani:dojo.style(id, "color");
    ts_cbox_ani = color1;
    var color2 = "#f52558";
    var 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_cbox").checked) {
        return true;
    } else {
        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");
    }
    lite_api_cb();
}

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;
            lite_api_cb();
        });
        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";
            lite_api_cb();
        });
        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");
        }
    }
}


