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){}
}

function retrievePortletData(_name, _url, _queryParams){

	var _getData = false;
	if ($("portlets_min")){
		
		var _minPortletList = $("portlets_min").value;
		if (_minPortletList.indexOf(_name) == -1 && $portlets[_name]){
			_getData = true;
		}
	}else{
		_getData = true;
	}
	
	if (_getData){

		if (_url.indexOf("maximized") != -1){
			document.getElementById(_name + '_rows').innerHTML = '<div class="portlet_max"><img class="spinner" alt="loading" src="' + CONTEXT_PATH + '/jsp/waiting_large.gif"/></div>';			
		}else {
			document.getElementById(_name + '_rows').innerHTML = '<img class="spinner" alt="loading" src="' + CONTEXT_PATH + '/jsp/waiting.gif"/>';
		}

		var params = "";
		if (_queryParams){
			for(param in _queryParams){
				if(param.indexOf("__") >= -1) {
					params += "&";
					params += param.replace(/^__/i, "");
					params += "=";
					params += _queryParams[param];
				}
			}
		}

		var requester = new Ajax.Request(_url + params, {method: 'get', asynchronous: true,
			onSuccess: function(xmlhttp){

				var serviceResponse = xmlhttp.responseText;
				var portletId = getPortletId(serviceResponse);
				document.getElementById( portletId + '_rows').innerHTML = serviceResponse;

				var $portletRows = document.getElementById(portletId + '_portlet_rows');
				if ($portletRows != null){
					$portletRows.style.overflowY = 'scroll';
					$portletRows.style.overflowX = 'hidden';
				}
			},
			onException: function(xmlhttp, exception){

				window.alert(exception);
			},
			onFailure: function(xmlhttp){

				window.alert("Error! Response Code = " +xmlhttp.status);
			}
		});

	}
}

function getPortletId(content)
{
	var regex = new RegExp("[A-Za-z0-9_-]*_portlet");
	var result = regex.exec(content);
	var portletId = result[0];
	return portletId.substring(0,portletId.indexOf("_portlet"));
}

function refreshPortlet(_portletId){

	retrievePortletData(_portletId, $portlets[_portletId]);
}

//This helper function is used to confirm the uploading of a quoute. 
//If the user chooses OK, then the document's HREF is set to that of 
//the link handed to this routine, else it does nothing.
function confirmSubmitted() {

	var msg;

	msg = "Application has been submitted as new business. ";
	msg += "Changes can not be resubmitted for upload.";

	return (confirm(msg));
}

//This helper function is used to confirm the uploading of a quoute. 
//If the user chooses OK, then the document's HREF is set to that of 
//the link handed to this routine, else it does nothing.
function confirmUpload() {

	var msg;

	msg = "Pending submit request will be cancelled. If you continue, ";
	msg += "the submit button must be reselected.";

	return (confirm(msg));
}

function setTextDecoration(id,show){
	var elem = document.getElementById(id);
	if (elem){
		if (show){
			elem.style.textDecoration = "underline";
		}else{
			elem.style.textDecoration = "";
		}
	}
}