/* Global var that contains HTTP request/response
 */
var http;
var debug=true;

// ******************************
// AJAX FUNCTIONS
// ******************************

/* Sends asynchronous HTTP request to the <param="serverURI">
 * and registers callback handling function named
 * <param="handler">
 */
function createXmlHttpRequestObject()  
{ 
  // will store the reference to the XMLHttpRequest object 
  var http = false;

  // this should work for all browsers except IE6 and older 
  try 
  { 
    // try to create XMLHttpRequest object 
    http = 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 && !http; i++)  
    { 
      try  
      {  
        // try to create XMLHttpRequest object 
        http = new ActiveXObject(XmlHttpVersions[i]); 
      }  
      catch (e) {} 
    } 
  } 
  // return the created object or display an error message 
  if (!http) 
    alert("Pretrazivac nije podrzan.\nPodrzani pretrazivaci su Internet Explorer verzije 5 na vise i Mozilla Firefox."); 
  else  
    return http; 
} 

//// global HttpRequestObject menjao Zoran
var http =createXmlHttpRequestObject();
function xmlRequest(serverURI, handler) {
    // Create cross-browser XML Http Request Object
	
	 http.abort();
	
	// Before sending the request, append dummy variable
	// that will be different all the times. This ensures
	// that the browser thinks it has to load a new page,
	// so nothing is retrieved from cache :)
	var antiCache=(""+Math.random()).substr(2);
	if(serverURI.indexOf("?")==-1) {
		// Just anti-cache var
		serverURI += "?noCache="+antiCache;
	}
	else {
		// Anti-cache var at the end of GET vars
		serverURI += "&noCache="+antiCache;
	}

	if(debug) alert("About to send: " + serverURI); // IMPORTANT DEBUG - DON'T DELETE!!!
	if(handler) http.onreadystatechange = eval(handler);
	else if(debug) http.onreadystatechange = validateResponse;
	http.open('GET', serverURI, true);
	http.send(null); // SEND HTTP REQUEST
}

/* Sends asynchronous HTTP POST request to the <param="serverURI">
 * and registers callback handling function named
 * <param="handler">
 */
function xmlPostRequest(serverURI, parameters, handler) {
    // Create cross-browser XML Http Request Object
	 http.abort();
	
	// Before sending the request, append dummy variable
	// that will be different all the times. This ensures
	// that the browser thinks it has to load a new page,
	// so nothing is retrieved from cache :)
	var antiCache=(""+Math.random()).substr(2);
	if(serverURI.indexOf("?")==-1) {
		// Just anti-cache var
		serverURI += "?noCache="+antiCache;
	}
	else {
		// Anti-cache var at the end of GET vars
		serverURI += "&noCache="+antiCache;
	}

	if(debug) alert("About to send: " + serverURI + "; parameters are " + parameters); // IMPORTANT DEBUG - DON'T DELETE!!!
	if(handler) http.onreadystatechange = eval(handler);
	else if(debug) http.onreadystatechange = validateResponse;
	http.open('POST', serverURI, true);
	try {
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // POST!!!
		http.setRequestHeader("Content-length", parameters.length); // POST!!!
		http.setRequestHeader("Connection", "close"); // POST!!!
		http.send(parameters); // SEND HTTP POST (!!!) REQUEST
	}
	catch(e) {
		alert("Error (" + e.name + ": " + e.message + ")");
	}
}

/* Validates HTTP reaponse; if response (global var http)
 * is final and valid returns true, if not returns false.
 */
function validateResponse() {
	// Is it a final response?
	if (http.readyState != 4) {
		return false;
	}

	// Do we have a good status?
	if (http.status != 200) {
        alert("There was a problem retrieving the XML data:\n" + http.statusText);
		return false;
	}
	
	if(debug) alert(http.responseText); // IMPORTANT DEBUG - DON'T DELETE!!!

	return true;
}

