
//是否开启debug模式
var ajax_debug_mode = false;

/**
 * 显示调试信息
 *
 * @param	str	调试信息
 * @return	null
 */
function ajax_debug(str_msg)
{
	if (ajax_debug_mode) alert('Debug: ' + str_msg);
}

/**
 * ajax初始化
 *
 * @return	obj
 */
function ajax_init()
{
	var obj_xhr;
	//ajax_debug('Ajax object init...');

	if (window.ActiveXObject)
	{
		try { obj_xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e)
		{
			try { obj_xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (E) {}
		}
	}
	else if (window.XMLHttpRequest) { 
		obj_xhr = new XMLHttpRequest(); 
	}
	//if (!obj_xhr) ajax_debug('Ajax object create failed...');
	return obj_xhr;
}

/**
 * ajax调用
 *
 * @param	str	调用方法
 * @param	str	访问地址
 * @param	func	回调函数
 * @param	str	POST方式提交时传的内容
 * @return	null
 */
function ajax_callback(str_method, str_url, obj_function, str_content)
{
	
	var obj_xhr = ajax_init();
	str_url = encodeURI(str_url);   
	//ajax_debug('Ajax method is ' + str_method);
	//ajax_debug('Ajax url is ' + str_url);
	//ajax_debug('Ajax post data is ' + str_content);
	obj_xhr.open(str_method, str_url, true);
	
	obj_xhr.onreadystatechange = function()
	{
		ajax_handler(obj_xhr, obj_function);
	}
	if (str_method == 'POST') obj_xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	if (!str_content) str_content = null;
	else str_content = encodeURI(str_content);
	obj_xhr.send(str_content);
}

/*
 * ajax回调函数
 *
 * @param	obj	回传的xhr对象
 * @param	func	执行函数
 * @return	null
 */
function ajax_handler(obj_xhr, obj_function)
{
	if (obj_xhr.readyState == 4)
	{
		if (obj_xhr.status == 200) obj_function(obj_xhr);
	}
}
