/*	Script: stdlib

	A library of standard javascript functions used throughout the site
*/


/*	Function: debug

	Just a happy little debug function that uses FireBug's console for output

	Parameters:
		text - text to send to the console
*/
function debug(text) {
	try {
		console.log(text);
	} catch (e) { } 
}

/*	Function: _getXmlHttpObject

	A compatibility function for instantiating XMLHttpObjects

	Returns:
		a new XMLHttpObject instance if a compatibility method could be found, null otherwise
*/
function _getXmlHttpObject() {
	// the various methods different browsers use for XMLHttpObjects
	var variants = [
		function() { return new XMLHttpRequest(); },
		function() { return new ActiveXObject("Msxml3.XMLHTTP"); },
		function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
		function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
	];

	var xmlHttp = null;

	// loop through the list of variants
	for (var i = 0; i < variants.length; i++) {
		try {
			// try to instantiate the object using this variant
			xmlHttp = variants[i]();

			// if that worked, short-circuit this function to use
			// this variant the next time it's called
			_getXmlHttpObject = variants[i];

			// and return the object we created (also breaking out
			// of the loop)
			return xmlHttp;
		} catch (e) {
			// if this variant didn't work, we'll just continue on
			// to the next one
			continue;
		}
	}

	// if all that failed, there is no hope, and so we'll short-circuit to
	// a function that just returns null
	_getXmlHttpObject = function() { return null; };

	return null;
}

/*	Function: _getElementById
	
	A compatibility function for getElementById
	
	Written in the same fashion as the compatibility function _getXmlHttpObject above 
	(so check that function out if you're curious how this works)

	Parameters:
		id - id of element to retrieve DOM object for
	
	Returns:
		the DOM object for the specified id if a compatibility method could be found,
		null otherwise
*/
function _getElementById(id) {
	var variants = [
		function(id) { return document.getElementById(id); },
		function(id) { return document.all[id]; },
		function(id) { return document.layers[id]; }
	];

	var element = null;

	for (var i = 0; i < variants.length; i++) {
		try {
			element = variants[i](id);

			_getElementById = variants[i];

			return element;
		} catch (e) {
			continue;
		}
	}

	_getElementById = function(id) { return null; }

	return null;
}

/*	Function: _getElementsByTagName

	A compatibility function for _getElementsByTagName

	Parameters:
		tagName - tagName to get elements for
		element - root element to search from
	
	Returns:
		an array of elements with the specified tagname
*/
function _getElementsByTagName(tagName, element) {
	if (element == null) element = document;

	return element.getElementsByTagName(tagName);
}

function _getElementsByClass(node, searchClass, tag) {
	var classElements = new Array();
	var els = _getElementsByTagName(tag, node);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}

	return classElements;
}

/*	Function: _xmlHttpRequest

	Perform an XMLHttpRequest to the given url

	Parameters:
		url - url to send request to
		postStr - string of POST variable to send
		callback - callback function to call once request has processed
*/
function _xmlHttpRequest(url, postStr, callback) {
	// instantiate an XMLHttpObject
	var xmlHttpObj = _getXmlHttpObject();

	// if that worked...
	if (xmlHttpObj != null) {
		// create a callback to monitor the state of the request
		xmlHttpObj.onreadystatechange = function() {
			// if the request was successful and the response is
			// complete...
			if (xmlHttpObj.readyState == 4) {
			//&& xmlHttpObj.status
			//&& xmlHttpObj.status == 200) {
				// perform the callback
				callback(
					xmlHttpObj.responseText,
					xmlHttpObj.responseXML
				);
			}
		}

		// if the POST string was set
		if (postStr != null) {
			// open a POST connection
			xmlHttpObj.open("POST", url, true);

			// set the headers properly
			xmlHttpObj.setRequestHeader(
				"Content-Type",
				"application/x-www-form-urlencoded"
			);

			// and send the data
			xmlHttpObj.send(postStr);
		} 
		
		// otherwise
		else {
			// open a GET connection
			xmlHttpObj.open("GET", url, true);

			// and send... nothing (but send it nonetheless)
			xmlHttpObj.send(null);
		}
	} else {
		debug("couldn't load xmlHttpObject");
	}
}

/*	Function: _replaceInnerHTML

	Replace the contents of an element with the response of an XMLHttpRequest

	Parameters:
		url - url to send the request to
		postStr - a string containing the POST variables
		element_id - element to replace the contents of the response with
*/
function _replaceInnerHTML(url, postStr, element_id) {
	// grab the element's object
	var elementObj = _getElementById(element_id);

	if (elementObj) {
		// create a simple callback to replace the element's inner html
		// with the response text
		var callback = function(text) {
			elementObj.innerHTML = text;
		}

		// and perform the request
		_xmlHttpRequest(url, postStr, callback);
	}
}

/*	Function: _sessionTimeoutHandler

	Sends user to inactivity logout after timeout occurs
*/

function _sessionTimeoutHandler() {
	window.location = "/users/auth/inactivity";
}

/*	Function: _stripSlashes

	Just a handy function to strip slashes from slash-escaped quote chars

	Parameters:
		str - input string
	
	Returns:
		slash free string
*/
function _stripSlashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
