
var	used_charset	= null;

function sendRequest(handler, url, content, target, errloc)
{
	var	method, ajaxRequest, contentStr = '';

	if(!content)
	{
		content = new Array();
	}

	if(window.XMLHttpRequest)
	{
		ajaxRequest = new XMLHttpRequest();
	}
	else
		if(window.ActiveXObject)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
		}

	if(!ajaxRequest)
		return false;

	for(var key in content)
	{
		if(contentStr)
			contentStr += '&';
		contentStr += key + '=' + content[key];
	}

	if(contentStr != '')
	{
		method = 'POST';
	}
	else
	{
		method = 'GET';
	}

	ajaxRequest.open(method, url);
	ajaxRequest.onreadystatechange = function()
	{
		handler(ajaxRequest, target, errloc);
	};

	var contType = 'application/x-www-form-urlencoded';
	if(typeof used_charset == 'string')
		contType += '; charset=' + used_charset;
	ajaxRequest.setRequestHeader('Content-Type', contType);

	ajaxRequest.send(contentStr);

	return true;
}

function requestHandler(ajaxRequest, target, errloc)
{
	var	resp,
		root,
		tgtn,
		ancs,
		alen;

	if(ajaxRequest.readyState == 4)
	{
		if(ajaxRequest.status == 200)
		{
			resp = ajaxRequest.responseText;
			if(resp.substr(0, 3) == '<x>')
				resp = resp.substr(4, resp.length - 9);
			tgtn = document.getElementById(target);
			if(tgtn && (typeof jQuery == "function"))
			{
				tgtn.innerHTML = resp;
				ancs = tgtn.getElementsByTagName('a');
				alen = ancs.length;
				for(i = 0; i < alen; i++)
				{
					if(ancs[i].rel == 'lightbox')
					{
						jQuery("a[rel^='lightbox']").slimbox();
						i = alen;
					}
				}
			}
		}
		else
			if(typeof errloc == 'string' && errloc.length)
				window.location.href.replace = errloc;
	}
}

function savejsVar(name, value)
{
	var	data = new Array;

	data['ajax_code'] = 'save_js_var';
	data['js_var_name'] = name;
	data['js_var_value'] = value;

//	sendRequest(requestHandler, app_home_uri + '/save_js_var.php', data, null, null);
	sendRequest(requestHandler, window.location.href, data, null, null);
}

