
ans0 = new Array('001','01','10','001','01');
ball = new Array(1,3,1,1,1);
testName = 'test';
showCorrWrong = false;
maxAttempts = 1;
startDoc = testName;
testDoc = testName+'1';

imgYes = new Image;
imgNo = new Image;
imgYes.src = 'yes.gif';
imgNo.src = 'no.gif';

regname='';
score=0;
score0=0;
attempt=0;


  resultEstimation = new Array('Probability restore defect pixels - 90%','Probability restore defect pixels - 80%','Probability restore defect pixels - 70%','Probability restore defect pixels - 60%','Probability restore defect pixels - 45%','Probability restore defect pixels - 20%' );
  
  resultPercent = new Array(1,0.85,0.75,0.65,0.55,0);    

//_____________________________________________________________________


function init()
{
  with (document.test)
  {
    for ( i=0; i<document.test.elements.length; i++)
    {
       elements[i].checked = false;
    };
  };

  document.res.mark.value = '';
  
  nmbrOfQuestions = ans0.length;
  
  ans = new Array(nmbrOfQuestions);
  ansResult = new Array(nmbrOfQuestions);
  answeredQuestions = new Array(nmbrOfQuestions);
};


//_____________________________________________________________________

function check()
{
  if ( attempt > maxAttempts ) exit;

  var imgSrc = '';
  var n = 0;
  ans[0] = '';

  with (document.test)
  {
    for ( var i=0; i<elements.length; i++ )
    {
       if ( (elements[i].type=='checkbox') || (elements[i].type=='radio') )
       {
         if ( parseInt(elements[i].value)!=n )
         {
            n = n+1;
            ans[n] = '';
         };

         if (elements[i].checked)
           { ans[n] = ans[n] + '1'; }
         else
           { ans[n] = ans[n] + '0'; }
       }
    };

    for (i=0; i<nmbrOfQuestions; i++)
    {
      if ( ans0[i]==ans[i] )
        { ansResult[i] = 1 }
      else
        { ansResult[i] = 0 };
    };

    maxScore = 0;
    score = 0;
    correctAnswers = 0;
    nmbrAnsweredQuestions = 0;
    missedQuestions = '';
    nmbrMissedQuestions = 0;

    for (i=0; i<nmbrOfQuestions; i++)
    {
      score = score + ansResult[i]*ball[i];
      maxScore = maxScore + ball[i];
      correctAnswers = correctAnswers + ansResult[i];

      if ( parseInt(ans[i])>0 )
        { answeredQuestions[i] = 1;
          nmbrAnsweredQuestions = nmbrAnsweredQuestions + 1;
          
          if (showCorrWrong)
          {
            if ( ansResult[i]==1 )
             { imgSrc = 'yes' }
            else
             { imgSrc = 'no' };
         
            var j = i+1;     
            eval('document.a'+j+'.src="'+imgSrc+'.gif"');
          }
        }
      else
        { answeredQuestions[i] = 0;
          missedQuestions = missedQuestions + ' ' + (i+1);
          nmbrMissedQuestions = nmbrMissedQuestions + 1;
        };
    };
   };   
};


//_____________________________________________________________________


function showResult()
{


  
  function ending1(nmbr) 
  {
   var ending;
   var a;
   
   if ( nmbr == Math.floor(nmbr) )
   {  
     if ( (nmbr>=10) && (nmbr<=19) )  
     { ending='s'
     }
     else
     { a = nmbr%10;
  
       if (a==1) { ending='' }
       else 
       { if ((a==2)||(a==3)||(a==4))  { ending='s' }
         else                         { ending='s' }
       };
     };
   }
   else {ending='s'};

   return (ending);  
  };
  
  function ending2(nmbr)  
  {
   var ending;
   var a;
  
   ending = 's';
   a = nmbr%10;

   if ( (nmbr==Math.floor(nmbr)) && (a==1) )
   {  ending = 's'
   };

   return (ending);  
  };  

  //---------------------------------------   
  
  attempt = attempt + 1;
  var s=''; 

  if ( attempt <= maxAttempts )
  {  
    if ( attempt>1 )
    {  s = s + attempt + '\n';
    }
  }
  else
  {
    s = s + 'You have already responded to this test.\n\n';
  };
  
 s = s + '';
      
  rate = '';

  if ( attempt==1 )
  {
    if (score==maxScore) {rate=resultEstimation[0]}
    else
     { for (i=1; i<resultEstimation.length; i++)
       { if ((score<(maxScore*resultPercent[i-1])) && (score>=(maxScore*resultPercent[i]))) 
         {  rate=resultEstimation[i];
            break
         }
       }
     }
   };  
   
   s = s + rate;
  
  if ( attempt>1 )
  {  s = s + '\n';
  };
  
  document.res.mark.value = s;

  score0 = score;
  
  cookStr = 'test=regname='+regname+':score='+score;
  document.cookie = cookStr;
};

//_____________________________________________________________________


function result()
{
  if ( nmbrMissedQuestions>0 )
  {
    var s1 = 'Warning: You have not answered all the questions. This may adversely affect the assessment of the likelihood!\n';
    //var s2;
 var s3 = '\nContinue to respond?';

 
    if (!(confirm(s1+s3)))
    { showResult() };

 }
  else { showResult() };

};

//_____________________________________________________________________
