/*
 ajax support
 Kell Gatherer May 2010
*/

var myRequest = getXMLHTTPRequest();

function getXMLHTTPRequest()
{
 var req = false;
 try 
   {
    req = new XMLHttpRequest(); /* e.g. Firefox */
   } 
 catch(err1) 
   {
   try 
     {
      req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
     } 
   catch(err2) 
     {
     try 
       {
        req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
       } 
       catch(err3) 
         {
          req = false;
         } 
     } 
   }
 return req;
}

function callAjax(barcode,projectid,thumb)
{
 myRand = parseInt(Math.random()*999999999999999);
 var url = "/project/ajax.php?project="+projectid+"&barcode="+barcode+"&thumb="+thumb+"&"+myRand; // build the URL of the server script
 myRequest.open("GET", url, true);            // ask our XMLHTTPRequest object to open a server connection
 myRequest.onreadystatechange = responseAjax; // prepare a function responseAjax() to run when the response has arrived
 myRequest.send(null);                        // and finally send the request
}

function responseAjax()
{
 if ( myRequest.readyState == 4 )   // we are only interested in readyState of 4, i.e. "completed"
    {
     if ( myRequest.status == 200 ) // if server HTTP response is "OK"
        {
         //alert("text: "+myRequest.responseText);
         response    = myRequest.responseText;
         parts       = response.split("|");
         barCode     = parts[0];
         iconName    = parts[1];
         noLocs      = parts[2];
         iconId      = "include"+barCode;
         tableId     = "table"+barCode;
         //alert("tableId: "+tableId);
         if ( iconName == "included" )
            {
             //document.getElementById(tableId).style.backgroundColor = "#ffffff";  
             document.getElementById(tableId).style.backgroundColor = "#ffffdd";
             iconTitle = "location is included in your project (click to remove)";
            }
         else
            {
             document.getElementById(tableId).style.backgroundColor = "#eeeeee";
             iconTitle = "click to include this location in your project";
            }
         iconGraphic = document.getElementById(iconId);
         iconGraphic.innerHTML = "<img src='/project/icons/"+iconName+".png' align='middle' border='0' title='"+iconTitle+"'>"; 
         ajaxLocs = document.getElementById("ajaxLocs");
         ajaxLocs.innerHTML = "locations: "+noLocs;   
        }
     else
        {
         // issue an error message for any other HTTP response
         alert("An error has occurred: " + myRequest.statusText);
        }
 }
}

