/**
 * Common Functions
 *
 * This script file contains functions to use on a global scale across
 * all pages of the website.
 * @author Jonathan Agbayani
 * @version 2.1 (2007.08.20)
 *
 */

/**
 * Creates and returns a browser specific XMLHttpRequest Object
 * @author Lee Babin (original), Jonathan Agbayani
 * @returns XMLHttpRequest object, ActiveXObject for IE browsers
 */
function getXMLHttpRequest()
{
	// Create a boolean variable to check for a valid Internet Explorer instance,
	var xmlhttp = false;

	// Check if we are using IE.
	try {
		// If the JavaScript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		// If not, then use the older ActiveX object.
		try {
			// If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			// Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	// If we are not using IE, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

/**
 * Runs an AJAX post request and automatically updates HTML based on response
 * @param string url the page to call
 * @param string params an encoded url query string
 * @returns void
 */
function ajax_post(url, params)
{
	var xmlhttp = getXMLHttpRequest();
	xmlhttp.open("POST", url);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			var rtxt = xmlhttp.responseText;
			var pos  = rtxt.indexOf("|");
			if (pos)
			{
				var elem_id = rtxt.substr(0, pos);
				var html    = rtxt.substr(pos + 1);
				document.getElementById(elem_id).innerHTML = html;
			}
		}
	}
	if (params != "") { params = "&" + params; }
	xmlhttp.send("mode=ajax" + params);
}

/**
 * Takes all form elements and put them into a query string for AJAX processes
 * @param form f the form to submit
 * @returns string of url query
 */
function toURLQuery(f)
{
	var query = "";
	for (var i = 0; i < f.elements.length; i++) {
		query += f.elements[i].name + "=" + escape(f.elements[i].value) + "&";
	}
	return query;
}

/**
 * Trims a string on both ends
 * @param string str string to trim
 * @return string str trimmed string
 */
function trim(str)
{
	// Use a regular expression to replace padding spaces with blanks
	return str.replace(/^\s*|\s*$/g, "");
}

/**
 * Checks if a string, after trimming, is an empty string
 * @param string str string to check
 * @returns boolean whether string is empty
 */
function empty(str)
{
	// If the string length is 0, then it is considered empty
	if (trim(str).length < 1) return true;
	return false;
}

/**
 * Validates a string to see if it is alphanumeric
 * By default it ignores spaces and does not allow underscores
 * @param string str
 * @param boolean withspaces default=true ignore spaces
 * @param boolean withunderscore default=false ignore underscores
 * @returns boolean
 */
function isAlphaNum(str, withspaces, withunderscore)
{
	if (withspaces == null || typeof withspaces != 'boolean')
		withspaces = true;
	if (withunderscore == null || typeof withunderscore != 'boolean')
		withunderscore = false;

	var regex;
	if (withspaces && !withunderscore) {
		regex = /^[a-z0-9 ]+$/i;
	} else if (!withspaces && !withunderscore) {
		regex = /^[a-z0-9]+$/i;
	} else if (!withspaces && withunderscore) {
		regex = /^[a-z0-9_]+$/i;
	} else if (withspaces && withunderscore) {
		regex = /^[a-z0-9 _]+$/i;
	}
	return regex.test(str);
}

/**
 * Validates a string to see if it is alphabetic
 * By default it ignores spaces and does not allow underscores
 * @param string str
 * @param boolean withspaces default=true ignore spaces
 * @param boolean withunderscore default=false ignore underscores
 * @returns boolean
 */
function isAlphabetic(str, withspaces, withunderscore)
{
	if (withspaces == null || typeof withspaces != 'boolean')
		withspaces = true;
	if (withunderscore == null || typeof withunderscore != 'boolean')
		withunderscore = false;

	var regex;
	if (withspaces && !withunderscore) {
		regex = /^[a-z ]+$/i;
	} else if (!withspaces && !withunderscore) {
		regex = /^[a-z]+$/i;
	} else if (!withspaces && withunderscore) {
		regex = /^[a-z_]+$/i;
	} else if (withspaces && withunderscore) {
		regex = /^[a-z _]+$/i;
	}
	return regex.test(str);
}

/**
 * Reenables all submit, reset, and button type inputs
 * @returns void
 */
function turnkey()
{
	for (var i = 0; i < document.forms.length; i++) {
		var f = document.forms[i];
		for (var j = 0; j < f.elements.length; j++) {
			var e = f.elements[j];
			if (e.type == "submit" || e.type == "reset" || e.type == "button")
				e.disabled = false;
		}
	}
}

function preload_menu()
{
	var img = new Image();
	img.src = "images/menu_over.png";
}

function menu_move(type)
{
	var elem = document.getElementById('menu_image');
	elem.src = "images/menu_" + type + ".png";
}

window.onload = function()
{
	preload_menu();
}
