var xmlhttp=loadXMLHTTP();


function loadXMLHTTP()
{
    var varXMLHTTP = null;

    if (window.ActiveXObject)
    {
      var versions = ['Microsoft.XMLHTTP', 'MSXML6.XMLHTTP', 'MSXML5.XMLHTTP', 'MSXML4.XMLHTTP', 'MSXML3.XMLHTTP', 'MSXML2.XMLHTTP', 'MSXML.XMLHTTP'];

      for (var i = 0; i < versions.length; i ++ )
      {
        try
        {
          varXMLHTTP = new ActiveXObject(versions[i]);
          break;
        }
        catch (ex)
        {
          continue;
        }
      }
    }
    else
    {
      varXMLHTTP = new XMLHttpRequest();
    }

    return varXMLHTTP;
}


function gotoRun(url, params, callback, transferMode, ActDiv)
{
    
	if (transferMode === "GET")
    {
      var d = new Date();

      url += params ? (url.indexOf("?") === - 1 ? "?" : "&") + params : "";
      url = encodeURI(url) + (url.indexOf("?") === - 1 ? "?" : "&") + d.getTime() + d.getMilliseconds();
      params = null;
    }
	
	try
	{
	
   	    onRunning(ActDiv);
		
		xmlhttp.open(transferMode, url);
		
		if (transferMode === "POST")
        {
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        }
		
	    xmlhttp.onreadystatechange = function() 
	    {
	          if (xmlhttp.readyState == 4)
              {
                switch ( xmlhttp.status )
                {
                  case 0:
                  case 200: // OK!
                /*
                 * If the request was to create a new resource
                 * (such as post an item to the database)
                 * You could instead return a status code of '201 Created'
                 */

                    if (typeof(onComplete) === "function")
                    {
                      onComplete();
                    }

                    if (typeof(callback) === "function")
                    {
					  callback.call(parseResult(xmlhttp), xmlhttp.responseText);
                    }
//					alert(xmlhttp.responseText);
                  break;

                  case 304: // Not Modified
                /*
                 * This would be used when your Ajax widget is
                 * checking for updated content,
                 * such as the Twitter interface.
                 */
                  break;

                  case 400: // Bad Request
                /*
                 * A bit like a safety net for requests by your JS interface
                 * that aren't supported on the server.
                 * "Your browser made a request that the server cannot understand"
                 */
                     alert("XmlHttpRequest status: [400] Bad Request");
                  break;

                  case 404: // Not Found
                    alert("XmlHttpRequest status: [404] \nThe requested URL was not found on this server.");
                  break;

                  case 409: // Conflict
                /*
                 * Perhaps your JavaScript request attempted to
                 * update a Database record
                 * but failed due to a conflict
                 * (eg: a field that must be unique)
                 */
                  break;

                  case 503: // Service Unavailable
                /*
                 * A resource that this request relies upon
                 * is currently unavailable
                 * (eg: a file is locked by another process)
                 */
                     alert("XmlHttpRequest status: [503] Service Unavailable");
                  break;

                  default:
                    alert("XmlHttpRequest status: [" + xmlhttp.status + "] Unknow status.");
                }
				
//			   xmlhttp = null;	
              }
	    }
	    if (xmlhttp != null) xmlhttp.send(params);
	}
	catch (ex)
	{
	    alert("/run() error:" + ex.description);
	}
	
}

  function parseParams(params)
  {
    var legalParams = "";
    params = params ? params : "";

    if (typeof(params) === "string")

    {
      legalParams = params;
    }

    return legalParams;
  }
  
  function parseResult(xmlhttp)
  {
    var result = null;

    result = this.preFilter(xmlhttp.responseText);

    return result;
  }
  
  function preFilter(result)
  {
    return result.replace(/\xEF\xBB\xBF/g, "");
  }
  
function onRunning(ActDiv)
{
//  document.getElementsByTagName('body').item(0).style.cursor = "wait";

  var process_request='<img src="./images/loader.gif" />';


      var obj = document.getElementById(ActDiv);
	
	  if ( obj && process_request)
      {
        obj.style.display = '';
        obj.innerHTML = process_request;

      }

}

function onComplete(ActDiv)
{
      var obj = document.getElementById(ActDiv);
	
	  if ( obj )
      {
        obj.innerHTML = '';
      }
}
