ÿþ/ / * * *   T h i s   c o d e   i s   c o p y r i g h t   2 0 0 2 - 2 0 0 3   b y   G a v i n   K i s t n e r ,   g a v i n @ r e f i n e r y . c o m  
 / / * * *   I t   i s   c o v e r e d   u n d e r   t h e   l i c e n s e   v i e w a b l e   a t   h t t p : / / p h r o g z . n e t / J S / _ R e u s e L i c e n s e . t x t  
 / / * * *   R e u s e   o r   m o d i f i c a t i o n   i s   f r e e   p r o v i d e d   y o u   a b i d e   b y   t h e   t e r m s   o f   t h a t   l i c e n s e .  
 / / * * *   ( I n c l u d i n g   t h e   f i r s t   t w o   l i n e s   a b o v e   i n   y o u r   s o u r c e   c o d e   s a t i s f i e s   t h e   c o n d i t i o n s . )  
  
 / /   A d d   a   n e w   s t y l e s h e e t   t o   t h e   d o c u m e n t ;  
 / /   u r l   [ o p t i o n a l ]   A   u r l   t o   a n   e x t e r n a l   s t y l e s h e e t   t o   u s e  
 / /   i d x   [ o p t i o n a l ]   T h e   i n d e x   i n   d o c u m e n t . s t y l e S h e e t s   t o   i n s e r t   t h e   n e w   s h e e t   b e f o r e  
 f u n c t i o n   A d d S t y l e S h e e t ( u r l , i d x ) {  
 	 v a r   c s s , b e f o r e = n u l l , h e a d = 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 ( " h e a d " ) [ 0 ] ;  
  
 	 i f   ( d o c u m e n t . c r e a t e E l e m e n t ) {  
 	 	 i f   ( u r l ) {  
 	 	 	 c s s   =   d o c u m e n t . c r e a t e E l e m e n t ( ' l i n k ' ) ;  
 	 	 	 c s s . r e l     =   ' s t y l e s h e e t ' ;  
 	 	 	 c s s . h r e f   =   u r l ;  
 	 	 }   e l s e   c s s   =   d o c u m e n t . c r e a t e E l e m e n t ( ' s t y l e ' ) ;  
 	 	 c s s . m e d i a   =   ' a l l ' ;  
 	 	 c s s . t y p e     =   ' t e x t / c s s ' ;  
  
 	 	 i f   ( i d x > = 0 ) {  
 	 	 	 f o r   ( v a r   i = 0 , c t = 0 , l e n = h e a d . c h i l d N o d e s . l e n g t h ; i < l e n ; i + + ) {  
 	 	 	 	 v a r   e l   =   h e a d . c h i l d N o d e s [ i ] ;  
 	 	 	 	 i f   ( ! e l . t a g N a m e )   c o n t i n u e ;  
 	 	 	 	 v a r   t a g N a m e   =   e l . t a g N a m e . t o L o w e r C a s e ( ) ;  
 	 	 	 	 i f   ( c t = = i d x ) {  
 	 	 	 	 	 b e f o r e   =   e l ;  
 	 	 	 	 	 b r e a k ;  
 	 	 	 	 }  
 	 	 	 	 i f   ( t a g N a m e = = ' s t y l e '   | |   t a g N a m e = = ' l i n k '   & &   ( e l . r e l   & &   e l . r e l . t o L o w e r C a s e ( ) = = ' s t y l e s h e e t '   | |   e l . t y p e   & &   e l . t y p e . t o L o w e r C a s e ( ) = = ' t e x t / c s s ' )   )   c t + + ;  
 	 	 	 }  
 	 	 }  
 	 	 h e a d . i n s e r t B e f o r e ( c s s , b e f o r e ) ;  
  
 	 	 r e t u r n   d o c u m e n t . s t y l e S h e e t s [ b e f o r e ? i d x : d o c u m e n t . s t y l e S h e e t s . l e n g t h - 1 ] ;  
 	 }   e l s e   r e t u r n   a l e r t ( " I   c a n ' t   c r e a t e   a   n e w   s t y l e s h e e t   f o r   y o u .   S o r r y . " ) ;  
 }  
 / /   e . g .   v a r   n e w B l a n k S h e e t A f t e r A l l O t h e r s   =   A d d S t y l e S h e e t ( ) ;    
 / /   e . g .   v a r   n e w B l a n k S h e e t B e f o r e A l l O t h e r s   =   A d d S t y l e S h e e t ( n u l l , 0 ) ;  
 / /   e . g .   v a r   e x t e r n a l S h e e t A f t e r O t h e r s   =   A d d S t y l e S h e e t ( ' h t t p : / / p h r o g z . n e t / J S / C l a s s e s / d o c s . c s s ' ) ;  
 / /   e . g .   v a r   e x t e r n a l S h e e t B e f o r e O t h e r s   =   A d d S t y l e S h e e t ( ' h t t p : / / p h r o g z . n e t / J S / C l a s s e s / d o c s . c s s ' , 0 ) ;  
  
  
 / /   C r o s s - b r o w s e r   m e t h o d   f o r   i n s e r t i n g   a   n e w   r u l e   i n t o   a n   e x i s t i n g   s t y l e s h e e t .  
 / /   s s               -   T h e   s t y l e s h e e t   t o   s t i c k   t h e   n e w   r u l e   i n  
 / /   s e l e c t o r   -   T h e   s t r i n g   v a l u e   t o   u s e   f o r   t h e   r u l e   s e l e c t o r  
 / /   s t y l e s       -   T h e   s t r i n g   s t y l e s   t o   u s e   w i t h   t h e   r u l e  
 f u n c t i o n   A d d R u l e ( s s , s e l e c t o r , s t y l e s ) {  
 	 i f   ( ! s s )   r e t u r n   f a l s e ;  
 	 i f   ( s s . i n s e r t R u l e )   r e t u r n   s s . i n s e r t R u l e ( s e l e c t o r + '   { ' + s t y l e s + ' } ' , s s . c s s R u l e s . l e n g t h ) ;  
 	 i f   ( s s . a d d R u l e ) {  
 	 	 s s . a d d R u l e ( s e l e c t o r , s t y l e s ) ;  
 	 	 r e t u r n   t r u e ;  
 	 }  
 	 r e t u r n   f a l s e ;  
 }  
  
 / /   e . g .   A d d R u l e (   d o c u m e n t . s t y l e S h e e t s [ 0 ]   ,   ' a : l i n k '   ,   ' c o l o r : b l u e ;   t e x t - d e c o r a t i o n : u n d e r l i n e '   ) ;  
 / /   e . g .   A d d R u l e (   A d d S t y l e S h e e t ( )   ,   ' h r '   ,   ' d i s p l a y : n o n e '   ) ; 
