// stores the reference to the XMLHttpRequest object
var xmlHttp1 = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp1;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp1 = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp1 = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp1 = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp1)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp1;
}

// make asynchronous HTTP request using the XMLHttpRequest object 

function replaceAll(OldString,FindString,ReplaceString) {
  	var SearchIndex = 0;
  	var NewString = ""; 
  	while (OldString.indexOf(FindString,SearchIndex) != -1)    {
    	NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
    	NewString += ReplaceString;
    	SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
 	}
  	NewString += OldString.substring(SearchIndex,OldString.length);
  	return NewString;
}

function metalib_results(search,new_search,metalib,newspaper,find_code2,find_code5, group_number,ckbox,title_subject)
{

  // proceed only if the xmlHttp1 object isn't busy
  if (xmlHttp1.readyState == 4 || xmlHttp1.readyState == 0)
  {

    try
    {
      ckbox=replaceAll(ckbox,'ckbox','ckbox[]');
    }
    catch(err){}
    // retrieve the name typed by the user on the form
        if (ckbox)
        {
        	if (search)xmlHttp1.open("GET", "useful/metalib_results.php?metalib=" + metalib + "&newspaper=" + newspaper +"&search=" + search +"&find_code2=" + find_code2 +"&find_code5=" + find_code5 + "&new_search=" + new_search+ "&set_group_number=" + group_number + "&title_subject=" + title_subject + "&ckbox[]=" + ckbox, true);  
		else xmlHttp1.open("GET", "useful/metalib_results.php", true);  
        }
        else
        {
	       	if (search)xmlHttp1.open("GET", "useful/metalib_results.php?metalib=" + metalib + "&newspaper=" + newspaper +"&search=" + search +"&find_code2=" + find_code2 +"&find_code5=" + find_code5 + "&new_search=" + new_search+ "&set_group_number=" + group_number + "&title_subject=" + title_subject, true);  
		else xmlHttp1.open("GET", "useful/metalib_results.php", true);  
	}
    
    // define the method to handle server responses
    xmlHttp1.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp1.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('metalib_results("'+search+'","'+new_search+'","'+metalib+'","'+newspaper+'","'+find_code2+'","'+find_code5+'","'+ group_number+'","'+ckbox+'","'+title_subject+'")', 673);
}



// executed automatically when a message is received from the server
function handleServerResponse() 
{
  try
  {
  // move forward only if the transaction has completed
  if (xmlHttp1.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp1.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp1.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      var rating_id;
      rating_id = xmlDocumentElement.firstChild.data;
      // update the client display using the data received from the server
      var xserver_results = rating_id.split('|');
      document.getElementById("divMessage").innerHTML = xserver_results[0].substring(1);
      document.getElementById("divMessageNewspaper").innerHTML = xserver_results[1];

      if (rating_id.charAt(0)==1)setTimeout('metalib_results()', 673);

    } 
    // a HTTP status different than 200 signals an error
    else 
    {
    //  alert("There was a problem accessing the server: " + xmlHttp1.statusText);
    }
  }
  }
  catch (e) 
  {
  }


}


function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
