
function validationReturn(data, fieldSelector, fieldType)
{
   if (false == data.isvalid)
   {
      $(fieldSelector + "-error").html(data.errormessage).show();
   }
   else
   {
      $(fieldSelector + "-error").hide();

      // If the field's value cannot be changed (checkbox, radio, select, etc.)
      // we cannot update its value.
      if ("radio" != fieldType &&
          "checkbox"  != fieldType &&
          "select"  != fieldType &&
          "hidden"  != fieldType)
      {
         $(fieldSelector).val(data.outputscreen);
      }
   }
}

function addValidation(fieldSelector, dataType, fieldType, isRequired, customError, params, includePath) {
   // isRequired is an optional argument set to TRUE if not supplied
   if (undefined === isRequired)
   {
      var isRequired = true;
   }
   // customError is an optional argument set to an empty string if not supplied
   if ( ! customError)
   {
      var customError = "";
   }
   // params is an optional argument set to an empty string if not supplied
   if ( ! params)
   {
      var params = "";
   }

   // Handle getting values of radio buttons separately
   if ("radio" == fieldType)
   {
      var watchSelector = fieldSelector.replace("#", ".");
      var valueSelector = fieldSelector.replace("#", ".") + ":checked";
   }
   else
   {
      var watchSelector = fieldSelector;
      var valueSelector = fieldSelector;
   }

   // If the dataType is a DATE, RADIO, SELECT or CHECKBOX we need to use
   // the change() event instead of blur()
   if ("date" == dataType ||
       "radio" == dataType ||
       "select" == dataType ||
       "checkbox" == dataType)
   {
      $(watchSelector).change(function() {

               var fieldValue = $(valueSelector).val();

               if ( ! isRequired && 0 == fieldValue.length)
               {
                  // Field is not required and has no value, so don't post for validation
                  // In case there was previously an error and now no data, remove error
                  $(fieldSelector + "-error").hide();
               }
               else
               {
                  // Field is required or it is not required (optional) but has data
                  $.post(includePath + "/classes/dsform/clientside-form-validation.php",
                  { type: dataType, value: fieldValue, isrequired: isRequired, customerror: customError, parameters: params},
                  function(data) {
                     validationReturn(data, fieldSelector);
                  },
                  "json");
               }
      });
   }
   else
   {
      $(watchSelector).blur(function() {

               var fieldValue = $(valueSelector).val();

               if ( ! isRequired && 0 == fieldValue.length)
               {
                  // Field is not required and has no value, so don't post for validation
                  // In case there was previously an error and now no data, remove error
                  $(fieldSelector + "-error").hide();
               }
               else
               {
                  // Field is required or it is not required (optional) but has data
                  $.post(includePath + "/classes/dsform/clientside-form-validation.php",
                  { type: dataType, value: fieldValue, isrequired: isRequired, customerror: customError, parameters: params},
                  function(data) {
                     validationReturn(data, fieldSelector);
                  },
                  "json");
               }
      });
   }

}

function addDependentValidation(fieldSelector, dataType, dependentFieldSelector, dependentWhenValueIs, isRequired, customError, params, includePath) {
   // isRequired is an optional argument set to TRUE if not supplied
   if (undefined === isRequired)
   {
      var isRequired = true;
   }
   // customError is an optional argument set to an empty string if not supplied
   if ( ! customError)
   {
      var customError = "";
   }
   // params is an optional argument set to an empty string if not supplied
   if ( ! params)
   {
      var params = "";
   }

   $(fieldSelector).blur(function() {
                     if ( ! isRequired && 0 == $(this).val().length)
                     {
                        // Field is not required and has no value, so don't post for validation
                        // In case there was previously an error and now no data, remove error
                        $(fieldSelector + '-error').hide();
                     }
                     else
                     {
                        // Now, only check this value if the dependent field has the correct value
                        if (dependentWhenValueIs == $(dependentFieldSelector).val())
                        {
                           // Field is required or it is not required (optional) but has data
                           $.post(includePath + "/classes/dsform/clientside-form-validation.php",
                           { type: dataType, value: $(this).val(), isrequired: isRequired, customerror: customError, parameters: params},
                           function(data) {
                              validationReturn(data, fieldSelector);
                           },
                           "json");
                        }
                     }

   });
}

function addWatchForFollowupField(watchedFieldSelector, watchedFieldType, valueToWatchFor, followupFieldSelector)
{
   if ("radio" == watchedFieldType)
   {
      // Check if the field should be showing now
      if (valueToWatchFor == $(watchedFieldSelector + ":checked").val())
      {
         // Show the follow-up question:
         $(followupFieldSelector).slideDown();
      }
      else
      {
         // Hide the follow-up question if it is currently visible
         $(followupFieldSelector + ":visible").slideUp();
      }

      // Add an event watch for showing it with user interaction
      $(watchedFieldSelector).click(function()
      {
         if (valueToWatchFor == $(watchedFieldSelector + ":checked").val())
         {
            // Show the follow-up question:
            $(followupFieldSelector).slideDown();
         }
         else
         {
            // Hide the follow-up question if it is currently visible
            $(followupFieldSelector + ":visible").slideUp();
            // We are taking the substr for the error DIV ID because it does not
            // contain the suffix '-followup'
            $(followupFieldSelector.substr(0,(followupFieldSelector.length - 9)) + '-error').hide();
         }
      });
   }
   else if ("checkbox" == watchedFieldType)
   {
      // Check if the field should be showing now
      // Look at all values and watch for the selected ones

      var initiallyShown = false;
      $(watchedFieldSelector + ":checked").each(function() {
         if (valueToWatchFor == $(this).val())
         {
            initiallyShown = true;
            return;
         }
      });

      if (initiallyShown)
      {
         // Show the follow-up question:
         $(followupFieldSelector).slideDown();
      }
      else
      {
         // Hide the follow-up question if it is currently visible
         $(followupFieldSelector + ":visible").slideUp();
      }


      // Add an event watch for showing it with user interaction
      $(watchedFieldSelector).click(function()
      {
         var shown = false;
         $(watchedFieldSelector + ":checked").each(function() {
            if (valueToWatchFor == $(this).val())
            {
               shown = true;
               return;
            }
         });

         if (shown)
         {
            // Show the follow-up question:
            $(followupFieldSelector).slideDown();
         }
         else
         {
            // Hide the follow-up question if it is currently visible
            $(followupFieldSelector + ":visible").slideUp();
            // We are taking the substr for the error DIV ID because it does not
            // contain the suffix '-followup'
            $(followupFieldSelector.substr(0,(followupFieldSelector.length - 9)) + '-error').hide();
         }
      });
   }
   else
   {
      // Check if the field should be showing now
      if (valueToWatchFor == $(watchedFieldSelector).val())
      {
         // Show the follow-up question:
         $(followupFieldSelector).slideDown();
      }

      // Add an event watch for showing it with user interaction
      $(watchedFieldSelector).blur(function()
      {
         if (valueToWatchFor == $(watchedFieldSelector).val())
         {
            // Show the follow-up question:
            $(followupFieldSelector).slideDown();
         }
         else
         {
            // Hide the follow-up question if it is currently visible
            $(followupFieldSelector + ":visible").slideUp();
            // We are taking the substr for the error DIV ID because it does not
            // contain the suffix '-followup'
            $(followupFieldSelector.substr(0,(followupFieldSelector.length - 9)) + '-error').hide();
         }
      });
   }
}

function addWatchForToggleGroup(watchedFieldSelector, watchedFieldType, valuesToWatchFor, toggleGroupSelector, eventToBind)
{
   if ("radio" == watchedFieldType)
   {
      // Check if the field should be showing now
      var matchFound = false;
      for (var i = 0; i < valuesToWatchFor.length; i++)
      {
         if (valuesToWatchFor[i] == $(watchedFieldSelector + ":checked").val())
         {
            // Show the field group
            $(toggleGroupSelector).show();
            matchFound = true;
            break;
         }
      }

      if ( ! matchFound)
      {
         // Hide the field group if it is currently visible
         $(toggleGroupSelector + ":visible").hide();
      }

      // Add an event watch for showing it with user interaction
      $(watchedFieldSelector).click(function()
      {
         var matchFound = false;
         for (var i = 0; i < valuesToWatchFor.length; i++)
         {
            if (valuesToWatchFor[i] == $(watchedFieldSelector + ":checked").val())
            {
               // Show the field group
               $(toggleGroupSelector).show();
               matchFound = true;
               break;
            }
         }

         if ( ! matchFound)
         {
            // Hide the field group if it is currently visible
            $(toggleGroupSelector + ":visible").hide();
         }

      });
   }
   else if ("text" == watchedFieldType ||
            "select" == watchedFieldType)
   {
      // Check if the field should be showing now
      var matchFound = false;
      for (var i = 0; i < valuesToWatchFor.length; i++)
      {
         if (valuesToWatchFor[i] == $(watchedFieldSelector).val())
         {
            // Show the field group
            $(toggleGroupSelector).show();
            matchFound = true;
            break;
         }
      }

      if ( ! matchFound)
      {
         // Hide the field group if it is currently visible
         $(toggleGroupSelector + ":visible").hide();
      }

      // Add an event watch for showing it with user interaction
      if ("change" == eventToBind)
      {
         $(watchedFieldSelector).change(function()
         {
            var matchFound = false;
            for (var i = 0; i < valuesToWatchFor.length; i++)
            {
               if (valuesToWatchFor[i] == $(watchedFieldSelector).val())
               {
                  // Show the field group
                  $(toggleGroupSelector).show();
                  matchFound = true;
                  break;
               }
            }

            if ( ! matchFound)
            {
               // Hide the field group if it is currently visible
               $(toggleGroupSelector + ":visible").hide();
            }

         });
      }
      else if ("keyup" == eventToBind)
      {
         $(watchedFieldSelector).keyup(function()
         {
            var matchFound = false;
            for (var i = 0; i < valuesToWatchFor.length; i++)
            {
               if (valuesToWatchFor[i] == $(watchedFieldSelector).val())
               {
                  // Show the field group
                  $(toggleGroupSelector).show();
                  matchFound = true;
                  break;
               }
            }

            if ( ! matchFound)
            {
               // Hide the field group if it is currently visible
               $(toggleGroupSelector + ":visible").hide();
            }

         });
      }
   }
}


