
//----------------
// Ranking
//----------------

function textCounter(field, countfield, maxlimit)
{
   if (field.value.length > maxlimit)
       field.value = field.value.substring(0, maxlimit);   
   else 
       countfield.value = maxlimit - field.value.length;
}

function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function IsEmptyOption(aTextField) {
   if (aTextField.selectedIndex == 0) {
      return true;
   }
   else { return false; }
}

function IsNumeric(aTextField)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < aTextField.value.length && IsNumber == true; i++) 
      { 
      Char = aTextField.value.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

//----------------------
// Single_choice_matrix
//----------------------

function single_choice_matrix_validation(page, question, prompt)
{
  // And Logic
  var validated = 1;  
  
  for(var i=1; i<=prompt; i++)
  {      
     if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i)))
     {            
         validated = 0;
         break;   
     }     
  }
  
  if (validated == 1)
  {
      document.getElementById("q_"+page+"_"+question+"_msg").className = "black";
      return true;
  }
  else
  {
      document.getElementById("q_"+page+"_"+question + "_msg").className = "required";
      return false;
  }
}

//----------------------
// Date
//----------------------

function date_validation(page, question, prompt)
{
  // And Logic
  var validated =1;
  for(var i=1; i<=prompt; i++)
  {      
    var box = document.getElementById("q_"+page+"_"+question+"_-"+i+"_month");
    var month = box.options[box.selectedIndex].value;
    box = document.getElementById("q_"+page+"_"+question+"_-"+i+"_day");
    var day = box.options[box.selectedIndex].value;
    box = document.getElementById("q_"+page+"_"+question+"_-"+i+"_year");
    var year = box.options[box.selectedIndex].value;

    if (!isDate(month+"/" + day + "/" + year))    
	
	/*
     if (   IsEmptyOption(document.getElementById("q_"+page+"_"+question+"_-"+prompt+"_month"))
         || IsEmptyOption(document.getElementById("q_"+page+"_"+question+"_-"+prompt+"_day"))
         || IsEmptyOption(document.getElementById("q_"+page+"_"+question+"_-"+prompt+"_year"))
         )
         */
     {            
         validated = 0;
         break;
     }  
  }
  
  if (validated == 1)
  {
      document.getElementById("q_"+page+"_"+question+"_msg").className = "black";
      return true;
  }
  else
  {
      document.getElementById("q_"+page+"_"+question + "_msg").className = "required";
      return false;
  }
}

function constant_sum_validation(page, question, prompt, total)
{
   // And Logic   
   var validated = 1;
   var total_number = 0
   for(var i=1; i<=prompt; i++)
   {
     if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i))
     || !IsNumeric(document.getElementById("q_"+page+"_"+question+"_-"+i)))
     {   validated = 0;
         break;
     }
     else
     {
        total_number += parseInt(document.getElementById("q_"+page+"_"+question+"_-"+i).value);
     }      
   }  
  
  if (validated == 1)
  {
     if (total_number == total)
     {
        document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
        return true;
     }
     else
     {
        document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
        return false;
     }     
  }   
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }  
   
}

//----------------
// Spinner
//----------------

function spinner_validation(page, question, prompt)
{
   // And Logic   
   var validated = 1;
   for(var i=1; i<=prompt; i++)
   {
     if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i))
     || !IsNumeric(document.getElementById("q_"+page+"_"+question+"_-"+i)))
     {   validated = 0;
         break;
     }       
   }
   
  if (validated == 1)
  { 
     document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
     return true;
  }
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }  
   
}


//----------------
// dropdownlist
//----------------

function dropdownlist_validation(page,question)
{
  if (IsEmptyOption(document.getElementById("q_"+page+"_"+question)))
  {
     //document.getElementById("q_" + prompt + "_msg").firstChild.nodeValue = "(*)Required Field";
     document.getElementById("q_"+page+"_"+question+"_msg").className = "required";     
     return false;
  }
  else 
  {
    //document.getElementById("q_" + prompt + "_msg").firstChild.nodeValue = "";
    document.getElementById("q_"+page+"_"+question+ "_msg").className = "black";     
    return true;
  }
  
}

//----------------
// comments
//----------------

function comments_validation(page, question)
{
  var validated = !IsEmpty(document.getElementById("q_"+page+"_"+question))
   
  if (validated == 1)
  { 
     document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
     return true;
  }
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }     
}

//----------------
// Single_Line
//----------------

function single_line_validation(page, question, prompt)
{
   // And Logic   
   var validated = 1;
   for(var i=1; i<=prompt; i++)
   {
     if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_-"+i)))
     {   validated = 0;
         break;
     }       
   }
   
  if (validated == 1)
  { 
     document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
     return true;
  }
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }  
   
}

//----------------
// single_choice
//----------------

function single_choice_validation(page,question,prompt)
{
  if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+prompt)))
  {
     //document.getElementById("q_" + prompt + "_msg").firstChild.nodeValue = "(*)Required Field";
     document.getElementById("q_"+page+"_"+question+"_msg").className = "required";     
     return false;
  }
  else 
  {
    if (document.getElementById("q_"+page+"_"+question+"_"+prompt).value > 0)
    {
       //document.getElementById("q_" + prompt + "_msg").firstChild.nodeValue = "";
       document.getElementById("q_"+page+"_"+question+ "_msg").className = "black";     
       return true;
    }
    else
    {
       if (document.getElementById("q_"+page+"_"+question+"_open_ended").value != "")
       {
          document.getElementById("q_"+page+"_"+question+ "_msg").className = "black";     
          return true;
       }
       else
       {
          document.getElementById("q_"+page+"_"+question+"_msg").className = "required";     
          return false;
       }     
    }
  }  
}

//----------------
// Multi_choice
//----------------
function multi_choice_validation(page, question, prompt, limit)
{

 // added @ 2008/07/31
   
   if (limit != null && limit != '' && limit > 0)
   {
      var count = 0;  
  
      for(var i=1; i<= prompt; i++)
      {        
        if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i)))
        {   
             count ++;            
        }         
      }    
       
      if (count != limit )
      {
          document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
          return false;
      }
      
   }

  // Or logic
  var validated = 0;  
  for(var i=1; i<= prompt; i++)
  {        
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i)))
     {   validated = 1;
         break;
     }         
  }    
  
  if (document.getElementById("q_"+page+"_"+question+"_-1") != null)
  {
     if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_-1")))
     {
        if (IsEmpty(document.getElementById("q_"+page+"_"+question+"_open_ended")))
        {
           validated = 0;
        }
        else
        {
           validated = 1;
        }
     }    
  }
  
  if (validated == 1)
  { 
     document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
     return true;
  }
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }  
}

//--------------------
// Multi_choice_matrix
//--------------------

function multi_choice_matrix_validation(page, question, prompt, option)
{
  // And logic
  var validated_i=1; validated_j=0;
  
  for(var i=1; i<= prompt; i++)
  {
     validated_j = 0;
     for(var j=1; j<=option; j++)
     {        
        if (!IsEmpty(document.getElementById("q_"+page+"_"+question+"_"+i+"_"+j)))
        {   validated_j = 1;
            break;
        }         
     }
     validated_i = validated_i && validated_j;
     
     if (validated_i==0) break;
  }
  
  if (validated_i == 1)
  { 
     document.getElementById("q_" + page + "_" + question + "_msg").className = "black";     
     return true;
  }
  else
  {
     document.getElementById("q_" + page + "_" + question + "_msg").className = "required";     
     return false;
  }  
  
}

