var httpNicknameStatus	  = getHTTPObject();
var MAX_FRIENDS = 9;
var MAX_FRIEND_NAME_SIZE = 12;
var MIN_NICKNAME_SIZE = 2;
var pendingResponse = false;
var doAnotherAfterPendDone = false;

function checkNickname()
{
//				document.getElementById('FRIEND_2').innerHTML = "<FONT COLOR=RED><b>Disabled </b></FONT>";
//				document.getElementById('FRIEND_5').innerHTML = "<FONT COLOR=RED><b>Temporarily</b></FONT>";
	var nickname = trim(document.FRIEND_FORM.Friend.value);
	try
	{
		// Waiting for last results, lets not hit the server again until they're back
		if (!pendingResponse)
		{
			if (nickname.length > MIN_NICKNAME_SIZE) {
				httpNicknameStatus.open("GET", "/atp/find-friends-provider.php?NICKNAME=" + escape(nickname) + "&X=" + Math.floor(Math.random() * 99999), true);
				httpNicknameStatus.onreadystatechange = handleNicknameResponse;
				httpNicknameStatus.send(null);
				pendingResponse = true;
			}
			else if (nickname) {
				// One is specified, but it's too small for us to check
				// Clear the divs initially
				j = 0;
				while (j < MAX_FRIENDS)
				{
					document.getElementById('FRIEND_'  + ++j).innerHTML = '&nbsp;';
				}
				document.getElementById('FRIEND_2').innerHTML = "<FONT COLOR=RED><b>" + (MIN_NICKNAME_SIZE+1) + " characters</b></FONT>";
				document.getElementById('FRIEND_5').innerHTML = "<FONT COLOR=RED><b>required</b></FONT>";
				document.getElementById('FRIEND_8').innerHTML = "<FONT COLOR=RED><b>to search</b></FONT>";
			}
			else {
				// Clear the divs, no nickname specified, no need to hit server
				j = 0;
				while (j < MAX_FRIENDS)
				{
					document.getElementById('FRIEND_'  + ++j).innerHTML = '&nbsp;';
				}
			}
		}
		else
			doAnotherAfterPendDone = true;
	}
	catch (e)
	{
//		var resultsDiv = document.getElementById('FindaFriendResultBox');
//		resultsDiv.innerHTML = 'Problem Retrieving Nicknames!';
	}

}

function handleNicknameResponse()
{
	try
	{
		if (httpNicknameStatus.readyState == 4)
		{
			pendingResponse = false;
			// if we held up and didn't hit the server when there was a pending request out there
			// and pending request is back now... lets ignore this response and do one more server hit with
			// what the user typed so we don't annoy him with these old results and we don't
			// ignore his last keystrokes
			if (doAnotherAfterPendDone) {
				doAnotherAfterPendDone = false;
				checkNickname();
			}
			else {
				if (httpNicknameStatus.responseText.indexOf('invalid') == -1) {
					// Use the XML DOM
					var xmlDocument = httpNicknameStatus.responseXML;

					//
					//  get the result object
					//
					var friendsCount = xmlDocument.getElementsByTagName('friends').item(0);
					var result	     = friendsCount.getAttribute('count');

					// Clear the divs initially
					j = 0;
					while (j < MAX_FRIENDS)
					{
						document.getElementById('FRIEND_'  + ++j).innerHTML = '&nbsp;';
					}
					if (result > 0) {
						var friends = xmlDocument.getElementsByTagName('friends');
						var friendsAdded = 0;
						i = 0;

						while (i < friends[0].childNodes.length) {
							// Continue only if childNode is a tag (ignores white space in some browsers)
							if (friends[0].childNodes[i].nodeType == 1) {
								nickName = friends[0].childNodes[i].getAttribute('nickname');
								if (friends[0].childNodes[i].getAttribute('nickname').length > MAX_FRIEND_NAME_SIZE) {
									nickName = friends[0].childNodes[i].getAttribute('nickname').substr(0, MAX_FRIEND_NAME_SIZE) + '...';
								}
								resultsDiv = document.getElementById('FRIEND_'  + ++friendsAdded);
								resultsDiv.innerHTML = '<a href="/atp/home.php?USER=' + friends[0].childNodes[i].getAttribute('user-id') + '&X=' + Math.floor(Math.random() * 99999) +
									'" title="' + friends[0].childNodes[i].getAttribute('nickname') + '">' + nickName + '</a>';
								i++;

							} // node_Type == 1
							// row is not of right nodeType
							else i++

							// Cap this at X rows
							if (friendsAdded >= MAX_FRIENDS) {
								break;
							}
						} // while loop
					}  // if results > 0
					else {
						// Put the "None Found" message in the middle
						document.getElementById('FRIEND_2').innerHTML = "<FONT COLOR=RED><b>No Matching</b></FONT>";
						document.getElementById('FRIEND_5').innerHTML = "<FONT COLOR=RED><b>Results Found</b></FONT>";
					}
				} // responseText = -1
			} // doAnotherAfterPendDone
		} // readyState == 4
	}
	catch (e)
	{
		pendingResponse = false;
	}

}

