

function ajaxObject(layer, url,log, inCB) {                                    // This is the object constructor
   var that=this;                                                    // A workaround for some javascript idiosyncrocies
   var updating = false;  
   var AJAX=null;  
   this.CB=inCB;                                         // Set to true if this object is already working on a request
   this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.
     log.debug("ciao from ajax Object");
                                            
   this.abort = function() {
    log.debug("aborting funcitons");
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }

     
   this.update = function(passData) {   
   log.debug("entering update function");                             // Initiates the server call.
      if (updating==true) { 
        log.debug("updating is set to true - returning from the function");
          return false; 
      }  
      //alert("entering update function");                         .
      updating=true;                                                 // Set the updating flag.
      that.AJAX = null;                                               // Initialize the AJAX variable.
      if (window.XMLHttpRequest) {                                   // Are we working with mozilla?
         that.AJAX=new XMLHttpRequest();                                  //  Yes -- this is mozilla.
      } else {                                                       // Not Mozilla, must be IE
         that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");                //  Wheee, ActiveX, how do we format c: again?
      }                                                              // End setup Ajax.
      if (that.AJAX==null) {                                              // If we couldn't initialize Ajax...
         alert("Your browser doesn't support AJAX.");                // Sorry msg.                                              
         return false                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
      } else {
         that.AJAX.onreadystatechange = function() {                      // When the browser has the request info..
            if (that.AJAX.readyState==4 || that.AJAX.readyState=="complete") { //   see if the complete flag is set.
               log.debug("ajax is in state  4"); 
                
               //LayerID.innerHTML=AJAX.responseText; 
               document.getElementById(thisLayer).innerHTML=that.AJAX.responseText;  
               
                                //   It is, so put the new data in the object's layer
          //     delete AJAX;
               that.AJAX=null;                                          //   delete the AJAX object since it's done.
               updating=false;                                       //   Set the updating flag to false so we can do a new request
               log.debug("about to call testing");
             //  log.debug("the value of CB is: "+CB);
                              log.debug("the value of that.CB is: "+that.CB); 
               if (that.CB!=null){
                   log.debug("calling testing");
                  
   /// this part of the code reloads the javascript code sent back by server through response Text.               
    var x = document.getElementById(thisLayer).getElementsByTagName("script");   
    for( var i=0; i < x.length; i++) {  
      eval(x[i].text);  
    } 
       
       
                  
        eval(that.CB);    /// we are calling the function by its name
              log.debug("end of calling testing");    
               }  else {
                   log.debug("call back is null or undefined");
               }

               that.callback();                                      //   Call the post-processing function.
            }                                                        // End Ajax readystate check.
         }  
         
         
                                             var browserName=navigator.appName;
                                    if (browserName=="Microsoft Internet Explorer")
                                    {                                         
                                         document.getElementById(thisLayer).innerHTML="<table cellpadding='0' cellspacing='0' border='0' width='100%' bgcolor='#ffffff' height='400'><tr><td width='100%' align='center' valign='middle'><img src='images/ajax-loader.gif' border='0'/></td></tr></table>";
                                      // document.getElementById('filter_pane_div').innerHTML="<p></p> "; 
                                    }
                                    else 
                                    { 
                                        document.getElementById(thisLayer).innerHTML="<table cellpadding='0' cellspacing='0' border='0' width='100%' bgcolor='#ffffff' height='400'><tr><td width='100%' align='center' valign='middle'><img src='images/ajax-loader.gif' border='0'/></td></tr></table>";
                                        //  document.getElementById('filter_pane_div').innerHTML="<p></p> "; 
                                    }
         
                                                                  // End create post-process fucntion block.
         var timestamp = new Date();                                 // Get a new date (this will make the url unique)
         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);   // Append date to url (so the browser doesn't cache the call)
        var uriTest=urlCall+'?'+'&timestamp='+(timestamp*1); 
         var params=passData+'&timestamp='+(timestamp*1);
         log.debug("uri is "+uri);
         log.debug("url Call is:"+urlCall);
 
         log.debug("opening post");

        that.AJAX.open("POST", urlCall, true);      

        log.debug("setting request header");
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-length", params.length);
  //              that.AJAX.setRequestHeader("Cache-control", "no-cache, no-store, must-revalidate");
        that.AJAX.setRequestHeader("Connection", "close")
        that.AJAX.send(params);      
                                             
         return true;                                                // Everything went a-ok.
      }                                                              // End Ajax setup aok if/else block                 
   }
      
   // This area set up on constructor calls.
   var thisLayer=layer;
   var LayerID = document.getElementById(layer);                     // Remember the layer associated with this object.
   var urlCall = url;                                                // Remember the url associated with this object.
}         



 function showFilterPanel(filter,log){
     
     log.debug("entering show filter Panel function");
    
 /// initailly we set all the buttons to inactive    
    document.getElementById("filter_topic_div").className ="filter_inactive";  
     document.getElementById("filter_topic_td").style.backgroundImage ="url(images/filter_inactiveNEW.gif)";
     log.debug("passed the first test");
    document.getElementById("filter_symptom_div").className ="filter_inactive";  
         document.getElementById("filter_symptom_td").style.backgroundImage ="url(images/filter_inactiveNEW.gif)";  
              log.debug("passed the second test");      
    document.getElementById("filter_treatment_div").className ="filter_inactive";  
         document.getElementById("filter_treatment_td").style.backgroundImage ="url(images/filter_inactiveNEW.gif)";   
              log.debug("passed the third test");     
    document.getElementById("filter_term_div").className ="filter_inactive";  
         document.getElementById("filter_term_td").style.backgroundImage ="url(images/filter_inactiveNEW.gif)";    

/// only certain buttons should be active        
log.debug("entering switch command");        
                switch (filter) {
                    case 'filter_topic':
                   document.getElementById("filter_topic_div").className ="filter_active"; 
                    document.getElementById("filter_topic_td").style.backgroundImage ="url(images/filter_activeNEW.gif)";     
                    break; 
                    case 'filter_symptom':
                   document.getElementById("filter_symptom_div").className ="filter_active"; 
                        document.getElementById("filter_symptom_td").style.backgroundImage ="url(images/filter_activeNEW.gif)";     
                    break;
                    case 'filter_treatment':
                   document.getElementById("filter_treatment_div").className ="filter_active";
                      document.getElementById("filter_treatment_td").style.backgroundImage ="url(images/filter_activeNEW.gif)";     
                    break;
                    case 'filter_term':
                   document.getElementById("filter_term_div").className ="filter_active";  
                       document.getElementById("filter_term_td").style.backgroundImage ="url(images/filter_activeNEW.gif)";     
                    break;
                    default:
                    log.debug("no appropriate filter selected");

                };
                log.debug("exiting the showFilter Panel function");

 }
 
 
  function showActionPanel(selected_tab,log){
     
     log.debug("entering show action Panel function");
    
 /// initailly we set all the buttons to inactive    
    document.getElementById("search_action_panel_treatment_td").style.display ="none";  
   // document.getElementById("search_action_panel_treatment_td").width='0';
    document.getElementById("search_action_panel_web_td").style.display ="none";
   //     document.getElementById("search_action_panel_web_td").width='0';
    document.getElementById("search_action_panel_question_td").style.display ="none";
   // document.getElementById("search_action_panel_question_td").width='0';
    document.getElementById("search_action_panel_no_results_td").style.display ="none";
   //     document.getElementById("search_action_panel_no_results_td").width='0';

/// only certain buttons should be active        
log.debug("entering switch command");        
                switch (selected_tab) {
                    case 'treatments' :
    document.getElementById("search_action_panel_treatment_td").style.display ="block";  
    //document.getElementById("search_action_panel_treatment_td").width='253';    
                    break; 
                    
                case 'goals' :
    document.getElementById("search_action_panel_treatment_td").style.display ="block";      
                    break; 
                    case 'web':
    document.getElementById("search_action_panel_web_td").style.display ="block"; 
  //          document.getElementById("search_action_panel_web_td").width='253';      
                    break;
                    case 'questions':
    document.getElementById("search_action_panel_question_td").style.display ="block";   
     //   document.getElementById("search_action_panel_question_td").width='253';      
                    break;
                    case 'articles' :
    document.getElementById("search_action_panel_article_td").style.display ="block";  
    //document.getElementById("search_action_panel_treatment_td").width='253';    
                    break;                    
                    case 'no_results':
    document.getElementById("search_action_panel_no_results_td").style.display ="block"; 
    //        document.getElementById("search_action_panel_no_results_td").width='253';       
                    break;
                    default:
                    log.debug("no appropriate tab selected");

                };
                
       //             document.getElementById("test_td").width='253';   
       //                                 document.getElementById("test_table").width='253'; 
       //                                 document.getElementById("big_table").width='903'; 
                log.debug("exiting the showAction Panel function");

 }
 
 
 function showFindPanelNEW(){
         //    alert ('find panel called');
         document.getElementById('master_tabsNEW_table').width='638';  
         document.getElementById("master_tabsNEW_table").style.backgroundImage ="url(images/cat_sort_border_top.gif)";  

         
                  document.getElementById('filter_pane_div').style.display='block'; 
         document.getElementById('corner_story_pane_div').style.display='none';          
         document.getElementById('corner_question_pane_div').style.display='none'; 
         document.getElementById('corner_link_pane_div').style.display='none'; 
     
     
    document.getElementById("findPanel").style.display = 'block';
    document.getElementById("storyPanel").style.display = 'none';
    document.getElementById("questionPanel").style.display = 'none';
    document.getElementById("linkPanel").style.display = 'none';    
}



function check_alerts_phrase(ajaxObject_Alerts_Phrase,log){
        log.debug("entering check_alerts_phrase function");  
    ap_stories=0;
    ap_questions=0;
    ap_best_web=0;
    ap_articles=0;
    ap_videos=0;
    
    

    if (document.getElementById('ap_stories')!=null){
        if (document.getElementById('ap_stories').checked){
          ap_stories=1;  
        } 
    }
        if (document.getElementById('ap_questions')!=null){
        if (document.getElementById('ap_questions').checked){
          ap_questions=1;  
       //   alert('question is one');
        }    else {
       //               alert('question is zero');  
        }
    }
        if (document.getElementById('ap_best_web')!=null){
        if (document.getElementById('ap_best_web').checked){
          ap_best_web=1;  
        } 
    }
        if (document.getElementById('ap_articles')!=null){
        if (document.getElementById('ap_articles').checked){
          ap_articles=1;  
        } 
    }
        if (document.getElementById('ap_videos')!=null){
        if (document.getElementById('ap_videos').checked){
          ap_videos=1;  
        } 
    }
    
    ajaxObject_Alerts_Phrase.update('ap_stories='+ap_stories+'&ap_questions='+ap_questions+
    '&ap_best_web='+ap_best_web+'&ap_articles='+ap_articles+'&ap_videos='+ap_videos, log);
    
    
        log.debug("exiting check_alerts_phrase function");  
}


function check_alerts_phrase_diary_panel(id,field,phrase,ajaxObject_Alerts_Phrase_Diary_Panel,log){
        log.debug("entering check_alerts_phrase_diary_panel function"); 
         
     if(document.getElementById(id).checked == true){
        field_value = 1;
    }else{
        field_value = 0;
    }
 //     alert(field+'='+field_value+'&phrase='+phrase);
    
    ajaxObject_Alerts_Phrase_Diary_Panel.update('field='+field+'&field_value='+field_value+
    '&phrase='+phrase, log);
    
    
        log.debug("exiting check_alerts_phrase_diary_panel function");  
}

  
