function openWindow(thePage, target, width, height, parms)
{
	try
	{
		if (!width) width = "700";
		if (!height) height = "500";
		if (!parms) parms = "resizable=yes,scrollbars=yes";

		// get dimensions to center the popup
		var screenWidth = screen.availWidth;
		var screenHeight = screen.availHeight;
		var leftPos = (screenWidth - width) / 2;
		var topPos  = (screenHeight - height) /2;

		var newWindow = window.open(thePage, target,"width=" + width + ",height=" + height + ",top=" + topPos + ",left=" + leftPos + "," + parms);
		newWindow.focus();
	}
	catch(exception){}
	return false;
}

var ZIP_CODE_COOKIE		= "zipcode";
var TAM_ERROR_PARAM		= "error";
var LOGIN_PARAM			= "login";

/* Cookie / Query Params functions */
var cookies = {};
try
{
	cookies = new Cookie(document, "migweb", 24, "/");
	cookies.load();
}catch(exception){}

var queryParams = {};

function initQueryParams()
{
	try
	{
		if (window.location["search"] && window.location["search"].length)
		{
			var query = window.location["search"].substr(1, window.location["search"].length);
			if (query.indexOf("&") > -1)
			{
				query = query.split("&");
				for(var i=0;i<query.length;i++)
				{
					var kvp = query[i].split("=");
					queryParams[(kvp[0])] = unescape(kvp[1]);
				}
			}
			else
			{
				query = query.split("=");
				queryParams[(query[0])] = unescape(query[1]);
			}
		}
	}catch(exception){}
}

function getCookie(_name)
{
	return cookies[_name];
}

function storeInCookie(_name, _value)
{
	try
	{
		cookies[_name] = _value;
		cookies.store();
	}catch(exception){}
}

function removeCookieValue(_name)
{
	try
	{
		cookies[_name] = "";
		cookies.store();
	}catch(exception){}
}

function checkZipCode(_url)
{
	if(cookies[ZIP_CODE_COOKIE])
	{
		var loc = window.location + "";
		if(!loc || loc.indexOf("Home") == -1)
		{
			window.location = _url;
		}
	}
}

function checkLogin()
{
	if(queryParams[LOGIN_PARAM] && queryParams[LOGIN_PARAM] == "yes")
	{
		removeCookieValue(ZIP_CODE_COOKIE);
	}
}

function checkTamError(_msgContainer)
{
	if(queryParams[TAM_ERROR_PARAM])
	{
		$(_msgContainer).innerHTML = queryParams[TAM_ERROR_PARAM];
		$(_msgContainer).style.display = "";
	}
}

initQueryParams();
/* ----------------------- */

function createURLToJahiaPage(_host, _context, _site, _page)
{
	return _host +"/"+ _context +"/Jahia/site/"+ _site +"/"+  _page;
}

/**
 * toggle content in target_htmlElementId between hidden and displayed while showing appropriate
 * labels to tell the user how to proceed
 *
 * @param showIdWhenHiding label to show when the target_htmlElementId element is being hidden
 * @param showIdWhenDisplaying label to show when the target_htmlElementId element is being expanded
 * @param target_htmlElementId div that contains the content that is being toggled
 */
function showHideBox(showIdWhenHiding, showIdWhenDisplaying, target_htmlElementId) {
	if (document.getElementById(target_htmlElementId) == null ) {
	// Element not found in html document
		return;
	} else {
	// Element found in html document
		if(document.getElementById(target_htmlElementId).style.display == '') {
			// currently shown, hide element
			document.getElementById(target_htmlElementId).style.display = 'none';
			document.getElementById(showIdWhenDisplaying).style.display = 'none';
			document.getElementById(showIdWhenHiding).style.display = '';
			
		} else {
			// currently hidden, show element
			document.getElementById(target_htmlElementId).style.display = '';
			document.getElementById(showIdWhenDisplaying).style.display = '';
			document.getElementById(showIdWhenHiding).style.display = 'none';
		}	
	}
}

var USER_PROPERTY_JSP = "/jsp/user_property.jsp"

function makeAsynGetRequest(_url)
{
	new Ajax.Request(_url, {method: 'get', asynchronous: true});
}

function getUserProperty(_name)
{
	makeAsynGetRequest(CONTEXT_PATH+USER_PROPERTY_JSP+"?action=get&site="+SITE_KEY+"&name="+_name);
}

function setUserProperty(_name, _value)
{
	makeAsynGetRequest(CONTEXT_PATH+USER_PROPERTY_JSP+"?action=add&site="+SITE_KEY+"&name="+_name+"&value="+_value);
}

function removeUserProperty(_name)
{
	makeAsynGetRequest(CONTEXT_PATH+USER_PROPERTY_JSP+"?action=remove&site="+SITE_KEY+"&name="+_name);
}

function setMinMax(_portletId, _winState)
{
	try
	{
		var _minportlets = $("portlets_min").value;
		var _newminlist = "";
		if(_minportlets)
		{
			var _portlets = _minportlets.split(",");
			var found = false;
			var counter = 0;
			for(var i=0;i<_portlets.length;i++)
			{
				counter++;

				if(!_portlets[i]) continue;

				if (_portlets[i] != _portletId)
				{
					if(document.getElementById(_portlets[i]))
					{
						_newminlist += _portlets[i];
					}
					else
					{
						continue;
					}
				}
				else
				{
					found = true;
					if ("max" != _winState)
					{
						_newminlist += _portlets[i];
					}
					else
					{
						continue;
					}
				}

				if (counter <= _portlets.length)
				{
					_newminlist += ",";
				}
			}

			if (!found) _newminlist += _portletId;
		}
		else
		{
			_newminlist += _portletId;
		}

		_minportlets = _newminlist

		$("portlets_min").value = _minportlets;

		setUserProperty("portlets_min", _minportlets);

	}catch(_exception){}
}