var tmtmy;

function hideAll()
{
  for (i=1;i<=maxstep;i++)
  {
    hide('test_' + i);
  }
  hide('join_request');
  hide('join_request_main');
  //hide('test_results');
}

function ch_step(curtest,step,curstep)
{
  var form_id = '#form_content_' + curstep;
  var choise_doit = false;
  if (curstep)
  {
    $(form_id).find('.radio_btn').each(function() {
      if (this.checked) { choise_doit = true; }
    });
  } else { choise_doit = true; }


  if (choise_doit)
  {
    hideAll();

    if (step == 'join')
    {
      if (joined) { getResultOfTest(); }

      if (!joined) byID('testname_id').innerHTML = test_name;
      byID('ramka_id').style['width'] = '60%';
      byID('test').style['textAlign'] = 'center';

      doProg(4,1);
    }
    else
    {
      show('test_' + step);
    }
    var q = '?curstep=' + step + '&cur_test=' + curtest;

    url = HTTP_FULL_PATH + '/callback/cb_katalogtestov_v2.php' + q;
    ajaxChainLoaderGet(url,f_null);
  } else { alert('Нужно выбрать варант ответа!'); }
}

function f_null()
{
  return true;
}

function onloadGetResOrJoin()
{
  if (joined)
    { getResultOfTest(); }
  else
    { getJoin(); }
}

function doWhenReg()
{
  getResultOfTest();
}

function getResultOfTest()
{
  var stringPost = 'getresurlt=true&cur_test='+cur_test;
  $('#div_of_cur_test').find('.radio_btn').each(function() {
    stringPost = stringPost + '&' + this.id + '=' + this.checked;
  });
  //alert(stringPost);
  url = HTTP_FULL_PATH + '/callback/cb_katalogtestov_v2.php';
  //alert(url);
  ajaxChainLoaderPost(url,stringPost,getResultOfTest_callback)
}

function getResultOfTest_callback()
{
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('ajaxtestres'); }
  if (nl)
  {
    var nli = nl.item(0);
    testresult = nli.getAttribute('testresult');
    byID('join_request').innerHTML = testresult;
  }
}

function tem(max,cur)
{
  if (cur <= max)
  {
    show('sus' + cur);
    el = byID('susp' + cur);
    if (el)
    {
      el.className = "gray";
      incr = rand(1,10);
      if (cur >= 10) { incr = rand(1,3); }
      if (incr < 1) { incr = 1; }
      cnt = parseInt(el.innerHTML) + incr;
      if (cnt >= 100) { el.className = "green"; cnt=100; cur++; byID('process_request').scrollTop = byID('process_request').scrollTop + 100; }
      byID('process_request').scrollTop = byID('process_request').scrollTop + 400;
      el.innerHTML = cnt + '%';
    }
    var curSpedTmt = rand(60,70);
    tmtmy = setTimeout('tem('+max+','+cur+')', curSpedTmt);
  } else { showRes(); }
}

function doProg(max,cur)
{
  for (i=cur;i<=max;i++) { byID('susp' + i).innerHTML = '0'; hide('sus' + i); }
  clearTimeout(tmtmy);
  show('process_request');
  tem(max, cur);
}

function showRes()
{
  hide('process_request');

  if (joined)
  {
  	byID('ramka_id').style['width'] = '100%';
    byID('test').style['textAlign'] = 'left';
  }

  show('join_request');
  show('join_request_main');
}