
/*

 Does all of our JS active/online and incoming messages updating

*/



// used frequently to stop buttons once pushed in ajax
function returnfalse () { return false; }



// for clearing and highlighting text/textarea inputs
function selectText () { this.select(); }
function clearText () { this.value = ''; this.onfocus = selectText; }
function clearText2 () { this.value = ''; this.onfocus = null; }

function dohighlighttext ()
{
	var els = document.getElementsByTagName('input');  // all text inputs
	for (i in els) { if (els[i].className == 'highlighttext') { els[i].onfocus = clearText; } }
	els = document.getElementsByTagName('textarea');  // all textareas as well
	for (i in els) { if (els[i].className == 'highlighttext') { els[i].onfocus = clearText; } }
}


// ignores incomplete ajax calls until they are finished & ready
//  and then passes the response data into the specified function
function processAJAXresponse (xhr, func)
{
	// make sure we've got all the data before we continue
	//  sometimes mozilla can give errors on the status attribute
	if (xhr.readyState != 4) return;
	try { if (xhr.status != "200") return; } catch (e) { return; }

	// process the data, now that it's finished loading
	//  (coming from JSON, eval-d into actual JS array of objects)
	func(eval(xhr.responseText));
}



// create the encoded post data string from a hash
function encodepostdatahash (pdh)
{
	// http://xkr.us/articles/javascript/encode-compare/
	var pd = []; for (k in pdh)
	 { pd.push(k + '=' + encodeURIComponent(pdh[k])); }
	return pd.join('&');
}



// creates and sends a generic ajax request
function sendAJAXrequest (url_link, call_func, post_data)
{
	// if there's no vCode defined for this page (should not happen)...
	//  all ajax calls MUST include the vCode from pageload on this site!
	// OR if not sending the ajax request anywhere, this is pointless
	// ... just give up (avoids errors in js error console)
	if (typeof(url_link) == 'undefined') { alert("Invalid ajax request!"); return; }
	if (typeof(vCode) == 'undefined') { vCode = ''; }
	if (typeof(aCode) == 'undefined') { aCode = ''; }

	// get our object for talking to the server
	var xhr = false;
	if (!xhr && window.XMLHttpRequest)  // w3c
	 { xhr = new XMLHttpRequest(); }
	if (!xhr && window.ActiveXObject)  // old IE
	 { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
	// no XHR object available with this UA
	if (!xhr) { alert("You need a better browser..."); return; }

	// send the request and set the callback function to receive the response
	//  we ned the anonymous func to allow multiple instances
	//  but we only get the response if we want to process it at all!
	xhr.open("POST", url_link, true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	post_data += '&v='+vCode+'&a='+aCode+'&b='+encodeURIComponent(navigator.userAgent);
	xhr.setRequestHeader("Content-Length", post_data.length);
	if (call_func) { xhr.onreadystatechange = function ()
	 { try { processAJAXresponse(xhr, call_func); } catch (r) {} }; }
	xhr.send(post_data);
}




// and use just one branching function
//  to process the different kinds of responses
function processKeepAlive (msgs)
{
/*
	// now update them with any IM messages they've received
	//  FIXME FIX ME -- coming soon...
	for (i in msgs) { alert(msgs[i]['message']); }


	// var can't have same name as id -- breaks IE!
	out = document.getElementById('outspace');
	if (out)
	{
		out.value += '>>>>' + msgs[0].sent;
		for (i in msgs) { out.value += i+' :: '+msgs[i]+"\n";
		 for (j in msgs[i]) { out.value += '	'+j+' :: '+msgs[i][j]+"\n";
		  for (k in msgs[i][j]) { out.value += '		'+k+' :: '+msgs[i][j][k]+"\n"; } } }
	}
*/
}


function updateActive ()
{
	// we always talk to the same page, in the same way, for our updates
	sendAJAXrequest('/keepalive', processKeepAlive);
}


// update user active/online every 5 seconds
if (typeof(stopKeepAlive) == 'undefined') { window.setInterval('updateActive()', '5000'); }

