

/*******************************************************************************************/
// Ajax utilities

var AjaxCount = 0
var AjaxID = 0
var interpreter = 'Handler.ashx'

function setAjaxStatus (id, status, msg)
{
  if (AjaxListener)
    AjaxListener (id, status, msg)
}

function getTransport()
{
  try
  {
    var t = new ActiveXObject('Msxml2.XMLHTTP')
    return t
  }
  catch (ex){}

  try
  {
    var t = new ActiveXObject('Microsoft.XMLHTTP')
    return t
  }
  catch (ex){}

  try
  {
    var t = new XMLHttpRequest()
    return t
  }
  catch (ex){}
  
  return false
}

function callAjax (url, callback, msg)
{
   var t = getTransport()
   var self = t
   var id = AjaxID++
   AjaxCount++
   t.open('GET', url, true)
   t.onreadystatechange = 
         function()
         {
           if (t.readyState == 4)
           {
// notify listener that we are done           	
   setAjaxStatus (id, 'finish')
             if (t.status == 200)
               callback(self)
             else
             {
             	 alert('Ajax request problem '+t.status)
             }
             AjaxCount--
           }
         };

// notify listener that we are started
   setAjaxStatus (id, 'start', msg)
   t.send(null)
}

function postAjax (url, parms, callback, msg)
{
   var t = getTransport()
   var self = t
   var id = AjaxID++
   AjaxCount++
   t.open('POST', url, true)
   t.onreadystatechange = 
         function()
         {
           if (t.readyState == 4)
           {
// notify listener that we are done           	
   setAjaxStatus (id, 'finish')
             if (t.status == 200)
               callback(self)
             else
             {
             	 alert('Ajax request problem '+t.status)
             }
             AjaxCount--
           }
         };

   t.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   t.setRequestHeader("Content-length", parms.length);
   t.setRequestHeader("Connection", "close");
   
// notify listener that we are started

   setAjaxStatus (id, 'start', msg)
   t.send(parms)
}

function callAjaxJSON (url, callback, msg)
{
	callAjax (url,
					     function(req)
					     {
try{
					     	 eval ('var o=' + req.responseText)
}
catch(ex)
{
  var o={error:'Ajax error'}
}
					     	 callback(o)
					     },
					  msg
            )
}

function postAjaxJSON (url, parms, callback, msg)
{
  postAjax (url, parms,
					     function(req)
					     {			     	 
					     	 eval ('var o=' + req.responseText)
					     	 callback(o)
					     },
					  msg
            )
}

function postAjaxPNG (url, parms, callback, msg)
{
  postAjax (url, parms,
					     function(req)
					     {
					     	 callback(req.responseText)
					     },
					  msg
            )
}
function postAjaxText (s, target, format)
{
  postAjax (interpreter, 's='+s+'&format='+format,
					     function(req)
					     {
					      alert (req.responseText)
  			        target.innerText = req.responseText
					     },
					  ''
            )
}

function getObject (s, callback, msg)
{
  var url = interpreter+"?s="+s;
  callAjax (url, callback, msg)
}

function getObjectJSON (s, callback, msg)
{
  var url = interpreter+"?s="+s+'&format=json';
  callAjaxJSON (url, callback, msg)
}

function postObjectJSON (s, callback, msg)
{
  var url = interpreter
  postAjaxJSON (url, 's='+s+'&format=json', callback, msg)
}

function postObjectPNG (s, callback, msg)
{
  var url = interpreter
  postAjaxPNG (url, 's='+s+'&format=png', callback, msg)
}

