var const_N = 20;
var test_coocie = '';
var dat = '';

function showTest()
  {
  hide('test_buttons');

  if (getCookie('test_coocie') == false)  { show('v'+test_i); show('test_buttons');}
    else
    // Считываем куку
    {
    st = new String(getCookie('test_coocie'));
    mc = st.split(',');

    for (i=0; i<const_N; i++)
      {
        if (i<=4)
          {
            j = i + 1;
            byID('vopr_'+j).selectedIndex = mc[i]-1;

          }
          else
            {
              j = i + 1;
              $('#vop'+j).find('input:radio').each(function(){
              if ($(this).val() == mc[i]) { this.checked = true; }
              });
            }
      }
    test_i = getCookie('test_N');
    $('#num_question').html(test_i);
    if (test_i<=const_N) { show('v'+test_i); show('test_buttons'); }
    else
      {
        showRes();
      }
    }
  }


function chStep(step)
  {
    if (step>0 && test_i>=const_N)
      {
        saveForm();
        if (dat == null) { alert('Выберите какой нибудь вариант!'); return false; }
        test_i++; saveN(); showRes(); showOtziv();
      }
      else
        {
          if (step>0 && test_i<=const_N-1)
          {
            saveForm();
            if (dat == null) { alert('Выберите какой нибудь вариант!'); return false; }
            hide('v'+test_i); test_i++; show('v'+test_i);            
            saveN();
            showOtziv();
          }
          if (step<0 && test_i>=2) { hide('v'+test_i); test_i--; show('v'+test_i); saveForm(); showOtziv(); } 
          $('#num_question').html(test_i);
        }
  }


function showRes()
  { 
  var j = test_i-1;
  hide('v'+j); 
  $('#podskazka_id').hide();    
  hide('test_buttons');
  r = rand(1, 3);
  show('result_id_'+r);
  }

// Обходит всю форму и записывает ее в куку
function saveForm()
  {
  var str = '', s = '', sel;

  for (i=1; i<=20; i++)
    {
      if (i<=5)
        {
          sel = byID('vopr_'+i);
          s = sel.options[sel.selectedIndex].value;
        }
        else if (i>5)
          {
            s = null;
            $('#vop'+i).find('input:radio').each(function(){

            if (this.checked){ s = this.value; }
            });

          }
    str += s + ',';
    if (test_i == i) { dat = s; }
    }
  setCookie('test_coocie', str, 1);
  setCookie('test_N', test_i, 1);
  }

function saveN()
  {
  setCookie('test_N', test_i, 1);
  }


function resAll()
  {
    for (i=1; i<=14; i++)
    {
      hide('otz_'+i);
    }
  }

function showOtziv()
  {
    resAll();
    var mass_new = new Array();
    for (i=0; i<=3; i++)
      {
        do
        {
      	  r = rand(1, 14);
      	  mass_new[i] = r;

        } while (searchElem(mass_new, r) == 1)
        show('otz_'+mass_new[i]);
      }
  }

function searchElem(m, elem)
  {
    ret = 0;
    //if (m.length == 1) return 0;
    for (j=0; j<m.length-1; j++)
      {
        if (m[j] == elem) ret = 1;
      }

    return ret;
  }



