ÿþ/ * 
     S o r t T a b l e 
     v e r s i o n   2 
     7 t h   A p r i l   2 0 0 7 
     S t u a r t   L a n g r i d g e ,   h t t p : / / w w w . k r y o g e n i x . o r g / c o d e / b r o w s e r / s o r t t a b l e / 
     
     I n s t r u c t i o n s : 
     D o w n l o a d   t h i s   f i l e 
     A d d   < s c r i p t   s r c = " s o r t t a b l e . j s " > < / s c r i p t >   t o   y o u r   H T M L 
     A d d   c l a s s = " s o r t a b l e "   t o   a n y   t a b l e   y o u ' d   l i k e   t o   m a k e   s o r t a b l e 
     C l i c k   o n   t h e   h e a d e r s   t o   s o r t 
     
     T h a n k s   t o   m a n y ,   m a n y   p e o p l e   f o r   c o n t r i b u t i o n s   a n d   s u g g e s t i o n s . 
     L i c e n c e d   a s   X 1 1 :   h t t p : / / w w w . k r y o g e n i x . o r g / c o d e / b r o w s e r / l i c e n c e . h t m l 
     T h i s   b a s i c a l l y   m e a n s :   d o   w h a t   y o u   w a n t   w i t h   i t . 
 * / 
 
   
 v a r   s t I s I E   =   / * @ c c _ o n ! @ * / f a l s e ; 
 
 s o r t t a b l e   =   { 
     i n i t :   f u n c t i o n ( )   { 
         / /   q u i t   i f   t h i s   f u n c t i o n   h a s   a l r e a d y   b e e n   c a l l e d 
         i f   ( a r g u m e n t s . c a l l e e . d o n e )   r e t u r n ; 
         / /   f l a g   t h i s   f u n c t i o n   s o   w e   d o n ' t   d o   t h e   s a m e   t h i n g   t w i c e 
         a r g u m e n t s . c a l l e e . d o n e   =   t r u e ; 
         / /   k i l l   t h e   t i m e r 
         i f   ( _ t i m e r )   c l e a r I n t e r v a l ( _ t i m e r ) ; 
         
         i f   ( ! d o c u m e n t . c r e a t e E l e m e n t   | |   ! d o c u m e n t . g e t E l e m e n t s B y T a g N a m e )   r e t u r n ; 
         
         s o r t t a b l e . D A T E _ R E   =   / ^ ( \ d \ d ? ) [ \ / \ . - ] ( \ d \ d ? ) [ \ / \ . - ] ( ( \ d \ d ) ? \ d \ d ) $ / ; 
         
         f o r E a c h ( d o c u m e n t . g e t E l e m e n t s B y T a g N a m e ( ' t a b l e ' ) ,   f u n c t i o n ( t a b l e )   { 
             i f   ( t a b l e . c l a s s N a m e . s e a r c h ( / \ b s o r t a b l e \ b / )   ! =   - 1 )   { 
                 s o r t t a b l e . m a k e S o r t a b l e ( t a b l e ) ; 
             } 
         } ) ; 
         
     } , 
     
     m a k e S o r t a b l e :   f u n c t i o n ( t a b l e )   { 
         i f   ( t a b l e . g e t E l e m e n t s B y T a g N a m e ( ' t h e a d ' ) . l e n g t h   = =   0 )   { 
             / /   t a b l e   d o e s n ' t   h a v e   a   t H e a d .   S i n c e   i t   s h o u l d   h a v e ,   c r e a t e   o n e   a n d 
             / /   p u t   t h e   f i r s t   t a b l e   r o w   i n   i t . 
             t h e   =   d o c u m e n t . c r e a t e E l e m e n t ( ' t h e a d ' ) ; 
             t h e . a p p e n d C h i l d ( t a b l e . r o w s [ 0 ] ) ; 
             t a b l e . i n s e r t B e f o r e ( t h e , t a b l e . f i r s t C h i l d ) ; 
         } 
         / /   S a f a r i   d o e s n ' t   s u p p o r t   t a b l e . t H e a d ,   s i g h 
         i f   ( t a b l e . t H e a d   = =   n u l l )   t a b l e . t H e a d   =   t a b l e . g e t E l e m e n t s B y T a g N a m e ( ' t h e a d ' ) [ 0 ] ; 
         
         i f   ( t a b l e . t H e a d . r o w s . l e n g t h   ! =   1 )   r e t u r n ;   / /   c a n ' t   c o p e   w i t h   t w o   h e a d e r   r o w s 
         
         / /   S o r t t a b l e   v 1   p u t   r o w s   w i t h   a   c l a s s   o f   " s o r t b o t t o m "   a t   t h e   b o t t o m   ( a s 
         / /   " t o t a l "   r o w s ,   f o r   e x a m p l e ) .   T h i s   i s   B & R ,   s i n c e   w h a t   y o u ' r e   s u p p o s e d 
         / /   t o   d o   i s   p u t   t h e m   i n   a   t f o o t .   S o ,   i f   t h e r e   a r e   s o r t b o t t o m   r o w s , 
         / /   f o r   b a c k w a r d s   c o m p a t i b i l i t y ,   m o v e   t h e m   t o   t f o o t   ( c r e a t i n g   i t   i f   n e e d e d ) . 
         s o r t b o t t o m r o w s   =   [ ] ; 
         f o r   ( v a r   i = 0 ;   i < t a b l e . r o w s . l e n g t h ;   i + + )   { 
             i f   ( t a b l e . r o w s [ i ] . c l a s s N a m e . s e a r c h ( / \ b s o r t b o t t o m \ b / )   ! =   - 1 )   { 
                 s o r t b o t t o m r o w s [ s o r t b o t t o m r o w s . l e n g t h ]   =   t a b l e . r o w s [ i ] ; 
             } 
         } 
         i f   ( s o r t b o t t o m r o w s )   { 
             i f   ( t a b l e . t F o o t   = =   n u l l )   { 
                 / /   t a b l e   d o e s n ' t   h a v e   a   t f o o t .   C r e a t e   o n e . 
                 t f o   =   d o c u m e n t . c r e a t e E l e m e n t ( ' t f o o t ' ) ; 
                 t a b l e . a p p e n d C h i l d ( t f o ) ; 
             } 
             f o r   ( v a r   i = 0 ;   i < s o r t b o t t o m r o w s . l e n g t h ;   i + + )   { 
                 t f o . a p p e n d C h i l d ( s o r t b o t t o m r o w s [ i ] ) ; 
             } 
             d e l e t e   s o r t b o t t o m r o w s ; 
         } 
         
         / /   w o r k   t h r o u g h   e a c h   c o l u m n   a n d   c a l c u l a t e   i t s   t y p e 
         h e a d r o w   =   t a b l e . t H e a d . r o w s [ 0 ] . c e l l s ; 
         f o r   ( v a r   i = 0 ;   i < h e a d r o w . l e n g t h ;   i + + )   { 
             / /   m a n u a l l y   o v e r r i d e   t h e   t y p e   w i t h   a   s o r t t a b l e _ t y p e   a t t r i b u t e 
             i f   ( ! h e a d r o w [ i ] . c l a s s N a m e . m a t c h ( / \ b s o r t t a b l e _ n o s o r t \ b / ) )   {   / /   s k i p   t h i s   c o l 
                 m t c h   =   h e a d r o w [ i ] . c l a s s N a m e . m a t c h ( / \ b s o r t t a b l e _ ( [ a - z 0 - 9 ] + ) \ b / ) ; 
                 i f   ( m t c h )   {   o v e r r i d e   =   m t c h [ 1 ] ;   } 
 	             i f   ( m t c h   & &   t y p e o f   s o r t t a b l e [ " s o r t _ " + o v e r r i d e ]   = =   ' f u n c t i o n ' )   { 
 	                 h e a d r o w [ i ] . s o r t t a b l e _ s o r t f u n c t i o n   =   s o r t t a b l e [ " s o r t _ " + o v e r r i d e ] ; 
 	             }   e l s e   { 
 	                 h e a d r o w [ i ] . s o r t t a b l e _ s o r t f u n c t i o n   =   s o r t t a b l e . g u e s s T y p e ( t a b l e , i ) ; 
 	             } 
 	             / /   m a k e   i t   c l i c k a b l e   t o   s o r t 
 	             h e a d r o w [ i ] . s o r t t a b l e _ c o l u m n i n d e x   =   i ; 
 	             h e a d r o w [ i ] . s o r t t a b l e _ t b o d y   =   t a b l e . t B o d i e s [ 0 ] ; 
 	             d e a n _ a d d E v e n t ( h e a d r o w [ i ] , " c l i c k " ,   f u n c t i o n ( e )   { 
 
                     i f   ( t h i s . c l a s s N a m e . s e a r c h ( / \ b s o r t t a b l e _ s o r t e d \ b / )   ! =   - 1 )   { 
                         / /   i f   w e ' r e   a l r e a d y   s o r t e d   b y   t h i s   c o l u m n ,   j u s t   
                         / /   r e v e r s e   t h e   t a b l e ,   w h i c h   i s   q u i c k e r 
                         s o r t t a b l e . r e v e r s e ( t h i s . s o r t t a b l e _ t b o d y ) ; 
                         t h i s . c l a s s N a m e   =   t h i s . c l a s s N a m e . r e p l a c e ( ' s o r t t a b l e _ s o r t e d ' , 
                                                                                                         ' s o r t t a b l e _ s o r t e d _ r e v e r s e ' ) ; 
                         t h i s . r e m o v e C h i l d ( d o c u m e n t . g e t E l e m e n t B y I d ( ' s o r t t a b l e _ s o r t f w d i n d ' ) ) ; 
                         s o r t r e v i n d   =   d o c u m e n t . c r e a t e E l e m e n t ( ' s p a n ' ) ; 
                         s o r t r e v i n d . i d   =   " s o r t t a b l e _ s o r t r e v i n d " ; 
                         s o r t r e v i n d . i n n e r H T M L   =   s t I s I E   ?   ' & n b s p < f o n t   f a c e = " w e b d i n g s " > 5 < / f o n t > '   :   ' & n b s p ; & # x 2 5 B 4 ; ' ; 
                         t h i s . a p p e n d C h i l d ( s o r t r e v i n d ) ; 
                         r e t u r n ; 
                     } 
                     i f   ( t h i s . c l a s s N a m e . s e a r c h ( / \ b s o r t t a b l e _ s o r t e d _ r e v e r s e \ b / )   ! =   - 1 )   { 
                         / /   i f   w e ' r e   a l r e a d y   s o r t e d   b y   t h i s   c o l u m n   i n   r e v e r s e ,   j u s t   
                         / /   r e - r e v e r s e   t h e   t a b l e ,   w h i c h   i s   q u i c k e r 
                         s o r t t a b l e . r e v e r s e ( t h i s . s o r t t a b l e _ t b o d y ) ; 
                         t h i s . c l a s s N a m e   =   t h i s . c l a s s N a m e . r e p l a c e ( ' s o r t t a b l e _ s o r t e d _ r e v e r s e ' , 
                                                                                                         ' s o r t t a b l e _ s o r t e d ' ) ; 
                         t h i s . r e m o v e C h i l d ( d o c u m e n t . g e t E l e m e n t B y I d ( ' s o r t t a b l e _ s o r t r e v i n d ' ) ) ; 
                         s o r t f w d i n d   =   d o c u m e n t . c r e a t e E l e m e n t ( ' s p a n ' ) ; 
                         s o r t f w d i n d . i d   =   " s o r t t a b l e _ s o r t f w d i n d " ; 
                         s o r t f w d i n d . i n n e r H T M L   =   s t I s I E   ?   ' & n b s p < f o n t   f a c e = " w e b d i n g s " > 6 < / f o n t > '   :   ' & n b s p ; & # x 2 5 B E ; ' ; 
                         t h i s . a p p e n d C h i l d ( s o r t f w d i n d ) ; 
                         r e t u r n ; 
                     } 
                     
                     / /   r e m o v e   s o r t t a b l e _ s o r t e d   c l a s s e s 
                     t h e a d r o w   =   t h i s . p a r e n t N o d e ; 
                     f o r E a c h ( t h e a d r o w . c h i l d N o d e s ,   f u n c t i o n ( c e l l )   { 
                         i f   ( c e l l . n o d e T y p e   = =   1 )   {   / /   a n   e l e m e n t 
                             c e l l . c l a s s N a m e   =   c e l l . c l a s s N a m e . r e p l a c e ( ' s o r t t a b l e _ s o r t e d _ r e v e r s e ' , ' ' ) ; 
                             c e l l . c l a s s N a m e   =   c e l l . c l a s s N a m e . r e p l a c e ( ' s o r t t a b l e _ s o r t e d ' , ' ' ) ; 
                         } 
                     } ) ; 
                     s o r t f w d i n d   =   d o c u m e n t . g e t E l e m e n t B y I d ( ' s o r t t a b l e _ s o r t f w d i n d ' ) ; 
                     i f   ( s o r t f w d i n d )   {   s o r t f w d i n d . p a r e n t N o d e . r e m o v e C h i l d ( s o r t f w d i n d ) ;   } 
                     s o r t r e v i n d   =   d o c u m e n t . g e t E l e m e n t B y I d ( ' s o r t t a b l e _ s o r t r e v i n d ' ) ; 
                     i f   ( s o r t r e v i n d )   {   s o r t r e v i n d . p a r e n t N o d e . r e m o v e C h i l d ( s o r t r e v i n d ) ;   } 
                     
                     t h i s . c l a s s N a m e   + =   '   s o r t t a b l e _ s o r t e d ' ; 
                     s o r t f w d i n d   =   d o c u m e n t . c r e a t e E l e m e n t ( ' s p a n ' ) ; 
                     s o r t f w d i n d . i d   =   " s o r t t a b l e _ s o r t f w d i n d " ; 
                     s o r t f w d i n d . i n n e r H T M L   =   s t I s I E   ?   ' & n b s p < f o n t   f a c e = " w e b d i n g s " > 6 < / f o n t > '   :   ' & n b s p ; & # x 2 5 B E ; ' ; 
                     t h i s . a p p e n d C h i l d ( s o r t f w d i n d ) ; 
 
 	                 / /   b u i l d   a n   a r r a y   t o   s o r t .   T h i s   i s   a   S c h w a r t z i a n   t r a n s f o r m   t h i n g , 
 	                 / /   i . e . ,   w e   " d e c o r a t e "   e a c h   r o w   w i t h   t h e   a c t u a l   s o r t   k e y , 
 	                 / /   s o r t   b a s e d   o n   t h e   s o r t   k e y s ,   a n d   t h e n   p u t   t h e   r o w s   b a c k   i n   o r d e r 
 	                 / /   w h i c h   i s   a   l o t   f a s t e r   b e c a u s e   y o u   o n l y   d o   g e t I n n e r T e x t   o n c e   p e r   r o w 
 	                 r o w _ a r r a y   =   [ ] ; 
 	                 c o l   =   t h i s . s o r t t a b l e _ c o l u m n i n d e x ; 
 	                 r o w s   =   t h i s . s o r t t a b l e _ t b o d y . r o w s ; 
 	                 f o r   ( v a r   j = 0 ;   j < r o w s . l e n g t h ;   j + + )   { 
 	                     r o w _ a r r a y [ r o w _ a r r a y . l e n g t h ]   =   [ s o r t t a b l e . g e t I n n e r T e x t ( r o w s [ j ] . c e l l s [ c o l ] ) ,   r o w s [ j ] ] ; 
 	                 } 
 	                 / *   I f   y o u   w a n t   a   s t a b l e   s o r t ,   u n c o m m e n t   t h e   f o l l o w i n g   l i n e   * / 
 	                 / / s o r t t a b l e . s h a k e r _ s o r t ( r o w _ a r r a y ,   t h i s . s o r t t a b l e _ s o r t f u n c t i o n ) ; 
 	                 / *   a n d   c o m m e n t   o u t   t h i s   o n e   * / 
 	                 r o w _ a r r a y . s o r t ( t h i s . s o r t t a b l e _ s o r t f u n c t i o n ) ; 
 	                 
 	                 t b   =   t h i s . s o r t t a b l e _ t b o d y ; 
 	                 f o r   ( v a r   j = 0 ;   j < r o w _ a r r a y . l e n g t h ;   j + + )   { 
 	                     t b . a p p e n d C h i l d ( r o w _ a r r a y [ j ] [ 1 ] ) ; 
 	                 } 
 	                 
 	                 d e l e t e   r o w _ a r r a y ; 
 	             } ) ; 
 	         } 
         } 
     } , 
     
     g u e s s T y p e :   f u n c t i o n ( t a b l e ,   c o l u m n )   { 
         / /   g u e s s   t h e   t y p e   o f   a   c o l u m n   b a s e d   o n   i t s   f i r s t   n o n - b l a n k   r o w 
         s o r t f n   =   s o r t t a b l e . s o r t _ a l p h a ; 
         f o r   ( v a r   i = 0 ;   i < t a b l e . t B o d i e s [ 0 ] . r o w s . l e n g t h ;   i + + )   { 
             t e x t   =   s o r t t a b l e . g e t I n n e r T e x t ( t a b l e . t B o d i e s [ 0 ] . r o w s [ i ] . c e l l s [ c o l u m n ] ) ; 
             i f   ( t e x t   ! =   ' ' )   { 
                 i f   ( t e x t . m a t c h ( / ^ - ? [ £ $ ¤ ] ? [ \ d , . ] + % ? $ / ) )   { 
                     r e t u r n   s o r t t a b l e . s o r t _ n u m e r i c ; 
                 } 
                 / /   c h e c k   f o r   a   d a t e :   d d / m m / y y y y   o r   d d / m m / y y   
                 / /   c a n   h a v e   /   o r   .   o r   -   a s   s e p a r a t o r 
                 / /   c a n   b e   m m / d d   a s   w e l l 
                 p o s s d a t e   =   t e x t . m a t c h ( s o r t t a b l e . D A T E _ R E ) 
                 i f   ( p o s s d a t e )   { 
                     / /   l o o k s   l i k e   a   d a t e 
                     f i r s t   =   p a r s e I n t ( p o s s d a t e [ 1 ] ) ; 
                     s e c o n d   =   p a r s e I n t ( p o s s d a t e [ 2 ] ) ; 
                     i f   ( f i r s t   >   1 2 )   { 
                         / /   d e f i n i t e l y   d d / m m 
                         r e t u r n   s o r t t a b l e . s o r t _ d d m m ; 
                     }   e l s e   i f   ( s e c o n d   >   1 2 )   { 
                         r e t u r n   s o r t t a b l e . s o r t _ m m d d ; 
                     }   e l s e   { 
                         / /   l o o k s   l i k e   a   d a t e ,   b u t   w e   c a n ' t   t e l l   w h i c h ,   s o   a s s u m e 
                         / /   t h a t   i t ' s   d d / m m   ( E n g l i s h   i m p e r i a l i s m ! )   a n d   k e e p   l o o k i n g 
                         s o r t f n   =   s o r t t a b l e . s o r t _ d d m m ; 
                     } 
                 } 
             } 
         } 
         r e t u r n   s o r t f n ; 
     } , 
     
     g e t I n n e r T e x t :   f u n c t i o n ( n o d e )   { 
         / /   g e t s   t h e   t e x t   w e   w a n t   t o   u s e   f o r   s o r t i n g   f o r   a   c e l l . 
         / /   s t r i p s   l e a d i n g   a n d   t r a i l i n g   w h i t e s p a c e . 
         / /   t h i s   i s   * n o t *   a   g e n e r i c   g e t I n n e r T e x t   f u n c t i o n ;   i t ' s   s p e c i a l   t o   s o r t t a b l e . 
         / /   f o r   e x a m p l e ,   y o u   c a n   o v e r r i d e   t h e   c e l l   t e x t   w i t h   a   c u s t o m k e y   a t t r i b u t e . 
         / /   i t   a l s o   g e t s   . v a l u e   f o r   < i n p u t >   f i e l d s . 
         
         h a s I n p u t s   =   ( t y p e o f   n o d e . g e t E l e m e n t s B y T a g N a m e   = =   ' f u n c t i o n ' )   & & 
                                   n o d e . g e t E l e m e n t s B y T a g N a m e ( ' i n p u t ' ) . l e n g t h ; 
         
         i f   ( n o d e . g e t A t t r i b u t e ( " s o r t t a b l e _ c u s t o m k e y " )   ! =   n u l l )   { 
             r e t u r n   n o d e . g e t A t t r i b u t e ( " s o r t t a b l e _ c u s t o m k e y " ) ; 
         } 
         e l s e   i f   ( t y p e o f   n o d e . t e x t C o n t e n t   ! =   ' u n d e f i n e d '   & &   ! h a s I n p u t s )   { 
             r e t u r n   n o d e . t e x t C o n t e n t . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
         } 
         e l s e   i f   ( t y p e o f   n o d e . i n n e r T e x t   ! =   ' u n d e f i n e d '   & &   ! h a s I n p u t s )   { 
             r e t u r n   n o d e . i n n e r T e x t . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
         } 
         e l s e   i f   ( t y p e o f   n o d e . t e x t   ! =   ' u n d e f i n e d '   & &   ! h a s I n p u t s )   { 
             r e t u r n   n o d e . t e x t . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
         } 
         e l s e   { 
             s w i t c h   ( n o d e . n o d e T y p e )   { 
                 c a s e   3 : 
                     i f   ( n o d e . n o d e N a m e . t o L o w e r C a s e ( )   = =   ' i n p u t ' )   { 
                         r e t u r n   n o d e . v a l u e . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
                     } 
                 c a s e   4 : 
                     r e t u r n   n o d e . n o d e V a l u e . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
                     b r e a k ; 
                 c a s e   1 : 
                 c a s e   1 1 : 
                     v a r   i n n e r T e x t   =   ' ' ; 
                     f o r   ( v a r   i   =   0 ;   i   <   n o d e . c h i l d N o d e s . l e n g t h ;   i + + )   { 
                         i n n e r T e x t   + =   s o r t t a b l e . g e t I n n e r T e x t ( n o d e . c h i l d N o d e s [ i ] ) ; 
                     } 
                     r e t u r n   i n n e r T e x t . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) ; 
                     b r e a k ; 
                 d e f a u l t : 
                     r e t u r n   ' ' ; 
             } 
         } 
     } , 
     
     r e v e r s e :   f u n c t i o n ( t b o d y )   { 
         / /   r e v e r s e   t h e   r o w s   i n   a   t b o d y 
         n e w r o w s   =   [ ] ; 
         f o r   ( v a r   i = 0 ;   i < t b o d y . r o w s . l e n g t h ;   i + + )   { 
             n e w r o w s [ n e w r o w s . l e n g t h ]   =   t b o d y . r o w s [ i ] ; 
         } 
         f o r   ( v a r   i = n e w r o w s . l e n g t h - 1 ;   i > = 0 ;   i - - )   { 
               t b o d y . a p p e n d C h i l d ( n e w r o w s [ i ] ) ; 
         } 
         d e l e t e   n e w r o w s ; 
     } , 
     
     / *   s o r t   f u n c t i o n s 
           e a c h   s o r t   f u n c t i o n   t a k e s   t w o   p a r a m e t e r s ,   a   a n d   b 
           y o u   a r e   c o m p a r i n g   a [ 0 ]   a n d   b [ 0 ]   * / 
     s o r t _ n u m e r i c :   f u n c t i o n ( a , b )   { 
         a a   =   p a r s e F l o a t ( a [ 0 ] . r e p l a c e ( / [ ^ 0 - 9 . - ] / g , ' ' ) ) ; 
         i f   ( i s N a N ( a a ) )   a a   =   0 ; 
         b b   =   p a r s e F l o a t ( b [ 0 ] . r e p l a c e ( / [ ^ 0 - 9 . - ] / g , ' ' ) ) ;   
         i f   ( i s N a N ( b b ) )   b b   =   0 ; 
         r e t u r n   a a - b b ; 
     } , 
     s o r t _ a l p h a :   f u n c t i o n ( a , b )   { 
         i f   ( a [ 0 ] = = b [ 0 ] )   r e t u r n   0 ; 
         i f   ( a [ 0 ] < b [ 0 ] )   r e t u r n   - 1 ; 
         r e t u r n   1 ; 
     } , 
     s o r t _ d d m m :   f u n c t i o n ( a , b )   { 
         m t c h   =   a [ 0 ] . m a t c h ( s o r t t a b l e . D A T E _ R E ) ; 
         y   =   m t c h [ 3 ] ;   m   =   m t c h [ 2 ] ;   d   =   m t c h [ 1 ] ; 
         i f   ( m . l e n g t h   = =   1 )   m   =   ' 0 ' + m ; 
         i f   ( d . l e n g t h   = =   1 )   d   =   ' 0 ' + d ; 
         d t 1   =   y + m + d ; 
         m t c h   =   b [ 0 ] . m a t c h ( s o r t t a b l e . D A T E _ R E ) ; 
         y   =   m t c h [ 3 ] ;   m   =   m t c h [ 2 ] ;   d   =   m t c h [ 1 ] ; 
         i f   ( m . l e n g t h   = =   1 )   m   =   ' 0 ' + m ; 
         i f   ( d . l e n g t h   = =   1 )   d   =   ' 0 ' + d ; 
         d t 2   =   y + m + d ; 
         i f   ( d t 1 = = d t 2 )   r e t u r n   0 ; 
         i f   ( d t 1 < d t 2 )   r e t u r n   - 1 ; 
         r e t u r n   1 ; 
     } , 
     s o r t _ m m d d :   f u n c t i o n ( a , b )   { 
         m t c h   =   a [ 0 ] . m a t c h ( s o r t t a b l e . D A T E _ R E ) ; 
         y   =   m t c h [ 3 ] ;   d   =   m t c h [ 2 ] ;   m   =   m t c h [ 1 ] ; 
         i f   ( m . l e n g t h   = =   1 )   m   =   ' 0 ' + m ; 
         i f   ( d . l e n g t h   = =   1 )   d   =   ' 0 ' + d ; 
         d t 1   =   y + m + d ; 
         m t c h   =   b [ 0 ] . m a t c h ( s o r t t a b l e . D A T E _ R E ) ; 
         y   =   m t c h [ 3 ] ;   d   =   m t c h [ 2 ] ;   m   =   m t c h [ 1 ] ; 
         i f   ( m . l e n g t h   = =   1 )   m   =   ' 0 ' + m ; 
         i f   ( d . l e n g t h   = =   1 )   d   =   ' 0 ' + d ; 
         d t 2   =   y + m + d ; 
         i f   ( d t 1 = = d t 2 )   r e t u r n   0 ; 
         i f   ( d t 1 < d t 2 )   r e t u r n   - 1 ; 
         r e t u r n   1 ; 
     } , 
     
     s h a k e r _ s o r t :   f u n c t i o n ( l i s t ,   c o m p _ f u n c )   { 
         / /   A   s t a b l e   s o r t   f u n c t i o n   t o   a l l o w   m u l t i - l e v e l   s o r t i n g   o f   d a t a 
         / /   s e e :   h t t p : / / e n . w i k i p e d i a . o r g / w i k i / C o c k t a i l _ s o r t 
         / /   t h a n k s   t o   J o s e p h   N a h m i a s 
         v a r   b   =   0 ; 
         v a r   t   =   l i s t . l e n g t h   -   1 ; 
         v a r   s w a p   =   t r u e ; 
 
         w h i l e ( s w a p )   { 
                 s w a p   =   f a l s e ; 
                 f o r ( v a r   i   =   b ;   i   <   t ;   + + i )   { 
                         i f   (   c o m p _ f u n c ( l i s t [ i ] ,   l i s t [ i + 1 ] )   >   0   )   { 
                                 v a r   q   =   l i s t [ i ] ;   l i s t [ i ]   =   l i s t [ i + 1 ] ;   l i s t [ i + 1 ]   =   q ; 
                                 s w a p   =   t r u e ; 
                         } 
                 }   / /   f o r 
                 t - - ; 
 
                 i f   ( ! s w a p )   b r e a k ; 
 
                 f o r ( v a r   i   =   t ;   i   >   b ;   - - i )   { 
                         i f   (   c o m p _ f u n c ( l i s t [ i ] ,   l i s t [ i - 1 ] )   <   0   )   { 
                                 v a r   q   =   l i s t [ i ] ;   l i s t [ i ]   =   l i s t [ i - 1 ] ;   l i s t [ i - 1 ]   =   q ; 
                                 s w a p   =   t r u e ; 
                         } 
                 }   / /   f o r 
                 b + + ; 
 
         }   / /   w h i l e ( s w a p ) 
     }     
 } 
 
 / *   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
       S u p p o r t i n g   f u n c t i o n s :   b u n d l e d   h e r e   t o   a v o i d   d e p e n d i n g   o n   a   l i b r a r y 
       * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *   * / 
 
 / /   D e a n   E d w a r d s / M a t t h i a s   M i l l e r / J o h n   R e s i g 
 
 / *   f o r   M o z i l l a / O p e r a 9   * / 
 i f   ( d o c u m e n t . a d d E v e n t L i s t e n e r )   { 
         d o c u m e n t . a d d E v e n t L i s t e n e r ( " D O M C o n t e n t L o a d e d " ,   s o r t t a b l e . i n i t ,   f a l s e ) ; 
 } 
 
 / *   f o r   I n t e r n e t   E x p l o r e r   * / 
 / * @ c c _ o n   @ * / 
 / * @ i f   ( @ _ w i n 3 2 ) 
         d o c u m e n t . w r i t e ( " < s c r i p t   i d = _ _ i e _ o n l o a d   d e f e r   s r c = j a v a s c r i p t : v o i d ( 0 ) > < \ / s c r i p t > " ) ; 
         v a r   s c r i p t   =   d o c u m e n t . g e t E l e m e n t B y I d ( " _ _ i e _ o n l o a d " ) ; 
         s c r i p t . o n r e a d y s t a t e c h a n g e   =   f u n c t i o n ( )   { 
                 i f   ( t h i s . r e a d y S t a t e   = =   " c o m p l e t e " )   { 
                         s o r t t a b l e . i n i t ( ) ;   / /   c a l l   t h e   o n l o a d   h a n d l e r 
                 } 
         } ; 
 / * @ e n d   @ * / 
 
 / *   f o r   S a f a r i   * / 
 i f   ( / W e b K i t / i . t e s t ( n a v i g a t o r . u s e r A g e n t ) )   {   / /   s n i f f 
         v a r   _ t i m e r   =   s e t I n t e r v a l ( f u n c t i o n ( )   { 
                 i f   ( / l o a d e d | c o m p l e t e / . t e s t ( d o c u m e n t . r e a d y S t a t e ) )   { 
                         s o r t t a b l e . i n i t ( ) ;   / /   c a l l   t h e   o n l o a d   h a n d l e r 
                 } 
         } ,   1 0 ) ; 
 } 
 
 / *   f o r   o t h e r   b r o w s e r s   * / 
 w i n d o w . o n l o a d   =   s o r t t a b l e . i n i t ; 
 
 / /   w r i t t e n   b y   D e a n   E d w a r d s ,   2 0 0 5 
 / /   w i t h   i n p u t   f r o m   T i n o   Z i j d e l ,   M a t t h i a s   M i l l e r ,   D i e g o   P e r i n i 
 
 / /   h t t p : / / d e a n . e d w a r d s . n a m e / w e b l o g / 2 0 0 5 / 1 0 / a d d - e v e n t / 
 
 f u n c t i o n   d e a n _ a d d E v e n t ( e l e m e n t ,   t y p e ,   h a n d l e r )   { 
 	 i f   ( e l e m e n t . a d d E v e n t L i s t e n e r )   { 
 	 	 e l e m e n t . a d d E v e n t L i s t e n e r ( t y p e ,   h a n d l e r ,   f a l s e ) ; 
 	 }   e l s e   { 
 	 	 / /   a s s i g n   e a c h   e v e n t   h a n d l e r   a   u n i q u e   I D 
 	 	 i f   ( ! h a n d l e r . $ $ g u i d )   h a n d l e r . $ $ g u i d   =   d e a n _ a d d E v e n t . g u i d + + ; 
 	 	 / /   c r e a t e   a   h a s h   t a b l e   o f   e v e n t   t y p e s   f o r   t h e   e l e m e n t 
 	 	 i f   ( ! e l e m e n t . e v e n t s )   e l e m e n t . e v e n t s   =   { } ; 
 	 	 / /   c r e a t e   a   h a s h   t a b l e   o f   e v e n t   h a n d l e r s   f o r   e a c h   e l e m e n t / e v e n t   p a i r 
 	 	 v a r   h a n d l e r s   =   e l e m e n t . e v e n t s [ t y p e ] ; 
 	 	 i f   ( ! h a n d l e r s )   { 
 	 	 	 h a n d l e r s   =   e l e m e n t . e v e n t s [ t y p e ]   =   { } ; 
 	 	 	 / /   s t o r e   t h e   e x i s t i n g   e v e n t   h a n d l e r   ( i f   t h e r e   i s   o n e ) 
 	 	 	 i f   ( e l e m e n t [ " o n "   +   t y p e ] )   { 
 	 	 	 	 h a n d l e r s [ 0 ]   =   e l e m e n t [ " o n "   +   t y p e ] ; 
 	 	 	 } 
 	 	 } 
 	 	 / /   s t o r e   t h e   e v e n t   h a n d l e r   i n   t h e   h a s h   t a b l e 
 	 	 h a n d l e r s [ h a n d l e r . $ $ g u i d ]   =   h a n d l e r ; 
 	 	 / /   a s s i g n   a   g l o b a l   e v e n t   h a n d l e r   t o   d o   a l l   t h e   w o r k 
 	 	 e l e m e n t [ " o n "   +   t y p e ]   =   h a n d l e E v e n t ; 
 	 } 
 } ; 
 / /   a   c o u n t e r   u s e d   t o   c r e a t e   u n i q u e   I D s 
 d e a n _ a d d E v e n t . g u i d   =   1 ; 
 
 f u n c t i o n   r e m o v e E v e n t ( e l e m e n t ,   t y p e ,   h a n d l e r )   { 
 	 i f   ( e l e m e n t . r e m o v e E v e n t L i s t e n e r )   { 
 	 	 e l e m e n t . r e m o v e E v e n t L i s t e n e r ( t y p e ,   h a n d l e r ,   f a l s e ) ; 
 	 }   e l s e   { 
 	 	 / /   d e l e t e   t h e   e v e n t   h a n d l e r   f r o m   t h e   h a s h   t a b l e 
 	 	 i f   ( e l e m e n t . e v e n t s   & &   e l e m e n t . e v e n t s [ t y p e ] )   { 
 	 	 	 d e l e t e   e l e m e n t . e v e n t s [ t y p e ] [ h a n d l e r . $ $ g u i d ] ; 
 	 	 } 
 	 } 
 } ; 
 
 f u n c t i o n   h a n d l e E v e n t ( e v e n t )   { 
 	 v a r   r e t u r n V a l u e   =   t r u e ; 
 	 / /   g r a b   t h e   e v e n t   o b j e c t   ( I E   u s e s   a   g l o b a l   e v e n t   o b j e c t ) 
 	 e v e n t   =   e v e n t   | |   f i x E v e n t ( ( ( t h i s . o w n e r D o c u m e n t   | |   t h i s . d o c u m e n t   | |   t h i s ) . p a r e n t W i n d o w   | |   w i n d o w ) . e v e n t ) ; 
 	 / /   g e t   a   r e f e r e n c e   t o   t h e   h a s h   t a b l e   o f   e v e n t   h a n d l e r s 
 	 v a r   h a n d l e r s   =   t h i s . e v e n t s [ e v e n t . t y p e ] ; 
 	 / /   e x e c u t e   e a c h   e v e n t   h a n d l e r 
 	 f o r   ( v a r   i   i n   h a n d l e r s )   { 
 	 	 t h i s . $ $ h a n d l e E v e n t   =   h a n d l e r s [ i ] ; 
 	 	 i f   ( t h i s . $ $ h a n d l e E v e n t ( e v e n t )   = = =   f a l s e )   { 
 	 	 	 r e t u r n V a l u e   =   f a l s e ; 
 	 	 } 
 	 } 
 	 r e t u r n   r e t u r n V a l u e ; 
 } ; 
 
 f u n c t i o n   f i x E v e n t ( e v e n t )   { 
 	 / /   a d d   W 3 C   s t a n d a r d   e v e n t   m e t h o d s 
 	 e v e n t . p r e v e n t D e f a u l t   =   f i x E v e n t . p r e v e n t D e f a u l t ; 
 	 e v e n t . s t o p P r o p a g a t i o n   =   f i x E v e n t . s t o p P r o p a g a t i o n ; 
 	 r e t u r n   e v e n t ; 
 } ; 
 f i x E v e n t . p r e v e n t D e f a u l t   =   f u n c t i o n ( )   { 
 	 t h i s . r e t u r n V a l u e   =   f a l s e ; 
 } ; 
 f i x E v e n t . s t o p P r o p a g a t i o n   =   f u n c t i o n ( )   { 
     t h i s . c a n c e l B u b b l e   =   t r u e ; 
 } 
 
 / /   D e a n ' s   f o r E a c h :   h t t p : / / d e a n . e d w a r d s . n a m e / b a s e / f o r E a c h . j s 
 / * 
 	 f o r E a c h ,   v e r s i o n   1 . 0 
 	 C o p y r i g h t   2 0 0 6 ,   D e a n   E d w a r d s 
 	 L i c e n s e :   h t t p : / / w w w . o p e n s o u r c e . o r g / l i c e n s e s / m i t - l i c e n s e . p h p 
 * / 
 
 / /   a r r a y - l i k e   e n u m e r a t i o n 
 i f   ( ! A r r a y . f o r E a c h )   {   / /   m o z i l l a   a l r e a d y   s u p p o r t s   t h i s 
 	 A r r a y . f o r E a c h   =   f u n c t i o n ( a r r a y ,   b l o c k ,   c o n t e x t )   { 
 	 	 f o r   ( v a r   i   =   0 ;   i   <   a r r a y . l e n g t h ;   i + + )   { 
 	 	 	 b l o c k . c a l l ( c o n t e x t ,   a r r a y [ i ] ,   i ,   a r r a y ) ; 
 	 	 } 
 	 } ; 
 } 
 
 / /   g e n e r i c   e n u m e r a t i o n 
 F u n c t i o n . p r o t o t y p e . f o r E a c h   =   f u n c t i o n ( o b j e c t ,   b l o c k ,   c o n t e x t )   { 
 	 f o r   ( v a r   k e y   i n   o b j e c t )   { 
 	 	 i f   ( t y p e o f   t h i s . p r o t o t y p e [ k e y ]   = =   " u n d e f i n e d " )   { 
 	 	 	 b l o c k . c a l l ( c o n t e x t ,   o b j e c t [ k e y ] ,   k e y ,   o b j e c t ) ; 
 	 	 } 
 	 } 
 } ; 
 
 / /   c h a r a c t e r   e n u m e r a t i o n 
 S t r i n g . f o r E a c h   =   f u n c t i o n ( s t r i n g ,   b l o c k ,   c o n t e x t )   { 
 	 A r r a y . f o r E a c h ( s t r i n g . s p l i t ( " " ) ,   f u n c t i o n ( c h r ,   i n d e x )   { 
 	 	 b l o c k . c a l l ( c o n t e x t ,   c h r ,   i n d e x ,   s t r i n g ) ; 
 	 } ) ; 
 } ; 
 
 / /   g l o b a l l y   r e s o l v e   f o r E a c h   e n u m e r a t i o n 
 v a r   f o r E a c h   =   f u n c t i o n ( o b j e c t ,   b l o c k ,   c o n t e x t )   { 
 	 i f   ( o b j e c t )   { 
 	 	 v a r   r e s o l v e   =   O b j e c t ;   / /   d e f a u l t 
 	 	 i f   ( o b j e c t   i n s t a n c e o f   F u n c t i o n )   { 
 	 	 	 / /   f u n c t i o n s   h a v e   a   " l e n g t h "   p r o p e r t y 
 	 	 	 r e s o l v e   =   F u n c t i o n ; 
 	 	 }   e l s e   i f   ( o b j e c t . f o r E a c h   i n s t a n c e o f   F u n c t i o n )   { 
 	 	 	 / /   t h e   o b j e c t   i m p l e m e n t s   a   c u s t o m   f o r E a c h   m e t h o d   s o   u s e   t h a t 
 	 	 	 o b j e c t . f o r E a c h ( b l o c k ,   c o n t e x t ) ; 
 	 	 	 r e t u r n ; 
 	 	 }   e l s e   i f   ( t y p e o f   o b j e c t   = =   " s t r i n g " )   { 
 	 	 	 / /   t h e   o b j e c t   i s   a   s t r i n g 
 	 	 	 r e s o l v e   =   S t r i n g ; 
 	 	 }   e l s e   i f   ( t y p e o f   o b j e c t . l e n g t h   = =   " n u m b e r " )   { 
 	 	 	 / /   t h e   o b j e c t   i s   a r r a y - l i k e 
 	 	 	 r e s o l v e   =   A r r a y ; 
 	 	 } 
 	 	 r e s o l v e . f o r E a c h ( o b j e c t ,   b l o c k ,   c o n t e x t ) ; 
 	 } 
 } ; 
 
