/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){
  var request_o; //declare the variable to hold the object.
  var browser = navigator.appName; //find the browser name
  if(browser == "Microsoft Internet Explorer"){
    /* Create the object using MSIE's method */
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    /* Create the object using other browser's method */
    request_o = new XMLHttpRequest();
  }
return request_o; //return the object
}

var http = createRequestObject();   //Create and hold the http request object.

/* AJAX function 1 to retreive sub categories via the PHP server file.   */
/* This function is called from the onChange dropdown of main categories */
/* with the value that was selected as a param to this function.         */
/* ===================================================================== */
function getSubCats(sel_val){

  /* If user chose to suggest a new main category, pop open the */
  /* suggestion window now and exit here */
  if (sel_val == 'suggest') {
    window.open ('http://www.myexp.org/suggest_cat.html','suggest_cat','location=1,status=1,menubar=no,toolbar=no,resizable=yes,width=430,height=340');
  exit;
  }

  /* First paint a message in the empty div, saying "Retrieving sub cats". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSC_message = progress_circle + 'Retrieving sub categories, please wait...';
  document.getElementById('sub_cat_list').innerHTML = RSC_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'categories.php?main_cat=' + sel_val;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleProducts;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}

/* Function called to handle the data returned from the php file. */
function handleProducts(){
 if(http.readyState == 4){
   var response = http.responseText;  //Get the response as text.
   //This is how the HTML target empty div is changed dynamically.
   document.getElementById('sub_cat_list').innerHTML = response;

//alert(response);

 }
}

/*
Version of getSubCats() for the submit process with no action on change sub.
*/ 

function getSubCats_passive(sel_val){

  /* If user chose to suggest a new main category, pop open the */
  /* suggestion window now and exit here */
  if (sel_val == 'suggest') {
    window.open ('http://www.myexp.org/suggest_cat.html','suggest_cat','location=1,status=1,menubar=no,toolbar=no,resizable=yes,width=430,height=340');
  exit;
  }

  /* First paint a message in the empty div, saying "Retrieving sub cats". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSC_message = progress_circle + 'Retrieving sub categories, please wait...';
  document.getElementById('sub_cat_list').innerHTML = RSC_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'categories_passive.php?main_cat=' + sel_val;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleProducts_passive;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}

/* Function called to handle the data returned from the php file. */
function handleProducts_passive(){
 if(http.readyState == 4){
   var response = http.responseText;  //Get the response as text.
   //This is how the HTML target empty div is changed dynamically.
   document.getElementById('sub_cat_list').innerHTML = response;

//alert('Hello from getSubCats_passive, code retieved is: ' + response);

 }
}

/* AJAX function 2 to retrieve story links. */
/* ======================================== */
function get_stories(main_cat_picked, sub_cat_picked){

  //alert('From get_stories function, main and sub cats are: ' + main_cat_picked + ', ' + sub_cat_picked);

  /* If user chose to suggest a new sub category, pop open the */
  /* suggestion window now and exit here */
  if (sub_cat_picked == 'suggest') {
    window.open ("http://www.myexp.org/suggest_cat.html","suggest_cat","location=1,status=1,menubar=0,toolbar=0,resizable=0,width=430,height=340");
  exit;
  }

  /* First paint a message in the empty div, saying "Retrieving stories". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSP_message = progress_circle + 'Retrieving entries, please wait...';
  document.getElementById('links_list').innerHTML = RSP_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'get_stories.php?main_cat_p=' + main_cat_picked + '&sub_cat_p=' + sub_cat_picked;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleStories;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}


/* Function called to handle the data returned from the php file. */
function handleStories(){
 if(http.readyState == 4){
   var response_2 = http.responseText;  //Get the response as text.
   //This is how the HTML target div is changed dynamically.
   document.getElementById('links_list').innerHTML = response_2;

//alert(response_2);

 }
}

/* Function to activate the Step 2 submission button.     */
/* It also handles suggestion of new sub cat from submit. */
/* ====================================================== */
function activateButt_browse(sub_cat_picked) {

  /* If user chose to suggest a new sub category, pop open the */
  /* suggestion window now and exit here */
  if (sub_cat_picked == 'suggest') {
    window.open ("http://www.myexp.org/suggest_cat.html","suggest_cat","location=1,status=1,menubar=0,toolbar=0,resizable=0,width=430,height=340");
  exit;
  }
  // Activate the button when sub cat is picked (changed).
  document.submit_form.go.disabled=false;

  // Code not used!
//  var active_code = '<img src="images/go_browse_button_t.gif" alt="Go browse button" name="BrowseButton" style="border:0px" />';
// document.getElementById('browse_button').innerHTML = active_code;

}


/* AJAX function 3 to retrieve the story. */
/* ======================================== */
function fetch_story(story_id) {

//  alert('From the fetch_story function, the story ID is: ' + story_id);

  /* First paint a message in the empty div, saying "Retrieving story". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSP_message = progress_circle + 'Retrieving the entry, please wait...';
  document.getElementById('story_text').innerHTML = RSP_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'get_the_story.php?the_story_id=' + story_id;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleStory;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}


/* Function called to handle the data returned from the php file. */
function handleStory(){
 if(http.readyState == 4){
   var response_3 = http.responseText;  //Get the response as text.
   //This is how the HTML target div is changed dynamically.
   document.getElementById('story_text').innerHTML = response_3;
//   document.getElementsByName('story_text_area').innerHTML = response_3;

//alert(response_3);

 }
}

/* AJAX function 4 to retrieve counter links. */
/* ========================================== */
function get_counters(story_id_picked){

  //alert('From get_stories function, main and sub cats are: ' + main_cat_picked + ', ' + sub_cat_picked);

  /* First paint a message in the empty div, saying "Retrieving counters". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSP_message = progress_circle + 'Retrieving entries, please wait...';
  document.getElementById('links_list').innerHTML = RSP_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'get_counters.php?story_id=' + story_id_picked;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleCounters;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}


/* Function called to handle the data returned from the php file. */
function handleCounters(){
 if(http.readyState == 4){
   var response_4 = http.responseText;  //Get the response as text.
   //This is how the HTML target div is changed dynamically.
   document.getElementById('links_list').innerHTML = response_4;

//alert(response_4);

 }
}

/* AJAX function 5 to retrieve the counter. */
/* ======================================== */
function fetch_counter(counter_id) {

//  alert('From the fetch_story function, the story ID is: ' + story_id);

  /* First paint a message in the empty div, saying "Retrieving story". */
  /* This will be helpful if the process takes a few seconds.              */
  var progress_circle = '<img src="images/progress_circle_small.gif" alt="CD_cover" style="margin:0px" /> ';
  var RSP_message = progress_circle + 'Retrieving the entry, please wait...';
  document.getElementById('story_text').innerHTML = RSP_message;

  /* Call the PHP script file. */
  // Build the 2nd parameter line first in a single var.
  var call_line = 'get_the_counter.php?the_counter_id=' + counter_id;

  http.open("get", call_line, true);

  /* Make sure we don't get a cached version of the feed!!!                 */
  /* One of these lines below (the last one tested) seems to be able        */
  /* to tell stupid IE to not cache the result from the server and          */
  /* show the same thing all the time.  FireFox does not have this problem. */
  /* How sweet it would be to charge Microsoft two days of my work wasted   */
  /* on this stupid IE problem.                                             */
  http.setRequestHeader('Pragma','no-cache');
  http.setRequestHeader('Cache-Control', 'must-revalidate');
  http.setRequestHeader('Cache-Control', 'no-cache');
  http.setRequestHeader( 'Cache-Control', 'no-store');
  http.setRequestHeader('Expires', 0);
  http.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");

  /* Define a function name to call for the results back. */
  http.onreadystatechange = handleCounter;

  /* Send the data to the PHP file - null is needed for the GET method. */
  http.send(null);
}


/* Function called to handle the data returned from the php file. */
function handleCounter(){
 if(http.readyState == 4){
   var response_5 = http.responseText;  //Get the response as text.
   //This is how the HTML target div is changed dynamically.
   document.getElementById('story_text').innerHTML = response_5;
//   document.getElementsByName('story_text_area').innerHTML = response_5;

//alert(response_3);

 }
}

