var agentObject;
var quoteId;
var e;
var error;
var regExp;
var oldQuote;
var oldAuthor;
var oldContext;
var uniqueId;

/**
* Quick and dirty replacement for vBulletin's now removed version.
*/
var arrayRef;
var newValue;
function array_push(arrayRef, newValue) {
	arrayRef.push(newValue);
}

/**
* Checks for either AJAX handler or server side errors
*
* @return	boolean	Whether the AJAX transaction was completed successfully or not
*
*/
function checkAgentStatus(agentObject) {
	if (agentObject.handler.readyState != 4) {
		//alert(quotes_phrase_javascript_error);
		return false;
	}
	else if (agentObject.handler.status != 200) {
		//alert(quotes_phrase_server_error + agent.handler.statusText);
		return false;
	}
	else {

		// handle no permission errors
		if (agentObject.handler.responseText == 'NoPermission') {
			alert(quotes_phrase_no_permission);
			return false;
		}
		else if (agentObject.handler.responseText == 'NoQuotes') {
			alert(quotes_phrase_no_quotes_matched);
			return false;
		}
		else {
			return true;
		}
	}
}

/**
* Gets all that are part of a group
*
* @param	string	Name of the tag to check
* @param	object	The regular expression to check the id against
*
* @return	array	All matching elements
*
*/
function getElementsByGroup(tagName, regExp) {

	// thank you so much for making getElementsByName() completely worthless, W3C
	var allTags = fetch_tags(document, tagName);
	var groupElements = new Array();
	var i = 0;

	while (allTags[i]) {
		if (regExp.test(allTags[i].id)) {
			array_push(groupElements, allTags[i]);
		}
		i++;
	}

	return groupElements;
}

/**
* Gets all elements of a quote (might be duplicates if used as BBCode)
*
* @param	string	Id of the element (excluding the $globalcounter add-on)
*
* @return	array	All matching elements
*
*/
function fetchObjectDupes(baseId) {
	var i = 0;
	var matchedObjects = new Array();

	while (true) {
		var element = fetch_object(baseId + '-' + i);
		if (!element) {
			break;
		}

		array_push(matchedObjects, element);
		i++;
	}

	return matchedObjects;
}

/**
* Displays the description of the category, since Firefox is the only one that displays the title of option tags
*
* @param	object	The option being hovered over
*
*/
function showCatDescription(option) {
	window.status = option.title ? option.title : '';
}

/**
* Hides the category description
*
*/
function hideCatDescription() {
	window.status = '';
}

/**
* Unchecks other categories if "All Categories" is selected
*
*/
function manageCatList() {
	var catList = fetch_object('categories');
	var action = catList.selectedIndex;
	action = catList.options[action].value;

	if (action == -1) {
		for (var i = 1; i < catList.length; i++) {
			catList[i].selected = false;
		}
	}
}

/**
* Reports a quote to moderators via AJAX
*
* @param	object	The event that was fired
* @param	integer	The quote's ID
*
*/
function reportQuote(e, quoteId) {
	do_an_e(e);
	var reason = prompt(quotes_phrase_reporting_reason, '');

	// null means they cancelled the dialog
	if (reason !== null) {
		var submitString = 'do=reportquote&quoteid=' + quoteId + '&reason=' + reason;

		try {
			var agent = new vB_AJAX_Handler(true);
			agent.onreadystatechange(function() {
				if (checkAgentStatus(agent)) {
					var reportObjects = fetchObjectDupes('report' + quoteId);
					var i = 0;
					while (reportObjects[i]) {
						reportObjects[i].style.display = 'none';
						i++;
					}
				}
			});
			agent.send('quotes.php', submitString + '&ajax=y');
		}
		catch (error) {
			window.location.href = 'quotes.php?' + SESSIONURL + submitString;
		}
	}
}
