Filter:   InfoImg
download CreditCardValidator.as
Language: ActionScript
Copyright: (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
LOC: 419
Project Info
Flex SDK(flex_sdk_2.zip)
Server: Adobe FLEX
Type: zip
...works\source\mx\validators\
   CreditCardValidator.as
   ...ardValidatorCardType.as
   CurrencyValidator.as
   ...ValidatorAlignSymbol.as
   DateValidator.as
   EmailValidator.as
   IValidatorListener.as
   NumberValidator.as
   PhoneNumberValidator.as
   RegExpValidationResult.as
   RegExpValidator.as
   SocialSecurityValidator.as
   StringValidator.as
   ValidationResult.as
   Validator.as
   ZipCodeValidator.as
   ...eValidatorDomainType.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
//  All Rights Reserved. The following is Source Code and is subject to all
//  restrictions on such code as contained in the End User License Agreement
//  accompanying this product.
//
////////////////////////////////////////////////////////////////////////////////

package mx.validators
{

import mx.managers.ISystemManager;
import mx.managers.SystemManager;
import mx.resources.ResourceBundle;
import mx.utils.StringUtil;
import mx.validators.IValidatorListener;

/**
 *  The CreditCardValidator class validates that a credit card number
 *  is the correct length, has the correct prefix, and passes
 *  the Luhn mod10 algorithm for the specified card type. 
 *  This validator does not check whether the credit card
 *  is an actual active credit card account.
 *
 *  <p>You can specify the input to the CreditCardValidator in two ways:</p>
 *  <ul>
 *    <li>Use the <code>cardNumberSource</code> and
 *    <code>cardNumberProperty</code> properties to specify
 *    the location of the credit card number, and the 
 *    <code>cardTypeSource</code> and <code>cardTypeProperty</code> properties
 *    to specify the location of the credit card type to validate.</li>
 *    <li>Use the <code>source</code> and 
 *    <code>property</code> properties to specify a single Object.
 *    The Object should contain the following fields:
 *    <ul>
 *        <li><code>cardType</code> - Specifies the type
 *        of credit card being validated. 
 *         <p>In MXML, use the values: <code>"American Express"</code>, 
 *            <code>"Diners Club"</code>, <code>"Discover"</code>, 
 *            <code>"MasterCard"</code>, or <code>"Visa"</code>.</p>
 *         <p>In ActionScript, use the static constants
 *            <code>CreditCardValidatorCardType.MASTER_CARD</code>, 
 *            <code>CreditCardValidatorCardType.VISA</code>, or
 *            <code>CreditCardValidatorCardType.AMERICAN_EXPRESS</code> 
 *            <code>CreditCardValidatorCardType.DISCOVER</code>, or
 *            <code>CreditCardValidatorCardType.DINERS_CLUB</code>.</p>
 *          </li>
 *       <li><code>cardNumber</code> - Specifies the number of the card
 *       being validated.</li>
 *     </ul>
 *    </li>
 *  </ul>
 *  
 *  <p>To perform the validation, it uses the following guidelines:</p>
 *  <p>Length:</p>
 *  <ol>
 *    <li>Visa: 13 or 16 digits</li> 
 *    <li>MasterCard: 16 digits</li> 
 *    <li>Discover: 16 digits</li> 
 *    <li>American Express: 15 digits</li> 
 *    <li>Diners Club: 14 digits or 16 digits if it also functions as MasterCard</li>
 *  </ol>
 *  Prefix:
 *  <ol>
 *    <li>Visa: 4</li> 
 *    <li>MasterCard: 51 to 55</li>
 *    <li>Discover: 6011</li>
 *    <li>American Express: 34 or 37</li>
 *    <li>Diners Club: 300 to 305, 36 or 38, 51 to 55</li>
 *  </ol>
 *  
 *  @mxml
 *
 *  <p>The <code>&lt;mx:CreditCardValidator&gt;</code> tag
 *  inherits all of the tag attributes of its superclass,
 *  and adds the following tag attributes:</p>
 *  
 *  <pre>
 *  &lt;mx:CreditCardValidator
 *    allowedFormatChars=" -" 
 *    cardNumberListener="<i>Object specified by cardNumberSource</i>"
 *    cardNumberProperty="<i>No default</i>"
 *    cardNumberSource="<i>No default</i>"
 *    cardTypeListener="<i>Object specified by cardTypeSource</i>"
 *    cardTypeProperty="<i>No default</i>"
 *    cardTypeSource="<i>No default</i>"
 *    invalidCharError= "Invalid characters in your credit card number. (Only enter numbers.)" 
 *    invalidNumberError="The credit card number is invalid." 
 *    noNumError="No credit card number specified." 
 *    noTypeError="No credit card type specified or the type is not valid." 
 *    wrongLengthError="Your credit card number contains the wrong number of digits." 
 *    wrongTypeError="Incorrect card type specified." 
 *  /&gt;
 *  </pre>
 *
 *  @see mx.validators.CreditCardValidatorCardType
 *  
 *  @includeExample examples/CreditCardValidatorExample.mxml
 */
public class CreditCardValidator extends Validator
{
	include "../core/Version.as";

	//--------------------------------------------------------------------------
	//
	//  Class initialization
	//
	//--------------------------------------------------------------------------

	loadResources();
	
	//--------------------------------------------------------------------------
	//
	//  Class resources
	//
	//--------------------------------------------------------------------------

	[ResourceBundle("validators")]

    /**
	 *  @private    
     */
	private static var packageResources:ResourceBundle;

    /**
	 *  @private    
     */
	private static var resourceInvalidCharError:String;

    /**
	 *  @private    
     */
	private static var resourceInvalidNumberError:String;

    /**
	 *  @private    
     */
	private static var resourceNoNumError:String;

    /**
	 *  @private    
     */
	private static var resourceNoTypeError:String;

    /**
	 *  @private    
     */
	private static var resourceWrongLengthError:String;

    /**
	 *  @private    
     */
	private static var resourceWrongTypeError:String;

	/**
	 *  @private    
     */	
	private static var resourceAllowedFormatChars:String;
	
	/**
	 *  @private    
     */	
	private static var resourceMissingCardType:String;	

	/**
	 *  @private    
     */	
	private static var resourceMissingCardNumber:String;	

	/**
	 *  @private    
     */	
	private static var resourceInvalidFormatChars:String;	

	/**
	 *  @private    
     */	
	private static var resourceCNSAttribute:String;

	/**
	 *  @private    
     */	
	private static var resourceCTSAttribute:String;

	//--------------------------------------------------------------------------
	//
	//  Class methods
	//
	//--------------------------------------------------------------------------

    /**
	 *  @private    
     *  Loads resources for this class.
     */
	private static function loadResources():void
	{
		resourceAllowedFormatChars =
			packageResources.getString("creditCardValidatorAllowedFormatChars");
				
		resourceInvalidFormatChars =
			packageResources.getString("invalidFormatChars");
			
		resourceInvalidCharError =
			packageResources.getString("invalidCharErrorCCV");

		resourceInvalidNumberError =
			packageResources.getString("invalidNumberError");

		resourceNoNumError =
			packageResources.getString("noNumError");

		resourceNoTypeError =
			packageResources.getString("noTypeError");

		resourceWrongLengthError =
			packageResources.getString("wrongLengthErrorCCV");

		resourceWrongTypeError =
			packageResources.getString("wrongTypeError");

		resourceMissingCardType =
			packageResources.getString("missingCardType");

		resourceMissingCardNumber =
			packageResources.getString("missingCardNumber");

		resourceCNSAttribute =
			packageResources.getString("CNSAttribute");

		resourceCTSAttribute =
			packageResources.getString("CTSAttribute");		
	}
	
	/**
	 *  Convenience method for calling a validator.
	 *  Each of the standard Flex validators has a similar convenience method.
	 *
	 *  @param validator The CreditCardValidator instance.
	 *
	 *  @param value A field to validate, which must contain
	 *  the following fields:
	 *  <ul>
	 *    <li><code>cardType</code> - Specifies the type of credit card being validated. 
	 *    Use the static constants
	 *    <code>CreditCardValidatorCardType.MASTER_CARD</code>, 
	 *    <code>CreditCardValidatorCardType.VISA</code>,
	 *    <code>CreditCardValidatorCardType.AMERICAN_EXPRESS</code>,
	 *    <code>CreditCardValidatorCardType.DISCOVER</code>, or
	 *    <code>CreditCardValidatorCardType.DINERS_CLUB</code>.</li>
	 *    <li><code>cardNumber</code> - Specifies the number of the card
	 *    being validated.</li></ul>
	 *
	 *  @param baseField Text representation of the subfield
	 *  specified in the value parameter. 
	 *  For example, if the <code>value</code> parameter
	 *  specifies value.date, the <code>baseField</code> value is "date".
	 *
	 *  @return An Array of ValidationResult objects, with one ValidationResult 
	 *  object for each field examined by the validator. 
	 *
	 *  @see mx.validators.ValidationResult
	 */
	public static function validateCreditCard(validator:CreditCardValidator,
											  value:Object,
											  baseField:String):Array
	{
		var results:Array = [];
	    var baseFieldDot:String = baseField ? baseField + "." : "";
		
		var valid:String = DECIMAL_DIGITS + validator.allowedFormatChars;
		var cardType:String = null;
		var cardNum:String = null;
		var digitsOnlyCardNum:String = "";

		var n:int;
		var i:int;
		
		try 
		{
			cardType = String(value.cardType);
		}
		catch(e:Error)
		{
			// Use the default value and move on
			throw new Error(resourceMissingCardType);
		}
		
		try 
		{
			cardNum = value.cardNumber;
		}
		catch(f:Error)
		{
			// Use the default value and move on
			throw new Error(resourceMissingCardNumber);
		}
		
        if (validator.required)
        {
            if (cardType.length == 0)
            {
				results.push(new ValidationResult(
					true, baseFieldDot + "cardType",
					"requiredField", validator.requiredFieldError));
            }

            if (!cardNum)
            {
                results.push(new ValidationResult(
					true, baseFieldDot + "cardNumber",
					"requiredField", validator.requiredFieldError));
            }
        }
		
		n = validator.allowedFormatChars.length;
		for (i = 0; i < n; i++)
		{
			if (DECIMAL_DIGITS.indexOf(validator.allowedFormatChars.charAt(i)) != -1)
				throw new Error(resourceInvalidFormatChars);
		}
		
		if (!cardType)
		{
			results.push(new ValidationResult(
				true, baseFieldDot + "cardType",
				"noType", validator.noTypeError));
		}
		else if (cardType != CreditCardValidatorCardType.MASTER_CARD &&
				 cardType != CreditCardValidatorCardType.VISA &&
				 cardType != CreditCardValidatorCardType.AMERICAN_EXPRESS &&
				 cardType != CreditCardValidatorCardType.DISCOVER &&
				 cardType != CreditCardValidatorCardType.DINERS_CLUB)
		{
			results.push(new ValidationResult(
				true, baseFieldDot + "cardType",
				"wrongType", validator.wrongTypeError));
		}

		if (!cardNum)
		{
			results.push(new ValidationResult(
				true, baseFieldDot + "cardNumber",
				"noNum", validator.noNumError));
		}

		if (cardNum)
		{
			n = cardNum.length;
			for (i = 0; i < n; i++)
			{
				var temp:String = "" + cardNum.substring(i, i + 1);
				if (valid.indexOf(temp) == -1)
				{
					results.push(new ValidationResult(
						true, baseFieldDot + "cardNumber",
						"invalidChar", validator.invalidCharError));
				}
				if (DECIMAL_DIGITS.indexOf(temp) != -1)
					digitsOnlyCardNum += temp;
			}
		}
		
		if (results.length > 0)
			return results;

		var cardNumLen:int = digitsOnlyCardNum.toString().length;
		var correctLen:Number = -1;
		var correctLen2:Number = -1;
		var correctPrefixArray:Array = [];
		
		// diner club cards with a beginning digit of 5 need to be treated as
		// master cards. Go to the following link for more info.
		// http://www.globalpaymentsinc.com/myglobal/industry_initiatives/mc-dc-canada.html
		if (cardType == CreditCardValidatorCardType.DINERS_CLUB &&
			digitsOnlyCardNum.charAt(0) == "5")
		{
			cardType = CreditCardValidatorCardType.MASTER_CARD;
		}
		
		switch (cardType)
		{
			case CreditCardValidatorCardType.MASTER_CARD:
			{
				correctLen = 16;
				correctPrefixArray.push("51");
				correctPrefixArray.push("52");
				correctPrefixArray.push("53");
				correctPrefixArray.push("54");
				correctPrefixArray.push("55");
				break;
			}

			case CreditCardValidatorCardType.VISA:
			{
				correctLen = 13;
				correctLen2 = 16;
				correctPrefixArray.push("4");
				break;
			}

			case CreditCardValidatorCardType.AMERICAN_EXPRESS:
			{
				correctLen = 15;
				correctPrefixArray.push("34");
				correctPrefixArray.push("37");
				break;
			}

			case CreditCardValidatorCardType.DISCOVER:
			{
				correctLen = 16;
				correctPrefixArray.push("6011");
				break;
			}

			case CreditCardValidatorCardType.DINERS_CLUB:
			{
				correctLen = 14;
				correctPrefixArray.push("300");
				correctPrefixArray.push("301");
				correctPrefixArray.push("302");
				correctPrefixArray.push("303");
				correctPrefixArray.push("304");
				correctPrefixArray.push("305");
				correctPrefixArray.push("36");
				correctPrefixArray.push("38");
				break;
			}

			default:
			{
				results.push(new ValidationResult(
					true, baseFieldDot + "cardType",
					"wrongType", validator.wrongTypeError));
				return results;
			}
		}

		if ((cardNumLen != correctLen) && (cardNumLen != correctLen2)) 
		{ 
			results.push(new ValidationResult(
				true, baseFieldDot + "cardNumber",
				"wrongLength", validator.wrongLengthError));
			return results;
		}

		// Validate the prefix
		var foundPrefix:Boolean = false;
		for (i = correctPrefixArray.length - 1; i >= 0; i--)
		{
			if (digitsOnlyCardNum.indexOf(correctPrefixArray[i]) == 0)
			{
				foundPrefix = true;
				break;
			}
		}

		if (!foundPrefix)
		{
			results.push(new ValidationResult(
				true, baseFieldDot + "cardNumber",
				"invalidNumber", validator.invalidNumberError));
			return results;
		}

		// Implement Luhn formula testing of this.cardNumber
		var doubledigit:Boolean = false;
		var checkdigit:int = 0;
		var tempdigit:int;
		for (i = cardNumLen - 1; i >= 0; i--)
		{
			tempdigit = Number(digitsOnlyCardNum.charAt(i));
			if (doubledigit)
			{
				tempdigit *= 2;
				checkdigit += (tempdigit % 10);
				if ((tempdigit / 10) >= 1.0)
					checkdigit++;
				doubledigit = false;
			}
			else
			{
				checkdigit = checkdigit + tempdigit;
				doubledigit = true;
			}
		}

		if ((checkdigit % 10) != 0)
		{
			results.push(new ValidationResult(
				true, baseFieldDot + "cardNumber",
				"invalidNumber", validator.invalidNumberError));
			return results;
		}

		return results;
	}

	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------

	/**
	 *  Constructor.
	 */
	public function CreditCardValidator()
	{
		super();

		bundleChanged();

		subFields = [ "cardNumber", "cardType" ];
	}

	//--------------------------------------------------------------------------
	//
	//  Overridden properties
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  actualListeners
	//----------------------------------

	/** 
	 *  @private
	 *  Returns either the listener or the source
	 *  for the cardType and cardNumber subfields.
	 */
	override protected function get actualListeners():Array
	{
		var results:Array = [];
		
		var typeResult:Object;
		if (_cardTypeListener)
			typeResult = _cardTypeListener;
		else if (_cardTypeSource)
			typeResult = _cardTypeSource;
			
		results.push(typeResult);
		if (typeResult is IValidatorListener)
			IValidatorListener(typeResult).validationSubField = "cardType";
			
		var numResult:Object;
		if (_cardNumberListener)
			numResult = _cardNumberListener;
		else if (_cardNumberSource)
			numResult = _cardNumberSource;
			
		results.push(numResult);
		if (numResult is IValidatorListener)
			IValidatorListener(numResult).validationSubField = "cardNumber";
				
		if (results.length > 0 && listener)
			results.push(listener);


		return results;
	}

	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  allowedFormatChars
	//----------------------------------

	/**
	 *  @private
	 *  Storage for the allowedFormatChars property.
	 */
	private var _allowedFormatChars:String;

	[Inspectable(category="General", defaultValue=" -")]

	/** 
	 *  The set of formatting characters allowed in the
	 *  <code>cardNumber</code> field.
	 *
	 *  @default " -" (space and dash)
	 */
	public function get allowedFormatChars():String
	{
		return _allowedFormatChars;	
	}

    /**
	 *  @private
	 */
	public function set allowedFormatChars(value:String):void
	{
		if (value != _allowedFormatChars)
		{
			var n:int = value.length;
			for (var i:int = 0; i < n; i++)
			{
				if (DECIMAL_DIGITS.indexOf(value.charAt(i)) != -1)
					throw new Error(resourceInvalidFormatChars);
			}
			_allowedFormatChars = value;
		}
	}
	
	//----------------------------------
	//  cardNumberListener
	//----------------------------------
	
	/**
	 *  @private
	 *  Storage for the cardNumberListener property.
	 */
	private var _cardNumberListener:IValidatorListener;
	
    [Inspectable(category="General")]

	/** 
	 *  The component that listens for the validation result
	 *  for the card number subfield. 
	 *  If none is specified, use the value specified
	 *  to the <code>cardNumberSource</code> property.
	 */
	public function get cardNumberListener():IValidatorListener
	{
		return _cardNumberListener;
	}
	
	/**
	 *  @private
	 */
	public function set cardNumberListener(value:IValidatorListener):void
	{
		if (_cardNumberListener == value)
			return;
			
		removeListenerHandler();
		
		_cardNumberListener = value;
		
		addListenerHandler();
	}
		
	//----------------------------------
	//  cardNumberProperty
	//----------------------------------

    [Inspectable(category="General")]

	/**
	 *  Name of the card number property to validate. 
	 *  This attribute is optional, but if you specify
	 *  the <code>cardNumberSource</code> property, 
	 *  you should also set this property.
	 */
	public var cardNumberProperty:String;
	
	//----------------------------------
	//  cardNumberSource
	//----------------------------------	

	/**
	 *  @private
	 *  Storage for the cardNumberSource property.
	 */
	private var _cardNumberSource:Object;
	
    [Inspectable(category="General")]

	/** 
	 *  Object that contains the value of the card number field.
	 *  If you specify a value for this property, you must also specify
	 *  a value for the <code>cardNumberProperty</code> property. 
	 *  Do not use this property if you set the <code>source</code> 
	 *  and <code>property</code> properties. 
	 */
	public function get cardNumberSource():Object
	{
		return _cardNumberSource;
	}
	
	/**
	 *  @private
	 */
	public function set cardNumberSource(value:Object):void
	{
		if (_cardNumberSource == value)
			return;
		
		if (value is String)
			throw new Error(StringUtil.substitute(resourceCNSAttribute, value));
			
		removeListenerHandler();	
		
		_cardNumberSource = value;
		
		addListenerHandler();
	}
	
	//----------------------------------
	//  cardTypeListener
	//----------------------------------
	
	/**
	 *  @private
	 *  Storage for the cardTypeListener property.
	 */
	private var _cardTypeListener:IValidatorListener;
	
    [Inspectable(category="General")]

	/** 
	 *  The component that listens for the validation result
	 *  for the card type subfield. 
	 *  If none is specified, then use the value
	 *  specified to the <code>cardTypeSource</code> property.
	 */
	public function get cardTypeListener():IValidatorListener
	{
		return _cardTypeListener;
	}
	
	/**
	 *  @private
	 */
	public function set cardTypeListener(value:IValidatorListener):void
	{
		if (_cardTypeListener == value)
			return;
			
		removeListenerHandler();
		
		_cardTypeListener = value;
		
		addListenerHandler();
	}
		
	//----------------------------------
	//  cardTypeProperty
	//----------------------------------

    [Inspectable(category="General")]

	/**
	 *  Name of the card type property to validate. 
	 *  This attribute is optional, but if you specify the
	 *  <code>cardTypeSource</code> property,
	 *  you should also set this property.
	 *
     *  <p>In MXML, valid values are:</p>
     *  <ul>
     *    <li><code>"American Express"</code></li>
     *    <li><code>"Diners Club"</code></li>
     *    <li><code>"Discover"</code></li>
     *    <li><code>"MasterCard"</code></li>
     *    <li><code>"Visa"</code></li>
     *  </ul>
	 *
	 *  <p>In ActionScript, you can use the following constants to set this property:</p>
	 *  <p><code>CreditCardValidatorCardType.AMERICAN_EXPRESS</code>, 
	 *  <code>CreditCardValidatorCardType.DINERS_CLUB</code>,
	 *  <code>CreditCardValidatorCardType.DISCOVER</code>, 
	 *  <code>CreditCardValidatorCardType.MASTER_CARD</code>, and 
	 *  <code>CreditCardValidatorCardType.VISA</code>.</p>
	 *
	 *  @see mx.validators.CreditCardValidatorCardType
	 */
	public var cardTypeProperty:String;
	
	//----------------------------------
	//  cardTypeSource
	//----------------------------------

	/**
	 *  @private
	 *  Storage for the cardTypeSource property.
	 */
	private var _cardTypeSource:Object;
	
    [Inspectable(category="General")]

	/** 
	 *  Object that contains the value of the card type field.
	 *  If you specify a value for this property, you must also specify
	 *  a value for the <code>cardTypeProperty</code> property. 
	 *  Do not use this property if you set the <code>source</code> 
	 *  and <code>property</code> properties. 
	 */
	public function get cardTypeSource():Object
	{
		return _cardTypeSource;
	}
	
	/**
	 *  @private
	 */
	public function set cardTypeSource(value:Object):void
	{
		if (_cardTypeSource == value)
			return;
			
		if (value is String)
			throw new Error(StringUtil.substitute(resourceCTSAttribute, value));
			
		removeListenerHandler();	
		
		_cardTypeSource = value;
		
		addListenerHandler();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Properties: Errors
	//
	//--------------------------------------------------------------------------

	//----------------------------------
	//  invalidCharError
	//----------------------------------
	
	[Inspectable(category="Errors", defaultValue="Invalid characters in your credit card number. (Only enter numbers.)")]

	/** 
	 *  Error message when the <code>cardNumber</code> field contains invalid characters.
	 *
	 *  @default "Invalid characters in your credit card number. (Enter numbers only.)"
	 */
	public var invalidCharError:String;

	//----------------------------------
	//  invalidNumberError
	//----------------------------------
	
	[Inspectable(category="Errors", defaultValue="The credit card number is invalid.")]
	
	/** 
	 *  Error message when the credit card number is invalid.
	 *
	 *  @default "The credit card number is invalid."
	 */
	public var invalidNumberError:String;

	//----------------------------------
	//  noNumError
	//----------------------------------

	[Inspectable(category="Errors", defaultValue="No credit card number specified.")]
	
	/** 
	 *  Error message when the <code>cardNumber</code> field is empty.
	 *
	 *  @default "No credit card number is specified."
	 */
	public var noNumError:String;
	
	//----------------------------------
	//  noTypeError
	//----------------------------------

	[Inspectable(category="Errors", defaultValue="No credit card type specified or the type is not valid.")]

	/** 
	 *  Error message when the <code>cardType</code> field is blank.
	 *
	 *  @default "No credit card type is specified or the type is not valid."
	 */
	public var noTypeError:String;

	//----------------------------------
	//  wrongLengthError
	//----------------------------------

	[Inspectable(category="Errors", defaultValue="Your credit card number contains the wrong number of digits.")]

	/**
	 *  Error message when the <code>cardNumber</code> field contains the wrong
	 *  number of digits for the specified credit card type.
	 *
	 *  @default "Your credit card number contains the wrong number of digits." 
	 */
	public var wrongLengthError:String;
	
	//----------------------------------
	//  wrongTypeError
	//----------------------------------

	[Inspectable(category="Errors", defaultValue="Incorrect card type specified.")]

	/** 
	 *  Error message the <code>cardType</code> field contains an invalid credit card type. 
	 *  You should use the predefined constants for the <code>cardType</code> field:
	 *  <code>CreditCardValidatorCardType.MASTER_CARD</code>,
	 *  <code>CreditCardValidatorCardType.VISA</code>, 
	 *  <code>CreditCardValidatorCardType.AMERICAN_EXPRESS</code>,
	 *  <code>CreditCardValidatorCardType.DISCOVER</code>, or 
	 *  <code>CreditCardValidatorCardType.DINERS_CLUB</code>.
	 *
	 *  @default "Incorrect card type is specified."
	 */
	public var wrongTypeError:String;

	//--------------------------------------------------------------------------
	//
	//  Overridden methods
	//
	//--------------------------------------------------------------------------

	/**
     *  Override of the base class <code>doValidation()</code> method
	 *  to validate a credit card number.
	 *
	 *  <p>You do not call this method directly;
	 *  Flex calls it as part of performing a validation.
	 *  If you create a custom Validator class, you must implement this method. </p>
	 *
	 *  @param value an Object to validate.
     *
	 *  @return An Array of ValidationResult objects, with one ValidationResult 
	 *  object for each field examined by the validator. 
	 */
	override protected function doValidation(value:Object):Array
    {
		var results:Array = super.doValidation(value);
		
		// Return if there are errors
		// or if the required property is set to false and length is 0.
		var val:String = value ? String(value) : "";
		if (results.length > 0 || ((val.length == 0) && !required))
			return results;
		else
		    return CreditCardValidator.validateCreditCard(this, value, null);
    }
	
	/**
	 *  @private
	 *  Grabs the data for the validator from two different sources
	 */
	override protected function getValueFromSource():Object
	{
		var useValue:Boolean = false;
	
		var value:Object = {};
		
		if (cardTypeSource && cardTypeProperty)
		{
			value.cardType = cardTypeSource[cardTypeProperty];
			useValue = true;
		}
		
		if (cardNumberSource && cardNumberProperty)
		{
			value.cardNumber = cardNumberSource[cardNumberProperty];
			useValue = true;
		}
	
		if (useValue)
			return value;
		else
			return super.getValueFromSource();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------

    /**
	 *  @private    
     *  Populates localizable properties from the loaded bundle for this class.
     */
	private function bundleChanged():void
	{
		allowedFormatChars = resourceAllowedFormatChars;
		
		invalidCharError = resourceInvalidCharError;
		invalidNumberError = resourceInvalidNumberError;
		noNumError = resourceNoNumError;
		noTypeError = resourceNoTypeError;
		wrongLengthError = resourceWrongLengthError;
		wrongTypeError = resourceWrongTypeError;
	}
}

}