/*******************************************************************
*
* Copyright (C) Transitor AB
*
* This source code is protected by copyright law and international
* treaties. Any unauthorised reproduction or distribution of this
* software in source or binary code form, or any part of it, is
* prohibited and may result in severe civil and criminal penalities,
* and will be prosecuted to the maximum extent possible under the
* law.
*
*******************************************************************/

/**
* Open a window and focus on it
*/
function openWindow(url, winName, options)
{
	winName = window.open(url,winName,options);
	winName.focus();
}

function selectLink(selector, location)
{
	selectLinkFrame(selector, 'main', location);
}

function selectLinkFrame(selector, theframe, location)
{
	var selectedValue = selector[selector.selectedIndex].value;
	var newLocation = location + selectedValue;
	parent.frames[theframe].location=newLocation;
}

function addOnLoad(theFunction)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = theFunction;
	}
	else
	{
		window.onload = function() {
			if (oldonload)
			{
				oldonload();
			}
			theFunction();
		}
	}
}

function addOnUnLoad(theFunction)
{
	var oldonload = window.onunload;
	if (typeof window.onunload != 'function')
	{
		window.onunload = theFunction;
	}
	else
	{
		window.onunload = function() {
			if (oldonload)
			{
				oldonload();
			}
			theFunction();
		}
	}
}

/* correctly handle PNG transparency in Win IE 5.5 or higher. */
function correctPNG()
{
	for(var i=0; i<document.images.length; i++)
	{
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}

function priceDelimiter(price)
{
	// added nullvalue check
	if (price == null)
		return "";
	var temp = price.toString();
	var commaindex = temp.lastIndexOf(".");
	var suffix = "";
	if (commaindex > 0)
	{
		suffix = temp.substring(commaindex);
		temp = temp.substring(0, commaindex);
	}
	var index = temp.length-3;
	var newPrice = temp;
	if (index >= 0)
		newPrice = temp.substring(index);
	for (var i = index; i >= 3; i=i-3 )
	{
		newPrice = temp.substring(i-3, i) + ' ' + newPrice;
		index = i-3;
	}
	if (index > 0)
		newPrice = temp.substring(0, index) + ' ' + newPrice;
	newPrice = newPrice + suffix;
	return newPrice;
}

//Activate all buttons not included in a plugin are enabled with this function, common is read after jquery init
$j(function(){
	//all hover and click logic for buttons, these are our current cases of elements with these classes
	$j("a.fg-button:not(.ui-state-disabled)," +
		"button.fg-button:not(.ui-state-disabled)," +
		"input.fg-button:not(.ui-state-disabled)," +
		"a.ui-button:not(.ui-state-disabled)," +
		"button.ui-button:not(.ui-state-disabled)," +
		"input.ui-button:not(.ui-state-disabled)")
	.hover(
		 function(){
			  $j(this).addClass("ui-state-hover");
		 },
		 function(){
			  $j(this).removeClass("ui-state-hover");
		 }
	)
});
