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