﻿var to;
function waitForIt(funct, time)
{
	clearTimeout(to);
	to = setTimeout(funct + ';', time);
}

function showHide(id, show)
{
	var el = document.getElementById(id);
    if (show)
		el.style.visibility = 'visible';
    else
		el.style.visibility = 'hidden';
}

function showHideDisplay(id, show)
{
	var el = document.getElementById(id);
    if (el != null)
	{
		if (show)
			el.style.display = '';
		else
			el.style.display = 'none';
	}
}

function setFocus(el)
{
	if (el != null)
	{
	    if (typeof(el.focus) != 'undefined')
		    el.focus();
		if (typeof(el.select) != 'undefined')
			el.select();
	}
}

function enableValidation(el, validate)
{
	if (el != null)
		el.enabled = validate;
}

function formatCurrency(amount)
{
	var i = parseFloat(String(amount).replace(',', '.'));
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s.replace('.', ',');
}

function formatDateTime(dateToFormat, time)
{
	if (dateToFormat == '')
		return '';
	
	time = time != null ? time : false;
	
	var date = new Date(dateToFormat);
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();
	
	if (isNaN(day) || isNaN(month) || isNaN(year))
		return '';
		
	var returnDate = PadDigits(day, 2) + '-' + PadDigits(++month, 2) + '-' + year;
	
	if (time)
	{
		var sec = date.getSeconds();
		var min = date.getMinutes();
		var hour = date.getHours();
		
		if (!isNaN(sec) && !isNaN(min) && !isNaN(hour))
			returnDate += ' ' + PadDigits(hour, 2) + ':' + PadDigits(min, 2) + ':' + PadDigits(sec, 2);
	}
	
	return returnDate;
}

function PadDigits(n, totalDigits, digit) 
{
	digit = digit != null ? digit : '0';
	
	n = n.toString();
	var pd = '';
	if (totalDigits > n.length)
	{
		for (i = 0; i < (totalDigits-n.length); i++)
		{
			pd += digit;
		}
	}
	return pd + n.toString();
}

function copyValue(orig, copy, overwrite)
{
	overwrite = overwrite == null ? false : overwrite;
	if (copy.value == '' || overwrite)
		copy.value = orig.value;
}

function checkBoxes(arr, check)
{
	for (var i = 0; i < arr.length; i++)
		document.getElementById(arr[i]).checked = check;
}

function checkAllBox(arr, box)
{
	for (var i = 0; i < arr.length; i++)
	{
		if (!document.getElementById(arr[i]).checked)
		{
			box.checked = false;
			return;
		}
	}
	
	box.checked = true;
}

function showHideFullScreenDiv(id, show)
{
	var el = document.getElementById(id);
    if (el != null)
	{
		if (show)
		{
			// TO SHOW FULLSCREEN IN IE...
			document.body.appendChild(el);
			
			var top = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
			el.style.top = top + 'px';
			el.style.display = '';
		}
		else
			el.style.display = 'none';
	}
}

var toFloat;
function floatDiv(id)
{
	var el = document.getElementById(id);
    if (el != null)
	{
		var top = ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
		el.style.top = top + 'px';
	}
	
	toFloat = setTimeout('floatDiv("' + id + '");', 100);
}

function setRadio(id, value)
{
	var radio = new Array();
	var j = 0;
	var input = document.getElementsByTagName('input');
	for (var i = 0; i < input.length; i++)
	{
		if (input[i].type == 'radio' && input[i].id == id)
		{
			radio[j] = input[i];
			j++;
		}
	}
	
	if (radio.length > 0)
	{
		for(var i = 0; i < radio.length; i++)
		{
			radio[i].checked = false;
			if(radio[i].value == value)
				radio[i].checked = true;
		}
	}
}

/**********************************************************************/

function fillAccent(text)
{
	var div = document.getElementById('divAccent');
	if (div != null)
		div.innerHTML = text;
}

function FloatDiv()
{
	this.div = null;
	this.offsetLeft = 5;
	this.offsetTop  = 5;
	this.margin = 10;
	this.stable = 5;
	this.swapHorizontal = false;	
	this.swapVertical = false;
	this.timer = null;
	
	this.fixE = function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
	
	this.positionDiv = function(e)
	{
		if (this.div)
		{
			e = this.fixE(e);
			var eX = e.clientX + document.documentElement.scrollLeft;
			var eY = e.clientY + document.documentElement.scrollTop;
			
			if (this.swapHorizontal)
				divX = eX - this.offsetLeft - this.div.offsetWidth;
			else
				divX = eX + this.offsetLeft;
			
			if (this.swapVertical)
				divY = eY - this.offsetTop - this.div.offsetHeight;
			else
				divY = eY + this.offsetTop;
			
			this.div.style.left = divX + 'px';
			this.div.style.top = divY + 'px';
			
			var spaceBottom = document.documentElement.clientHeight - eY - this.div.offsetHeight - this.margin + document.documentElement.scrollTop;
			if (spaceBottom < 0 && !this.swapVertical)
				this.swapVertical = true;
			else if (this.swapVertical && (spaceBottom - this.stable) > 0)
				this.swapVertical = false;
			
			var spaceRight = document.documentElement.clientWidth - eX - this.div.offsetWidth - this.margin + document.documentElement.scrollLeft;
			var spaceLeft = eX - this.div.offsetWidth - this.margin;
			if (spaceRight > this.stable)
				this.swapHorizontal = false;
			else if (spaceLeft > this.stable)
				this.swapHorizontal = true;
			else
				this.swapHorizontal = false;
						
			this.div.style.visibility = 'visible';
			
			if (this.timer)
				clearTimeout(this.timer);
			this.timer = setTimeout("hideDiv();", 10000);
		}
	}
	
	this.showDiv = function(id, e)
	{
		var div = document.getElementById(id);
		document.body.appendChild(div);
		if (div)
		{
			if (this.div)
				this.hideDiv();
			this.div = div;
			document.onmousemove = positionDiv;
			this.positionDiv(e);
		}
	}
	
	this.hideDiv = function ()
	{
		if (this.timer)
			clearTimeout(this.timer);
		this.timer = null;
		document.onmousemove = null;
		if (this.div)
		{
			this.div.style.visibility = 'hidden';
			this.div = null;
		}
	}
}

var floatDiv = new FloatDiv();

function showDiv(id, e)
{
	floatDiv.showDiv(id, e);
}

function hideDiv()
{
	floatDiv.hideDiv();
}

function positionDiv(e)
{
	floatDiv.positionDiv(e);
}

/**********************************************************************/