﻿var utils = {
	ua : navigator.userAgent.toLowerCase(),
	uaVersion: parseFloat((navigator.userAgent.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1]),

	// browser name
	ie : (document.all && !window.opera),

	getPageSize : function ()
	{
		var d = document;
		var w = window;
		var iebody = d.compatMode && d.compatMode != 'BackCompat' ? d.documentElement : d.body;
		
		var width = utils.ie ? iebody.clientWidth : (d.documentElement.clientWidth || self.innerWidth);
		var height = utils.ie ? iebody.clientHeight : self.innerHeight;
		
		utils.page =
		{
			width: width,
			height: height,		
			scrollLeft: utils.ie ? iebody.scrollLeft : pageXOffset,
			scrollTop: utils.ie ? iebody.scrollTop : pageYOffset
		}
		
		return utils.page;
	},

	getPosition : function(el)
	{
		var p =
		{
			x: el.offsetLeft,
			y: el.offsetTop
		};
		while (el.offsetParent)
		{
			el = el.offsetParent;
			p.x += el.offsetLeft;
			p.y += el.offsetTop;
			if (el != document.body && el != document.documentElement)
			{
				p.x -= el.scrollLeft;
				p.y -= el.scrollTop;
			}
		}
		return p;
	},
	
	push : function (arr, val)
	{
		arr[arr.length] = val;
	},

	addEventListener : function (el, event, func)
	{
		if (el == document && event == 'ready')
			utils.push([], func);
		try
		{
			el.addEventListener(event, func, false);
		}
		catch (e)
		{
			try
			{
				el.detachEvent('on'+ event, func);
				el.attachEvent('on'+ event, func);
			}
			catch (e)
			{
				el['on'+ event] = func;
			}
		} 
	},

	removeEventListener : function (el, event, func)
	{
		try
		{
			el.removeEventListener(event, func, false);
		}
		catch (e)
		{
			try
			{
				el.detachEvent('on'+ event, func);
			}
			catch (e)
			{
				el['on'+ event] = null;
			}
		}
	},
	
	getElementSize : function(el)
	{
		var p =
		{
			height: el.offsetHeight,
			width: el.offsetWidth
		};

		return p;
	},

	centerX : function(winWidth)
	{
		return (screen.availWidth - winWidth) / 2;
	},

	centerY : function(winHeight)
	{
		return (screen.availHeight - winHeight) / 2;
	},

	centerElement: function(elementToCenter, parent)//parent is either element in which the elementToCenter	should be centered, or the current page if null
	{
		if (elementToCenter != null)
		{
			var elementSize = utils.getElementSize(elementToCenter);

			var X = 0;
			var Y = 0;

			if (parent == null)
			{
				var elementPosition = utils.getPosition(elementToCenter);
				if (utils.ie)
				{
					X = ((screen.availWidth - elementSize.width - window.screenLeft) / 2) - elementPosition.x;
					Y = ((screen.availHeight - elementSize.height - window.screenTop) / 2) - elementPosition.y;
				}
				else
				{
					X = ((window.innerWidth - elementSize.width) / 2) - elementPosition.x;
					Y = ((window.innerHeight - elementSize.height) / 2) - elementPosition.y;
				}
			}
			else
			{
				var parentSize = utils.getElementSize(parent);
				X = (parentSize.width - elementSize.width) / 2;
				Y = (parentSize.height - elementSize.height) / 2;
			}

			elementToCenter.style.position = 'absolute';
			elementToCenter.style.top = Y + 'px';
			elementToCenter.style.left = X + 'px';
		}
	},
	
	getUaVersion : function()
	{
		var isIE = ((utils.ua.indexOf("msie") != -1) && (utils.ua.indexOf("opera") == -1) && (utils.ua.indexOf("webtv") == -1));
		var isSafari = (utils.ua.indexOf('safari') != - 1);

		// browser version
		var versionMinor = (isIE && parseFloat(navigator.appVersion) >= 4) ? parseFloat(utils.ua.substring(utils.ua.indexOf('msie ') + 5 )) : parseFloat(navigator.appVersion);
		var versionMajor = parseInt(versionMinor);
		var isWin = (utils.ua.indexOf('win') != -1);
		var isWin32 = (isWin && ( utils.ua.indexOf('95') != -1 || utils.ua.indexOf('98') != -1 || utils.ua.indexOf('nt') != -1 || utils.ua.indexOf('win32') != -1 || utils.ua.indexOf('32bit') != -1) );
		var isMac = (utils.ua.indexOf('mac') != -1);
		
		var isIE4x = (isIE && versionMajor == 4);
		var isIE4up = (isIE && versionMajor >= 4);
		var isIE5x = (isIE && versionMajor == 5);
		var isIE55 = (isIE && versionMinor == 5.5);
		var isIE5up = (isIE && versionMajor >= 5);
		var isIE6x = (isIE && versionMajor == 6);
		var isIE6up = (isIE && versionMajor >= 6);
		var isIE7x = (isIE && versionMajor == 7);
		var isIE7up = (isIE && versionMajor >= 7);
		var isIE8x = (isIE && versionMajor == 8);
		var isIE8up = (isIE && versionMajor >= 8);
			
		var version =
		{
			isIE : isIE,
			isSafari : isSafari,
			versionMinor : versionMinor,
			versionMajor : versionMajor,
			isWin : isWin,
			isWin32 : isWin32,
			isMac : isMac,

			isIE4x : isIE4x,
			isIE4up : isIE4up,
			isIE5x : isIE5x,
			isIE55 : isIE55,
			isIE5up : isIE5up,
			isIE6x : isIE6x,
			isIE6up : isIE6up,
			isIE7x : isIE7x,
			isIE7up : isIE7up,
			isIE8x : isIE8x,
			isIE8up : isIE8up
		};
		
		utils.version = version;
		return version;
	},
	
	pageResize : function(main,footer)
	{
		var page = utils.getPageSize();
		var mainHeight = page.height;
		
		if (main != null)
			mainHeight -= utils.getPosition(main).y;
		if (footer != null)
			mainHeight -= utils.getElementSize(footer).height;
		
		if (mainHeight < 0)
			mainHeight = 0;
			
		if (main != null)
			main.style.height = mainHeight + 'px';
	},
	
	blockKeys : 
	{
		operators : function()
		{
		   var key = window.event.keyCode;
		   if ( (key > 59 && key < 63) || key==13 )
			  return;
		   else
			  window.event.returnValue = null;
		},

		unsignedInteger : function()
		{
		   var key = window.event.keyCode;
		   if ( (key > 47 && key < 58)  || key==13)
			  return;
		   else
			  window.event.returnValue = null;
		},

		integer : function()
		{
		   var key = window.event.keyCode;
		   if ( (key > 47 && key < 58)  || key==13 || key==45)
			  return;
		   else
			  window.event.returnValue = null;
		},

		unsignedFloat : function()
		{
		   var key = window.event.keyCode;
		   if ( (key > 47 && key < 58)  || key==13 || key==44)
			  return;
		   else
			  window.event.returnValue = null;
		},

		float : function()
		{
		   var key = window.event.keyCode;
		   if ( (key > 47 && key < 58)  || key==13 || key==44 || key==45)
			  return;
		   else
			  window.event.returnValue = null;
		},

		date : function()
		{
			var udalost = window.event;
			var nKeyCode =  udalost.keyCode ? udalost.keyCode : udalost.which ? udalost.which : void 0;
			if ((nKeyCode>=48 && nKeyCode<=57) || nKeyCode==46 || nKeyCode==37 || nKeyCode==39 || nKeyCode==8 || nKeyCode==13 || nKeyCode==9 || nKeyCode==58)
				return true;
			else
				return false;
		},

		time : function()
		{
		   var key = window.event.keyCode;
		   if ((key > 47 && key <= 58) || key==13)
			  return;
		   else
			  window.event.returnValue = null;
		}
	},
	
	formatInteger: function(i)
	{
		i += '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(i))
		{
			i = i.replace(rgx, '$1' + '.' + '$2');
		}
		return i;
	},

	formatFloat: function(d)
	{
		d += '';
		parts = d.split('.');
		intPart = parts[0];
		if (intPart.length == 0)
			intPart = '0';
		if (parts.length == 1)
			decPart = '00';
		else
		{
			decPart = parts[1];
			if (decPart.length == 0)
				decPart = '00';
			else if (decPart.length == 1)
				decPart += '0';
			else if (decPart.length > 2)
				decPart = decPart.substring(0,2);	
		}

		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(intPart))
		{
			intPart = intPart.replace(rgx, '$1' + '.' + '$2');
		}
		return intPart + ',' + decPart;
	}
};

//zaokrouhlovani na desetinna cisla
Number.prototype.toFixed=function(x)
{
	var temp=this;
	temp=Math.round(temp*Math.pow(10,x))/Math.pow(10,x);
	return temp;
}

String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); };

utils.addEventListener(window, 'load', function() {
	utils.getPageSize();
});

utils.addEventListener(window, 'resize', function() {
	utils.getPageSize();
});

