// $Revision: 8516 $ $Date: 2007-11-12 16:13:26 -0200 (seg, 12 nov 2007) $
var LumisPortal = new LumisPortal();
document.onkeydown = LumisPortal.onKeyDown;

function LumisPortal()
{
	this.onKeyDown = onKeyDown;
	this.onRefresh = onRefresh;
	this.onSubmitForm = onSubmitForm;
	this.onPrint = onPrint;
	this.getCookie = getCookie;
	this.addMessage = addMessage;
	this.onBodyLoad = onBodyLoad;
	this.checkForMessages = checkForMessages;
	this.gotoPage = gotoPage;
	this.htmlEncode = htmlEncode;
	this.mainName = "main.jsp";
	this.pageParameterChannelIdName = "lumChannelId";
	this.pageParameterPageIdName = "lumPageId";
	this.isSubmitting = false;
	this.renderInterfaceInstance = renderInterfaceInstance;
	this.getXmlHttpObject = getXmlHttpObject;
	
	if(window.addEventListener)
	{
		try
		{
			window.addEventListener("pageshow", onPageShow, false);
		}
		catch(e)
		{
			;
		}
	}
				
	function onPageShow()
	{
		this.isSubmitting = false;
	}
	
	function gotoPage(lumPageId, lumChannelId)
	{
		var url = this.mainName;
		if(lumPageId != null)
			url += "?"+this.pageParameterPageIdName+"="+lumPageId;
		if(lumChannelId != null)
		{
			if(lumPageId == null)
				url += "?";
			else
				url += "&";
			url += this.pageParameterChannelIdName+"="+lumChannelId;
		}
		
		window.location.href = url;
	}
	
	function onKeyDown()
	{
		if(this.isSubmitting)
			return;
		
		if(window.event)
		{
			if(window.event.keyCode == 123)
			{
				document.forms["LumisPortalForm"].elements["lumMode"].value = 1;
				
				if(window.event.ctrlKey)
					document.forms["LumisPortalForm"].elements["lumSafeRenderMode"].value = 1;
					
				document.forms["LumisPortalForm"].submit();
				this.isSubmitting = true;
			}
		}
	}
	
	function onRefresh()
	{
		if(this.isSubmitting)
			return;

		document.forms["LumisPortalForm"].submit();
		this.isSubmitting = true;
	}
	
	function onPrint(bUseNewWindow)
	{
		if(this.isSubmitting)
			return;

		if(bUseNewWindow)
		{
			var strFormTarget = document.forms['LumisPortalForm'].target;
			
			document.forms['LumisPortalForm'].target = "_blank";
			document.forms['LumisPortalForm'].elements['lumPrinting'].value = '1';
			document.forms["LumisPortalForm"].submit();
			
			document.forms['LumisPortalForm'].target = strFormTarget;
			document.forms['LumisPortalForm'].elements['lumPrinting'].value = '0';
		}
		else
		{
			document.forms['LumisPortalForm'].elements['lumPrinting'].value = '1';
			document.forms["LumisPortalForm"].submit();
			
			this.isSubmitting = true;
		}	
	}

	function onSubmitForm(formName, destId, destType, bProcessAction, includeAnchor, updateElementId)
	{
		if(this.isSubmitting)
			return;

		if(!formHasFileInputs(formName))
		{
			if(destType == null)
				destType = "lumII";
			
			var parameters = "<parameters destId=\""+destId+"\" destType=\""+destType+"\">";
			parameters += getFormParameters(formName);
			parameters += "</parameters>";
	
			var portalForm = document.forms["LumisPortalForm"];
			
			portalForm.elements["lumNewParams"].value = parameters;
			
			var originalPortalFormAction = portalForm.action;
			
			if(destId != null)
				portalForm.action += "&"+destType+"="+destId;
			
			if(includeAnchor != undefined)
				portalForm.action += "#" + includeAnchor;
			
			if(bProcessAction)
			{
				portalForm.elements["lumA"].value = "1";
			}
			else
			{
				var pForm = document.forms[formName];
				if(pForm && pForm.action)
				{
					var strFormAction = pForm.action;
					
					if(updateElementId != null)
					{
						var containerElement = document.getElementById(updateElementId);
						if(containerElement)
						{
							renderInterfaceInstance(containerElement, destId);
							portalForm.elements["lumNewParams"].value = "";
							portalForm.action = originalPortalFormAction;
							return;
						}
					}
					else if(strFormAction.search("lumClientRendered=1") != -1)
					{
						var interfaceDiv = document.getElementById("lumIId"+destId);
						if(interfaceDiv)
						{
							renderInterfaceInstance(interfaceDiv, destId);
							portalForm.elements["lumNewParams"].value = "";
							portalForm.action = originalPortalFormAction;
							return;
						}
					}
				}
			}
				
			portalForm.submit();
		}
		else
		{
			try
			{
				var formObject = document.forms[formName];
				
				if(bProcessAction)
				{
					if(formObject.elements["lumA"] != null)
						formObject.elements["lumA"].value="1";
				}
				formObject.submit();
			}
			catch(e)
			{
				if (lumis_doui_control_fileupload_FileUploadControl_msgFileNotFound)
					alert(lumis_doui_control_fileupload_FileUploadControl_msgFileNotFound);
				else
					alert(e.message);
				return;
			}
		}
		
		this.isSubmitting = true;
	}
	
	function formHasFileInputs(formName)
	{
		var pForm = document.forms[formName];
		for(var i=0; i<pForm.elements.length; i++)
		{
			pElement = pForm.elements[i];
			if(pElement.type == "file")
				return true;
		}
		
		return false;
	}

	function getFormParameters(formName)
	{
		var strFormParams = "";
		var pForm = document.forms[formName];
		var pElement;
		
		var strFormAction = pForm.action;
		if(strFormAction && strFormAction.length)
			strFormParams += '<p n="lumFormAction">' + htmlEncode(strFormAction) + '</p>';

		for(var i=0; i<pForm.elements.length; i++)
		{
			pElement = pForm.elements[i];
			
			if(!pElement.type || !pElement.name || !pElement.name.length)
				continue;
			if(pElement.type == "button" || pElement.type == "submit")
				continue;
			else if(pElement.type == "select-one" && pElement.selectedIndex != -1)
			{
				var strValue = pElement.options[pElement.selectedIndex].value;
				if(strValue.length)
					strFormParams += '<p n="'+pElement.name+'">' + htmlEncode(strValue) + '</p>';
			}
			else if(pElement.type == "select-multiple")
			{
				var strValue = "";
				for(var j=0; j<pElement.options.length; j++)
				{
					var strValue = pElement.options[j].value;
					if(pElement.options[j].selected && strValue.length)
						strFormParams += '<p n="'+pElement.name+'">' + htmlEncode(strValue) + '</p>';
				}
			}
			else
			{
				if((pElement.type == "checkbox" || pElement.type == "radio") && !pElement.checked)
					continue;
				
				var strValue = pElement.value;
				
				if(strValue.length)
					strFormParams += '<p n="'+pElement.name+'">' + htmlEncode(strValue) + '</p>';
			}
		}

		return strFormParams;
	}

	function getCookie(sName)
	{
		// cookies are separated by semicolons
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++)
		{
			// a name/value pair (a crumb) is separated by an equal sign
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0]) 
			{
				var strToDecode = aCrumb[1];
				
				// replace '+' by space, because javascript decoding miss it
				strToDecode = strToDecode.replace(/\+/g, ' ');
				
				if (decodeURIComponent)
					return decodeURIComponent(strToDecode);
				else
					return unescape(strToDecode); // fallback for old browsers. May not display unicode correctly.
			}
		}

		// a cookie with the requested name does not exist
		return null;
	}
	
	function addMessage(message)
	{
		var pMsg = document.forms['LumisPortalForm'].elements['LumisClientMessage'];
		
		if (pMsg.value != '')
			pMsg.value += "\n";
		pMsg.value += message;
	}
	
	function onBodyLoad()
	{
		// clear any parameters set by doing a [back] in browser
		var portalForm = document.forms["LumisPortalForm"];
		portalForm.elements["lumNewParams"].value = "";
		portalForm.elements["lumA"].value = "";
			
		checkForMessages();
	}
	
	function checkForMessages()
	{
		var strClientMessage = document.forms["LumisPortalForm"].elements["LumisClientMessage"].value;
		if (strClientMessage != "") 
			window.setTimeout(showMessages, 1);
	}
	
	function showMessages()
	{
		var strClientMessage = document.forms["LumisPortalForm"].elements["LumisClientMessage"].value;
		if (strClientMessage != "") 
		{ 
			alert(strClientMessage);
			document.forms["LumisPortalForm"].elements["LumisClientMessage"].value = "";
		}
	}

	function htmlEncode(text)
	{
		if ( typeof( text ) != "string" )
			text = text.toString() ;
	
		text = text.replace(/&/g, "&amp;") ;
		text = text.replace(/"/g, "&quot;") ;
		text = text.replace(/</g, "&lt;") ;
		text = text.replace(/>/g, "&gt;") ;
		text = text.replace(/'/g, "&#39;") ;
	
		return text ;
	}
	
	function renderInterfaceInstance(interfaceInstanceElement, interfaceInstanceId)
	{
		var xmlHttp = getXmlHttpObject();
		
		// get the interface content and render it
		var strRequest = "lumPrevParams="+document.forms["LumisPortalForm"].elements["lumPrevParams"].value;
		
		strRequest += "&lumNewParams=";
		if (encodeURIComponent)
			strRequest += encodeURIComponent(document.forms["LumisPortalForm"].elements["lumNewParams"].value);
		else
			strRequest += escape(document.forms["LumisPortalForm"].elements["lumNewParams"].value);
		
		xmlHttp.open("POST", document.forms["LumisPortalRenderInterfaceForm"].action+"&lumScript=1&lumRenderII="+interfaceInstanceId, false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(strRequest);
		
		interfaceInstanceElement.innerHTML = xmlHttp.responseText;
		
		xmlHttp = null;
	}

	function getXmlHttpObject()
	{
		var httpObj = null;
		
		if (window.XMLHttpRequest)
			httpObj=new XMLHttpRequest();
		else
			httpObj=new ActiveXObject("Microsoft.XMLHTTP");
			
		return httpObj;
	}
}
