//**************************************************************************
//                               ajax_core.js
//                            -------------------
//   begin                : Saturday, Jul 16, 2005
//   copyright            : (C) 2005 alcaeus
//   email                : mods@alcaeus.org
//
//   $Id: ajax_core.js,v 1.4 2006/02/04 11:12:16 alcaeus Exp $
//
//**************************************************************************

//**************************************************************************
//
//   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.
//
//**************************************************************************


//
// This is the only value you should change
// It defines the time in milliseconds that the script waits before automatically submitting the fields for usernames (New PM and username search)
//
var KEYUP_TIMEOUT = 500;

var request = null;
var error_handler = '';

// Don't want to use const, var works in JS 1.0 as well :)
var AJAX_OP_COMPLETED = 0;
var AJAX_ERROR = 1;
var AJAX_CRITICAL_ERROR = 2;
var AJAX_POST_SUBJECT_EDITED = 3;
var AJAX_POST_TEXT_EDITED = 4;
var AJAX_POLL_RESULT = 5;
var AJAX_WATCH_TOPIC = 6;
var AJAX_LOCK_TOPIC = 7;
var AJAX_MARK_TOPIC = 8;
var AJAX_MARK_FORUM = 9;
var AJAX_PM_USERNAME_FOUND = 10;
var AJAX_PM_USERNAME_SELECT = 11;
var AJAX_PM_USERNAME_ERROR = 12;
var AJAX_PREVIEW = 13;

var AJAX_DEBUG_RESULTS = 0;
var AJAX_DEBUG_REQUEST_ERRORS = 0;
var AJAX_DEBUG_HTML_ERRORS = 0;

//
// Determine whether AJAX is available
//
if (window.XMLHttpRequest)
{
	var tempvar = new XMLHttpRequest();
	ajax_core_defined = (tempvar == null) ? 0 : 1;
	delete(tempvar);
}
//Use the IE/Windows ActiveX version
else if (window.ActiveXObject)
{
	var tempvar= new ActiveXObject("Microsoft.XMLHTTP");
	ajax_core_defined = (tempvar == null) ? 0 : 1;
	delete(tempvar);
}
else
{
	ajax_core_defined = 0;
}

//
// General function. This one is the mother of all AJAX functions ;)
//
function loadXMLDoc(url, params, submitmethod, changehandler)
{
	if ((submitmethod != 'GET') && (submitmethod != 'POST'))
	{
		submitmethod = 'GET';
	}
	
	var request_url = phpbb_root_path + ((submitmethod == 'GET') ? url + '?' + params : url);
	var request_params = (submitmethod == 'GET') ? null : params;
	
	//Use the native object available in all browsers (IE >= 7)
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		var is_activex = false;
	}
	//Use the ActiveX version for IE < 7
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
		var is_activex = true;
	}
	
	if (!request)
	{
		return false;
	}
	
	eval("request.onreadystatechange = "+changehandler);
	request.open(submitmethod, request_url, true);
	if (submitmethod == 'POST')
	{
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	}
	if ((request_params == null) && is_activex)
	{
		request.send();
	}
	else
	{
		request.send(request_params);
	}
	
	return true;
}

function getTagValues(tagname, haystack)
{
	var tag_array = haystack.getElementsByTagName(tagname);
	var result_array = Array();
	for (i = 0; i < tag_array.length; i++)
	{
		result_array[i] = (tag_array[i].firstChild && tag_array[i].firstChild.data) ? tag_array[i].firstChild.data : '';
	}
	return result_array;
}

function getFirstTagValue(tagname, haystack)
{
	var tag_array = haystack.getElementsByTagName(tagname);
	if ((tag_array.length > 0) && (tag_array[0].firstChild))
	{
		return (tag_array[0].firstChild.data) ? tag_array[0].firstChild.data : '';
	}
	return '';
}


//
// 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)
	{
		var result_code = AJAX_OP_COMPLETED;
		var error_msg = '';
		//If the request wasn't successful, we just hide any information we have.
		if (request.status == 200)
		{
			var response = request.responseXML.documentElement;
			if (AJAX_DEBUG_RESULTS)
			{
				alert(request.responseText);
			}
			//Don't react if no valid response was received
			if (response != null)
			{
				result_code = getFirstTagValue('result', response);
				error_msg = getFirstTagValue('error_msg', response);
			}
		}
		
		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')
	{
		var repl = Array(1);
		repl[0] = replacements;
	}
	else
	{
		var repl = replacements;
	}
	
	while (((charindex = text.indexOf('%s')) >= 0) && (i < repl.length))
	{
		var temptext = text.substr(0, charindex);
		text = temptext + repl[i] + text.substr(charindex+2, text.length);
		i++;
	}
	
	return text;
}

function getElementById(ElementId)
{
	if (document.documentElement)
	{
		return document.getElementById(ElementId);
	}
	else
	{
		return document.all[ElementId];
	}
}

function rtrim(text)
{
	if (text == '')
	{
		return '';
	}
	
	var part = '';
	var i = text.length;
	do
	{
		part = text.substring(i-1, i);
		i--;
	} while ((part == ' ') || (part == '\n') || (part == '\r'));
	text = text.substring(0, i+1);
	
	return text;
}
	
function ltrim(text)
{
	if (text == '')
	{
		return '';
	}
	
	var part = '';
	var i = 0;
	do
	{
		part = text.substring(i, i+1);
		i++;
	} while ((part == ' ') || (part == '\n') || (part == '\r'));
	text = text.substring(i-1, text.length);
	
	return text;
}

function trim(text)
{
	return ltrim(rtrim(text));
}

function setClickEventHandler(obj, handler)
{
	if (obj.onclick)
	{
		eval('obj.onclick = function() { '+handler+' }');
	}
	else
	{
		obj.setAttribute('onclick', handler, 'false');
	}
}

function setInnerText(obj, newtext)
{
	if (newtext == '')
	{
		newtext = '&nbsp;';
	}
	
	if (obj.innerText)
	{
		obj.innerText = newtext;
	}
	else if (obj.firstChild)
	{
		obj.firstChild.nodeValue = newtext;
	}
	else
	{
		obj.innerHTML = newtext;
	}
}

