// JS pro Online testy

/**
 *  provede se po nacteni stranky
 */ 
 
var flashMovie;



function onLineTestOnLoad()
{
 
  if(document.getElementById('OnLineTestRegForm'))
  {
      $("#OnlineTestErrorMessage").hide();
      setOnLineTestRegFormValidation();
      
  }
  
  if(document.getElementById('OnLineTestQuestions'))
  {

     $("#OnLineTestTimeToEnd").show();
     $("#OnLineTestQuestionsList").hide();
     onlineTestTimer();
     $("#OnLineTestQuestionsList").slideDown(3000);
     
     
     
     $("#OnLineTestQuestionsFormSubmit").click( function(){checkOnlineTestSection(false); return false;});
          
     // ULOZIME INFO, ZDA MA ZAPLI JS A FUNGUJE AJAX
     $.ajax({
        url: _SERVER_PATH+"inc/ajax/online-tests-checkjavascript.php",
        global: false,
        type: "POST",
        data: "javascript=on",
        dataType: "text",
        cache: "false",
        async: true,
        success: function(msg){
            // nema zadny navrat....
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown
            // will have info
            alert(errorThrown +': '+textStatus);
        }
    });
     

  }
  
}


function setOnLineTestRegFormValidation()
{
  test_type = $("#testType").attr('value');

    
/*
  $.validator.setDefaults({
	submitHandler: function() { alert("submitted!"); }
  });  
  */
  
  //Test pro verejnost
  
  if(test_type == 'polyglot')
  {
  $("#OnLineTestRegForm").validate({
		rules: {
			jmeno: "required",
			prijmeni: "required",
			email: {
				required: true,
				email: true
			},
			telefon: {
				required: true,
				phone: true
			},
			oblast: "required",
			kurz: "required"
			
		},
		messages: {
			jmeno: "Musíte vyplnit Vaše křestní jméno",
			prijmeni: "Musíte vyplnit Vaše příjmení",
			email: "Musíte vyplnit Vaší e-mailovou adresu",
			telefon: "Musíte vyplnit Vaš telefon",
			oblast: "Vyplňte oblast, kde chcete kurz absolvovat",
			kurz: "Vyplňte typ kurzu, jaký chcete kurz absolvovat"
		}
	});  
  
  }
  
  if(test_type == 'eon')
  {
  $("#OnLineTestRegForm").validate({
		rules: {
			jmeno: "required",
			prijmeni: "required",
			email: {
				required: true,
				email: true
			},
			telefon: {
				required: true,
				phone: true
			},
			pracoviste: "required",
      heslo:"required"
			
			
		},
		messages: {
			jmeno: "Musíte vyplnit Vaše křestní jméno",
			prijmeni: "Musíte vyplnit Vaše příjmení",
			email: "Musíte vyplnit Vaší e-mailovou adresu",
			telefon: "Musíte vyplnit Vaš telefon",
			pracoviste: "Vyplňte kde pracujete",
			heslo: "Vyplňte heslo pro vstup do testu"
		}
	});  
  
  }
  
  if(test_type == 'rwe')
  {
  $("#OnLineTestRegForm").validate({
		rules: {
			jmeno: "required",
			prijmeni: "required",
			email: {
				required: true,
				email: true
			},
			telefon: {
				required: true,
				phone: true
			},
			pracoviste: "required",
			spolecnost: "required",
      heslo:"required"
			
			
		},
		messages: {
			jmeno: "Musíte vyplnit Vaše křestní jméno",
			prijmeni: "Musíte vyplnit Vaše příjmení",
			email: "Musíte vyplnit Vaší e-mailovou adresu",
			telefon: "Musíte vyplnit Vaš telefon",
			pracoviste: "Vyplňte kde pracujete",
			spolecnost: "Vyplňte kde pracujete",
			heslo: "Vyplňte heslo pro vstup do testu"
		}
	});  
  
  }
  
  
}

var counter = -1;
var timerOnlineTest = 0;
var maxTestTime = 3600;



function onlineTestTimer()
{
    if(counter == -1 ) counter = onLineTestTimeToEnd;
    if(counter>maxTestTime) counter = maxTestTime;
      
    min = Math.floor(counter / 60);
    
    sec = counter - min*60; 
    
    /*
    if(sec % 59 == 0){
    //flashMovie.hodinyPosun(min);
    }
    */


    if(sec % 59 == 0 && counter != maxTestTime){
      flashMovie = document.getElementById("ei_test");
      if(flashMovie){
        flashMovie.hodinyPosun(60-min);
      }
    }

    sec = sec.toString();
    
    if(sec.length == 1) sec = "0"+sec;
    
    $("#OnLineTestTimeToEndClocks").text(min+":"+sec);
        
    counter--;
    
    
    if(counter)
    {
        timerOnlineTest = setTimeout("onlineTestTimer()",1000);
    }
    else
    {
        //$("#OnLineTestQuestionsForm").submit();
        
        //Jeste kontrola casu se serverem..
        
        testAlias = $("#OnLineTestAlias").attr('value');

        $.ajax({
        url: _SERVER_PATH+"inc/ajax/online-tests-checktimetoend.php",
        global: false,
        type: "POST",
        data: "testAlias="+testAlias,
        dataType: "text",
        cache: "false",
        async: true,
        success: function(msg){
                    if(msg == 'OK')
                    {
                        checkOnlineTestSection(true);                        
                    }
                    else
                    {
                        // pokud neni msg OK, je to cislo, ktere oznacuje pocet vterin do konce
                        counter = msg;
                        onlineTestTimer();        
                    }
                    
                 },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
                  // typically only one of textStatus or errorThrown
                  // will have info
                     alert(errorThrown +': '+textStatus);
               }
        });


        
    }
}

/**
 * AJAX funkce, zajisti overeni sekce testu a zamezi odeslani formulare standardnim zpusobem 
 * @return false;
 */ 

function checkOnlineTestSection(timeEnd)
{
    
       
    //Ziskame odpovedi z formulare..
    $(window).scrollTop();
    answersArray = $(":radio");
    
    radios = new Array();
    str = '';
    answersArray.each (
       function (i, el)
       {
          if(el.checked){
            radios.push($(el).attr('id') + "_" + el.value);
          }
       }
    );
    
    radios.join(';');
    
   
    sectionID = $("#OnLineTestSectionID").attr('value');
    sectionID = parseInt(sectionID);
    $("#OnLineTestSectionID").attr('value',sectionID+1);
    
    
    
    testAlias = $("#OnLineTestAlias").attr('value');

    $("#OnLineTestQuestionsList").html(_LOADING_IMAGE);
    
    $.ajax({
        url: _SERVER_PATH+"inc/ajax/online-tests-checktest.php",
        global: false,
        type: "POST",
        data: "action=checkOnlineTestSection&testAlias="+testAlias+"&timeEnd="+timeEnd+"&sectionID="+sectionID+"&radios="+radios,
        dataType: "html",
        cache: "false",
        async: true,
        success: function(msg){
            
            //TEST UZ JE HOTOVY NEBO UZIVATEL NEPREKROCIL V POSLEDNI SEKCI MINIMALNI PROCENTO SPRAVNYCH ODPOVEDI
            if(msg == 'END')
            {
              clearTimeout(timerOnlineTest);
              $("#OnLineTestTimeToEnd").hide();
              $("#OnLineTestQuestionsFormSubmit").hide();
              $("#OnLineTestQuestionsList").html('Vaš test byl úspěšně odeslán. Budeme vás kontaktovat.');
            }
            else
            //TEST NENI HOTOVY A JESTE ZBYVA NEJAKA SEKCE
            {             

              //sectionID = $("#OnLineTestSectionID").attr('value');
              //sectionID = parseInt(sectionID);
              //$("#OnLineTestSectionID").attr('value',sectionID+1); 
                       
              $("#OnLineTestQuestionsList").html(msg);
            
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown
            // will have info
            alert(errorThrown +': '+textStatus);
        }
    });
    
    return false;
}
