// The main function in the scirpt. It makes the asynchronous request to the remote page, 
// represented by its path (the parameter: strUrl), puts the required Query String parameters.
// And links the handler function that will accept the result and applies it to the page.
function request_data(strUrl, strFunc, strParams, handler){ 
	var strHandler = 'stateChangeHandler';

	// Create the xmlHttp object to use in the request.
	// handler is the function that will hande the data which is received back. (asynchronous)
	xmlHttp = getXMLHttpRequest(); //GetXmlHttpObject(handler);
	
	// Put the parameters like QueryString variables.
	var url = MakeUrl(strUrl, strFunc, strParams);

	// Send the xmlHttp get to the specified url.
	xmlHttp_Get(xmlHttp, noCache(url), handler); 
}

var xmlHttp; 
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf('Opera6') != -1) || 
(navigator.userAgent.indexOf('Opera/6') != -1)) ? 1 : 0;
// netscape, safari, mozilla behave the same

function GetXmlHttpObject(handler) { 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

	//Depending on the browser, try to create the xmlHttp object 
	if (is_ie){ 
		//The object to create depends on version of IE 
		//If it isn't greater that ie5, then default to the Msxml2.XMLHTTP object 	             
		//Attempt to create the object 
		try {
			objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");		 
		} catch (e) {
			try {
			objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
			return; 
			}
		}
		objXmlHttp.onreadystatechange = handler;
	} 
	else if (is_opera){ 
		//Opera has some issues with xmlHttp object functionality 
		alert('Opera detected. The page may not behave as expected.'); 
		return; 
	} 
	else{ 
		// Mozilla | Netscape | Safari 
		objXmlHttp = new XMLHttpRequest(); 
		objXmlHttp.onload = handler; 
		objXmlHttp.onerror = handler; 
	} 
		
	//Return the instantiated object 
	return objXmlHttp; 
}		



function MakeUrl(strUrl, strFunc, strParams) {
	var url = strUrl;
	var i = 1;
	var urlLength = 0;
	urlLength = url.length;

	while ((i < urlLength) && (url.charAt(i) != '?'))
	{ 
		i++;
	}
	
	if ((i >= urlLength) || (url.charAt(i) != '?'))
	{
		url = url + '?NameF=' + strFunc + "&Param=" + strParams;
	}
	else
	{
		url = url + '&NameF=' + strFunc + "&Param=" + strParams;
	}
	 
	return url;
}
		
// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url, handler) { 
	xmlhttp.onreadystatechange = handler;
	xmlhttp.open('GET', url, true); //("GET", noCache(url), true);
	xmlhttp.send(null); 
}

 // HELPER FUNCTIONS

 // IE likes to cache so we will fix it's wagon!
 function noCache(url){
    var arr = url.split('?');   
    
    // QueryString
    var qs = 'nocache=' + new Date().getTime();
	if(arr[1]) qs = '&' + qs;
	else	   qs = '?' + qs;
	
	return url + qs; 
 } 

// NEW THINGS

// Define a list of Microsoft XML HTTP ProgIDs.
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  "Msxml2.XMLHTTP.7.0",
  "Msxml2.XMLHTTP.6.0",
  "Msxml2.XMLHTTP.5.0",
  "Msxml2.XMLHTTP.4.0",
  "MSXML2.XMLHTTP.3.0",
  "MSXML2.XMLHTTP",
  "Microsoft.XMLHTTP"
);

// Returns XMLHttpRequest object.
//
function getXMLHttpRequest()
{
  var httpRequest = null;

  // Create the appropriate HttpRequest object for the browser.
  if (window.XMLHttpRequest != null)
    httpRequest = new window.XMLHttpRequest();
  else if (window.ActiveXObject != null)
  {
    // Must be IE, find the right ActiveXObject.
    var success = false;
    for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
    {
      try
      {
        httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
        success = true;
      }
      catch (ex)
      {}
    }
  }

  // Display an error if we couldn't create one.
  if (httpRequest == null)
    alert("Error in HttpRequest():\n\n" + "Cannot create an XMLHttpRequest object.");

  // Return the created HttpRequest object.
  return httpRequest;
}