// ----------------------------------------------------------------------------
//       File Name: enduser_surveys.js
//       Subsystem: enduser
//   Document Type: Javascript include file
//         Purpose: contains all javascript for survey validation
// ----------------------------------------------------------------------------
var survey_question_type_text   = 1;
var survey_question_type_choice = 2;
var error_span_classname = "rnt_question_error";
var old_classname;
var error_id;

function check_survey_fields(form_name, fld_data, fields)
{
   with (fld_data) for (i = 0; (i < fields.length) && fields[i].type; i++)
   {
      fld = eval('document.'+form_name+'.'+'q_'+fields[i].id);
      
      if( (fields[i].min > 0) || (fields[i].max > 0) )
      {
         // Reset the background if on a previous submit this question
         // was flagged as an error.
         if(fields[i].id == error_id)
            reset_span(fields[i].id);

         if( fields[i].type == survey_question_type_text )
         {

            if(fields[i].min > 0 || fields[i].max > 0)
            {
               if(fields[i].min > 0 && fld.value.length < fields[i].min)
               {
                  // Not worried about this at the moment as we dont have a min for text fields
                  alert('\''+fields[i].question_text+'\' '+" not long enough.");
                  fld.focus();
                  show_error_span(fields[i].id);
                  return(false);
               }

               if(fields[i].max > 0 && fld.value.length > fields[i].max)
               {
                  alert('\''+fields[i].question_text+'\' '+ fld_too_mny_chars_msg);
                  fld.focus();
                  show_error_span(fields[i].id);
                  return(false);
               }
            }
         }
         else
         {
            var num_selected = 0;
            var can_focus    = 1;
            // We have to go through and find all the elements associated
            // with this question.  We'll drop them into an array so we
            // can count them.
            if(fld)
            {
               // Need to handle buttons & select elements differently
               if(fld.options)
               {
                  var selected_array = new Array();
                  var j;
                  var count = 0;

                  for (j=0; j<fld.options.length; j++) 
                  {
                     if (fld.options[j].selected && fld.options[j].value != '') 
                     {
                        selected_array[count] = fld.options[j].value;
                        count++;
                     }
                  }

                  num_selected = selected_array.length;
               }
               else
               {
                  // radio buttons. fld is an array
                  for (j=0; j<fld.length; j++) 
                  {
                     if (fld[j].checked)
                     {
                        num_selected++;
                     }
                  }
                  can_focus = 0;
               }

            }
            else
            {
               // This is a checkbox question
               var element_array = new Array;
               var j;
               element_array = fields[i].elements.split(":");
               can_focus = 0;

               for(j = 0; j < element_array.length; j++)
               {
                  fld = eval('document.'+form_name+'.'+element_array[j]);
                  if(fld && fld.checked)
                  {
                     num_selected++;
                  }
               }  
            }

            // Now check the values against the restrictions
            if(fields[i].min > 0 && num_selected < fields[i].min)
            {
               alert('\''+fields[i].question_text+'\' '+ too_few_options_msg);
               if(can_focus)
                  fld.focus();
               show_error_span(fields[i].id);
               return(false);
            }
            if(fields[i].max > 0 && num_selected > fields[i].max)
            {
               alert('\''+fields[i].question_text+'\' '+ too_many_options_msg);
               if(can_focus)
                  fld.focus();
               show_error_span(fields[i].id);
               return(false);
            }
         }
      }
   }

   return(true);
}

function show_error_span(span_id)
{
   span = document.getElementById("q" + span_id);
   if(span)
   {     
      old_class  = span.className;
      span.className = error_span_classname;
      error_id = span_id;
   }
}

function reset_span(span_id)
{
   span = document.getElementById("q" + span_id);
   span.className = old_classname;
}
