var _browser=null, _el_body=null, _el_html=null;
var _lightBox=null;

/**
 * A tool for JS Developers:
 * Dump an object's content to a string
 * @author unknown - contact me if you know who the original author is
 */
function cfr_dump(obj, level)
{
	var dumped_text = "";
	if(!level) level = 0;

	var level_padding = "";
	for(var j=0;j<level+1;j++)
		level_padding += "&nbsp;&nbsp;&nbsp;&nbsp;";

	if(typeof(arr) == 'object')
	{
		for(var item in arr)
		{
			var value = arr[item];

			if(typeof(value) == 'object')
			{
				dumped_text += "("+level+") "+level_padding + "'" + item + "' ...<br />\n";
				dumped_text += dump(value,level+1);
			}
			else
			{
				dumped_text += "("+level+") "+level_padding + "'" + item + "' => \"" + value + "\"<br />\n";
			}
		}
	}
	else
	{
		dumped_text = arr+" ("+typeof(arr)+")";
	}
	return dumped_text;
}

/**
 * Whenever possible, do not use browser detection.
 * Capabilities detection is much cleaner.
 */
function cfr_detectBrowser()
{
	if(_browser)
		return _browser;
	var browseStr = navigator.userAgent.toLowerCase();
	if(browseStr.indexOf('msie')+1 > 0)
		_browser = 'ie';
	else
		_browser = '?';
	return _browser;
}

function cfr_cancelInterval(fn)
{
	clearInterval(fn);
}

/**
 * All apologies for this! Unfortunately, JS doesn't come with a true wait() method
 */
function cfr_wait(numberMillis)
{
	var now = new Date();
	var delay = now.getTime() + numberMillis;
	while (true)
	{
		now = new Date();
		if (now.getTime() > delay)
			return;
	}
}

function cfr_click(name)
{
	var element = document.getElementById(name);
	if(!element)
		return;
	element.onclick = null;
	element.click();
}

//
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function cfr_getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(parseInt(pageWidth),parseInt(pageHeight),parseInt(windowWidth),parseInt(windowHeight));
	return arrayPageSize;
}

document.getElementsByClassName = function(className)
{
	var expression = new RegExp('(^| )'+className+'( |$)');
	var allelements = document.body.getElementsByTagName('*');
	var elements = [];
	for(var i=0; i<allelements.length; i++)
	{
		var element = allelements[i];
		if(expression.test(element.className))
		{
			elements.push(element);
		}
	}
	return elements;
};

//-----------------------------------------------------------
// LIGHTBOX Code
//-----------------------------------------------------------
function lb_initialize(content, blocking, width, height)
{
	/*
	 * 11/18/2007: no need for this old lightbox for now.
	 * It doesn't even work in some browsers...
	$('.lbOn').click(function() { 
		$.blockUI(content, { border: '0px' }); 
		setTimeout("cfr_click('submit')", 1000);
	});
	*/
}

//-----------------------------------------------------------
function cfr_alert(msg)
{
	var imgpath = _includes()+"/pleasewait.gif";
	// Preload image so that we will be able to display it later on
	var preimg = new Image();
	preimg.src = imgpath;
	//
	var pb = "<img id='pwimg' src='"+imgpath+"'>";
	return pb;
}

function cfr_busy()
{
	var imgpath = _includes()+"/processingmedium.gif";
	// Preload image so that we will be able to display it later on
	var preimg = new Image();
	preimg.src = imgpath;
	//
	var pb = "<img id='pwimg' src='"+imgpath+"'>";
	return pb;
}

function cfr_onDom(f)
{
	$(document).ready(function () {eval(f);});
}

var pCookie = ''; // Used to store temp. cookie settings
function setCookie(name, value, expires)
{
	// First, does this cookie already exist? If so, we will be adding to it
	var cookie = name + '=' + value;
	if(expires)
	{
		var d = new Date();
		d.setTime((expires * 1000) + date.getTime());
		cookie += '; expires=' + d.toGMTString();
	}
	document.cookie = cookie;
}

function getCookie(name)
{
	var searchS = name + '=';
	var cookies = document.cookie.split(';');
	for(var i=0; i<cookies.length; i++)
	{
		var cookie = cookies[i];
		while (cookie.charAt(0)==' ')
			cookie = cookie.substring(1, cookie.length);
		if (cookie.indexOf(searchS) == 0)
			return cookie.substring(searchS.length, cookie.length);
	}
	return null;
}

