/*************
Filename: common_ajax.js
Description: commonly used functions for ajax application.

Author: Mark Lo <mark@man169.com>
Date: 19th Jan, 2007
Version: 1.4

Copyright (c) 1999~2007 Super Ease Development Limited. All rights reserved.

*************/

// global variables
var request=null;


function httpRequest(reqType, url, async, respHandle) {
	if (window.XMLHttpRequest) {
		request=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request=new ActiveXObject("Msxml2.XMLHTTP");
		if (!request) {
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	// null test for request
	if (request) {
		// if the reqType is POST, then the 5th argument of this function is the POSTed data
		if (reqType.toLowerCase()!="post") {//GET
			initReq(reqType, url, async, respHandle);
		} else {
			//post
			var args = arguments[4];
			if (args!=null && args.length>0) {
				initReq(reqType, url, async, respHandle, args);
			} 
		}
	} else {
		alert("You browser does not permit the use of all of this application's features!");
	}
}


function initReq(reqType, url, async, respHandle) {
	try
	{
		// Specify the function to handle the HTTP response
		request.onreadystatechange=respHandle;
		request.open(reqType, url, async);
		// if the reqType is 'POST', then the 5th argument of this function is the POSTed data
		if (reqType.toLowerCase()=="post") {
			request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			request.send(arguments[4]);
		} else { // method GET
			request.send(null);
		}
	}
	catch (e)
	{
		alert("Cannot contact the server.\n Error detail: "+e.message+"\nreqType:"+reqType+"\nurl:"+url+"\nasync:"+async+"\nrespHandle:"+respHandle);
	}
}
