Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7226 anupam.sin 1
/*
2
 * jQuery validation plug-in pre-1.5.2
3
 *
4
 * http://bassistance.de/jquery-plugins/jquery-plugin-validation/
5
 * http://docs.jquery.com/Plugins/Validation
6
 *
7
 * Copyright (c) 2006 - 2008 Jörn Zaefferer
8
 *
9
 * $Id: jquery.validate.js 6243 2009-02-19 11:40:49Z joern.zaefferer $
10
 *
11
 * Dual licensed under the MIT and GPL licenses:
12
 *   http://www.opensource.org/licenses/mit-license.php
13
 *   http://www.gnu.org/licenses/gpl.html
14
 */
15
 
16
(function($) {
17
 
18
  $.extend($.fn, {
19
    // http://docs.jquery.com/Plugins/Validation/validate
20
    validate: function( options ) {
21
 
22
      // if nothing is selected, return nothing; can't chain anyway
23
      if (!this.length) {
24
        options && options.debug && window.console && console.warn( "nothing selected, can't validate, returning nothing" );
25
        return;
26
      }
27
 
28
      // check if a validator for this form was already created
29
      var validator = $.data(this[0], 'validator');
30
      if ( validator ) {
31
        return validator;
32
      }
33
 
34
      validator = new $.validator( options, this[0] );
35
      $.data(this[0], 'validator', validator);
36
 
37
      if ( validator.settings.onsubmit ) {
38
 
39
        // allow suppresing validation by adding a cancel class to the submit button
40
        this.find("input, button").filter(".cancel").click(function() {
41
          validator.cancelSubmit = true;
42
        });
43
 
44
        // validate the form on submit
45
        this.submit( function( event ) {
46
          if ( validator.settings.debug )
47
            // prevent form submit to be able to see console output
48
            event.preventDefault();
49
 
50
          function handle() {
51
            if ( validator.settings.submitHandler ) {
52
              validator.settings.submitHandler.call( validator, validator.currentForm );
53
              return false;
54
            }
55
            return true;
56
          }
57
 
58
          // prevent submit for invalid forms or custom submit handlers
59
          if ( validator.cancelSubmit ) {
60
            validator.cancelSubmit = false;
61
            return handle();
62
          }
63
          if ( validator.form() ) {
64
            if ( validator.pendingRequest ) {
65
              validator.formSubmitted = true;
66
              return false;
67
            }
68
            return handle();
69
          } else {
70
            validator.focusInvalid();
71
            return false;
72
          }
73
        });
74
      }
75
 
76
      return validator;
77
    },
78
    // http://docs.jquery.com/Plugins/Validation/valid
79
    valid: function() {
80
      if ( $(this[0]).is('form')) {
81
        return this.validate().form();
82
      } else {
83
        var valid = false;
84
        var validator = $(this[0].form).validate();
85
        this.each(function() {
86
          valid |= validator.element(this);
87
        });
88
        return valid;
89
      }
90
    },
91
    // attributes: space seperated list of attributes to retrieve and remove
92
    removeAttrs: function(attributes) {
93
      var result = {},
94
      $element = this;
95
      $.each(attributes.split(/\s/), function(index, value) {
96
        result[value] = $element.attr(value);
97
        $element.removeAttr(value);
98
      });
99
      return result;
100
    },
101
    // http://docs.jquery.com/Plugins/Validation/rules
102
    rules: function(command, argument) {
103
      var element = this[0];
104
 
105
      if (command) {
106
        var settings = $.data(element.form, 'validator').settings;
107
        var staticRules = settings.rules;
108
        var existingRules = $.validator.staticRules(element);
109
        switch(command) {
110
          case "add":
111
            $.extend(existingRules, $.validator.normalizeRule(argument));
112
            staticRules[element.name] = existingRules;
113
            if (argument.messages)
114
              settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
115
            break;
116
          case "remove":
117
            if (!argument) {
118
              delete staticRules[element.name];
119
              return existingRules;
120
            }
121
            var filtered = {};
122
            $.each(argument.split(/\s/), function(index, method) {
123
              filtered[method] = existingRules[method];
124
              delete existingRules[method];
125
            });
126
            return filtered;
127
        }
128
      }
129
 
130
      var data = $.validator.normalizeRules(
131
        $.extend(
132
        {},
133
          $.validator.metadataRules(element),
134
          $.validator.classRules(element),
135
          $.validator.attributeRules(element),
136
          $.validator.staticRules(element)
137
          ), element);
138
 
139
      // make sure required is at front
140
      if (data.required) {
141
        var param = data.required;
142
        delete data.required;
143
        data = $.extend({
144
          required: param
145
        }, data);
146
      }
147
 
148
      return data;
149
    }
150
  });
151
 
152
  // Custom selectors
153
  $.extend($.expr[":"], {
154
    // http://docs.jquery.com/Plugins/Validation/blank
155
    blank: function(a) {
156
      return !$.trim(a.value);
157
    },
158
    // http://docs.jquery.com/Plugins/Validation/filled
159
    filled: function(a) {
160
      return !!$.trim(a.value);
161
    },
162
    // http://docs.jquery.com/Plugins/Validation/unchecked
163
    unchecked: function(a) {
164
      return !a.checked;
165
    }
166
  });
167
 
168
 
169
  $.format = function(source, params) {
170
    if ( arguments.length == 1 )
171
      return function() {
172
        var args = $.makeArray(arguments);
173
        args.unshift(source);
174
        return $.format.apply( this, args );
175
      };
176
    if ( arguments.length > 2 && params.constructor != Array  ) {
177
      params = $.makeArray(arguments).slice(1);
178
    }
179
    if ( params.constructor != Array ) {
180
      params = [ params ];
181
    }
182
    $.each(params, function(i, n) {
183
      source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
184
    });
185
    return source;
186
  };
187
 
188
  // constructor for validator
189
  $.validator = function( options, form ) {
190
    this.settings = $.extend( {}, $.validator.defaults, options );
191
    this.currentForm = form;
192
    this.init();
193
  };
194
 
195
  $.extend($.validator, {
196
 
197
    defaults: {
198
      messages: {},
199
      groups: {},
200
      rules: {},
201
      errorClass: "error",
202
      errorElement: "label",
203
      focusInvalid: true,
204
      errorContainer: $( [] ),
205
      errorLabelContainer: $( [] ),
206
      onsubmit: true,
207
      ignore: [],
208
      ignoreTitle: false,
209
      onfocusin: function(element) {
210
        this.lastActive = element;
211
 
212
        // hide error label and remove error class on focus if enabled
213
        if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
214
          this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
215
          this.errorsFor(element).hide();
216
        }
217
      },
218
      onfocusout: function(element) {
219
        if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) {
220
          this.element(element);
221
        }
222
      },
223
      onkeyup: function(element) {
224
        if ( element.name in this.submitted || element == this.lastElement ) {
225
          this.element(element);
226
        }
227
      },
228
      onclick: function(element) {
229
        if ( element.name in this.submitted )
230
          this.element(element);
231
      },
232
      highlight: function( element, errorClass ) {
233
        $( element ).addClass( errorClass );
234
      },
235
      unhighlight: function( element, errorClass ) {
236
        $( element ).removeClass( errorClass );
237
      }
238
    },
239
 
240
    // http://docs.jquery.com/Plugins/Validation/Validator/setDefaults
241
    setDefaults: function(settings) {
242
      $.extend( $.validator.defaults, settings );
243
    },
244
 
245
    messages: {
246
      required: "This field is required.",
247
      remote: "Please fix this field.",
248
      email: "Please enter a valid email address.",
249
      url: "Please enter a valid URL.",
250
      date: "Please enter a valid date.",
251
      dateISO: "Please enter a valid date (ISO).",
252
      dateDE: "Bitte geben Sie ein gültiges Datum ein.",
253
      number: "Please enter a valid number.",
254
      numberDE: "Bitte geben Sie eine Nummer ein.",
255
      digits: "Please enter only digits",
256
      creditcard: "Please enter a valid credit card number.",
257
      equalTo: "Please enter the same value again.",
258
      accept: "Please enter a value with a valid extension.",
259
      maxlength: $.format("Please enter no more than {0} characters."),
260
      minlength: $.format("Please enter at least {0} characters."),
261
      rangelength: $.format("Please enter a value between {0} and {1} characters long."),
262
      range: $.format("Please enter a value between {0} and {1}."),
263
      max: $.format("Please enter a value less than or equal to {0}."),
264
      min: $.format("Please enter a value greater than or equal to {0}.")
265
    },
266
 
267
    autoCreateRanges: false,
268
 
269
    prototype: {
270
 
271
      init: function() {
272
        this.labelContainer = $(this.settings.errorLabelContainer);
273
        this.errorContext = this.labelContainer.length && this.labelContainer || $(this.currentForm);
274
        this.containers = $(this.settings.errorContainer).add( this.settings.errorLabelContainer );
275
        this.submitted = {};
276
        this.valueCache = {};
277
        this.pendingRequest = 0;
278
        this.pending = {};
279
        this.invalid = {};
280
        this.reset();
281
 
282
        var groups = (this.groups = {});
283
        $.each(this.settings.groups, function(key, value) {
284
          $.each(value.split(/\s/), function(index, name) {
285
            groups[name] = key;
286
          });
287
        });
288
        var rules = this.settings.rules;
289
        $.each(rules, function(key, value) {
290
          rules[key] = $.validator.normalizeRule(value);
291
        });
292
 
293
        function delegate(event) {
294
          var validator = $.data(this[0].form, "validator");
295
          validator.settings["on" + event.type] && validator.settings["on" + event.type].call(validator, this[0] );
296
        }
297
        $(this.currentForm)
298
        .delegate("focusin focusout keyup", ":text, :password, :file, select, textarea", delegate)
299
        .delegate("click", ":radio, :checkbox", delegate);
300
 
301
        if (this.settings.invalidHandler)
302
          $(this.currentForm).bind("invalid-form.validate", this.settings.invalidHandler);
303
      },
304
 
305
      // http://docs.jquery.com/Plugins/Validation/Validator/form
306
      form: function() {
307
        this.checkForm();
308
        $.extend(this.submitted, this.errorMap);
309
        this.invalid = $.extend({}, this.errorMap);
310
        if (!this.valid())
311
          $(this.currentForm).triggerHandler("invalid-form", [this]);
312
        this.showErrors();
313
        return this.valid();
314
      },
315
 
316
      checkForm: function() {
317
        this.prepareForm();
318
        for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
319
          this.check( elements[i] );
320
        }
321
        return this.valid();
322
      },
323
 
324
      // http://docs.jquery.com/Plugins/Validation/Validator/element
325
      element: function( element ) {
326
        element = this.clean( element );
327
        this.lastElement = element;
328
        this.prepareElement( element );
329
        this.currentElements = $(element);
330
        var result = this.check( element );
331
        if ( result ) {
332
          delete this.invalid[element.name];
333
        } else {
334
          this.invalid[element.name] = true;
335
        }
336
        if ( !this.numberOfInvalids() ) {
337
          // Hide error containers on last error
338
          this.toHide = this.toHide.add( this.containers );
339
        }
340
        this.showErrors();
341
        return result;
342
      },
343
 
344
      // http://docs.jquery.com/Plugins/Validation/Validator/showErrors
345
      showErrors: function(errors) {
346
        if(errors) {
347
          // add items to error list and map
348
          $.extend( this.errorMap, errors );
349
          this.errorList = [];
350
          for ( var name in errors ) {
351
            this.errorList.push({
352
              message: errors[name],
353
              element: this.findByName(name)[0]
354
            });
355
          }
356
          // remove items from success list
357
          this.successList = $.grep( this.successList, function(element) {
358
            return !(element.name in errors);
359
          });
360
        }
361
        this.settings.showErrors
362
        ? this.settings.showErrors.call( this, this.errorMap, this.errorList )
363
        : this.defaultShowErrors();
364
      },
365
 
366
      // http://docs.jquery.com/Plugins/Validation/Validator/resetForm
367
      resetForm: function() {
368
        if ( $.fn.resetForm )
369
          $( this.currentForm ).resetForm();
370
        this.submitted = {};
371
        this.prepareForm();
372
        this.hideErrors();
373
        this.elements().removeClass( this.settings.errorClass );
374
      },
375
 
376
      numberOfInvalids: function() {
377
        return this.objectLength(this.invalid);
378
      },
379
 
380
      objectLength: function( obj ) {
381
        var count = 0;
382
        for ( var i in obj )
383
          count++;
384
        return count;
385
      },
386
 
387
      hideErrors: function() {
388
        this.addWrapper( this.toHide ).hide();
389
      },
390
 
391
      valid: function() {
392
        return this.size() == 0;
393
      },
394
 
395
      size: function() {
396
        return this.errorList.length;
397
      },
398
 
399
      focusInvalid: function() {
400
        if( this.settings.focusInvalid ) {
401
          try {
402
            $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []).filter(":visible").focus();
403
          } catch(e) {
404
          // ignore IE throwing errors when focusing hidden elements
405
          }
406
        }
407
      },
408
 
409
      findLastActive: function() {
410
        var lastActive = this.lastActive;
411
        return lastActive && $.grep(this.errorList, function(n) {
412
          return n.element.name == lastActive.name;
413
        }).length == 1 && lastActive;
414
      },
415
 
416
      elements: function() {
417
        var validator = this,
418
        rulesCache = {};
419
 
420
        // select all valid inputs inside the form (no submit or reset buttons)
421
        // workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
422
        return $([]).add(this.currentForm.elements)
423
        .filter(":input")
424
        .not(":submit, :reset, :image, [disabled]")
425
        .not( this.settings.ignore )
426
        .filter(function() {
427
          !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
428
 
429
          // select only the first element for each name, and only those with rules specified
430
          if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
431
            return false;
432
 
433
          rulesCache[this.name] = true;
434
          return true;
435
        });
436
      },
437
 
438
      clean: function( selector ) {
439
        return $( selector )[0];
440
      },
441
 
442
      errors: function() {
443
        return $( this.settings.errorElement + "." + this.settings.errorClass, this.errorContext );
444
      },
445
 
446
      reset: function() {
447
        this.successList = [];
448
        this.errorList = [];
449
        this.errorMap = {};
450
        this.toShow = $([]);
451
        this.toHide = $([]);
452
        this.formSubmitted = false;
453
        this.currentElements = $([]);
454
      },
455
 
456
      prepareForm: function() {
457
        this.reset();
458
        this.toHide = this.errors().add( this.containers );
459
      },
460
 
461
      prepareElement: function( element ) {
462
        this.reset();
463
        this.toHide = this.errorsFor(element);
464
      },
465
 
466
      check: function( element ) {
467
        element = this.clean( element );
468
 
469
        // if radio/checkbox, validate first element in group instead
470
        if (this.checkable(element)) {
471
          element = this.findByName( element.name )[0];
472
        }
473
 
474
        var rules = $(element).rules();
475
        var dependencyMismatch = false;
476
        for( method in rules ) {
477
          var rule = {
478
            method: method,
479
            parameters: rules[method]
480
          };
481
          try {
482
            var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );
483
 
484
            // if a method indicates that the field is optional and therefore valid,
485
            // don't mark it as valid when there are no other rules
486
            if ( result == "dependency-mismatch" ) {
487
              dependencyMismatch = true;
488
              continue;
489
            }
490
            dependencyMismatch = false;
491
 
492
            if ( result == "pending" ) {
493
              this.toHide = this.toHide.not( this.errorsFor(element) );
494
              return;
495
            }
496
 
497
            if( !result ) {
498
              this.formatAndAdd( element, rule );
499
              return false;
500
            }
501
          } catch(e) {
502
            this.settings.debug && window.console && console.log("exception occured when checking element " + element.id
503
              + ", check the '" + rule.method + "' method");
504
            throw e;
505
          }
506
        }
507
        if (dependencyMismatch)
508
          return;
509
        if ( this.objectLength(rules) )
510
          this.successList.push(element);
511
        return true;
512
      },
513
 
514
      // return the custom message for the given element and validation method
515
      // specified in the element's "messages" metadata
516
      customMetaMessage: function(element, method) {
517
        if (!$.metadata)
518
          return;
519
 
520
        var meta = this.settings.meta
521
        ? $(element).metadata()[this.settings.meta]
522
        : $(element).metadata();
523
 
524
        return meta && meta.messages && meta.messages[method];
525
      },
526
 
527
      // return the custom message for the given element name and validation method
528
      customMessage: function( name, method ) {
529
        var m = this.settings.messages[name];
530
        return m && (m.constructor == String
531
          ? m
532
          : m[method]);
533
      },
534
 
535
      // return the first defined argument, allowing empty strings
536
      findDefined: function() {
537
        for(var i = 0; i < arguments.length; i++) {
538
          if (arguments[i] !== undefined)
539
            return arguments[i];
540
        }
541
        return undefined;
542
      },
543
 
544
      defaultMessage: function( element, method) {
545
        return this.findDefined(
546
          this.customMessage( element.name, method ),
547
          this.customMetaMessage( element, method ),
548
          // title is never undefined, so handle empty string as undefined
549
          !this.settings.ignoreTitle && element.title || undefined,
550
          $.validator.messages[method],
551
          "<strong>Warning: No message defined for " + element.name + "</strong>"
552
          );
553
      },
554
 
555
      formatAndAdd: function( element, rule ) {
556
        var message = this.defaultMessage( element, rule.method );
557
        if ( typeof message == "function" )
558
          message = message.call(this, rule.parameters, element);
559
        this.errorList.push({
560
          message: message,
561
          element: element
562
        });
563
        this.errorMap[element.name] = message;
564
        this.submitted[element.name] = message;
565
      },
566
 
567
      addWrapper: function(toToggle) {
568
        if ( this.settings.wrapper )
569
          toToggle = toToggle.add( toToggle.parents( this.settings.wrapper ) );
570
        return toToggle;
571
      },
572
 
573
      defaultShowErrors: function() {
574
        for ( var i = 0; this.errorList[i]; i++ ) {
575
          var error = this.errorList[i];
576
          this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass );
577
          this.showLabel( error.element, error.message );
578
        }
579
        if( this.errorList.length ) {
580
          this.toShow = this.toShow.add( this.containers );
581
        }
582
        if (this.settings.success) {
583
          for ( var i = 0; this.successList[i]; i++ ) {
584
            this.showLabel( this.successList[i] );
585
          }
586
        }
587
        if (this.settings.unhighlight) {
588
          for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
589
            this.settings.unhighlight.call( this, elements[i], this.settings.errorClass );
590
          }
591
        }
592
        this.toHide = this.toHide.not( this.toShow );
593
        this.hideErrors();
594
        this.addWrapper( this.toShow ).show();
595
      },
596
 
597
      validElements: function() {
598
        return this.currentElements.not(this.invalidElements());
599
      },
600
 
601
      invalidElements: function() {
602
        return $(this.errorList).map(function() {
603
          return this.element;
604
        });
605
      },
606
 
607
      showLabel: function(element, message) {
608
        var label = this.errorsFor( element );
609
        if ( label.length ) {
610
          // refresh error/success class
611
          label.removeClass().addClass( this.settings.errorClass );
612
 
613
          // check if we have a generated label, replace the message then
614
          label.attr("generated") && label.html(message);
615
        } else {
616
          // create label
617
          label = $("<" + this.settings.errorElement + "/>")
618
          .attr({
619
            "for":  this.idOrName(element),
620
            generated: true
621
          })
622
          .addClass(this.settings.errorClass)
623
          .html(message || "");
624
          if ( this.settings.wrapper ) {
625
            // make sure the element is visible, even in IE
626
            // actually showing the wrapped element is handled elsewhere
627
            label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent();
628
          }
629
          if ( !this.labelContainer.append(label).length )
630
            this.settings.errorPlacement
631
            ? this.settings.errorPlacement(label, $(element) )
632
            : label.insertAfter(element);
633
        }
634
        if ( !message && this.settings.success ) {
635
          label.text("");
636
          typeof this.settings.success == "string"
637
          ? label.addClass( this.settings.success )
638
          : this.settings.success( label );
639
        }
640
        this.toShow = this.toShow.add(label);
641
      },
642
 
643
      errorsFor: function(element) {
644
        return this.errors().filter("[for='" + this.idOrName(element) + "']");
645
      },
646
 
647
      idOrName: function(element) {
648
        return this.groups[element.name] || (this.checkable(element) ? element.name : element.id || element.name);
649
      },
650
 
651
      checkable: function( element ) {
652
        return /radio|checkbox/i.test(element.type);
653
      },
654
 
655
      findByName: function( name ) {
656
        // select by name and filter by form for performance over form.find("[name=...]")
657
        var form = this.currentForm;
658
        return $(document.getElementsByName(name)).map(function(index, element) {
659
          return element.form == form && element.name == name && element  || null;
660
        });
661
      },
662
 
663
      getLength: function(value, element) {
664
        switch( element.nodeName.toLowerCase() ) {
665
          case 'select':
666
            return $("option:selected", element).length;
667
          case 'input':
668
            if( this.checkable( element) )
669
              return this.findByName(element.name).filter(':checked').length;
670
        }
671
        return value.length;
672
      },
673
 
674
      depend: function(param, element) {
675
        return this.dependTypes[typeof param]
676
        ? this.dependTypes[typeof param](param, element)
677
        : true;
678
      },
679
 
680
      dependTypes: {
681
        "boolean": function(param, element) {
682
          return param;
683
        },
684
        "string": function(param, element) {
685
          return !!$(param, element.form).length;
686
        },
687
        "function": function(param, element) {
688
          return param(element);
689
        }
690
      },
691
 
692
      optional: function(element) {
693
        return !$.validator.methods.required.call(this, $.trim(element.value), element) && "dependency-mismatch";
694
      },
695
 
696
      startRequest: function(element) {
697
        if (!this.pending[element.name]) {
698
          this.pendingRequest++;
699
          this.pending[element.name] = true;
700
        }
701
      },
702
 
703
      stopRequest: function(element, valid) {
704
        this.pendingRequest--;
705
        // sometimes synchronization fails, make sure pendingRequest is never < 0
706
        if (this.pendingRequest < 0)
707
          this.pendingRequest = 0;
708
        delete this.pending[element.name];
709
        if ( valid && this.pendingRequest == 0 && this.formSubmitted && this.form() ) {
710
          $(this.currentForm).submit();
711
        } else if (!valid && this.pendingRequest == 0 && this.formSubmitted) {
712
          $(this.currentForm).triggerHandler("invalid-form", [this]);
713
        }
714
      },
715
 
716
      previousValue: function(element) {
717
        return $.data(element, "previousValue") || $.data(element, "previousValue", previous = {
718
          old: null,
719
          valid: true,
720
          message: this.defaultMessage( element, "remote" )
721
        });
722
      }
723
 
724
    },
725
 
726
    classRuleSettings: {
727
      required: {
728
        required: true
729
      },
730
      email: {
731
        email: true
732
      },
733
      url: {
734
        url: true
735
      },
736
      date: {
737
        date: true
738
      },
739
      dateISO: {
740
        dateISO: true
741
      },
742
      dateDE: {
743
        dateDE: true
744
      },
745
      number: {
746
        number: true
747
      },
748
      numberDE: {
749
        numberDE: true
750
      },
751
      digits: {
752
        digits: true
753
      },
754
      creditcard: {
755
        creditcard: true
756
      }
757
    },
758
 
759
    addClassRules: function(className, rules) {
760
      className.constructor == String ?
761
      this.classRuleSettings[className] = rules :
762
      $.extend(this.classRuleSettings, className);
763
    },
764
 
765
    classRules: function(element) {
766
      var rules = {};
767
      var classes = $(element).attr('class');
768
      classes && $.each(classes.split(' '), function() {
769
        if (this in $.validator.classRuleSettings) {
770
          $.extend(rules, $.validator.classRuleSettings[this]);
771
        }
772
      });
773
      return rules;
774
    },
775
 
776
    attributeRules: function(element) {
777
      var rules = {};
778
      var $element = $(element);
779
 
780
      for (method in $.validator.methods) {
781
        var value = $element.attr(method);
782
        if (value) {
783
          rules[method] = value;
784
        }
785
      }
786
 
787
      // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
788
      if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
789
        delete rules.maxlength;
790
      }
791
 
792
      return rules;
793
    },
794
 
795
    metadataRules: function(element) {
796
      if (!$.metadata) return {};
797
 
798
      var meta = $.data(element.form, 'validator').settings.meta;
799
      return meta ?
800
      $(element).metadata()[meta] :
801
      $(element).metadata();
802
    },
803
 
804
    staticRules: function(element) {
805
      var rules = {};
806
      var validator = $.data(element.form, 'validator');
807
      if (validator.settings.rules) {
808
        rules = $.validator.normalizeRule(validator.settings.rules[element.name]) || {};
809
      }
810
      return rules;
811
    },
812
 
813
    normalizeRules: function(rules, element) {
814
      // handle dependency check
815
      $.each(rules, function(prop, val) {
816
        // ignore rule when param is explicitly false, eg. required:false
817
        if (val === false) {
818
          delete rules[prop];
819
          return;
820
        }
821
        if (val.param || val.depends) {
822
          var keepRule = true;
823
          switch (typeof val.depends) {
824
            case "string":
825
              keepRule = !!$(val.depends, element.form).length;
826
              break;
827
            case "function":
828
              keepRule = val.depends.call(element, element);
829
              break;
830
          }
831
          if (keepRule) {
832
            rules[prop] = val.param !== undefined ? val.param : true;
833
          } else {
834
            delete rules[prop];
835
          }
836
        }
837
      });
838
 
839
      // evaluate parameters
840
      $.each(rules, function(rule, parameter) {
841
        rules[rule] = $.isFunction(parameter) ? parameter(element) : parameter;
842
      });
843
 
844
      // clean number parameters
845
      $.each(['minlength', 'maxlength', 'min', 'max'], function() {
846
        if (rules[this]) {
847
          rules[this] = Number(rules[this]);
848
        }
849
      });
850
      $.each(['rangelength', 'range'], function() {
851
        if (rules[this]) {
852
          rules[this] = [Number(rules[this][0]), Number(rules[this][1])];
853
        }
854
      });
855
 
856
      if ($.validator.autoCreateRanges) {
857
        // auto-create ranges
858
        if (rules.min && rules.max) {
859
          rules.range = [rules.min, rules.max];
860
          delete rules.min;
861
          delete rules.max;
862
        }
863
        if (rules.minlength && rules.maxlength) {
864
          rules.rangelength = [rules.minlength, rules.maxlength];
865
          delete rules.minlength;
866
          delete rules.maxlength;
867
        }
868
      }
869
 
870
      // To support custom messages in metadata ignore rule methods titled "messages"
871
      if (rules.messages) {
872
        delete rules.messages
873
      }
874
 
875
      return rules;
876
    },
877
 
878
    // Converts a simple string to a {string: true} rule, e.g., "required" to {required:true}
879
    normalizeRule: function(data) {
880
      if( typeof data == "string" ) {
881
        var transformed = {};
882
        $.each(data.split(/\s/), function() {
883
          transformed[this] = true;
884
        });
885
        data = transformed;
886
      }
887
      return data;
888
    },
889
 
890
    // http://docs.jquery.com/Plugins/Validation/Validator/addMethod
891
    addMethod: function(name, method, message) {
892
      $.validator.methods[name] = method;
893
      $.validator.messages[name] = message;
894
      if (method.length < 3) {
895
        $.validator.addClassRules(name, $.validator.normalizeRule(name));
896
      }
897
    },
898
 
899
    methods: {
900
 
901
      // http://docs.jquery.com/Plugins/Validation/Methods/required
902
      required: function(value, element, param) {
903
        // check if dependency is met
904
        if ( !this.depend(param, element) )
905
          return "dependency-mismatch";
906
        switch( element.nodeName.toLowerCase() ) {
907
          case 'select':
908
            var options = $("option:selected", element);
909
            return options.length > 0 && ( element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
910
          case 'input':
911
            if ( this.checkable(element) )
912
              return this.getLength(value, element) > 0;
913
          default:
914
            return $.trim(value).length > 0;
915
        }
916
      },
917
 
918
      // http://docs.jquery.com/Plugins/Validation/Methods/remote
919
      remote: function(value, element, param) {
920
        if ( this.optional(element) )
921
          return "dependency-mismatch";
922
 
923
        var previous = this.previousValue(element);
924
 
925
        if (!this.settings.messages[element.name] )
926
          this.settings.messages[element.name] = {};
927
        this.settings.messages[element.name].remote = typeof previous.message == "function" ? previous.message(value) : previous.message;
928
 
929
        param = typeof param == "string" && {
930
          url:param
931
        } || param;
932
 
933
        if ( previous.old !== value ) {
934
          previous.old = value;
935
          var validator = this;
936
          this.startRequest(element);
937
          var data = {};
938
          data[element.name] = value;
939
          $.ajax($.extend(true, {
940
            url: param,
941
            mode: "abort",
942
            port: "validate" + element.name,
943
            dataType: "json",
944
            data: data,
945
            success: function(response) {
946
              if ( response ) {
947
                var submitted = validator.formSubmitted;
948
                validator.prepareElement(element);
949
                validator.formSubmitted = submitted;
950
                validator.successList.push(element);
951
                validator.showErrors();
952
              } else {
953
                var errors = {};
954
                errors[element.name] =  response || validator.defaultMessage( element, "remote" );
955
                validator.showErrors(errors);
956
              }
957
              previous.valid = response;
958
              validator.stopRequest(element, response);
959
            }
960
          }, param));
961
          return "pending";
962
        } else if( this.pending[element.name] ) {
963
          return "pending";
964
        }
965
        return previous.valid;
966
      },
967
 
968
      // http://docs.jquery.com/Plugins/Validation/Methods/minlength
969
      minlength: function(value, element, param) {
970
        return this.optional(element) || this.getLength($.trim(value), element) >= param;
971
      },
972
 
973
      // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
974
      maxlength: function(value, element, param) {
975
        return this.optional(element) || this.getLength($.trim(value), element) <= param;
976
      },
977
 
978
      // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
979
      rangelength: function(value, element, param) {
980
        var length = this.getLength($.trim(value), element);
981
        return this.optional(element) || ( length >= param[0] && length <= param[1] );
982
      },
983
 
984
      // http://docs.jquery.com/Plugins/Validation/Methods/min
985
      min: function( value, element, param ) {
986
        return this.optional(element) || value >= param;
987
      },
988
 
989
      // http://docs.jquery.com/Plugins/Validation/Methods/max
990
      max: function( value, element, param ) {
991
        return this.optional(element) || value <= param;
992
      },
993
 
994
      // http://docs.jquery.com/Plugins/Validation/Methods/range
995
      range: function( value, element, param ) {
996
        return this.optional(element) || ( value >= param[0] && value <= param[1] );
997
      },
998
 
999
      // http://docs.jquery.com/Plugins/Validation/Methods/email
1000
      email: function(value, element) {
1001
        // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
1002
        return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
1003
      },
1004
 
1005
      // http://docs.jquery.com/Plugins/Validation/Methods/url
1006
      url: function(value, element) {
1007
        // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
1008
        return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
1009
      },
1010
 
1011
      // http://docs.jquery.com/Plugins/Validation/Methods/date
1012
      date: function(value, element) {
1013
        return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
1014
      },
1015
 
1016
      // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
1017
      dateISO: function(value, element) {
1018
        return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
1019
      },
1020
 
1021
      // http://docs.jquery.com/Plugins/Validation/Methods/dateDE
1022
      dateDE: function(value, element) {
1023
        return this.optional(element) || /^\d\d?\.\d\d?\.\d\d\d?\d?$/.test(value);
1024
      },
1025
 
1026
      // http://docs.jquery.com/Plugins/Validation/Methods/number
1027
      number: function(value, element) {
1028
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
1029
      },
1030
 
1031
      // http://docs.jquery.com/Plugins/Validation/Methods/numberDE
1032
      numberDE: function(value, element) {
1033
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
1034
      },
1035
 
1036
      // http://docs.jquery.com/Plugins/Validation/Methods/digits
1037
      digits: function(value, element) {
1038
        return this.optional(element) || /^\d+$/.test(value);
1039
      },
1040
 
1041
      // http://docs.jquery.com/Plugins/Validation/Methods/creditcard
1042
      // based on http://en.wikipedia.org/wiki/Luhn
1043
      creditcard: function(value, element) {
1044
        if ( this.optional(element) )
1045
          return "dependency-mismatch";
1046
        // accept only digits and dashes
1047
        if (/[^0-9-]+/.test(value))
1048
          return false;
1049
        var nCheck = 0,
1050
        nDigit = 0,
1051
        bEven = false;
1052
 
1053
        value = value.replace(/\D/g, "");
1054
 
1055
        for (n = value.length - 1; n >= 0; n--) {
1056
          var cDigit = value.charAt(n);
1057
          var nDigit = parseInt(cDigit, 10);
1058
          if (bEven) {
1059
            if ((nDigit *= 2) > 9)
1060
              nDigit -= 9;
1061
          }
1062
          nCheck += nDigit;
1063
          bEven = !bEven;
1064
        }
1065
 
1066
        return (nCheck % 10) == 0;
1067
      },
1068
 
1069
      // http://docs.jquery.com/Plugins/Validation/Methods/accept
1070
      accept: function(value, element, param) {
1071
        param = typeof param == "string" ? param : "png|jpe?g|gif";
1072
        return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));
1073
      },
1074
 
1075
      // http://docs.jquery.com/Plugins/Validation/Methods/equalTo
1076
      equalTo: function(value, element, param) {
1077
        return value == $(param).val();
1078
      }
1079
 
1080
    }
1081
 
1082
  });
1083
 
1084
})(jQuery);
1085
 
1086
// ajax mode: abort
1087
// usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
1088
// if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() 
1089
 
1090
(function($) {
1091
  var ajax = $.ajax;
1092
  var pendingRequests = {};
1093
  $.ajax = function(settings) {
1094
    // create settings for compatibility with ajaxSetup
1095
    settings = $.extend(settings, $.extend({}, $.ajaxSettings, settings));
1096
    var port = settings.port;
1097
    if (settings.mode == "abort") {
1098
      if ( pendingRequests[port] ) {
1099
        pendingRequests[port].abort();
1100
      }
1101
      return (pendingRequests[port] = ajax.apply(this, arguments));
1102
    }
1103
    return ajax.apply(this, arguments);
1104
  };
1105
})(jQuery);
1106
 
1107
// provides cross-browser focusin and focusout events
1108
// IE has native support, in other browsers, use event caputuring (neither bubbles)
1109
 
1110
// provides delegate(type: String, delegate: Selector, handler: Callback) plugin for easier event delegation
1111
// handler is only called when $(event.target).is(delegate), in the scope of the jquery-object for event.target 
1112
 
1113
// provides triggerEvent(type: String, target: Element) to trigger delegated events
1114
 
1115
(function($) {
1116
  $.each({
1117
    focus: 'focusin',
1118
    blur: 'focusout'
1119
  }, function( original, fix ){
1120
    $.event.special[fix] = {
1121
      setup:function() {
1122
        if ( $.browser.msie ) return false;
1123
        this.addEventListener( original, $.event.special[fix].handler, true );
1124
      },
1125
      teardown:function() {
1126
        if ( $.browser.msie ) return false;
1127
        this.removeEventListener( original,
1128
          $.event.special[fix].handler, true );
1129
      },
1130
      handler: function(e) {
1131
        arguments[0] = $.event.fix(e);
1132
        arguments[0].type = fix;
1133
        return $.event.handle.apply(this, arguments);
1134
      }
1135
    };
1136
  });
1137
  $.extend($.fn, {
1138
    delegate: function(type, delegate, handler) {
1139
      return this.bind(type, function(event) {
1140
        var target = $(event.target);
1141
        if (target.is(delegate)) {
1142
          return handler.apply(target, arguments);
1143
        }
1144
      });
1145
    },
1146
    triggerEvent: function(type, target) {
1147
      return this.triggerHandler(type, [$.event.fix({
1148
        type: type,
1149
        target: target
1150
      })]);
1151
    }
1152
  })
1153
})(jQuery);