	      var pollInterval=120000;
	      var timer=null;
	      var req=null; 

				function trim(s) {
				  while (s.substring(0,1) == ' ') {
				    s = s.substring(1,s.length);
				  }
				  while (s.substring(s.length-1,s.length) == ' ') {
				    s = s.substring(0,s.length-1);
				  }
				  return s;
				}	
						
				function showNewObjectDiv(objectname)
				{
					var newprocessTitle = trim(document.forms['newprocessform'].newprocesstitle.value);
					if(newprocessTitle=='')
					{
						alert('Bitte geben Sie einen Titel für den neu anzulegenden Vorgang ein.');
					}
					else
					{
						document.forms[objectname].newobject_process_name.value=newprocessTitle;
						document.forms[objectname].process_id.value='0';
						document.getElementById('crm_'+objectname).style.visibility='visible';
					}
				}

				function showNewObjectMail(objectname,folderId,processId)
				{
					var newprocessTitle = trim(document.forms['newprocessform'].newprocesstitle.value);
					if(newprocessTitle=='')
					{
						alert('Bitte geben Sie einen Titel für den neu anzulegenden Vorgang ein.');
					}
					else
					{
						document.forms[objectname].newobject_process_name.value=newprocessTitle;
						document.forms[objectname].process_id.value='0';
						openMailPopup('newmail',folderId,processId,0,0);
//						document.getElementById('crm_'+objectname).style.visibility='visible';
					}
				}
			 
				function adjustIFrameSize(id)
				{
					var myIframe = document.getElementById(id);
					if (myIframe)
					{
						if (myIframe.contentDocument &&
						myIframe.contentDocument.body.offsetHeight)
						{
							// W3C DOM (and Mozilla) syntax
							myIframe.height =
							myIframe.contentDocument.body.offsetHeight+20;
						}
						else if (myIframe.Document &&
						myIframe.Document.body.scrollHeight)
						{
							// IE DOM syntax
							myIframe.height =
							myIframe.Document.body.scrollHeight;
						}
					}
				}
				
			function checkMailFormInput(formname)
			{
				if(document.getElementById("mailaddressdiv_"+formname).style.display!='none')
				{
					return false;
				}
				var myForm = document.forms[formname];
				if(trim(myForm.newmail_receiver.value)=='' || trim(myForm.newmail_title.value)=='' || trim(myForm.newmail_content.value)=='')
				{
					alert('Empfänger, Betreff und Inhalt einer Mail dürfen nicht leer sein.');
					return false;
				}
				else
					return true;
			}	
				
			function checkMailIn()
		  {
		  	if(document.getElementById("inboxanz")!=null)
		  	{
		  		//alert('checkMailIn');
					var rand = Math.random();
				  var url="/servlet/gib.crmapp.servlet.MaxMailInIdServlet?rnd="+rand;
				  getReq();
				  if (req != undefined)
				  {
				    req.onreadystatechange = function() {isLoadedCheckMailIn(url);};
				    req.open("GET", url, true);
				    req.send("");
				  }
				}
			}

		  function setSrc(url)
		  {
		  	if (req.readyState == 4 && req.responseText)
		  	{				  					  		  		  	
		  		document.getElementsByTagName("body")[0].innerHTML=req.responseText;	
		  		window.print();
		  		window.close();	  		  
				}
			}
			
			function getPrintableMail(entity,id)
			{
					var rand = Math.random();
				  var url="/servlet/gib.crmapp.servlet.MailServlet?rnd="+rand+"&entity="+entity+"&id="+id;
				  getReq();
				  if (req != undefined)
				  {
				    req.onreadystatechange = function() {setSrc(url);};
				    req.open("GET", url, true);
				    req.send("");
				  }
			}

			function pollMails()
			{
					var rand = Math.random();
				  var url="/servlet/gib.crmapp.servlet.PollMailServlet?rnd="+rand;
				  getReq();
				  if (req != undefined)
				  {
				    req.onreadystatechange = function() {polled(url);};
				    req.open("GET", url, true);
				    req.send("");
				  }
			}

		  function polled(url)
		  {
		  	if (req.readyState == 4 && req.responseText)
		  	{
		  		checkMailIn();
				}
			}
			
		  function EvalSound(soundobj)
		  {
		  	var theSoundDiv=document.getElementById(soundobj);
		  	theSoundDiv.innerHTML='<embed style=\"position:absolute;width:0;height:0\" src=\"/sound/ding.wav\" type=\"application/x-mplayer2\" autostart=\"true\" loop=\"false\" enablejavascript=\"true\"/>';
			}

		  function getReq()
		  {
			  if (window.XMLHttpRequest)
			  {
			    req = new XMLHttpRequest();
			  }
			  else if (window.ActiveXObject)
			  {
			    req = new ActiveXObject("Microsoft.XMLHTTP");
			  }
		  }

			timer=window.setTimeout("checkMailIn()",pollInterval);
			
			  var reqAddresses = null;
			  
			  function getReqAddresses()
			  {
				  if (window.XMLHttpRequest)
				  {
				    reqAddresses = new XMLHttpRequest();
				  }
				  else if (window.ActiveXObject)
				  {
				    reqAddresses = new ActiveXObject("Microsoft.XMLHTTP");
				  }
			  }			
			
				var addrPos=-1;
				var posMax=-1;
				var lastHighlight=-1;
			
				function getAddresses(mailmode,e)
				{
					var formname = mailmode;
					newmailreceiver=document.forms[formname].newmail_receiver;								
					var newmailreceivervalue = newmailreceiver.value;
					if(newmailreceivervalue.indexOf(',')!=-1)
						newmailreceivervalue = newmailreceivervalue.substring(newmailreceivervalue.lastIndexOf(',')+1);																		
					var theDiv=document.getElementById('mailaddressdiv_'+mailmode);
					if(newmailreceivervalue.length>0)
					{										
						if(e.keyCode==40) //cursor down
						{
							addrPos++;
							if(addrPos>=posMax)
							{
								addrPos=posMax-1;
							}
							highlight();
						}
						else if(e.keyCode==38) //cursor up
						{
							addrPos--;
							if(addrPos<0)
							{
								addrPos=0;
							}
							highlight();
						}
						else if(e.keyCode==13) //enter
						{
							if(addrPos>=0&&addrPos<posMax)
							{
								var theLink=document.getElementById("addresslink"+addrPos);
								eval(theLink.getAttribute("onclick"));
							}
						}
						else
						{
							//dropdown per ajax neu schreiben
							var rand = Math.random();
						  var url="/servlet/gib.crmapp.servlet.MailAddressServlet?rnd="+rand+"&searchstr="+escape(newmailreceivervalue);
						  getReqAddresses();
						  if (reqAddresses != undefined)
						  {			  	
						    reqAddresses.onreadystatechange = function() {writeAddresses(url,formname);};
						    reqAddresses.open("GET", url, true);
						    reqAddresses.send("");			    
						  }
						}		
					}		
					else
					{
						addrPos=-1;
						theDiv.innerHTML="";
						theDiv.style.display='none';
					}
				}

				function highlight()
				{
					var theFormerLink=document.getElementById("addresslink"+lastHighlight);
					if(theFormerLink!=null)
					{
						theFormerLink.style.backgroundColor='#ffffff';
					}
					var theLink=document.getElementById("addresslink"+addrPos);
					theLink.style.backgroundColor='#FFDDDD';
					lastHighlight=addrPos;
				}
			
			  function writeAddresses(url,mailmode)
			  {
			  	if (reqAddresses.readyState == 4 && reqAddresses.responseText!=null)
			  	{			  		
						addrPos=-1;
			  		var formname = mailmode;		  		
						var theDiv=document.getElementById('mailaddressdiv_'+mailmode);						
						var newDivContent='';						
		  			addresses=reqAddresses.responseXML.firstChild;
		  			theChild=addresses.firstChild;
		  			while(theChild!=null&&theChild.nodeType==3)
		  			{
		  				theChild=theChild.nextSibling;
		  			}
		  			var arrPos=0;
			  		while(theChild!=null)
			  		{
			  			if(theChild.nodeType!=3)
			  			{
								theName=theChild.getAttribute("addressvalue");
				  			theNameUnesc = unescape(theName);				  			
				  			theNameUnesc = theNameUnesc.replace( new RegExp( "\\\+", "g" ), " " );
								theNick=theChild.getAttribute("nickvalue");
				  			theNickUnesc = unescape(theNick);				  			
				  			theNickUnesc = theNickUnesc.replace( new RegExp( "\\\+", "g" ), " " );
				  			if(theNickUnesc!=null && theNickUnesc!='')
				  				theNameUnesc =	theNickUnesc+' &lt;'+theNameUnesc+'&gt'; 											
				  			newDivContent+='<img src="/images/crm/crm_woman.gif">&#160;<a style="text-decoration:none;font-weight:normal;color:#555555;" href="#" id="addresslink'+arrPos+'" onClick=setAddressVal("'+formname+'","'+theName+'","'+theNick+'");>'+theNameUnesc+'</a><br>';
				  			arrPos++;
				  		}
			  			theChild=theChild.nextSibling;
			  		}
			  		posMax=arrPos;
			  		if(arrPos==0)
			  		{
							addrPos=-1;
							theDiv.innerHTML="";
							theDiv.style.display='none';
			  		}
			  		else
			  		{												
							theDiv.innerHTML=newDivContent;
							if(newDivContent!='')
								theDiv.style.display='block';
						}
			  	}
			  }	
			  
	  		function getAddressVal(mailmode,name,nick)
	  		{
		 			theNameUnesc = unescape(name);
		 			theNameUnesc = theNameUnesc.replace( new RegExp( "\\\+", "g" ), " " );
		 			theNickUnesc = unescape(nick);
		 			theNickUnesc = theNickUnesc.replace( new RegExp( "\\\+", "g" ), " " );	
				  if(theNickUnesc!=null && theNickUnesc!='')
				  	theNameUnesc =	theNickUnesc+' <'+theNameUnesc+'>';
				  return(theNameUnesc);
	  		}

	  		function setAddressVal(mailmode,name,nick)
		  	{
		  		theNameUnesc=getAddressVal(mailmode,name,nick);
				  var formname = mailmode;		 				 			
		 			newmailreceiver=document.forms[formname].newmail_receiver;
		 			var newmailreceivervalue = newmailreceiver.value;
					if(newmailreceivervalue.indexOf(',')!=-1)
						newmailreceivervalue = newmailreceivervalue.substring(0,newmailreceivervalue.lastIndexOf(',')+1);
					else
						newmailreceivervalue = '';		 			
		 			newmailreceiver.value=newmailreceivervalue+theNameUnesc+',';
		 			var theDiv=document.getElementById('mailaddressdiv_'+mailmode);
		 			theDiv.innerHTML="";
					theDiv.style.display='none';
		 		}	
		 		
		 		//MultiFileUpload - bisher noch nicht verwendet
				function MultiSelector( list_target, max )
				{				
					// Where to write the list
					this.list_target = list_target;
					// How many elements?
					this.count = 0;
					// How many elements?
					this.id = 1;
					// Is there a maximum?
					if( max )
					{
						this.max = max;
					} 
					else 
					{
						this.max = -1;
					};		 		
					/**
					 * Add a new file input element
					 */
					this.addElement = function( element )
					{				
						// Make sure it's a file input element
						if( element.tagName == 'INPUT' && element.type == 'file' )
						{				
							// Element name -- what number am I?
							element.name = 'data' + this.id++;				
							// Add reference to this object
							element.multi_selector = this;				
							// What to do when a file is selected
							element.onchange = function()
							{				
								// New file input
								var new_element = document.createElement( 'input' );
								new_element.type = 'file';				
								new_element.className = 'onlineform';
								// Add new element
								this.parentNode.insertBefore( new_element, this );				
								// Apply 'update' to element
								this.multi_selector.addElement( new_element );				
								// Update list
								this.multi_selector.addListRow( this );				
								// Hide this: we can't use display:none because Safari doesn't like it
								this.style.position = 'absolute';
								this.style.left = '-1000px';				
							};
							// If we've reached maximum number, disable input element
							if( this.max != -1 && this.count >= this.max )
							{
								element.disabled = true;
							};				
							// File element counter
							this.count++;
							// Most recent element
							this.current_element = element;							
						} 
						else 
						{
							// This can only be applied to file input elements!
							alert( 'Error: not a file input element' );
						};				
					};				
					/**
					 * Add a new row to the list of files
					 */
					this.addListRow = function( element )
					{				
						// Row div
						var new_row = document.createElement( 'div' );				
						// Delete button
						var new_row_button = document.createElement( 'input' );
						new_row_button.type = 'image';
						new_row_button.src='/images/crm/loeschen.gif';
						new_row_button.style.width='12px';
						new_row_button.style.height='12px';
						new_row_button.style.verticalAlign='middle';
						//new_row_button.value = 'Delete';				
						// References
						new_row.element = element;				
						// Delete function
						new_row_button.onclick= function()
						{				
							// Remove element from form							
							this.parentNode.element.parentNode.removeChild( this.parentNode.element );				
							// Remove this row from the list
							this.parentNode.parentNode.removeChild( this.parentNode );				
							// Decrement counter
							this.parentNode.element.multi_selector.count--;				
							// Re-enable input element (if it's disabled)
							this.parentNode.element.multi_selector.current_element.disabled = false;				
							// Appease Safari
							//    without it Safari wants to reload the browser window
							//    which nixes your already queued uploads
							return false;
						};														
						
						
						// Set row value
						new_row.innerHTML='';	
						// Add button						
						new_row.appendChild( new_row_button );
					  new_row.appendChild(document.createTextNode(' '+element.value));
																
						// Add it to the list
						this.list_target.appendChild( new_row );						
					};				
				};		 					

				function initializeAddrDiv(formname)
				{
					document.getElementById('crm_addrdiv').style.visibility='visible';
					var rand = Math.random();
				  getReqAddresses();
				  var newmailreceiver=document.forms[formname].newmail_receiver;
				  var newmailreceivervalue = newmailreceiver.value;
				  var url="/servlet/gib.crmapp.servlet.MailAddressServlet?rnd="+rand+"&searchstr=&chosenaddrs="+escape(newmailreceivervalue);
				  if (reqAddresses != undefined)
				  {			  	
				    reqAddresses.onreadystatechange = function() {writeAddresses2(url,formname);};
				    reqAddresses.open("GET", url, true);
				    reqAddresses.send("");			    
				  }
				}

			  function writeAddresses2(url,mailmode)
			  {
			  	if (reqAddresses.readyState == 4 && reqAddresses.responseText!=null)
			  	{
			  		document.getElementById('mailrecfield').value=mailmode;
						addrPos=-1;
						var fromSelect=document.getElementById('addrfrom');
						var toSelect=document.getElementById('addrto');
		  			addresses=reqAddresses.responseXML.firstChild;
		  			theChild=addresses.firstChild;
		  			while(theChild!=null&&theChild.nodeType==3)
		  			{
		  				theChild=theChild.nextSibling;
		  			}
		  			var arrPos=0;
			  		while(theChild!=null)
			  		{
			  			if(theChild.nodeType!=3)
			  			{
								theName=theChild.getAttribute("addressvalue");
				  			theNameUnesc = unescape(theName);				  			
				  			theNameUnesc = theNameUnesc.replace( new RegExp( "\\\+", "g" ), " " );
				  			theName=theNameUnesc;
								theNick=theChild.getAttribute("nickvalue");
				  			theNickUnesc = unescape(theNick);				  			
				  			theNickUnesc = theNickUnesc.replace( new RegExp( "\\\+", "g" ), " " );
				  			theNick=theNickUnesc;
				  			if(theNickUnesc!=null && theNickUnesc!='')
				  			{
				  				theNameUnesc =	theNickUnesc+' &lt;'+theNameUnesc+'&gt';
				  			}
				  			if(theNick!=null && theNick!='')
				  				theName =	theNick+' <'+theName+'>';
								var objOption = document.createElement("option");
								objOption.text = theName;
								objOption.value = theNameUnesc;
								var objSelect=fromSelect;
								if(theChild.getAttribute("selected")!=null)
								{
									objSelect=toSelect;
								}
								if(document.all && !window.opera)
								  {objSelect.add(objOption);}
								 else
								  {objSelect.add(objOption, null);}				  			
				  		}
			  			theChild=theChild.nextSibling;
			  		}
			  		posMax=arrPos;												
			  	}
			  }	

				function toLeft()
				{
					var fromSelect=document.getElementById('addrfrom');
					var toSelect=document.getElementById('addrto');
					moveLB(toSelect,fromSelect);
				}

				function toRight()
				{
					var fromSelect=document.getElementById('addrfrom');
					var toSelect=document.getElementById('addrto');
					moveLB(fromSelect,toSelect);
				}
				
				function moveLB(from, to)
				{
					var offered = new Array();
					var chosen = new Array();
					var entries = new Object();
				
					for(var i = 0; i < from.length; i++)
					{
						entries[from[i].text] = from[i].value;
						if(from[i].selected == true)
						{
							chosen[chosen.length] = from[i].text;
						}
						else
						{
					  	offered[offered.length] = from[i].text;
						}
					}
					for(i = 0; i < to.length; i++)
					{
						entries[to[i].text] = to[i].value;
						chosen[chosen.length] = to[i].text;
					}
					from.length = 0;
					to.length = 0;
					offered.sort(caseinsensitive);
					chosen.sort(caseinsensitive);
					for(var j = 0; j < offered.length; j++)
					{
						from[j] = new Option(offered[j], entries[offered[j]]);
					}
					for(j = 0; j < chosen.length; j++)
					{
						to[j] = new Option(chosen[j], entries[chosen[j]]);
					}
				}
				
				function caseinsensitive(str1,str2)
				{
					return(str2.toLowerCase()<str1.toLowerCase())
				}
				
				function writeBack()
				{
					var mailreceiverformname=document.getElementById('mailrecfield').value;
					var mailreceiver=document.forms[mailreceiverformname].newmail_receiver;
					var mailAddrs="";
					var toSelect=document.getElementById('addrto');
					var fromSelect=document.getElementById('addrfrom');
					for(i = 0; i < toSelect.length; i++)
					{
						mailAddrs+=unescape(toSelect[i].text);
						if(i+1<toSelect.length)
						{
							mailAddrs+=",";
						}
					}
					mailreceiver.value=mailAddrs;
					fromSelect.length = 0;
					toSelect.length = 0;
					document.getElementById('crm_addrdiv').style.visibility='hidden';
				}

