//===========================================================================
// (C) 1997-2007 Jesse Quattlebaum, All Rights Reserved.
//===========================================================================

//===========================================================================
// File:           baseUtils.js
// Purpose:        to provide basic javascript functionality
// History:        2005.10.10 - JQuattlebaum
//===========================================================================

//configuration object to keep track of things going on
if(typeof baseUtils_configObj != 'object') {
	baseUtils_configObj = new Object();
}

//===========================================================================
// Function:       print_r()
// Purpose:        return structure of an object
// Example:        alert(print_r(OObj, true, '\t'));
// IMPORTANT NOTE: When using the recursive flag of this function
//                 you *have* to be careful as JS likes to loop back around
//                 between objects infinitly.  a future 'seen' flag will
//                 (hopefully) get built in.
// History:        2005.12.02 - JQuattlebaum - Initial Revision
//===========================================================================

function print_r(OObj, recurse, prependingSpace) {
	if(typeof OObj == 'object') {
		var treeDisplay = '';
		for(var key in OObj) {
			treeDisplay += prependingSpace+'['+key+'] => \''+OObj[key]+'\' ('+typeof OObj[key]+')\n';
			if(recurse && typeof OObj[key] == 'object') {
				treeDisplay += print_r(OObj[key], recurse, prependingSpace+'\t');
			}
		}
		return treeDisplay;
	} else {
		return 'not an object!';
	}
}

//===========================================================================

//===========================================================================
// Functions:     genPointerObject(), getObject(), getFormObject()
// Purpose:       to store a cached version of pointers to
//                objects we are focused on so we don't have
//                to keep calling getElementsById and document.forms
// History:       2005.10.10 - JQuattlebaum - Initial Revision
//===========================================================================

//register a branch in the config for the object pointers
if(typeof baseUtils_configObj['objectPointers'] != 'object') {
	try {
		baseUtils_configObj['objectPointers'] = new Object();
		baseUtils_configObj['objectPointers']['objPtr'] = new Object();
		baseUtils_configObj['objectPointers']['formPtr'] = new Object();
	} catch (e) {
		baseUtils_configObj['objectPointers'] = new Object();
		baseUtils_configObj['objectPointers']['objPtr'] = new Object();
		baseUtils_configObj['objectPointers']['formPtr'] = new Object();
	}
}

function getObject(pointerName) {
	if(typeof baseUtils_configObj['objectPointers']['objPtr'][pointerName] != 'object') {
		baseUtils_configObj['objectPointers']['objPtr'][pointerName] = document.getElementById(pointerName);
	}
	return baseUtils_configObj['objectPointers']['objPtr'][pointerName];
}
function getFormObject(formName, pointerName) {
	if(typeof baseUtils_configObj['objectPointers']['formPtr'][pointerName] != 'object') {
		baseUtils_configObj['objectPointers']['formPtr'][pointerName] = document.forms[formName][pointerName];
	}
	return baseUtils_configObj['objectPointers']['formPtr'][pointerName];
}

//===========================================================================

//===========================================================================
// Functions:     showCell(), hideCell(), swapCellDisplay()
// Purpose:       do basic display handling for specific cells
// History:       2006.01.09 - JQuattlebaum - Initial Revision
//===========================================================================

//configuration object to keep track of things going on
function primeDispCache(id) {
	if(typeof baseUtils_configObj['disp_cache'] != 'object') {
		baseUtils_configObj['disp_cache'] = new Object();
	}
	if(typeof baseUtils_configObj['disp_cache'][id] != 'object') {
		baseUtils_configObj['disp_cache'][id] = new Object();
	}
	if(typeof baseUtils_configObj['disp_cache'][id]['style'] != 'object') {
		baseUtils_configObj['disp_cache'][id]['style'] = new Object();
	}
}
function showCell(id, display_flag) {
	//alert('id: '+id);
	primeDispCache(id);
	if(display_flag == undefined) {
		var display_flag = '';
	}
	if(typeof baseUtils_configObj['disp_cache'][id] == 'object') {
		if(typeof baseUtils_configObj['disp_cache'][id]['style'] == 'object') {
			if(typeof baseUtils_configObj['disp_cache'][id]['style']['display'] == 'string') {
				display_flag = baseUtils_configObj['disp_cache'][id]['style']['display'];
				//alert('using cached display_flag: '+display_flag);
			}
		}
	}
	if(display_flag == undefined) {
		display_flag = '';
	}
	//alert('display_flag: '+display_flag);
	if(typeof getObject(id) == 'object') {
		//alert('id: '+id+'\n\n'+typeof getObject(id)+'\n\ndisplay: '+getObject(id).style.display);
		//alert('display_flag: '+display_flag);
		getObject(id).style.display = display_flag;
		return true;
	} else {
		return false;
	}
}
function hideCell(id) {
	primeDispCache(id);
	if(typeof getObject(id) == 'object') {
		getObject(id).style.display = 'none';
		return true;
	} else {
		return false;
	}
}
function swapCellDisplay(id) {
	primeDispCache(id);
	if(typeof getObject(id) == 'object') {
		if(getObject(id).style.display == '' || getObject(id).style.display == 'inline' || getObject(id).style.display == 'block') {
			baseUtils_configObj['disp_cache'][id]['style']['display'] = getObject(id).style.display;
			return hideCell(id);
		} else {
			return showCell(id);
		}
	} else {
		return false;
	}
}

//===========================================================================

//===========================================================================
// Functions:     registerDebug(), toggleDebugOutput()
// Purpose:       do debug flipping on the screen
// History:       2006.5.2 - JQuattlebaum - Initial Revision
//===========================================================================

//register a branch in the config for the debug objects to be registered in for flipping visually
if(typeof baseUtils_configObj['debugOutputs'] != 'object') {
	baseUtils_configObj['debugOutputs'] = new Object();
}

function registerDebug(id) {
	if(typeof getObject(id) == 'object') {
		baseUtils_configObj['debugOutputs'][id] = true;
		//should probably hide it here just to make sure we don't ever
		//hit a point where we have some showing and some hiding and
		//hitting the switch just reverses it - i think we will mostly
		//want debug stuff to be shown but sometimes we might want to
		//show only a single one..  in that case we'll have to write
		//an expansion to handle that
		hideCell(id);
	} else {
		return false;
	}
}
function toggleDebugOutput() {
	for(var k in baseUtils_configObj['debugOutputs']) {
		swapCellDisplay(k);
	}
}

//===========================================================================

//===========================================================================
// Functions:     onKeyDown(), onKeyUp(), eventhandler()
// Purpose:       keep track of the keyboard and react accordingly
// History:       2006.5.12 - JQuattlebaum - Initial Revision
//===========================================================================

// actively handle keyboard events
var moz = false;
if (navigator.appName == "Mozilla" || (navigator.appName == "Netscape" && navigator.appVersion.indexOf("4.") == -1)) {
	moz = true;
}
if (moz) {
	document.addEventListener("keydown",baseUtils_onKeyDown,true);
	document.addEventListener("keyup",baseUtils_onKeyUp,true);
	//register a branch in the config for firefox to keep track of keypresses to catch a ctrl-q
	if(typeof baseUtils_configObj['keyboardListener'] != 'object') {
		baseUtils_configObj['keyboardListener'] = new Object();
	}

} else if(navigator.appName == "Netscape" && navigator.appVersion.indexOf("4.x") != -1) {
	document.captureEvents(Event.BASEUTILS_ONKEYDOWN);
	document.captureEvents(Event.BASEUTILS_ONKEYUP);
}
if (!moz) {
	document.onkeypress=baseUtils_onKeyDown;
	document.onkeyup=baseUtils_onKeyUp;
}

function baseUtils_onKeyDown(e) {
	if (navigator.appName == "Microsoft Internet Explorer")  {
		eventObj = window.event;
	} else if (moz) {
		eventObj = e;
		baseUtils_configObj['keyboardListener'][eventObj.keyCode] = 'down';
	}
	baseUtils_eventHandler(eventObj);
}
function baseUtils_onKeyUp(e) {
	if (navigator.appName == "Microsoft Internet Explorer")  {
		eventObj = window.event;
	} else if (moz) {
		eventObj = e;
		baseUtils_configObj['keyboardListener'][eventObj.keyCode] = 'up';
	}
	baseUtils_eventHandler(eventObj);
}

function baseUtils_eventHandler(eventObj) {
	//=[TOGGLE DEBUG OUTPUT
	//look for CTRL-D (IE) or CTRL-Q (FF) and do visual debug flipping
	if (navigator.appName == "Microsoft Internet Explorer")  {
		if (window.event.keyCode == 113 || window.event.keyCode == 81 || window.event.keyCode == 68 || window.event.keyCode == 100) {
			if(window.event.ctrlKey) {
				//CTRL-D detected
				toggleDebugOutput();
			}
		}
	} else if (moz) {
		if(baseUtils_configObj['keyboardListener']['17'] == 'down' && baseUtils_configObj['keyboardListener']['81'] == 'down') {
			//CTRL-Q detected
			//get rid of the keyboard listeners for these keys (as they are being pressed and js is reacting the unpress does not get registered by the keyboard listener so manually doing it is the only solution)
			baseUtils_configObj['keyboardListener']['17'] = '';
			baseUtils_configObj['keyboardListener']['81'] = '';
			toggleDebugOutput();
		}
	}
	//======================
}
//===========================================================================

//===========================================================================
// Functions:     ltrim(), rtrim(), trim()
// Purpose:       provide trim functionality
// History:       2006.5.13 - JQuattlebaum - Initial Revision
//                2006.8.3 - JQuattlebaum - found a better example (noel)
//===========================================================================
	function ltrim(str){
		return str.replace(/^\s+/g,'');
	}
	function rtrim(str){
		return str.replace(/\s+$/g,'');
	}
	function trim(str){
		return ltrim(rtrim(str));
	}

	//truthfully?  these were stolen.  thanks noel.
	function is_array(obj){
		return ( typeof(obj) == "object" && obj.length != "undefined" );
	}
	function in_array(needle,haystack) {
	    for (i in haystack) {
	        if (haystack[i] == needle) return true;
	    }
	    return false;
	}
//===========================================================================

//===========================================================================
// Function:       isEmailValid(str)
// Purpose:        only if str is a valid email address return true
// History:        2007.10.21 - JQuattlebaum - Initial Revision
//===========================================================================
function isEmailValid(str) {
	var invalidRegExp = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var regExp = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	if(!invalidRegExp.test(str) && regExp.test(str)) {
		return true;
	}
	return false;
}
//alert('baseUtils is loading.  to get rid of this message go to the /common/shared_content/js/ directory, edit the baseUtils.js file and look at the very bottom.   you will find this message.  enjoy.  -psy');
