/////////////////// NOT AJAX 
function openTagBox() {
	var taglink = document.getElementById('action-taglink');
	taglink.style.display = 'none';
	var tagbox = document.getElementById('action-tagbox');
	tagbox.style.display = 'block';
	var tagform = document.getElementById('action-tagform');
	tagform.tags.focus();

	return false;
}
///////////////////////////////////////
function deleteTag(quoteID, tag) {
	return ajaxSend(prefix_url + 'method=deleteTag&quoteid=' + quoteID + '&tag=' +
			tag, resp_deleteTag);
}
function resp_deleteTag(responseXML) {
	var response = responseXML.documentElement;

	var method = response.getElementsByTagName('method')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;

	if ( result == 1 ) {
		var quoteID = response.getElementsByTagName('quoteid')[0].firstChild.data;
		// this is all tags space separated that were actually added
		var tag = response.getElementsByTagName('tag')[0].firstChild.data;
		inpage_deleteTag(quoteID, tag);
	} else {
		// failure
	}	
}

function addTag(quoteID) {
	var form = document.getElementById('action-tagform');
	return ajaxSend(prefix_url + 'method=addTag&quoteid=' + quoteID + '&tags=' +
			escape(form.tags.value), resp_addTag);
}
function resp_addTag(responseXML) {
	var response = responseXML.documentElement;

	var method = response.getElementsByTagName('method')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;

	if ( result == 1 ) {
		var quoteID = response.getElementsByTagName('quoteid')[0].firstChild.data;
		// this is all tags space separated that were actually added
		var tags = response.getElementsByTagName('tags')[0].firstChild.data;
		var username = response.getElementsByTagName('username')[0].firstChild.data;
		if ( tags == '' ) {
			// no new tags found
		} else {
			inpage_addTag(quoteID, tags, username);
		}
	} else {
		// failure
	}	
	var form = document.getElementById('action-tagform');
	form.tags.value = '';
}

// favoriting feature

// function showTagAddBox(quoteID) {
// 	ajaxSend(prefix_url + 'method=showTagAddBox&quoteid=' + quoteID,
// 			resp_showTagAddBox);
// }
// function resp_showTagAddBox(responseXML) {
// 	var response = responseXML.documentElement;

// 	var method = response.getElementsByTagName('method')[0].firstChild.data;
// 	var quoteID = response.getElementsByTagName('quoteid')[0].firstChild.data;
// 	var result = response.getElementsByTagName('result')[0].firstChild.data;

// 	if ( result == 1 ) {
// 		var quote = document.getElementById('quote-' + quoteID);
// 		//quote.setAttribute('class', 'quote owner-favorite');

// 		var addLink = document.getElementById('action-tag-' + quoteID);
// 		addLink.style.display = 'none';

// 		var addLinkBox = document.getElementById('action-tagbox-' + quoteID);
// 		addLinkBox.style.display = 'block';
// 		// focus addLinkBox ??

// 		//faver.setAttribute('href', 'javascript:unfavQuote(' + quoteID + ')');
// 		//faver.firstChild.nodeValue = 'unfav';
// 	} else {
// 		// failure
// 	}	
// }

function hideSystemMessage() {
	return ajaxSend(prefix_url + 'method=hideSystemMessage',
		resp_hideSystemMessage);
}
function resp_hideSystemMessage(responseXML) {
	var response = responseXML.documentElement;

	var method = response.getElementsByTagName('method')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;

	if ( result == 1 ) {
		inpage_hideSystemMessage();
	} else {
		// failure
	}	
}

function favQuote(quoteID) {
	return ajaxSend(prefix_url + 'method=favQuote&quoteid=' + quoteID,
		resp_favQuote);
}
function resp_favQuote(responseXML) {
	var response = responseXML.documentElement;

	var method = response.getElementsByTagName('method')[0].firstChild.data;
	var quoteID = response.getElementsByTagName('quoteid')[0].firstChild.data;
	var toggleshow = response.getElementsByTagName('toggleshow')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;

	if ( result == 1 ) {
		inpage_favQuote(quoteID, toggleshow);
	} else {
		// failure
	}	
}


function unfavQuote(quoteID) {
	return ajaxSend(prefix_url + 'method=unfavQuote&quoteid=' + quoteID,
		resp_unfavQuote);
}
function resp_unfavQuote(responseXML) {
	var response = responseXML.documentElement;

	var method = response.getElementsByTagName('method')[0].firstChild.data;
	var quoteID = response.getElementsByTagName('quoteid')[0].firstChild.data;
	var toggleshow = response.getElementsByTagName('toggleshow')[0].firstChild.data;
	var result = response.getElementsByTagName('result')[0].firstChild.data;

	if ( result == 1 ) {
		inpage_unfavQuote(quoteID, toggleshow);
	} else {
		// failure
	}
}

////////////////////////////////////////

// threadsafe asynchronous XMLHTTPRequest code
// http://www.xml.com/cs/user/view/cs_msg/2815
function ajaxSend(url, callback) {

	// we use a javascript feature here called "inner functions" using these
    // means the local variables retain their values after the outer function
    // has returned. this is useful for thread safety, so reassigning the
    // onreadystatechange function doesn't stomp over earlier requests.

	function ajaxBindCallback() {
		//readyState Status Codes:
		//	0 = uninitialized
		//	1 = loading
		//	2 = loaded
		//	3 = interactive
		//	4 = complete
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if ( ajaxCallback ) {
					ajaxCallback(ajaxRequest.responseXML);
				} else {
					alert('no callback defined');
				}
			} else {
				alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
			}
		}
	}

	// use a local variable to hold our request and callback until the inner
    // function is called...

	var ajaxRequest = null;
	var ajaxCallback = callback;

	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if ( window.ActiveXObject ) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if ( ajaxRequest ) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}

	// just because
	return false;
}
