function _debug(t) {
	if ($('debug') != null) {
		$('debug').value = t+"\n"+$('debug').value;
	}
}

function _escape(s) {
	var retval;

	retval = encodeURIComponent(s);
	//retval = retval.replace(/=/g, '%3D');

	return(retval);
}

var _RESP_STATUS = 0;
var _RESP_CODE = 1;
var _RESP_DATA = 2;
function _parseResponse(response) {
	var status, code, data;

	status = 'ER';
	code = 0;
	data = null;
	
	if (response.length >= 2) status = response.substr(0,2);
	if (response.length >= 12) code = Math.floor(response.substr(2,10));
	if (response.length > 12) data = response.substr(12);

	var retval = new Array();

	retval[_RESP_STATUS] = status;
	retval[_RESP_CODE] = code;
	retval[_RESP_DATA] = data;

	return(retval);
}

function _submitMainForm(action) {
	var f = document.getElementById('mainform');
	if (f != null) {
		f.action.value = action;
		f.submit();
	}
}

// correctly handle PNG transparency in Win IE 5.5 or higher. 
function _correctPNG() {
	for(var i=0; i<document.images.length; i++) {
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}

var _higlightedSection = null;
function _highlightNav(e, enable) {
	if (enable == null) enable = 1;
	if (enable == 0 && _higlightedSection != e) {
		$(e).style.color = '#2c6975';
		$(e+'_arrow').style.display = "none";
	} else {
		if (_higlightedSection == null) { _higlightedSection = e; }
		$(e).style.color = '#fff';
		$(e+'_arrow').style.display = "inline";
	}
}

var _statusMessageTimer = new Array();
function _updateStatusMessage(msg, n) {
	if (n == null) n = 1;
	if ($('statusmsg'+n) != null) {
		clearTimeout(_statusMessageTimer[n]);
		$('statusmsg'+n).innerHTML = msg;
		Effect.Appear('statusmsg'+n, { duration: 0.25 } );
		_statusMessageTimer[n] = setTimeout("_hideStatusMessage('"+n+"')", 5000);
	}
}

function _hideStatusMessage(n) {
	if ($('statusmsg'+n) != null) {
		Effect.Fade('statusmsg'+n, { duration: 0.25 } );
	}
}

var _currentInfoBoxTipId = null;
var _currentInfoBoxTipTrigger = null;
var _currentInfoBoxTipTimer = null;
function _showInfobox(trigger) {
	var x = 0;
	var y = 0;
	var content = '';
	_currentInfoBoxTipId = null;
	_currentInfoBoxTipTrigger = null;

	clearTimeout(_currentInfoBoxTipTimer);

	for(var i=0;i<_infoTips.length;i++) {
		var data = _infoTips[i].split(/\|/);
		if (data.length == 5) {
			if (data[0] == trigger) {
				tip_id = data[1];
				x = Math.floor(data[2]);
				y = Math.floor(data[3]);
				content = data[4];

				_infoTips.splice(i, 1); // Remove this element

				_currentInfoBoxTipId = tip_id;
				_currentInfoBoxTipTrigger = trigger;
			}
		}
	}

	if (content == '') return;

	$('infobox_content').innerHTML = content;
	if (x < 300) {
		x = x + 88;
		Element.show('infobox_arrow_left');
		Element.hide('infobox_arrow_right');
	} else {
		x = x - 227 - 88;
		Element.show('infobox_arrow_right');
		Element.hide('infobox_arrow_left');
	}
	$('infobox').style.left = x + 'px';
	$('infobox_arrow_left').style.margin = '25px 0 0 -87px';
	$('infobox_arrow_right').style.margin = '25px 0 0 222px';
	$('infobox').style.top = (y - 110 + 35) + 'px';
	$('infobox_close').href = "javascript:_closeInfobox(1)";

	Element.show('infobox');

	_currentInfoBoxTipTimer = setTimeout('_closeInfobox()', 15000);
}

function _closeInfobox(dontshowagain) {
	Element.hide('infobox');

	clearTimeout(_currentInfoBoxTipTimer);

	if (dontshowagain) {
		_markTipViewed(_currentInfoBoxTipId);
	}

	// Show next tip for this trigger
	if (_currentInfoBoxTipTrigger != null) {
		setTimeout("_showInfobox(_currentInfoBoxTipTrigger)", 250);
	}
}

function _markTipViewed(tip_id) {
	new Ajax.Request('/action_managetips.php',
		{
			asynchronous: true,
			parameters: 'action=mark&tip_id='+escape(tip_id),
			onSuccess: _markTipViewedHandler,
			onFailure: _markTipViewedErrorHandler
		}
	);
}

function _markTipViewedHandler(req) {
	var data = _parseResponse(req.responseText);
	if (data[_RESP_STATUS] == 'OK') {
		_debug('Tip marked as viewed.');
	} else {
		_debug(req.responseText);
	}
}

function _markTipViewedErrorHandler(req) {
	_debug(req.responseText);
}

function _printWindow(url) {
	hwnd = window.open(url, 'print', 'width=50,height=50,resizeable=yes,toolbar=no,statusbar=no,menubar=yes,scrollbar=no');
}

function _getDimensions(e) {
	var obj = $(e);

	var w = obj.style.width;
	var h = obj.style.height;
	w = Math.floor(w.replace(/px/, ''));
	if (w < 200) w = 200;
	h = Math.floor(h.replace(/px/, ''));
	var x = y = 0;
	if (obj.offsetParent) {
		x = obj.offsetLeft;
		y = obj.offsetTop;
		while (obj = obj.offsetParent) {
			if (obj.id != null && obj.id != 'page' && obj.id != 'maincontent' && obj.id != 'wrap') {
				x += obj.offsetLeft;
				y += obj.offsetTop;
			}
		}
	}

	var retval = new Array();
	retval[0] = x;
	retval[1] = y;
	retval[2] = w;
	retval[3] = h;
	return(retval);
}
