function validateAnswer(userAnswered, context){
  if( userAnswered.indexOf('$') > 0 ){
    return false;
  }else if( context.vars.length > 0 ){
    var validAnswer = context.answer.toUpperCase();
    var userAnswered = userAnswered.toUpperCase();
    var possibleAnswers = new Array();
    for(var i = 0; i < context.vars.length; i++){
      for( var j = 0; j < context.vars[i].values.length; j++ ){
        if(i == 0){
          var clause = validAnswer.replace( context.vars[i].var.toUpperCase(), context.vars[i].values[j] );
          possibleAnswers.push(clause);
        }else{
          var size = possibleAnswers.length;
          for(var k = 0; k < size; k++){
            validAnswer = possibleAnswers[k].toUpperCase();
            var clause = validAnswer.replace( context.vars[i].var.toUpperCase(), context.vars[i].values[j] );
            possibleAnswers.push(clause);

            if( userAnswered.toUpperCase() == clause.toUpperCase() ){
              return true;
            }

          }
        }
      }
    }
  }else if(userAnswered.toUpperCase() == context.answer.toUpperCase()){
    return true;
  }else{
    return false;
  }
}

function getValidAnswer(context){
    if( context.vars.length > 0 ){
      var validAnswer = context.answer.toUpperCase();
      var possibleAnswers = new Array();
      for(var i = 0; i < context.vars.length; i++){
        for( var j = 0; j < context.vars[i].values.length; j++ ){
          if(i == 0){
            var clause = validAnswer.replace( context.vars[i].var.toUpperCase(), context.vars[i].values[j] );
            possibleAnswers.push(clause);
          }else{
            var size = possibleAnswers.length;
            for(var k = 0; k < size; k++){
              validAnswer = possibleAnswers[k].toUpperCase();
              var clause = validAnswer.replace( context.vars[i].var.toUpperCase(), context.vars[i].values[j] );
              possibleAnswers.push(clause);


            }
          }
        }
      }
      var cleanPossibleAnswers = new Array();
      for(var i = 0; i < possibleAnswers.length; i++){
        if( possibleAnswers[i].indexOf('$') < 0  ){
          cleanPossibleAnswers.push( possibleAnswers[i] );
        }
      }
     var randNumber = Math.floor( Math.random() * cleanPossibleAnswers.length );
      return cleanPossibleAnswers[randNumber];

    }else{
      return context.answer;
    }
}

function trimDoubleSpace(value){
  var val = value.replace('  ', ' ');
  var i = 0;
  while( i < value.length){
    val = val.replace('  ', ' ');
    i++;
  }
  return val;
}


function replaceAll( text, busca, reemplaza ){
  while (text.toString().indexOf(busca) != -1)
      text = text.toString().replace(busca,reemplaza);
  return text;

}


//upload info on server
function updateStatusDaily(id, valueStatus){
  var savedAllDailies = false;
  var savedActivesDailies = false;
  var allDailies = JSON.parse( localStorage.allDailies ); 
  
  for(var i=0; i < allDailies.length; i++){
    if( allDailies[i].id == id ){
      allDailies[i].status = valueStatus;
      savedAllDailies = true;
    }
  }
  localStorage.allDailies = JSON.stringify( allDailies );

  var activeDailies = JSON.parse( localStorage.activeDailies );
  for(var i=0; i < activeDailies.length; i++){
    if( activeDailies[i].id == id ){
      activeDailies[i].status = valueStatus;
      if(valueStatus == 0) activeDailies[i].errors++;
      if(valueStatus == 1) activeDailies[i].datePass = moment().format('YYYY-MM-DD HH:mm:ss');
      savedactiveDailies = true;

    }
  }
  localStorage.activeDailies = JSON.stringify( activeDailies );
  
  if( savedAllDailies && savedActivesDailies ){
    return true;
  }else{
    return false;
  }

}

//Para adaptar el contexto en answer. Quedaba mal maquetado si eran contextos cortos
function sizeBoxContext(){
  var letters = $('.answer_ballon p').text().length;
  var fs = $('.answer_ballon p').css('font-size').replace('px', '');
  console.log('Letters: '+letters+' - Font-size: '+fs);

  if( letters < 50){
    var fs = fs*1.28;
    $('.answer_ballon p').css({'margin-top':'23%', 'font-size' : fs+'px'});
    $('.teacher_wp.answer').css({'width' : '28%', 'right':'10%'});
  }
  else if( letters < 100){
    var fs = fs*1.2;
    $('.answer_ballon p').css({'margin-top':'20%', 'font-size' : fs+'px'});
    $('.teacher_wp.answer').css({'width' : '28%', 'right':'10%'});
  }
  else if( letters < 120){
    var fs = fs*1.18;
    $('.answer_ballon p').css({'margin-top':'15%', 'font-size' : fs+'px'});
  }
  else if( letters < 150){
    var fs = fs*1.1;
    $('.answer_ballon p').css({'margin-top':'10%', 'font-size' : fs+'px'});
  }
  
  
  
}

function overflowDisable(){
  $('body').css('overflow-y', 'hidden');
  $('#wrapper').css('overflow-y', 'hidden');
}
function overflowActive(){
  $('body').css('overflow-y', 'visible');
  $('#wrapper').css('overflow-y', 'visible');
}