// JavaScript Document
function ShowEmailLink(name, domain)
{
	document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
	document.write(name + '@' + domain + '</a>');
}

// JavaScript Document
function IsBetween(x, y, minx, miny, maxx, maxy)
{
	if (x >= minx && x <= maxx && y >= miny && y <= maxy) return true;
	return false;
}

function ModifyListByValue(val, list, add)
{
	var	items	= $(list);
	var	reg		= new RegExp("(^|,)" + val + "(,|$)", "g");
	var pos		= reg.exec(items.val());
	if (pos != null)
	{
		car			= (RegExp.$1 == ',' && RegExp.$2 == ',') ? "," : "";
		items.val(items.val().replace(reg, car));
	}
	if (add)
	{
		if (items.val() == "") items.val(val);
		else items.val(items.val() + "," + val);
	}
}

function ModifyListByItem(obj, list)
{
	ModifyListByValue(obj.value, list, obj.checked);
}

function ModifyListByList(items, list, add)
{
	$(items).each(function() {
		ModifyListByValue($(this).val(), list, add);
		this.checked	= add;
	});
}

function ParseInt(text)
{
	if (typeof text == "undefined") return 0;
	var	result = parseInt(text.replace("px", ""));
	if (isNaN(result)) return 0;
	return result;
}

function EncodeRE(s)
{
	return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}

function QueryString(str, key)
{
	var	re			= new RegExp("[?&]" + key + "=([^&$]*)", "i");
	var	offset		= str.search(re);
	if ( offset == -1 ) return null;
	return RegExp.$1;
}

function ParseUri(sourceUri)
{
	var uriPartNames	= ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
	var uriParts		= new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
	var uri				= {};
	
	for(var i = 0; i < 10; i++)
	{
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	// Always end directoryPath with a trailing backslash if a path was present in the source URI
	// Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
	if(uri.directoryPath.length > 0)
	{
		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	
	return uri;
}

function ConfirmURL(url, message)
{
	if (typeof message == "undefined") message = "Esti sigur?";
	if (confirm(message)) location.href = url;
}

function FitImage(obj)
{
	var	parent			= obj.offsetParent();
	var	containerWidth	= parent.width();
	var	containerHeight	= parent.height();
	var	imageWidth		= obj.width();
	var	imageHeight		= obj.height();
	var	width			= 0;
	var	height			= 0;
	var	resize			= false;
	if (imageWidth > containerWidth)
	{
		width	= containerWidth;
		height	= imageHeight * (width / imageWidth);
		resize	= true;
	}
	if (height > containerHeight)
	{
		height	= containerHeight;
		width	= imageWidth * (height / imageHeight);
		resize	= true;
	}
	if (resize)
	{
		obj.width(width);
		obj.height(height);
	}
	obj.css("left", (parent.width() - obj.width()) / 2 + ParseInt(parent.css("padding-left")));
	obj.css("top", (parent.height() - obj.height()) / 2 + ParseInt(parent.css("padding-top")));
	obj.css("visibility", "visible");
}

function GetVerticalSpace(obj)
{
	return	ParseInt(obj.css("padding-top"))
			+ ParseInt(obj.css("padding-bottom"))
			+ ParseInt(obj.css("border-top-width"))
			+ ParseInt(obj.css("border-bottom-width"))
			+ ParseInt(obj.css("margin-top"))
			+ ParseInt(obj.css("margin-bottom"));
}

function GetHorizontalSpace(obj)
{
	return	ParseInt(obj.css("padding-left"))
			+ ParseInt(obj.css("padding-right"))
			+ ParseInt(obj.css("border-left-width"))
			+ ParseInt(obj.css("border-right-width"))
			+ ParseInt(obj.css("margin-left"))
			+ ParseInt(obj.css("margin-right"));
}

function SetItemOver(id)
{
	// Mouse hover event for the list
	$("#" + id + " .item").hover(function() {$(this).addClass("itemOver");}, function() {$(this).removeClass("itemOver");});
}

//return str with the first letter capitalised
function UpperCaseFirst(str)
{
	//extract the first letter of str
	var letter = str.substr(0,1);
	//return letter capitalised concatonated with
	//str minus it's first letter
	return letter.toUpperCase() + str.substr(1);
}

//return str with the first letter capitalised
function LowerCaseFirst(str)
{
	//extract the first letter of str
	var letter = str.substr(0,1);
	//return letter capitalised concatonated with
	//str minus it's first letter
	return letter.toLowerCase() + str.substr(1);
}
