/* Copyright (c) 2006 Wang, Chun-Pin All rights reserved.
 *
 * Version:	$Id: ajax.js,v 1.1 2007/09/17 02:14:53 alex Exp $
 *
 */
 
/* Make HTTP request. Create a HTTP request and return.
 * Return values:
 *	Success: HttpRequest
 *	Fail:	false
 */
function AjaxHTTPInit()
{
	/* Create a new XMLHttpRequest object to talk to the Web server */	
	var HttpRequest = false;

	/* The following part is for IE, Firefox does not support new ActiveXObject() */

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		HttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			HttpRequest = false;
		}
	}
	@end @*/

	/* This part is for firefox */
	if (!HttpRequest && typeof XMLHttpRequest != 'undefined') {
		HttpRequest = new XMLHttpRequest();
	}

	if (!HttpRequest) {
		alert("Failed to Initializing XMLHttpRequest!");
	}

	return HttpRequest;
}

/* Make HTTP POST request */
function AjaxHTTPPost(HttpRequest, url, param, CallBackHandler)
{
	// Open an HTTP POST connection to the shopping cart servlet.
	// Third parameter specifies request is asynchronous.
	HttpRequest.open("POST", url, true);

	HttpRequest.onreadystatechange = CallBackHandler;

	// Specify that the body of the request contains form data
	HttpRequest.setRequestHeader("Content-Type", 
								 "application/x-www-form-urlencoded");

	// Send form encoded data stating that I want to add the 
	// specified item to the cart.
	HttpRequest.send(param);
}

/* Make HTTP GET request */
function AjaxHTTPGet(HttpRequest, url, CallBackHandler)
{
	// Open a connection to the server
	HttpRequest.open("GET", url, true);

	// Setup a function for the server to run when it's done
	HttpRequest.onreadystatechange = CallBackHandler;

	// Send the request
	HttpRequest.send(null);
}

