// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = false;
var sajax_request_type = "GET";
var ajax_works=true;

function sajax_debug(text) 
{
	if (sajax_debug_mode)
	{
		alert("RSD: " + text);
	}//end if
}//end sajax_debug

function sajax_init_object() 
{
	sajax_debug("sajax_init_object() called..");
	
	var A;
	try 
	{
		A=new ActiveXObject("Msxml2.XMLHTTP");
	}//end try
	catch (e) 
	{
		try 
		{
			A=new ActiveXObject("Microsoft.XMLHTTP");
		}//end try
		catch (oc) 
		{
			A=null;
		}//end catch
	}//end catch

	if(!A && typeof XMLHttpRequest != "undefined")
	{
		A = new XMLHttpRequest();
	}//end if
	if (!A)
	{
		ajax_works = false;
		sajax_debug("Could not create connection object.");
	}//end if
	return A;
}//end sajax_init_object

function sajax_do_call(remote_uri,func_name, args) 
{
	var i, x, n;
	var uri;
	var post_data;
	
	uri = remote_uri;

	if (sajax_request_type == "GET") 
	{
		if (uri.indexOf("?") == -1) 
		{
			uri = uri + "?rs=" + escape(func_name);
		}//end if
		else
		{
			uri = uri + "&rs=" + escape(func_name);
		}//end else
		for (i = 0; i < args.length-1; i++) 
		{
			uri = uri + "&rsargs[]=" + escape(args[i]);
		}//end for
		uri = uri + "&rsrnd=" + new Date().getTime();
		post_data = null;
	}//end if
	else 
	{
		post_data = "rs=" + escape(func_name);
		for (i = 0; i < args.length-1; i++) 
		{
			post_data = post_data + "&rsargs[]=" + escape(args[i]);
		}//end for
	}//end else
	
	x = sajax_init_object();

	x.open(sajax_request_type, uri, true);
	if (sajax_request_type == "POST") 
	{
		try 
		{
				x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
				x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    } 
		catch (ex) { }
	}//end if
	x.onreadystatechange = 
			function() 
			{
				if (x.readyState != 4) 
				{
					return;
				}//end if
				sajax_debug("received " + x.responseText);
				
				var status;
				var data;
				//alert(x.responseText.substring(2));
				status = x.responseText.charAt(0);
				data = x.responseText.substring(2);
				if (status == "-") 
				{
					alert("Error: " + data);
				}//end if
				else  
				{
					args[args.length-1](data);
				}//end else
			}//end function

	x.send(post_data);
	sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
	sajax_debug(func_name + " waiting..");
	delete x;
}//end sajax_do_call