/* General and GUI utility functions */
var isIE;
var isIE6;
var displayTableRow;
var displayTbody;

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(oForm) {
	var dirTo = document.getElementById('dir').value;
	var alignTo;
	if (dirTo == 'ltr') {
		alignTo = 'left';
	}
	else {
		alignTo = 'right';
	}
	var inputs = document.forms[oForm].elements;
	for (i = 0;i < inputs.length;i++) {
		if (inputs[i].type == 'text' || inputs[i].type == 'textarea') {
			inputs[i].style.direction = dirTo;
			inputs[i].style.textAlign = alignTo;
		}
	}
	if (tinyMCE.activeEditor) {
		tinyMCE.activeEditor.settings.directionality = dirTo;
	}
	if (document.getElementById('preview')) {
		document.getElementById('preview').style.direction = dirTo;
		var kids = document.getElementById('preview').childNodes
		for (var i = 0;i < kids.length;i++) {
			if (kids[i].style) {
				kids[i].style.textAlign = alignTo;
			}
		}
	};
}

function displayModalDialogBox(message) {
	if (navigator.appVersion.indexOf('MSIE 6.0') > -1 && navigator.appVersion.indexOf('MSIE 7.0') < 0) {
		alert(message);
		return false;
	}
	message = '<p>' + message + '</p>';
	message = message.replace(/\n/gi,'</p><p>');
	if (myAlert == '') {
		myAlert = new ModalBox();
		myAlert.append();
	}
	messageTitle = myAlert.box.innerHTML;
	myAlert.show(message);
	return true;
};

function hideModalDialogBox() {
	myAlert.hide();
};

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 changeImg (sId, src) {
	document.getElementById(sId).src = src;
};

function show(sId, display) {
	if (typeof(display) == 'undefined') {
		display = 'block'
	}
	document.getElementById(sId).style.display = display;
};

function hide(sId) {
	document.getElementById(sId).style.display = 'none';
};

function toggleDisplay(sId) {
	if ($(sId).style.display != 'block') {
		$(sId).style.display = 'block';
	}
	else {
		$(sId).style.display = 'none';
	}
};

function show_vis(sId) {
	document.getElementById(sId).style.visibility = 'visible';
};

function hide_vis(sId) {
	document.getElementById(sId).style.visibility = 'hidden';
};

/*
 * if (listItem.getElement('ul.links')) {
			var submenu = listItem.getElement('ul.links');
			submenu.setStyle('visibility','visible');
			var myFx = new Fx.Slide(submenu);
			submenu.slide('hide');
		}
		var menuImage = listItem.getElement('.menuImage');
		var currentSrc = menuImage.src.replace(/^.*\//,'');
		listItem.addEvent('mouseover',function() {
			
			if (currentSrc.indexOf('Current') < 0) {
				var newSrc;
				currentSrc = currentSrc.replace(/\.gif/,'');
				newSrc = currentSrc + 'Current';
				rollMenuImg(menuImage.id,'Images/' + lang + '/' + newSrc + '.gif');
			}
			
			if (typeof(submenu) != 'undefined') {
				submenu.slide('in');
			}
		});
		listItem.addEvent('mouseout',function() {
			
			if (listItem.className.indexOf('current') < 0) {
				var newSrc;
				newSrc = currentSrc.replace(/Current\.gif/,'');
				rollMenuImg(menuImage.id,'Images/' + lang + '/' + newSrc + '.gif');
			}
			
			if (typeof(submenu) != 'undefined') {
				submenu.slide('out');
			}
		})
 */

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 detectIe () {
	if (navigator.appVersion.indexOf('MSIE') > -1) {
		displayTableRow = 'block';
		displayTbody = 'block';
		isIE = true;
		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');
			}
			isIE6 = true;
		}
	}
	else {
		displayTableRow = 'table-row';
		displayTbody = 'table-row-group';
		
	}
		
};

function goFwd(movingBox, fwd, bkwd, srollAmount) {
	if( typeof(srollAmount) == 'undefined') {
		srollAmount = 5
	}
	var movingBox1 = document.getElementById(movingBox);
	var fwdButton = document.getElementById(fwd);
	var bkButton = document.getElementById(bkwd);
	var tailHeight = movingBox1.offsetHeight - movingBox1.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;
			movingBox1.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);
};

function clearForm() {
	var formElemenets = document.forms['contact'].elements;
	for (var i = 0;i < formElemenets.length;i++) {
		if (formElemenets[i].type != 'submit' && formElemenets[i].type != 'button') {
			formElemenets[i].value = '';
		}
	}
};
