var ajax_system_busy = false;
var ajax_request_objects = null;
var current_browser_type_ie = 0;

function create_ajax_object()
{
	var ro;
	if(current_browser_type_ie == 1)
	{
	ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else
	{
	ro = new XMLHttpRequest();
	}
	return ro;
}

function utf_encode(str) 
{
str = str.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < str.length; n++) 
	{
  var c = str.charCodeAt(n);
  if (c < 128) 
  	{
    utftext += String.fromCharCode(c);
  	}
  else if((c > 127) && (c < 2048)) 
  	{
    utftext += String.fromCharCode((c >> 6) | 192);
    utftext += String.fromCharCode((c & 63) | 128);
  	}
  else 
  	{
    utftext += String.fromCharCode((c >> 12) | 224);
    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    utftext += String.fromCharCode((c & 63) | 128);
  	}
	}
return utftext;
}

function utf_decode(utftext) 
{
if(utftext == null) 
	return '';
if(utftext == undefined)
	return '';
if(utftext.length == 0)
	return '';
//return utftext;
var str = "";
var i = 0;
var c = c1 = c2 = 0;
if(utftext == null)
	return '';
while ( i < utftext.length ) 
	{

	c = utftext.charCodeAt(i);
	
	if (c < 128) 
		{
    str += String.fromCharCode(c);
    i++;
		}
	else if((c > 191) && (c < 224)) 
		{
    c2 = utftext.charCodeAt(i+1);
    str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    i += 2;
		}
	else 
		{
		c2 = utftext.charCodeAt(i+1);
		c3 = utftext.charCodeAt(i+2);
		str += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
		i += 3;
		}
	}
return str;
}

function init_ajax(ajax_objects)
{
var browser = navigator.appName;

if(browser == "Microsoft Internet Explorer")
	{
	current_browser_type_ie = 1;
	}
else
	{
 	current_browser_type_ie = 0;
	}
if(ajax_objects == null)
	{
	ajax_objects = 7;
	}
	
ajax_request_objects = new Array();
var i;
for(i = 0; i < ajax_objects; ++i)
	{
	ajax_request_objects[i] = new Array();
	ajax_request_objects[i]['obj'] = null;
	ajax_request_objects[i]['callback'] = null;
	}
ajax_system_busy = false;
}

function get_ajax_request_object()
{
var index = -1;
var i;
capture_ajax_system();
for(i = 0; i < ajax_request_objects.length; ++i)
	{
	if(ajax_request_objects[i]['obj'] == null)
		{
		index = i;
		break;
		}
	}
release_ajax_system();
return index;
}

function is_ajax_object_active(id)
{
var index = (id >> 24)&0xFF;
var ret = false;
capture_ajax_system();
if(ajax_request_objects[index]['obj'] != null)
	{
	ret = (ajax_request_objects[index]['id'] == id)?true:false;
	}
else
	{
	ret = false;
	}	
release_ajax_system();
return ret;
}

function get_active_ajax_object()
{
var index = -1;
var i;
capture_ajax_system();
for(i = 0; i < ajax_request_objects.length; ++i)
	{
	var request_obj = ajax_request_objects[i];
	if(request_obj['obj'] != null)
		{
		if(request_obj['obj'].readyState == 4)
			{
			index = i;
			break;
			}
		}
	}
release_ajax_system();
return index;
}

function handle_ajax_response()
{
var index = get_active_ajax_object();
if(index != -1)
	{
	hide_loading();
	capture_ajax_system();
	var response = ajax_request_objects[index]['obj'].responseText;
	var arr = response.split("|");
	arr.shift();
	var callback = ajax_request_objects[index]['callback']
	ajax_request_objects[index]['obj'] = null;
	release_ajax_system();
	if(callback != null)
		{
		//sanitize the response
		arr = sanitize_ajax_response(arr);
		callback(arr);
		}
	}
}

function sanitize_ajax_response(arr)
{
var i; 
var temp;
for(i = 0; i < arr.length; ++i)
	{
	arr[i] = utf_decode(arr[i]);	
	}
return arr;
}

function sanitize_rqst_url_args(args)
{
var final_url ='';
var str;
for (var i in args)
	{
	str = args[i].toString();
	final_url += (i+"="+escape(utf_encode(str))+"&");
	}
return final_url;
}

function ajax_get_request(rqst_url, args, callback_function, show_dialog)
{
var index = get_ajax_request_object();
if(index == -1)
	{
	alert("Out of ajax resource");
	return -1;
	}

//sanitize the rqst_url
rqst_url = rqst_url + '?'+sanitize_rqst_url_args(args);
//alert(rqst_url);
if(show_dialog == null)
	{
	show_loading();
	}
var obj = create_ajax_object();

capture_ajax_system();
var id = ((new Date()).getTime()&0x00FFFFFF) | (index << 24);	
ajax_request_objects[index]['obj'] = obj;
ajax_request_objects[index]['callback'] = callback_function;
ajax_request_objects[index]['id'] = id;
release_ajax_system();
obj.open('get', rqst_url, true);
obj.onreadystatechange = handle_ajax_response;
obj.send(null);

return id;
}

function ajax_post_request(post_url, args, callback_function, show_dialog)
{
var index = get_ajax_request_object();
if(index == -1)
	{
	alert("Out of ajax resource");
	return -1;
	}

//sanitize the rqst_url
args = sanitize_rqst_url_args(args);

if(show_dialog == null)
	{
	show_loading();
	}

var obj = create_ajax_object();

capture_ajax_system();
var id = ((new Date()).getTime()&0x00FFFFFF) | (index << 24);	
ajax_request_objects[index]['obj'] = obj;
ajax_request_objects[index]['callback'] = callback_function;
ajax_request_objects[index]['id'] = id;
release_ajax_system();
obj.open('post', post_url, true);
obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
obj.onreadystatechange = handle_ajax_response;
obj.send(args);
return id;
}

function release_ajax_system()
{
//ajax_system_busy = false;
}

function capture_ajax_system()
{
//while(ajax_system_busy);
//ajax_system_busy = true;
}