function redirect(url)
{
    document.location.href = url;
}

//TODO Deprecate the msg option and assume a URL is passed in that does an AJAX request to populate the pane...?
function showModalLayer(urlOrMsg, w, h)
{
	var msg;
	var url;
	
	if (!w) w = 500;
	if (!h) h = 300;
	
	if (urlOrMsg)
	{
		if (urlOrMsg.charAt(0) == '/')
		{
			msg = 'Loading...';
			url = urlOrMsg;
		}
		else
		{
			msg = urlOrMsg;
		}
	}
	
	if (!$('modalLayer'))
	{
	    $$('body')[0].insert({top: '<div id="modalLayer"></div>'});
	}
    
    if (msg)
    {
        var paneLeft = parseInt((document.viewport.getWidth() - w) / 2);
        var paneTop = parseInt((document.viewport.getHeight() - h) / 3);
        
        if( paneTop < 0 )
        {
        	paneTop = 0;
        }
        
        var overflowStyleY = 'auto';
        
        if(document.viewport.getHeight()<h)
    	{
    	   h = (document.viewport.getHeight())-50;
    	}
        
        var overflowStyleX = 'hidden';

        if(document.viewport.getWidth()<w)
    	{
    	   overflowStyleX = 'auto';	
    	   h = (document.viewport.getWidth())-50;
    	}
        
        if ($('modalPane'))
        {
        	$('modalPane').update(msg);
        }
        else
        {
        	$$('body')[0].insert({bottom: '<div id="modalPane" style="overflow-x:' + overflowStyleX + ';width:' + w + 'px; height:' + h + 'px; top:' + paneTop + 'px; left:' + paneLeft + 'px;overflow-y:'+ overflowStyleY +'">' + msg + '</div>'});
        }
    }
    
    if (url)
    {
	    new Ajax.Updater('modalPane', url, 
		{
	    	method: 'get',
	    	evalScripts: true,
	    	onFailure: function(transport) {
	    		alert('Error getting page to load into modal pane');
	    	}
		});
    }
}

function hideModalLayer()
{
    $('modalLayer').remove();
    if ($('modalPane')) {
        $('modalPane').remove();
    }
}

// THis is a JS clone of generateCurie() in our PHP code.
function generateCurie(uri)
{
	var slash1 = uri.indexOf('/')+1;
	var slash2= uri.indexOf('/', slash1)+1; 
	var slash3= uri.indexOf('/', slash2)+1;
	var path = uri.substring(slash3);
	
	return '[tenant:' + path + ']';
}

function addReplaceQueryStringParam(url, key, val)
{
	var regex = new RegExp("([?|&])" + key + "=.*?(&|$)", "i");

	if (url.match(regex))
	{
		return url.replace(regex, '$1' + escape(key) + "=" + escape(val) + '$2');
	}
	else
	{
		if (url.indexOf('?') == -1)
		{
			return url + '?' + escape(key) + "=" + escape(val);
		}
		else
		{
			return url + '&' + escape(key) + "=" + escape(val);
		}
	}
}

function showHideElement(showElementId, hideElementId)
{
	if($(showElementId))
	{
		$(showElementId).show();
	}
	
	if($(hideElementId))
	{
		$(hideElementId).hide();
	}
}

function confirmPublish(listUri)
{
    showModalLayer('/ui/forms/publishConfirmation.html?listUri='+escape(listUri));
}

function confirmPublishOK()
{
    $('formButtons').hide();
    $('modalPageTitle').update('Please wait');
    $('modalPageSpinner').show();
    $('publishText').update('<p>Changes to the list are being published - this may take a few moments.</p>');
}
