/* ---------------------------------------------------------------------------------------- *\
|*	tools.js																				*|
|*	Miscellaneous javascript routines														*|
|*	© Copyright 2008, 2009 Mynor Ramos.  All rights reserved.								*|
\* ---------------------------------------------------------------------------------------- */

// variable that will tell us if the user is using IE
var MSIE = (navigator.appName.toLowerCase().indexOf("microsoft") != -1);

/* ----------------------------------------------------------------------------------------
	Returns true if the supplied is a valid email address
*/
function isValideMailAddress(eMailAddress)
{
	return eMailAddress.match(/^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/);
}

/* ----------------------------------------------------------------------------------------
	Returns true if the supplied is a valid URL address
*/
function isValidURLAddress(URLAddress)
{
	return URLAddress.match(/^[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/);
}

/* ----------------------------------------------------------------------------------------
	Restricts the valid keys to letters (upper and lower case), numbers, hyphen and
	underscore with no spaces
*/
function restrictInputKeysToCommonSet(e) {
var keyPressed;

	(!MSIE) ? (keyPressed = e.which) : (keyPressed = window.event.keyCode);
	
	// accept numbers, lettes (upper and lower case), the enter key as it does nothing, hyphen and underscore
	if ((keyPressed == 45 || keyPressed == 95 || keyPressed == 0 || keyPressed == 13 || keyPressed == 8) || (keyPressed >= 48 && keyPressed <= 57) || (keyPressed >= 65 && keyPressed <=90) || (keyPressed >= 97 && keyPressed <= 122)) {
		return true;
	} else { // if any other character is entered, display a warning message for 3 seconds
		return false;
	}
}

/* ----------------------------------------------------------------------------------------
	Restricts the valid keys to numbers, spaces and parenthesis
*/
function restrictInputKeysToPhoneFormat(e) {
var keyPressed;

	(!MSIE) ? (keyPressed = e.which) : (keyPressed = window.event.keyCode);
	
	// accept numbers, parenthesis, and space
	if ((keyPressed == 32 || keyPressed == 40 || keyPressed == 41 || keyPressed == 0 || keyPressed == 13 || keyPressed == 8) || (keyPressed >= 48 && keyPressed <= 57)) {
		return true;
	} else { // if any other character is entered, display a warning message for 3 seconds
		return false;
	}
}

/* ----------------------------------------------------------------------------------------
	Restricts the valid keys to numbers and decimal point only
*/
function restrictInputKeysToCurrencyFormat(e) {
var keyPressed;
var object;

	if (e.srcElement) {
		object = e.srcElement;
	} else if (e.target) {
		object = e.target;
	}

	(!MSIE) ? (keyPressed = e.which) : (keyPressed = window.event.keyCode);
	
	// accept numbers, parenthesis, and space
	if (((keyPressed == 46 && object.value.indexOf(".") == -1) || keyPressed == 0 || keyPressed == 13 || keyPressed == 8) || (keyPressed >= 48 && keyPressed <= 57)) {
		return true;
	} else { // if any other character is entered, display a warning message for 3 seconds
		return false;
	}
}

/* ----------------------------------------------------------------------------------------
	Restricts the valid keys to numbers and decimal point only
*/
function restrictInputKeysToNumbers(e) {
var keyPressed;
var object;

	if (e.srcElement) {
		object = e.srcElement;
	} else if (e.target) {
		object = e.target;
	}

	(!MSIE) ? (keyPressed = e.which) : (keyPressed = window.event.keyCode);
	
	// accept numbers, parenthesis, and space
	if ((keyPressed == 0 || keyPressed == 13 || keyPressed == 8) || (keyPressed >= 48 && keyPressed <= 57)) {
		return true;
	} else { // if any other character is entered, display a warning message for 3 seconds
		return false;
	}
}

/* ----------------------------------------------------------------------------------------
	Returns an AJAX obectj
*/

function xmlRequestObj()
{
	var ajaxObj = null;
	
	try {
		ajaxObj = new XMLHttpRequest();
	}
	catch (e) {
		try {
			ajaxObj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return ajaxObj;
}

/* ----------------------------------------------------------------------------------------
	Fades an element
*/

function fade(id, level)
{
	var element = document.getElementById(id).style;
	
	element.opacity = level / 100;
	element.MozOpacity = level / 100;
	element.filter = "alpha(opacity=" + level + ")";
}

/* ----------------------------------------------------------------------------------------
	Starts fading in
*/

function fadeIn(id, level, timeoutId)
{
	var element = document.getElementById(id).style;
	var newLevel = level;
	
	if (timeoutId) { // if the timeout id is provided, end it
		window.clearTimeout(timeoutId);
		alert(timeoutId);
	}
	
	if (newLevel >= 0) // make visible
		element.visibility = "visible";

	if (newLevel < 100) {
		fade(id, newLevel); // set opacity a little more visible each time
		window.setTimeout(function() { fadeIn(id, newLevel + 5); }, 50); // call recursively
		
	} else {
		return window.setTimeout('fadeOut("' + id + '", 100)', 3000);
	}
}

/* ----------------------------------------------------------------------------------------
	Fades an element out, called by fadein
*/

function fadeOut(id, level)
{
	var element = document.getElementById(id).style;
	var newLevel = level;

	// set opacity a little more transparent each time
	fade(id, newLevel);
	
	if (newLevel > 0) // if not completely transparent, call recursively
		window.setTimeout(function() { fadeOut(id, newLevel - 5); }, 50);
	else
		element.visibility = "hidden";
}