// JavaScript Document

/*
Usage: This function used to detect what browser visitor using
Return:
	IE : Browser is Internet Explorer
	Mozilla : Browser is Mozilla Firefox , Netscape
*/
var request;

/* Wrapper function for constructing a request object.
 Parameters:
  reqType: The HTTP request type, such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
  
function httpRequest(reqType,url,asynch,respName,params) {
    //Mozilla-based browsers
    if(window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (!request){
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    //the request could still be null if neither ActiveXObject
    //initialization succeeded
    if(request) {
        initReq(reqType,url,asynch,respName,params);
    } else {
		alert("Your browser does not permit the use of all of this application's features!");
    }
}

/* Initialize a request object that is already constructed.
 Parameters:
   reqType: The HTTP request type, such as GET or POST.
   url: The URL of the server program.
   isAsynch: Whether to send the request asynchronously or not. */
function initReq(reqType,url,isAsynch,respName,params){
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange = respName;
    request.open(reqType,url,isAsynch);
    /* Set the Content-Type header for a POST request */
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(params);
}
//httpRequest('POST','index.php?m=public&a=ae_detail','true','show','id=1&nam=trung')
function DetectBrowser()
{
	if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1)
	{
		return "Mozilla";
	}
	else
	{
		return "IE";
	}
}

/*
Usage: create requrest object
Return: Request object
*/
function getHttpRequestObject()
{
	var theRequestObject;
	var theBrowser;
	
	theBrowser = DetectBrowser();
	
	if (theBrowser == "IE")
	{
		theRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		theRequestObject = new XMLHttpRequest();
	}
	
	return theRequestObject;
}

/*function setRequestHeader(theHttpRequestObject , theMethod , theURL)
{
	if (theMethod == "POST")
	{
		theHttpRequestObject.setRequestHeader("Method", "POST " + theURL + " HTTP/1.1");
		theHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}
}*/

function doRequest(theHttpRequestObject , theMethod , theURL , theArgs , theHandledFunction)
{
	theHttpRequestObject.open(theMethod, theURL, true);
	
	setRequestHeader(theHttpRequestObject , theMethod , theURL);
	
	theHttpRequestObject.onreadystatechange = theHandledFunction;
	
	theHttpRequestObject.send(theArgs);
}