﻿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 setImageResize(id,image,maxWidth,maxHeight)
//{
//	var w, h;
//	
//	var pic;
//	pic = document.getElementById(id);
//	
//	var img;
//	img = new Image();
//	
//	img.onload = function()
//	{
//		//IS IMAGE TO WIDE?
//		if (img.width >= maxWidth)
//		{
//			h = Math.round((img.height/(img.width/maxWidth)));
//			w = maxWidth;
//			//IS IMAGE TO HIGH AFTER RESIZING THE WIDTH?
//			if (h >= maxHeight)
//			{
//				w = Math.round((w/(h/maxHeight)));
//				h = maxHeight;
//			}
//		}
//		//IS IMAGE TO HIGH?
//		else if (img.height >= maxHeight)
//		{
//			w = Math.round((img.width/(img.height/maxHeight)));
//			h = maxHeight;
//		}
//		else
//		{
//			w = img.width
//			h = img.height
//		}
//		
//		delete img;
//		
//		pic.width = w;
//		pic.height = h;
//		pic.src = image;
//	};
//	
//	img.src = image;
//}

//function emptyIMG(id)
//{
//	var img = document.getElementById(id);
//	if (img != null)
//	{
//		img.src = '';
//	}
//}

//function FloatDiv()
//{
//	this.div = null;
//	this.offsetLeft = 10;
//	this.offsetTop  = 10;
//	this.margin = 15;
//	this.stable = 10;
//	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;
//				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
//				//document.getElementById('alignTD').align = 'right';
//			}
//			else
//			{
//				divX = eX + this.offsetLeft;
//				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
//				//document.getElementById('alignTD').align = 'left';
//			}
//			
//			if (this.swapVertical)
//			{
//				divY = eY - this.offsetTop - this.div.offsetHeight;
//				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
//				//document.getElementById('alignTD').vAlign = 'bottom';
//			}
//			else
//			{
//				divY = eY + this.offsetTop;
//				//EXTRA FOR ALIGNMENT BECAUSE IMG IN DIV IS DYNAMICLY SCALED
//				//document.getElementById('alignTD').vAlign = 'top';
//			}
//			
//			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);
//}

/**********************************************************************/
