//**************************************************************************
//                               ajax_core.js
//                            -------------------
//   begin                : Saturday, Jul 16, 2005
//   copyright            : (C) 2005 alcaeus
//   email                : alcaeus@gmx.net
//
//**************************************************************************

//**************************************************************************
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//   However, the use of this script is _strictly_ limited to phpBB. If 
//   want to use it in another project, please contact me at the email
//   address listed above
//
//**************************************************************************


var request = null;
var error_handler = '';
var ajax_core_defined = 1;

//
// General function. This one is the mother of all AJAX functions ;)
//
function loadXMLDoc(content_url, changehandler)
{
	//Use the native object available in Mozilla, Safari, ...
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		eval("request.onreadystatechange = "+changehandler);
		request.open("GET", content_url, true);
		request.send(null);
	}
	//Use the IE/Windows ActiveX version
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request)
		{
			eval("request.onreadystatechange = "+changehandler);
			request.open("GET", content_url, true);
			request.send();
		}
	}
}


//
// This function is used to parse any standard error file
function error_req_change()
{
	//Check if the request is completed, if not, just skip over
	if (request.readyState == 4)
	{
		result_code = 0;
		error_msg = '';
		//If the request wasn't successful, we just hide any information we have.
		if (request.status == 200)
		{
			response = request.responseXML.documentElement;
			//Don't react if no valid response was received
			if (response != null)
			{
				result_code_ar = response.getElementsByTagName('result');
				if (result_code_ar.length > 0)
				{
					result_code = result_code_ar[0].firstChild.data;
				}
				
				if (result_code != 0)
				{
					//No need to look for an error message, 0 means we don't have one ;)
					error_msg_ar = response.getElementsByTagName('error_msg');
					if (error_msg_ar.length > 0)
					{
						error_msg = error_msg_ar[0].firstChild.data;
					}
				}
			}
		}
		
		eval(error_handler+"(result_code, error_msg);");
		delete request;
	}
}

//
// Just like sprintf() in php
// replacements can be any type
//
function sprintf(text, replacements)
{
	var i = 0;
	//This prevents us from having to create an array for replacements with one value
	//checking for type 'object' may not be really smart, but who cares ;)
	if ((typeof replacements) != 'object')
	{
		repl = Array(1);
		repl[0] = replacements;
	}
	else
	{
		repl = replacements;
	}
	
	while (((charindex = text.indexOf('%s')) >= 0) && (i < repl.length))
	{
		temptext = text.substr(0, charindex);
		text = temptext + repl[i] + text.substr(charindex+2, text.length);
		i++;
	}
	
	return text;
}