function __FT_ErrorHandler(element, error)
{
   if (typeof(originalBorder[$(element).attr('name')]) == "undefined")
   {
      originalBorder[$(element).attr('name')] = new Array();
      originalBorder[$(element).attr('name')] = $(element).css('border-top-color');
      $(element).css('border-color','#ff0000');
   }
}
function __FT_CompleteHandler(element)
{
   if (typeof(originalBorder[$(element).attr('name')]) != "undefined")
   {
      $(element).css('border-color',originalBorder[$(element).attr('name')]);
      delete(originalBorder[$(element).attr('name')]);
   }
   
   return false;
}
$.fn.formTool = function(optionParm)
{
   options = {
// These vars are for matching functions to the elements
      'classNotEmpty': '__FT_NotEmpty',
      'classNotDefault': '__FT_NotDefault',
      'classIsMail': '__FT_IsMail',
//      'classIsHttp': '',
//      'classIsPhone': '',
//      'classIsDate': '',
//      'classIsTime': '',
// These vars are for applying functions to the buttons
      'classClearForm': '',
      'classSubmitForm': '__FT_SubmitForm',
// These vars are for calling functions after specific operations
      'functionOnStart': '',
      'functionOnComplete': '',
      'functionOnFieldStart': '',
      'functionOnFieldComplete': __FT_CompleteHandler,
      'functionOnFieldError': __FT_ErrorHandler,
      'functionOnFocus': '',
      'functionOnBlur': ''
   };
   $.extend(options,optionParm);
   
   formArray = new Array();
   originalBorder = new Array();
   $('input',this).each(
      function()
      {   
         if ($(this).attr('value') != undefined)
            formArray[$(this).attr('name')] = $(this).attr('value');
         else
            formArray[$(this).attr('name')] = '';
      }
   );
      
   actualForm = this;
   if (options['classClearForm'] != '')
      $('.'+options['classClearForm'],this).click(
         function()
         {
            $('input[@type=checkbox]',actualForm).each(
               function()
               {
                  $(this).removeAttr('checked');
               }
            );
            $(":radio",actualForm).val([]);
            $('input',actualForm).each(
               function()
               {
                  $(this).attr('value',formArray[$(this).attr('name')]);
                  if (typeof(originalBorder[$(this).attr('name')]) != "undefined")
                  {
                     $(this).css('border-color',originalBorder[$(this).attr('name')]);
                     delete(originalBorder[$(this).attr('name')]);
                  }
               }
            );
            $('textarea',actualForm).each(
               function()
               {   
                  $(this).val('');
               }
            );
         }
      );
   $('.'+options['classSubmitForm'],this).click(
      function()
      {
         error = false;
      
         if (options['functionOnStart'] != '')
            options['functionOnStart'](actualForm);

         $('input,textarea,select',actualForm).each(
            function()
            {
               if (options['functionOnFieldStart'] != '')
                  options['functionOnFieldStart'](this);
               if ($(this).hasClass(options['classNotEmpty']))
               {
                  if (typeof($(this).attr('value')) == 'undefined' || $(this).attr('value') == '')
                  {
                     options['functionOnFieldError'](this, 'empty');
                     error = true;
                  }
               }
               if ($(this).hasClass(options['classNotDefault']))
               {
                  if ($(this).attr('value') == formArray[$(this).attr('name')])
                  {
                     options['functionOnFieldError'](this, 'default');
                     error = true;
                  }
               }
               if ($(this).hasClass(options['classIsMail']))
               {
                  if ($(this).attr('value') != undefined && !$(this).attr('value').match(/[a-z\d]+(?:[\-\.\_][a-z\d]+)*[a-z\d]+@[\w\d]+(?:[-\.][a-z\d][a-z\d\-]*[a-z\d])*[a-z\d]+\.([a-z]{2,4})/))
                  {
                     options['functionOnFieldError'](this, 'mail');
                     error = true;
                  }
               }
               if (error != true && options['functionOnFieldComplete'] != '')
                  options['functionOnFieldComplete'](this);
            }
         );

         if (options['functionOnComplete'] != '')
            options['functionOnComplete'](actualForm);
         
         if (error == true)
         {
            error = false;
            alert('Bitte überprüfen Sie, ob das Formular korrekt ausgefüllt ist.');
            return false;
         }
         else
         {
            actualForm.submit();
         }
      }
   );
};
