// The validation methods here could be used as plugins to a question validator,
// to give the question checker different behaviors

// Summation of values from a grid
function Summation(aQuestion)
{
   var i, qs, sum, value;

   sum = 0;
   if(aQuestion.type == 8)
   {
      qs = aQuestion.questions;

      value = 0;
      for(i = 0; i < qs.length; i++)
      {
         if(qs[i].visible)
         {
            value = parseFloat(document["query"][qs[i].label].value);

            if(document["query"][qs[i].label].value == "")
            {
               value = 0;
            }
            else if(isNaN(value))
            {
               return NaN;
            }

            sum += value;
         }
      }
   }
   else
   {
      alert("Warning: Summation can only be applied to question of type text grid");
   }

   return sum;
}

// Total validator
function ValidateTotal(aQuestion, validTotal)
{
   var total = Summation(aQuestion);

   if(aQuestion.type == 8)
   {
      if(isNaN(total))
      {
         return false;
      }
      else if(total == validTotal)
      {
         return true;
      }
   }
   else
   {
      alert("Warning: Summation can only be applied to question of type text grid");
   }

   return false;
}

// Uniqueness validator
function ValidateUniqueness(aQuestion, validValues)
{
   var i, x, qs, value, sum, autonom, result, validation;

   result = false;
   autonom = false;
   if(aQuestion.type == 6)
   {
      qs = aQuestion.questions;
      validation = new Array();

      if(validValues.length == 0)
      {
         for(i = 0; i < qs[0].options.length; i++)
         {
            validValues.push(qs[0].options[i].value);
         }
      }

      for(i = 0; i < validValues.length; i++)
      {
         validation[validValues[i].toString()] = 0;
      }

      value = 0;
      for(i = 0; i < qs.length; i++)
      {
         if(qs[i].visible)
         {
				for (j = 0; j < document["query"][qs[i].label].length; j++)
				{
				   if (document["query"][qs[i].label][j].checked)
				   {
				      value = document["query"][qs[i].label][j].value;

				      if(validation[value.toString()] != undefined)
				      {
                     validation[value.toString()]++;
                  }
				   }
            }
         }
      }

      result = true;
      for(i = 0; i < validValues.length; i++)
      {
         if(1 < validation[validValues[i].toString()])
         {
            result = false
            break;
         }
      }
   }
   else if(aQuestion.type == 8)
   {
      qs = aQuestion.questions;
      validation = new Array();
      if(validValues != "undefined")
      {
         for(i = 0; i < validValues.length; i++)
         {
            validation[validValues.toString()] = 0;
         }
      }
      else
      {
         validation = new Array();
         autonom = true;
      }

      value = 0;
      for(i = 0; i < qs.length; i++)
      {
         if(qs[i].visible)
         {
            value = parseFloat(document["query"][qs[i].label].value);

            if(isNaN(value))
            {
               return NaN;
            }

            if(autonom && validation[value.toString()] == "undefined")
            {
               validation[value.toString()] = 1;
            }
            else if(validation[value.toString()] != undefined)
            {
               validation[value.toString()]++;
            }
         }
      }

      sum = 0;
      for(x in validation)
      {
         sum += x;
      }

      if(sum == validation.length)
      {
         result = true;
      }
   }
   else
   {
      alert("Warning: Validation can only be applied to question of type single and text grid");
   }

   return result;
}

// Singlegrid validation
function SinglegridValidator(aQuestion, maxAnswers, message)
{
   var i, j, qs, val, sqanscnt;

   qs = aQuestion.questions;
   aos = new Array();

   // Initialize array used to check answer options
   for(i = 0; i < qs[0].options.length; i++)
   {
      aos[i] = 0;
   }

   // Perform counting
   sqanscnt = 0;
   for(i = 0; i < qs.length; i++)
   {  
      val = -1;

      for (j = 0; j < document["query"][qs[i].label].length; j++) {
         if (document["query"][qs[i].label][j].checked)
         {
            val = j;
         }
      }

      if(val > -1)
      {
         aos[val]++;
         sqanscnt++;
      }
   }

   // Perform check
   if(sqanscnt > maxAnswers)
   {
      alert(message);
      aQuestion.reset();
      return false;
   }

   for(i = 0; i < aos.length; i++)
   {      
      if(aos[i] > 1)
      {
         alert(message);
         aQuestion.reset();
         return false;
      }
   }

   // Checked and everything is ok
   return true;
}

function CheckAnswerUniqueness(aQuestion)
{
   var i, j, option, question, answers, checked;

   answers = new Array();

   for(i = 0; i < aQuestion.questions[0].options.length; i++)
   {
      option = aQuestion.questions[0].options[i];
      answers[option.index] = 0;
   }

   for(i = 0; i < aQuestion.questions.length; i++)
   {
      question = aQuestion.questions[i];

      if(question.visible)
      {
         for(j = 0; j < question.options.length; j++)
         {
            option = question.options[j];
            checked = document["query"][question.label][option.index].checked;

            if(checked)
            {
               answers[option.index] += 1;
            }
         }
      }
   }

   for(i = 0; i < answers.length; i++)
   {
      if(answers[i] > 1)
      {
         return false;
      }
   }

   return true;
}

function validateEmail(textToValidate, regexString)
{
   var regex;
   if(typeof(regexString) == "string" && regexString != "")
     regex = new RegExp(regexString); // User defined regex string
    //else if (emailValidatorRegexString != null)
        //regex = new RegExp(emailValidatorRegexString); // Regex string of CatGlobe which is included in QuestionnaireViewerEx.aspx.cs
    else
        regex = new RegExp("^([\\w-]+|[\\w-][\\w\\.-]*[\\w-])@([\\w-\\u00e6\\u00f8\\u00e5]+|[\\w-\\u00e6\\u00f8\\u00e5]+(\\.[\\w-\\u00e6\\u00f8\\u00e5]+){1,})(\\.[a-zA-Z]{2,})$");  // If no user defined or catglobe regex string then use this one.
     
   if(typeof(textToValidate) != "string")
      return false;
   else
      return (textToValidate.match(regex) != null);   
}

/*
 * Examples of use:
 *

 *
 * ValidateTotal, for validation of percentages
 *
   quest.unit = "%";
   quest.size = 6;
   quest.maxlength = 6;

   function SpecialQC()
   {
      if(ValidateTotal(quest, 100))
      {
         return true;
      }
      else
      {
         alert("De indtastede procenter skal summere til 100%");
      }
   }

   questioncheck = SpecialQC;

*/
