/* General and GUI utility functions */

function formatOutput (num) {
	num += '';
	if (num.indexOf('.') > -1 && num.indexOf('.') == num.length - 2) {
		num += '0';
	}
	return num;
}

function formatOutputWithTrim (num) {
	var floored_num = Math.round(num);
	var dec = num - floored_num;
	dec = Math.round(dec*100)/100;
	if (dec == 0) {
		dec = '.00';
	}
	num = floored_num + dec;
	nStr = num + '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function change_img (s_id, src) {
	document.getElementById(s_id).src = _IG_GetImageUrl('http://' + src_url +'.labpixies.com/campaigns/expenses/images/' + src);
}

function show(s_id, display) {
	if( typeof(display) == 'undefined') {
		display = 'block'
	}
	document.getElementById(s_id).style.display = display;
}

function hide(s_id) {
	if (!document.getElementById(s_id)) {
		return false;
	}
	document.getElementById(s_id).style.display = 'none';
}

function show_vis(s_id) {
	document.getElementById(s_id).style.visibility = 'visible';
}

function hide_vis(s_id) {
	document.getElementById(s_id).style.visibility = 'hidden';
}

function slideOpen (sId, finalHeight) {
	var slidingElement = document.getElementById(sId);
	slidingElement.style.height = '1px';
	var actualHeight = 2;
	show (sId);
	var slideInt = setInterval(function () {
		while (actualHeight < finalHeight ) {
			slidingElement.style.height = actualHeight + 'px';
			actualHeight++;
		}
		clearInterval(slideInt);
	}, 500);
}

function slideClose (sId) {
	var slidingElement = document.getElementById(sId);
	var actualHeight = slidingElement.offsetHeight;
	var slideCloseInt = setInterval(function () {
		while (actualHeight >= 1 ) {
			slidingElement.style.height = actualHeight + 'px';
			actualHeight--;
		}
		hide (sId);
		clearInterval(slideCloseInt);
	}, 500);
}

function detectIe6 () {
	if (navigator.appVersion.indexOf('MSIE 6.0') > -1 && navigator.appVersion.indexOf('MSIE 7.0') < 0) {
		if(document.getElementById('ie6Detector')) {
			document.getElementById('ie6Detector').innerHTML = '<p><strong>Your Internet is not up to date</strong>.  This is not the last version of Microsoft Internet Explorer</p>\n<p>You can enjoy your web surfing much more bu upgrading your browser, and it is even free!</p>\n<p><a href="http://www.mozilla-europe.org/en/products/firefox/" target="_blank">Install <strong>Firefox</strong></a> | <a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx" target="_blank">Upgrade your Microsoft Internet Explorer</a> | <a href="http://www.uweb.ucsb.edu/~mguidry/firefox/" target="_blank"><strong>what\'s best for me?</strong></a></p>\n<p class="button" onclick ="slideClose(\'ie6Detector\');">Later. Now, please go away.</p>';
			slideOpen('ie6Detector', 65);
		}
	}
}

var step = 32;
var initPosition = 0;

var intStep = 20;
var listPos = 0;

function goFwd(movingBox, fwd, bkwd, srollAmount) {
	if( typeof(srollAmount) == 'undefined') {
		srollAmount = 5
	}
	var movingBox = document.getElementById(movingBox);
	var fwdButton = document.getElementById(fwd);
	var bkButton = document.getElementById(bkwd);
	var tailHeight = movingBox.offsetHeight - movingBox.parentNode.offsetHeight;
	if (bkButton.style.display != 'block') {
		bkButton.style.display = 'block';
	}
	stopAt = -tailHeight;
	var intCounter = 0;
	var slide = setInterval(function() {
		if (intCounter <= step) {
			initPosition -= srollAmount;
			intCounter += 5;
			movingBox.style.top = initPosition + 'px';
		}
		else {
			clearInterval(slide);
			if (initPosition <= stopAt) {
				fwdButton.style.display = 'none';
			}
		}
	},50);
}


function goBack(movingBox, fwd, bkwd, srollAmount) {
	if( typeof(srollAmount) == 'undefined') {
		srollAmount = 5
	}
	var movingBox = document.getElementById(movingBox);
	var fwdButton = document.getElementById(fwd);
	var bkButton = document.getElementById(bkwd);
	if (fwdButton.style.display != 'block') {
		fwdButton.style.display = 'block';
	}
	var intCounter = 0;
	var slide = setInterval(function() {
		if (intCounter <= step) {
			initPosition += srollAmount;
			intCounter += 5;
			movingBox.style.top = initPosition + 'px';
		}
		else {
			clearInterval(slide);
			if (initPosition >= 0) {
				movingBox.style.top = 0
				bkButton.style.display = 'none';
			}
		}
	},50);
}


