ÿþ/ *  
  
 *   P r i c e   F o r m a t   j Q u e r y   P l u g i n  
 *   B y   E d u a r d o   C u d u c o s  
 *   c u d u c o s   [ a t ]   g m a i l   [ d o t ]   c o m  
 *   V e r s i o n :   1 . 1  
 *   R e l e a s e :   2 0 0 9 - 0 2 - 1 0  
  
 *   o r i g i n a l   c h a r   l i m i t   b y   F l ýÿv i o   S i l v e i r a   < h t t p : / / f l a v i o s i l v e i r a . c o m >  
 *   o r i g i n a l   k e y d o w n   e v e n t   a t t a c h m e n t   b y   K a i h u a   Q i  
 *   k e y d o w n   f i x e s   b y   T h a s m o   < h t t p : / / t h a s m o . c o m >  
  
 * /  
  
 ( f u n c t i o n ( $ )   {  
  
         $ . f n . p r i c e F o r m a t   =   f u n c t i o n ( o p t i o n s )   {  
  
                 v a r   d e f a u l t s   =   {  
                         p r e f i x :   ' U S $   ' ,  
                         c e n t s S e p a r a t o r :   ' . ' ,  
                         t h o u s a n d s S e p a r a t o r :   ' , ' ,  
                         l i m i t :   f a l s e ,  
                         c e n t s L i m i t :   2  
                 } ;  
  
                 v a r   o p t i o n s   =   $ . e x t e n d ( d e f a u l t s ,   o p t i o n s ) ;  
  
                 r e t u r n   t h i s . e a c h ( f u n c t i o n ( )   {  
  
                         / /   p r e   d e f i n e d   o p t i o n s  
                         v a r   o b j   =   $ ( t h i s ) ;  
                         v a r   i s _ n u m b e r   =   / [ 0 - 9 ] / ;  
  
                         / /   l o a d   t h e   p l u g g i n g s   s e t t i n g s  
                         v a r   p r e f i x   =   o p t i o n s . p r e f i x ;  
                         v a r   c e n t s S e p a r a t o r   =   o p t i o n s . c e n t s S e p a r a t o r ;  
                         v a r   t h o u s a n d s S e p a r a t o r   =   o p t i o n s . t h o u s a n d s S e p a r a t o r ;  
                         v a r   l i m i t   =   o p t i o n s . l i m i t ;  
                         v a r   c e n t s L i m i t   =   o p t i o n s . c e n t s L i m i t ;  
  
                         / /   s k i p   e v e r y t h i n g   t h a t   i s n ' t   a   n u m b e r  
                         / /   a n d   a l s o   s k i p   t h e   l e f t   z e r o e s  
                         f u n c t i o n   t o _ n u m b e r s ( s t r )   {  
                                 v a r   f o r m a t t e d   =   ' ' ;  
                                 f o r   ( v a r   i   =   0 ;   i   <   ( s t r . l e n g t h ) ;   i + + )   {  
                                         c h a r   =   s t r . c h a r A t ( i ) ;  
                                         i f   ( f o r m a t t e d . l e n g t h   = =   0   & &   c h a r   = =   0 )   c h a r   =   f a l s e ;  
                                         i f   ( c h a r   & &   c h a r . m a t c h ( i s _ n u m b e r ) )   {  
                                                 i f   ( l i m i t )   {  
                                                         i f   ( f o r m a t t e d . l e n g t h   <   l i m i t )   f o r m a t t e d   =   f o r m a t t e d   +   c h a r ;  
                                                 }   e l s e   {  
                                                         f o r m a t t e d   =   f o r m a t t e d   +   c h a r ;  
                                                 }  
                                         }  
                                 }  
                                 r e t u r n   f o r m a t t e d ;  
                         }  
  
                         / /   f o r m a t   t o   f i l l   w i t h   z e r o s   t o   c o m p l e t e   c e n t s   c h a r s  
                         f u n c t i o n   f i l l _ w i t h _ z e r o e s ( s t r )   {  
                                 w h i l e   ( s t r . l e n g t h   <   ( c e n t s L i m i t   +   1 ) )   s t r   =   ' 0 '   +   s t r ;  
                                 r e t u r n   s t r ;  
                         }  
  
                         / /   f o r m a t   a s   p r i c e  
                         f u n c t i o n   p r i c e _ f o r m a t ( s t r )   {  
  
                                 / /   f o r m a t t i n g   s e t t i n g s  
                                 v a r   f o r m a t t e d   =   f i l l _ w i t h _ z e r o e s ( t o _ n u m b e r s ( s t r ) ) ;  
                                 v a r   t h o u s a n d s F o r m a t t e d   =   ' ' ;  
                                 v a r   t h o u s a n d s C o u n t   =   0 ;  
  
                                 / /   s p l i t   i n t e g e r   f r o m   c e n t s  
                                 v a r   c e n t s V a l   =   f o r m a t t e d . s u b s t r ( f o r m a t t e d . l e n g t h   -   c e n t s L i m i t ,   c e n t s L i m i t ) ;  
                                 v a r   i n t e g e r V a l   =   f o r m a t t e d . s u b s t r ( 0 ,   f o r m a t t e d . l e n g t h   -   c e n t s L i m i t ) ;  
  
                                 / /   a p p l y   c e n t s   p o n t u a t i o n  
                                 f o r m a t t e d   =   i n t e g e r V a l   +   c e n t s S e p a r a t o r   +   c e n t s V a l ;  
  
                                 / /   a p p l y   t h o u s a n d s   p o n t u a t i o n  
                                 i f   ( t h o u s a n d s S e p a r a t o r )   {  
                                         f o r   ( v a r   j   =   i n t e g e r V a l . l e n g t h ;   j   >   0 ;   j - - )   {  
                                                 c h a r   =   i n t e g e r V a l . s u b s t r ( j   -   1 ,   1 ) ;  
                                                 t h o u s a n d s C o u n t + + ;  
                                                 i f   ( t h o u s a n d s C o u n t   %   3   = =   0 )   c h a r   =   t h o u s a n d s S e p a r a t o r   +   c h a r ;  
                                                 t h o u s a n d s F o r m a t t e d   =   c h a r   +   t h o u s a n d s F o r m a t t e d ;  
                                         }  
                                         i f   ( t h o u s a n d s F o r m a t t e d . s u b s t r ( 0 ,   1 )   = =   t h o u s a n d s S e p a r a t o r )   t h o u s a n d s F o r m a t t e d   =   t h o u s a n d s F o r m a t t e d . s u b s t r i n g ( 1 ,   t h o u s a n d s F o r m a t t e d . l e n g t h ) ;  
                                         f o r m a t t e d   =   t h o u s a n d s F o r m a t t e d   +   c e n t s S e p a r a t o r   +   c e n t s V a l ;  
                                 }  
  
                                 / /   a p p l y   t h e   p r e f i x  
                                 i f   ( p r e f i x )   f o r m a t t e d   =   p r e f i x   +   f o r m a t t e d ;  
  
                                 r e t u r n   f o r m a t t e d ;  
  
                         }  
  
                         / /   f i l t e r   w h a t   u s e r   t y p e   ( o n l y   n u m b e r s   a n d   f u n c t i o n a l   k e y s )  
                         f u n c t i o n   k e y _ c h e c k ( e )   {  
  
                                 v a r   c o d e   =   ( e . k e y C o d e   ?   e . k e y C o d e   :   e . w h i c h ) ;  
                                 v a r   t y p e d   =   S t r i n g . f r o m C h a r C o d e ( c o d e ) ;  
                                 v a r   f u n c t i o n a l   =   f a l s e ;  
                                 v a r   s t r   =   o b j . v a l ( ) ;  
                                 v a r   n e w V a l u e   =   p r i c e _ f o r m a t ( s t r   +   t y p e d ) ;  
  
                                 / /   a l l o w   k e y p a d   n u m b e r s ,   0   t o   9  
                                 i f   ( c o d e   > =   9 6   & &   c o d e   < =   1 0 5 )   f u n c t i o n a l   =   t r u e ;  
  
                                 / /   c h e c k   B a c k s p a c e ,   T a b ,   E n t e r ,   a n d   l e f t / r i g h t   a r r o w s  
                                 i f   ( c o d e   = =   8 )   f u n c t i o n a l   =   t r u e ;  
                                 i f   ( c o d e   = =   9 )   f u n c t i o n a l   =   t r u e ;  
                                 i f   ( c o d e   = =   1 3 )   f u n c t i o n a l   =   t r u e ;  
                                 i f   ( c o d e   = =   3 7 )   f u n c t i o n a l   =   t r u e ;  
                                 i f   ( c o d e   = =   3 9 )   f u n c t i o n a l   =   t r u e ;  
  
                                 i f   ( ! f u n c t i o n a l )   {  
                                         e . p r e v e n t D e f a u l t ( ) ;  
                                         e . s t o p P r o p a g a t i o n ( ) ;  
                                         i f   ( s t r   ! =   n e w V a l u e )   o b j . v a l ( n e w V a l u e ) ;  
                                 }  
  
                         }  
  
                         / /   i n s t e r   f o r m a t t e d   p r i c e   a s   a   v a l u e   o f   a n   i n p u t   f i e l d  
                         f u n c t i o n   p r i c e _ i t ( )   {  
                                 v a r   s t r   =   o b j . v a l ( ) ;  
                                 v a r   p r i c e   =   p r i c e _ f o r m a t ( s t r ) ;  
                                 i f   ( s t r   ! =   p r i c e )   o b j . v a l ( p r i c e ) ;  
                         }  
  
                         / /   b i n d   t h e   a c t i o n s  
                         $ ( t h i s ) . b i n d ( ' k e y d o w n ' ,   k e y _ c h e c k ) ;  
                         $ ( t h i s ) . b i n d ( ' k e y u p ' ,   p r i c e _ i t ) ;  
                         i f   ( $ ( t h i s ) . v a l ( ) . l e n g t h   >   0 )   p r i c e _ i t ( ) ;  
  
                 } ) ;  
  
         } ;  
  
 } ) ( j Q u e r y ) ; 
