(function($) {

  $.fn.prefill = function()
  {
    return this.each(function()
    {
      $(this).attr('prefillvalue',$(this).val()).focus(function() {($(this).val() == $(this).attr('prefillvalue')) ? $(this).val("") : null;}).blur(function() {($(this).val() == "") ? $(this).val($(this).attr('prefillvalue')) : null;}).blur();
    });
  }

  $.fn.form = function()
  {
    return this.each(function()
    {
      var _form = $(this);

      _form.find('div.submit input[type=submit]').each(function () {
        $(this).replaceWith('<span class="submit small-button">' + $(this).val() + '</span>');
      });
      _form.find('div.submit span.small-button').button();

      _form.find('.submit').click(function()
      {
        var _isvalid = true;

        _form.find('input:file').keyup();

        _form.find(':input[isrequiredfieldvalid],select[isrequiredfieldvalid]').each(function()
        {
          if($(this).attr('isrequiredfieldvalid') == 'false')
          {
            $('div.error .required-field-error-'+$(this).attr('name')).css({display:'block'});
            _isvalid = false;
          }
          else
          {
            $('div.error .required-field-error-'+$(this).attr('name')).css({display:'none'});
          }
        });

        _form.find(':input[isrequiredemailvalid]').each(function()
        {
          if($(this).attr('isrequiredemailvalid') == 'false')
          {
            $('div.error .required-email-address-'+$(this).attr('name')).css({display:'block'});
            _isvalid = false;
          }
          else
          {
            $('div.error .required-email-address-'+$(this).attr('name')).css({display:'none'});
          }
        });

        _form.find(':input[isrequiredmatchvalid]').each(function()
        {
          if($(this).attr('isrequiredmatchvalid') == 'false')
          {
            $('div.error .required-match-'+$(this).attr('name')).css({display:'block'});
            _isvalid = false;
          }
          else
          {
            $('div.error .required-match-'+$(this).attr('name')).css({display:'none'});
          }
        });

        _form.find(':input[isrequiredlengthvalid]').each(function()
        {
          if($(this).attr('isrequiredlengthvalid') == 'false')
          {
            $('div.error .required-length-'+$(this).attr('name')).css({display:'block'});
            _isvalid = false;
          }
          else
          {
            $('div.error .required-length-'+$(this).attr('name')).css({display:'none'});
          }
        });

        if(_isvalid)
        {
          //fncAjax(_form.find('.submit'));   //add parameter by wkkang 2009.01.13
          $(this).parents('form').submit();
        }
        else
        {
          //$('html,body').animate({scrollTop:_form.offset().top},500,"linear");
        }
      });

      _form.find('.reset').click(function()
      {
        _form.find(':input').each(function()
        {
          if(this.type == 'text' || this.type == 'password' || this.tagName.toLowerCase() == 'textarea')
          {
            if(typeof($(this).attr('prefillvalue')) != 'undefined')
            {
              $(this).val($(this).attr('prefillvalue'));
            }
            else
            {
              $(this).val("");
            }
          }

          if(this.type == 'checkbox' || this.type == 'radio')
          {
            this.checked = false;
          }

          if(this.tagName.toLowerCase() == 'select')
          {
            this.selectedIndex = 0;
          }
        });

        $('select#attachments').change();
      });

      _form.find('.cancel').click(function()
      {
        $(this).parents('form').css({display:'none'});
        $(this).parents('div:first').find('span.toggle').css({display:'block'});
        $(this).parents('div:first').find('div.info').css({display:'block'});

        $(this).parents('div.edit-avatar').css({height:120});

        $(this).parents('form').find('div.error span').css({display:'none'});

        _form.find(':input').each(function()
        {
          if(this.type == 'text' || this.type == 'password' || this.tagName.toLowerCase() == 'textarea')
          {
            if(typeof($(this).attr('prefillvalue')) != 'undefined')
            {
              $(this).val($(this).attr('prefillvalue'));
            }
            else if(typeof($(this).attr('defaultvalue')) != 'undefined')
            {
              $(this).val($(this).attr('defaultvalue')).keyup();
            }
            else
            {
              $(this).val("");
            }
          }

          if(this.type == 'checkbox' || this.type == 'radio')
          {
            if($(this).attr('defaultvalue') == 'true')
            {
              this.checked = true;
            }
            else
            {
              this.checked = false;
            }
          }

          if(this.tagName.toLowerCase() == 'select')
          {
            this.selectedIndex = 0;
          }
        });

        $('select#attachments').change();
      });

      _form.find('textarea').each(function()
      {
        if(typeof($(this).attr('maxlength')) != 'undefined')
        {
          if($(this).attr('maxlength') != '')
          {
            $(this).keyup(function()
            {
              if($(this).val().length > $(this).attr('maxlength'))
              {
                $(this).val($(this).val().substr(0,$(this).attr('maxlength')));
              }
            });
          }
        }
      });

      _form.find('input:text').each(function()
      {
        $(this).attr('defaultvalue',$(this).val());
      });

      _form.find('input:checkbox,input:radio').each(function()
      {
        $(this).attr('defaultvalue',this.checked);
      });

      _form.find('select#attachments').each(function()
      {
        var _attachments = $(this);

        _attachments.change(function()
        {
          $('input:file').css({display:'none'});
          $('input:file:lt('+$(this).val()+')').css({display:'block'});
        });

        _attachments.change();
      });

      _form.find('input.required-field,textarea.required-field').each(function()
      {
        var _input = $(this);

        _input.keyup(function()
        {
          if($.trim(_input.val()) == "" || $.trim(_input.val()) == _input.attr('prefillvalue'))
          {
            _input.attr('isrequiredfieldvalid','false');
          }
          else
          {
            _input.attr('isrequiredfieldvalid','true');
          }
        });

        _input.keyup();
      });

      _form.find('select.required-field').each(function()
      {
        var _select = $(this);

        _select.change(function()
        {
          if(_select.val() == 'null')
          {
            _select.attr('isrequiredfieldvalid','false');
          }
          else
          {
            _select.attr('isrequiredfieldvalid','true');
          }
        });

        _select.change();
      });

      _form.find(':checkbox.required-field').each(function()
      {
        var _checkbox = $(this);

        _checkbox.click(function()
        {
          if(!this.checked)
          {
            _checkbox.attr('isrequiredfieldvalid','false');
          }
          else
          {
            _checkbox.attr('isrequiredfieldvalid','true');
          }
        });

        _checkbox.attr('checked','').attr('isrequiredfieldvalid','false');
      });

      _form.find('input.required-email-address').each(function()
      {
        var _input = $(this);
        var _emailRegex = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/i;

        _input.keyup(function()
        {
          if(!_emailRegex.test($(this).val()))
          {
            _input.attr('isrequiredemailvalid','false');
          }
          else
          {
            _input.attr('isrequiredemailvalid','true');
          }
        });

        _input.keyup();
      });

      _form.find('input.match').each(function()
      {
        var _input = $(this);

        _input.keyup(function()
        {
          var _match = $(this).val();
          var _isvalid = true;

          $(this).parents('form:first').find('.match').each(function()
          {
            ($(this).val() != _match) ? _isvalid = false : null;
          });

          if(_isvalid)
          {
            $(this).parents('form:first').find('.match').attr('isrequiredmatchvalid','true');
          }
          else
          {
            $(this).parents('form:first').find('.match').attr('isrequiredmatchvalid','false');
          }
        });
      });

      _form.find('input.length').each(function()
      {
        var _input = $(this);
        var _length = _input.attr('title');

        _input.attr('title','');

        _input.keyup(function()
        {
          if(_input.val().length < _length)
          {
            _input.attr('isrequiredlengthvalid','false');
          }
          else
          {
            _input.attr('isrequiredlengthvalid','true');
          }
        });
      });
    });
  }

})(jQuery);

