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

function addValidation(fieldSelector, dataType, isRequired, customError, params) {
   // 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 = "";
   }

   // If the dataType is a DATE we need to use the change() event instead of blur()
   if ("date" == dataType)
   {
      $(fieldSelector).change(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
                        {
                           // Field is required or it is not required (optional) but has data
                           $.post("/include/classes/dsform/clientside-form-validation.php",
                           { type: dataType, value: $(this).val(), isrequired: isRequired, customerror: customError, parameters: params},
                           function(data) {
                              validationReturn(data, fieldSelector);
                           },
                           "json");
                        }

      });
   }
   else
   {
      $(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
                        {
                           // Field is required or it is not required (optional) but has data
                           $.post("/include/classes/dsform/clientside-form-validation.php",
                           { type: dataType, value: $(this).val(), isrequired: isRequired, customerror: customError, parameters: params},
                           function(data) {
                              validationReturn(data, fieldSelector);
                           },
                           "json");
                        }

      });
   }
}

function addDependentValidation(fieldSelector, dataType, dependentFieldSelector, dependentWhenValueIs, isRequired, customError, params) {
   // 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("/include/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
   {
      // 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();
            }

         });
      }
   }
}


