Event.observe(document, 'dom:loaded',
    function(e) {
       
       function isZip(s) {
            // Check for correct zip code
            reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
            if (!reZip.test(s) || s == 00000) {
                return false;
            }
            return true;
        }
        
       //re enable submit button on load. Needed when user hits back button!
       if ( (x = $('courseFinderSubmit')) ){
            x.disabled = false;
       
            Event.observe('course-form', 'submit', function(event) {
                
                //clear any remaining alerts
                if ($('finder-empty') || $('invalid-zip')) {
                    $('finder-empty').hide();
                    $('invalid-zip').hide();
                }   
                
                if( ($('range-city').value == '') && ($('range-zip').value == '') && ($('range-state').value == '') ) {
                    Event.stop(event);
                    new Effect.BlindDown( 'finder-empty', { duration:.5, queue:'global' });
                    /*new Effect.BlindUp(
                        'finder-empty',
                        {queue: 'global',delay: 4}
                    );*/
                } else if( isZip($('range-zip').value) == false && ($('range-city').value == '') && ($('range-state').value == '') ) {
                    Event.stop(event);
                    new Effect.BlindDown('invalid-zip', { duration:.5, queue: 'global' });
                } else {
                    $('courseFinderSubmit').value = 'Searching...';
                    $('courseFinderSubmit').disabled = true;
                }
            });
       }
       
       //re enable submit button on load. Needed when user hits back button!
       if ( (x = $('proFinderSubmit')) ){
            x.disabled = false;
            Event.observe('pro-form', 'submit', function(event) {
                //clear any remaining alerts
                if ($('finder-empty') || $('invalid-zip')) {
                    $('finder-empty').hide();
                    $('invalid-zip').hide();
                }   
                
                if( ($('pro-city').value == '') && ($('pro-zip').value == '') && ($('pro-state').value == '') ) {
                    Event.stop(event);
                    new Effect.BlindDown('pro-finder-empty', { duration:.5, queue:'global' });
                    /*new Effect.BlindUp(
                        'finder-empty',
                        {queue: 'global',delay: 4}
                    );*/
                } else if( isZip($('pro-zip').value) == false && ($('pro-city').value == '') && ($('pro-state').value == '') ) {
                    Event.stop(event);
                    new Effect.BlindDown('pro-invalid-zip', { duration:.5, queue: 'global' });
                } else {
                    $('proFinderSubmit').value = 'Searching...';
                    $('proFinderSubmit').disabled = true;
                }
            });
       }
       
       if ( (x = $('rangeFinderSumbit')) ) {
            //validation and 'Searching...' button value here
       }
       
       //Google Maps
       if($('map')){
       	$$('#map small').invoke('hide');
       	gmload();
       }
       
       //if #commentform is there
        if ($('commentform')) {
            //Hide the #repond div
            
            $$('.cat4respond').invoke('hide');
            $$('.cat4respond #btn_close').invoke('show');
            $$('.cat4respond').invoke('addClassName','comment-pop');
            
            $$('.cat6respond').invoke('hide');
            $$('.cat6respond #btn_close').invoke('show');
            $$('.cat6respond').invoke('addClassName','comment-pop');
            
            $$('.cat3respond').invoke('hide');
            $$('.cat3respond #btn_close').invoke('show');
            $$('.cat3respond').invoke('addClassName','comment-pop');
            
            if ($('btn_rate')){
                Event.observe('btn_rate', 'click', function(event) {
                    Event.stop(event);
                    
                    
                    //if we have a threaded comment form open we must close it.
                    if($('commentform')){
                        //cancel reply function from wp-threaded-comment.php line 885
                        movecfm(null,0,1,null);
                    }
                    
                    new Effect.Appear('respond', {to: 1, duration: .5});
                    
                });
            }

            //Close button for comment pop-up
            Event.observe('btn_close', 'click', function(event) {
                Event.stop(event);
                new Effect.Fade('respond', {to: 0, duration: .5});
                //$('respond').removeClassName('comment-pop');
                //$('overlay').hide();
            });
            
            Event.observe('overlay', 'click', function(event) {
                Event.stop(event);
                $('respond').hide();
            });
            
            Event.observe('bottom-review-btn', 'click', function(event){
                Event.stop(event);
                new Effect.Appear('respond', {to: 1, duration: .5});
                
                $('respond').scrollTo();
            });
        }
       
    }
    
    
);

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;