// Parses a string representing a currency (e.g. "$100,000.00") and returns a float
function moneyToFloat(string)
{
    return parseFloat(string.replace(/[^\d\.]/g,''));
}

// This function relies on .NET's policy of appending _x to the end of each radio button in a group
function getCheckedRadioValue(listContainerId)
{
    for(var i=0;;i++)
    {
        var radio = $(listContainerId + "_" + i);
        if (!radio) return;
        else if (radio.checked) return radio.value;
    }
}

// Finds the position of the specified element
function findPosition(element)
{
	var left=0;
	var top=0;
	
	if(element.offsetParent)
	{	
		left=element.offsetLeft;
		top=element.offsetTop;
		
		while(element=element.offsetParent)
		{
			left+=element.offsetLeft;
			top+=element.offsetTop;
		}
	}
	return[left,top];
}

function openURL()
{
	window.location = 'http://www.mimco.com.au/Collection.aspx';
}

// Positions an absolutely positioned element relative to another element
function positionLayer(relativeTo,layerId,xOffset,yOffset)
{			
	var position=findPosition($(relativeTo));
	
	if(xOffset)
	{
		position[0]+=xOffset;
	}
	
	if(yOffset)
	{
		position[1]+=yOffset;
	}
	
	var layer=$(layerId);
	var absoluteOffset=findPosition(layer.offsetParent?layer.offsetParent:layer.parentNode);
	layer.style.left=(position[0]-absoluteOffset[0])+'px';
	layer.style.top=(position[1]-absoluteOffset[1])+'px';
}

// Displays a hidden, absolutely-positioned element and positions it at an offset to an element
function showAndPositionLayer(relativeTo,layerId,xOffset,yOffset,ieXOffset,ieYOffset)
{
	var appearEffect;
	
	if (Prototype.Browser.IE)
	{
		if (ieXOffset)
			xOffset=ieXOffset;
		if (ieYOffset)
			yOffset=ieYOffset;
			
		appearEffect = '$(\'' + layerId + '\').style.display=\'block\';';
	}
	else
	{
		appearEffect = 'Effect.Appear(\'' + layerId + '\', {duration: .5});';
	}
	
	positionLayer(relativeTo,layerId,xOffset,yOffset);
	setTimeout(appearEffect,200);
}

// Disables/enables an element and attaches/detaches the class 'disabled'
function enable(element, enabled)
{
	if (element){
		element.disabled = !enabled;
		
		if (enabled)
			element.removeClassName('disabled');
		else
			element.addClassName('disabled');
	}
}

function popup(target,targetWindowName,options)
{
	targetWindowName = targetWindowName.replace(/\s/g,'');
	var newWindow=window.open(target,targetWindowName,options);
	
	if (!newWindow.opener)
		newWindow.opener=self;
	
	newWindow.focus();
}

function preloadImage(imgSrc)
{	
	var loadImg = new Image();
	loadImg.src = imgSrc;
}

function closeWindow(showPrompt, message)
{
    var response;

    if (showPrompt)
    {
        var prompt = message || "Are you sure you wish to close this window?";
        response = confirm(prompt);
    }

    if (response || !showPrompt)
    {
        if (opener)
        {
            self.close();
        }

        return true;
    }
    
    return false;
}

