﻿function checkAnders(id, value)
{
	showHideDisplay(id, value == 'other');
}

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 maxLength(field, max, countField)
{
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
	else if (countField != null)
		countField.value = max - field.value.length;
}

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);
}

/**********************************************************************/

function setCookie(key, value, days)
{
    days = days && days != null ? days : 365
    
    var date = new Date();
	date.setTime(date.getTime() + (days*24*60*60*1000));
	var expires = "; expires=" + date.toGMTString();
	
	document.cookie = key + "=" + value + expires + "; path=/";
}

function getCookie(key)
{
	var nameEQ = key + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
		    c = c.substring(1,c.length);
		    
		if (c.indexOf(nameEQ) == 0)
		    return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delCookie(key)
{
	setCookie(key,"",-1);
}

/**********************************************************************/

/**********************************************************************/

function setFontSize(step)
{
    var old = getCookie('fontSize');
    old = old && old != null ? old : 1;
    
    if (!step || step == null)
    {
        step = old;
        old = 1;
    }
    
    if (step != old)
    {
        if (step > old)
            fontSizePlusPlus(true, false, (step - old));
        else
            fontSizeMinMin(true, false, (old - step));
    }
    
    setCookie('fontSize', step);
}

/*
function fontSizePlusPlus()
{
    var css = document.styleSheets[0].cssRules ? document.styleSheets[0].cssRules : document.styleSheets[0].rules;
    for (i = 0; i < css.length; i++)
        if (css.item(i).style.fontSize)
        {
            var size, ext;
            size = parseFloat(css.item(i).style.fontSize);
            ext = css.item(i).style.fontSize.replace(size, '');
            switch (ext)
            {
                case 'pt': case 'px':
                    size = size + 1;
                    break;
                
                case 'em':
                    size = size + 0.1;
                    break;
                    
                case '%':
                    size = size + 10;
                    break;
            }
            css.item(i).style.fontSize = size + ext;
        }
}

function fontSizeMinMin()
{
    var css = document.styleSheets[0].cssRules ? document.styleSheets[0].cssRules : document.styleSheets[0].rules;
    for (i = 0; i < css.length; i++)
        if (css.item(i).style.fontSize)
        {
            var size, ext;
            size = parseFloat(css.item(i).style.fontSize);
            ext = css.item(i).style.fontSize.replace(size, '');
            switch (ext)
            {
                case 'pt': case 'px':
                    size = size - 1;
                    break;
                
                case 'em':
                    size = size - 0.1;
                    break;
                    
                case '%':
                    size = size - 10;
                    break;
            }
            css.item(i).style.fontSize = size + ext;
        }
}
*/

function fontSizePlusPlus(css, dom, step)
{
    css = css != null ? css : true;
    dom = dom != null ? dom : true;
    
    step = step && step != null ? step : 1;
    
    if (css)
        walkStyleSheets(true, step);
    if (dom)
        walkDOM(document.body, true, step);
}

function fontSizeMinMin(css, dom, step)
{
    css = css != null ? css : true;
    dom = dom != null ? dom : true;
    
    step = step && step != null ? step : 1;
    
    if (css)
        walkStyleSheets(false, step);
    if (dom)
        walkDOM(document.body, false, step);
}

function walkStyleSheets(up, step)
{
    up = up != null ? up : true;
    
    step = step && step != null ? step : 1;
    
    var css;
    for (var i = 0; i < document.styleSheets.length; i++)
    {
        css = document.styleSheets[i].cssRules ? document.styleSheets[i].cssRules : document.styleSheets[i].rules;
        for (var j = 0; j < css.length; j++)
            if (css.item(j).style.fontSize)
                newFontSize(css.item(j), up, step);
    }
}

function walkDOM(root, up, step)
{
    up = up != null ? up : true;
    step = step && step != null ? step : 1;
    
    if (root.hasChildNodes())
        for (var i = 0; i < root.childNodes.length; i++)
            walkDOM(root.childNodes[i], up, step);
    
    if (root.style && root.style.fontSize)
        newFontSize(root, up, step);
}

function newFontSize(obj, up, step)
{
    up = up != null ? up : true;
    step = step && step != null ? step : 1;
    
    var size, type, add;
    size = parseFloat(obj.style.fontSize);
    type = obj.style.fontSize.replace(size, '');
    
    switch (type)
    {
        case 'pt': case 'px':
            add = step * 1;
            break;
        
        case 'em':
            add = step * 0.1;
            break;
            
        case '%':
            add = step * 10;
            break;
    }
    
    if (!up)
        add = add * -1;
    
    obj.style.fontSize = (size + add) + type;
}

/**********************************************************************/