/* General and GUI utility functions */


function addOption(sId,sValue,sTxt, isSelected) {
	var theOption = document.createElement('option');
	theOption.value = sValue;
	theOption.text = sTxt;
	if (isSelected) {
		theOption.selected = true;
	}
	try {
		document.getElementById(sId).add(theOption,null);
	}
	/* IE only */
	catch(e) {
		document.getElementById(sId).add(theOption);
	}
}

function toggleDirection(currentLang) {
	var inputs = document.forms[0].elements;
	var currentDir = 'ltr';
	if (currentLang == 'he') {
		currentDir = 'rtl';
	}
	for (i = 0;i < inputs.length;i++) {
		if (inputs[i].type == 'text' || inputs[i].type == 'textarea') {
			inputs[i].style.direction = currentDir;
		}
	}
}

function formatOutput (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) {
	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 = messages.ie6Text;
			slideOpen('ie6Detector', 65);
		}
		var images = document.getElementsByTagName('img');
		for (var i=0;i < images.length;i++) {
			images[i].src = images[i].src.replace(/png/i,'gif');
		}
	}
}