
/* the XMLHttp object for communicating with the server */
var xmlHttpGetLJPosting = createXmlHttpRequestObject();

var xmlAllLJPostings;
var post_num=0;
var news_num=0;

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

/* initiate HTTP request to retrieve suggestions for the current keyword */
function getLJPosting() 
{    
        document.getElementById("n_arr_next").style.visibility = "hidden";
        showNews(0);
       
        document.getElementById("arr_prev").style.visibility = "hidden";
        document.getElementById("arr_next").style.visibility = "hidden";

         /* if the XMLHttpRequest object isn't busy with a previous request... */
          if (xmlHttpGetLJPosting.readyState == 4 || 
              xmlHttpGetLJPosting.readyState == 0) 
          {   		  
            xmlHttpGetLJPosting.open("GET", "servlet/MyAJAX", true);
            xmlHttpGetLJPosting.onreadystatechange = handleGetLJPosting; 
            xmlHttpGetLJPosting.send(null);
          }
          // if the XMLHttpRequest object is busy...
          else
          {
            timeoutId = setTimeout("getLJPosting()", 1000);
          }
  }

  /* handles the server's response containing the suggestions for the requested keyword  */
function handleGetLJPosting()
{
  //if the process is completed, decide what to do with the returned data
  if (xmlHttpGetLJPosting.readyState == 4) 
  {
    // only if HTTP status is "OK"
    if (xmlHttpGetLJPosting.status == 200) 
    { 
          var xmlResponse = xmlHttpGetLJPosting.responseXML;

         xmlAllLJPostings = xmlResponse;
          //xmlDocumentElement = xmlResponse.documentElement;
         //helloMessage=xmlDocumentElement.firstChild.data;
         showPosting(0);
    }
}
}


function showPrevPosting()
 {
    post_num++;
    showPosting(post_num);
 }

function showNextPosting()
 {
    post_num--;
    showPosting(post_num);
 }

  function showPosting(num)
{
  if (num==24)   //total_postings=LJItem.length;
       document.getElementById("arr_prev").style.visibility = "hidden";
 else
       document.getElementById("arr_prev").style.visibility = "visible";

 if (num==0)
      document.getElementById("arr_next").style.visibility = "hidden";
else
      document.getElementById("arr_next").style.visibility = "visible";

   LJItem=xmlAllLJPostings.getElementsByTagName('item');

if(navigator.appName.indexOf("Explorer")!=-1) //this is explorer
{
  LJTitle = LJItem(post_num).getElementsByTagName('title');
  LJText = LJItem(post_num).getElementsByTagName('description');
  LJPubDate = LJItem(post_num).getElementsByTagName('pubDate');
  }
else
{
  LJTitle = LJItem[post_num].getElementsByTagName('title');
  LJText = LJItem[post_num].getElementsByTagName('description');
  LJPubDate = LJItem[post_num].getElementsByTagName('pubDate');
}

 if (LJTitle[0])
    Title=LJTitle[0].firstChild.data;
else
   Title="";

  completeResponse="<b>"+Title+"</b><br>"+LJText[0].firstChild.data+"<br><div align=right><i>"+LJPubDate[0].firstChild.data+"</i></div>";
   
   document.getElementById("LJOutputText").innerHTML=completeResponse;
}



function showNews(newsOffset)
{
   if (newsOffset==(total_news-2))   //total_postings=LJItem.length;
       document.getElementById("n_arr_prev").style.visibility = "hidden";
 else
       document.getElementById("n_arr_prev").style.visibility = "visible";

 if (newsOffset==0)
      document.getElementById("n_arr_next").style.visibility = "hidden";
 else
      document.getElementById("n_arr_next").style.visibility = "visible";

   outputNews="";
    
    for (i=(total_news-newsOffset);  i>=(2-newsOffset); i--)
       outputNews+=news[i]+"<hr>";

   document.getElementById("NewsText").innerHTML=news[total_news-newsOffset]+"<hr class=\"red\"/>"+news[total_news-newsOffset-1]+"<hr class=\"black\"/>"+news[total_news-newsOffset-2];
}

function showPrevNews()
{
   news_num++;
   showNews(news_num);
}

function showNextNews()
{
   news_num--;
   showNews(news_num);
}
