﻿/*
Author:			Dennis Milandt
Description:	Common utilities
*/

var Utils =
{
	// Get object from id
	getObj: function(elementId)
	{
		return document.getElementById(elementId);
	},

	// Popup window
	popUp: function(url, windowName, width, height, extraParameters)
	{
		var str = 'height=' + height + ',width=' + width + ',' + extraParameters;
		if (parseInt(navigator.appVersion) > 3)
			str += ',left=' + (screen.width - width) / 2 + ',top=' + parseInt((screen.height - h) / 3);
		return this.window.open(url, windowName, str);
	},

	// Sorts any listbox
	sortListBox: function(objListbox)
	{
		var x;
		var temparr = new Array(objListbox.options.length);
		for (x = 0; x < objListbox.options.length; x++)
			temparr[x] = objListbox.options[x].value + "||" + objListbox.options[x].text + "||" + objListbox.options[x].className;
		temparr.sort();
		for (x = 0; x < temparr.length; x++)
		{
			optionArr = temparr[x].split("||")
			objListbox.options[x] = new Option(optionArr[1], optionArr[0]);
			objListbox.options[x].className = optionArr[2];
		}
	},

	// Create a cookie
	createCookie: function(name, value, days)
	{
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else
			var expires = "";

		document.cookie = name + "=" + value + expires + "; path=/";
	},

	// Read a cookie
	readCookie: function(name)
	{
		var nameEQ = name + "=";
		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;
	},

	// Delete a cookie
	deleteCookie: function(cookieName)
	{
		createCookie(cookieName, "", -1);
	},

	// Get absolute left for an object
	getAbsLeft: function(object)
	{
		var iY = 0;
		if (object)
			while (object.offsetParent)
		{
			iY += parseInt(object.offsetLeft);
			object = object.offsetParent;
		}
		return iY;
	},

	// Get absolute top for object
	getAbsTop: function(object)
	{
		var iX = 0;
		if (object)
			while (object.offsetParent)
		{
			iX += parseInt(object.offsetTop);
			object = object.offsetParent;
		}
		return iX
	},

	selectAll: function(obj)
	{
		obj.focus();
		obj.select();
	}
};