function answers(oForm) {
    var str='';
    for (var i=0; i < oForm.elements.length; i++) {
        if(oForm.elements[i].type == "checkbox" && oForm.elements[i].checked) str+=oForm.elements[i].value;
    }
    return str;
}

function GoTestStart() {
   document.getElementById("GoTest").style.display = "block";
   document.getElementById("GoTestWelcome").style.display = "none";
   document.getElementById("GoTestResult").style.display = "none";
   document.getElementById("GoTestRegistrationError").style.display = "none";
   document.getElementById("GoTestCertificate").style.display = "none";
   document.getElementById("GoTestPrintResult").style.display = "none";               
   GoTest(0);  
}



function GoTestReg(value) {    

    var r = new JsHttpRequest();

    r.onreadystatechange = function() {
        if (r.readyState == 4) {            

        //document.getElementById('debug').innerHTML = r.responseText;
        
        if(r.responseJS.registration==1) {
          document.getElementById("GoTestResult").style.display = "block";
          document.getElementById("GoTestRegistration").style.display = "none";
          ShowCertificate();
        } else {
          document.getElementById("GoTestRegistrationError").style.display = "block";          
        }
        

    }    
  }
    r.open( 'POST', '/gotest/registration.php', true);
    r.send( { q: value } );
}

function ShowCertificate() {
  document.getElementById('GoTestCertificate').innerHTML='<img src="/gotest/cert/certificate.gif?p=1">';
  document.getElementById("GoTestCertificate").style.display = "block"; 
  document.getElementById("GoTestPrintResult").style.display = "block";
                   
}

function GoTest(value) {

    var req = new JsHttpRequest();

    req.onreadystatechange = function() {
        if (req.readyState == 4) {     
        
        //document.getElementById('debug').innerHTML = req.responseText;       

            var divElement = document.getElementById("GoTestQuestion");
            divElement.innerHTML='';

      
            
            if(req.responseJS.finish==1) {
            
                document.getElementById("GoTest").style.display = "none";               
                document.getElementById("GoTestResult").style.display = "block";                 
                                
                
                var percent=req.responseJS.percent;
                good=false;
                if(percent>80) good=true;
                
                if(good) {                   
                  var show = document.getElementById("GoTestResultGood");
                  var hide = document.getElementById("GoTestResultBad");
                } else {
                  var hide = document.getElementById("GoTestResultGood");
                  var show = document.getElementById("GoTestResultBad");
                }
                
                show.style.display = "block";  
                hide.style.display = "none";  
                
                document.getElementById('GoTestBalls').innerHTML=req.responseJS.percent;
                
                if(req.responseJS.needRegistration!=1 && good) {
                  ShowCertificate();                                 
                }

            } else {
            
                document.getElementById('GoTestTitle').innerHTML=req.responseJS.ques;
                document.getElementById('GoTestQNum').innerHTML=req.responseJS.qNum;
                document.getElementById('GoTestMaxNum').innerHTML=req.responseJS.qMaxNum;
                
                var answers=req.responseJS.answers;
                for( answer in answers ) {
                    var cb = document.createElement( "input" );
                    cb.type = "checkbox";
                    cb.id = "Ans[]";
                    cb.name = "Ans1[]";
                    cb.value = answer;                    
                    divElement.appendChild(cb);

                    var imgElement = document.createElement("IMG");
                    imgElement.setAttribute("src", 'gotest/src/'+answers[answer]);
                    imgElement.setAttribute("width", 150);
                    imgElement.setAttribute("height", 150);
                    divElement.appendChild(imgElement);

                    divElement.appendChild(document.createElement("br"));
                    divElement.appendChild(document.createElement("br"));
                }
                var b = document.createElement('input');
                b.type = 'submit';
                b.value = 'Ответить';
                divElement.appendChild(b);
            }
            
             if(req.responseJS.needRegistration==1 && good) {
                
                document.getElementById('GoTestRegistration').style.display = "block";
                document.getElementById('GoTestResult').style.display = "none";
            }


        }
    }

    req.open( 'POST', '/gotest/req.php', true);
    req.send( { answer: value } );
}

