function updatefields(value,fields)
{
	aFields = fields.split(',');
	//0 => fieldname 1 => maxlength
	for(i=0;i<aFields.length;i+=2)
	{
		ret = value;
		if(ret == '')
		{
			ret = '&nbsp;';
		}

		if(ret.length > aFields[i+1])
		{
			ret = ret.substr(0,aFields[i+1]-3) + '...';
		}
		MMO(aFields[i]).innerHTML = clearCrap(ret);
	}
	
}

function clearCrap(value)
{
	var match = /<(?:.|\s)*?>/g;
	value = value.replace(match,"");
	if(value == '')
	{
		value = '&nbsp;';
	}
	return value;
}

function toggle_readonly(id)
{
	var id = id.replace(/[1-3]{1}$/,"");
	for(var i=1;i<=3;i++)
	{
		if($('#'+id+''+i).attr('readonly'))
		{
			$('#'+id+''+i).removeAttr('readonly');
		}
		else
		{
			$('#'+id+''+i).attr('readonly','readonly');
		}
		
	}
}
/**
* Pagelink
*/

$(document).ready(
	function()
	{
		$('#main-header-pagelink').click(function()
		{
			if(!$('#main-header-input').is(':visible'))
			{
				$('#main-header-infos').hide('slide',{ direction: 'left' }, 500);
				$('#main-header-input').animate({borderWidth:'0px'},500);
				ajax_get_page_link();
			}
			else
			{
				$('#main-header-input').hide("slide",{direction:"left"},500);
				$('#main-header-infos').animate({borderWidth:'0px'},500);
				$('#main-header-infos').show("slide",{direction:"left"},500);
				$('#pagelink-input').val('');
			}
		});
	}
);

function toClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
}

/**
* Mo, 10.12.2000 into date object
*/
update_enddate = function(date)
{
	var startdate = strToDate(date);
	var enddate = strToDate($('#enddate').val());
	var daynames = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
	
	if(enddate < startdate)
	{
		$('#enddate').val(date);
	}
}

function strToDate(strDate)
{
	var arrDate;
	if(strDate.match(/\ /))
	{
		arrDate = strDate.split(" ");
		strDate = arrDate[1];
	}
	arrDate = strDate.split(".");
	return new Date((arrDate[2]*1+2000),(arrDate[1]-1),arrDate[0],3,0,0);
}

/**
*
*Gantt Chart close on click
*
*/

$(document).bind('click',
	function(e)
	{
		var $clicked=$(e.target); // get the element clicked
		if($clicked.parents().is('#overDiv') && !$clicked.is('#od-close'))
		{
		}
		else
		{
				$('#overDiv').html('');
		}
		
		if($('#quick-export-div').is(':visible') && !$clicked.parents().is('#quick-export-div') && !$clicked.is('#quick-export') && !$clicked.parents().is('#quick-export'))
		{
			hideQuickExport();
		}
	}
);

$(document).mousedown(function(e){
	var $clicked=$(e.target);
	if($clicked.parents().is('#ganttchart') || $clicked.parents().is('#middle-budget-content') || $clicked.parents().is('#imp-rep-cos'))
	{
		$gcposx = e.pageX;// - this.offsetLeft;
		$gcposy = e.pageY;
	}
   });

/*
$(function() {
		$(".draggable").draggable();
	});
*/

/**
*
* EMDESK / MPM Login switch
* 
*/
	/*
$(document).ready(
	function()
	{
		
	
		$("#mpm-login").click(function(){
			$("#emd-login").removeClass('emlogin-active');
			$("#emd-login").addClass('emlogin-inactive');
			$("#mpm-login").removeClass('emlogin-inactive');
			$("#mpm-login").addClass('emlogin-active');
			$("#emdlogin-state").val(1);
			
			})
		$("#emd-login").click(function(){
			$("#mpm-login").removeClass('emlogin-active');
			$("#mpm-login").addClass('emlogin-inactive');
			$("#emd-login").removeClass('emlogin-inactive');
			$("#emd-login").addClass('emlogin-active');
			$("#emdlogin-state").val(0);
			})

	}
);*/

/**
* Session timeout Warning
*/
/*
$(document).ready(
	function()
	{
		var date = new Date();
		var session_starttime = Math.floor(date.getTime()/1000);
		window.setTimeout("check_sessiontimeout("+session_starttime+")",60000);
	}
);
check_sessiontimeout = function(session_starttime,showed)
{
	var date = new Date();
	
	var time = Math.floor(date.getTime()/1000);
	var time_elapsed = time-session_starttime;
	
	if((time_elapsed >= 5) && !showed)
	{
		showed = true;
		$('.global-session-alert').animate({height:'148px', width: '150px', top:'-150px', left:'-150px'});
		$('#global_session_alert').html("Your session is already idle for "+Math.round(time_elapsed/60)+" minutes. Your action is required within the next "+Math.round((parseInt($('#session-timeout').val())-time_elapsed)/60)+" minutes before the session is closed.");
	}
	window.setTimeout("check_sessiontimeout("+session_starttime+","+showed+")",60000);
}
*/


/**
* Quick Export
*/

$(document).ready(function(){
	$('#quick-export')	
	.hover(
	function()
		{
			if(!$('#quick-export-div').is(':visible'))
			{
				$('#quick-export-img').css('margin-top','2px');
			}
		},
	function()
		{
			if(!$('#quick-export-div').is(':visible'))
			{
				$('#quick-export-img').css('margin-top','0px');
			}
		}
	)
	.click(
		function()
		{
			if($('#quick-export-div').is(':visible'))
			{
				$('#quick-export-div').fadeOut("fast");
				//$(this).removeClass('quick-export-on');
				$('#quick-export-img').attr('src','../images/buttons/arrow_quickexport_down.gif');
				$('#quick-export-img').css('margin-top','0px');
			}
			else
			{
				$('#quick-export-div').fadeIn("fast");
				//$(this).addClass('quick-export-on');
				$('#quick-export-img').attr('src','../images/buttons/arrow_quickexport_up.gif');
				$('#quick-export-img').css('margin-top','0px');
			}
			
		}
	)
	.keydown(function(event)
		{
			switch(event.keyCode)
			{
				case 27:
					hideQuickExport();
				break;
			}
		}
	
	);
	$('div.quick-export-item').hover(
	function()
		{
			$(this).addClass('quick-export-item-hover');
		},
	function()
		{
			$(this).removeClass('quick-export-item-hover');
		}
	);
});

hideQuickExport = function()
{
	$('#quick-export-div').fadeOut("fast");
	$('#quick-export-img').attr('src','../images/buttons/arrow_quickexport_down.gif');
	$('#quick-export-img').css('margin-top','0px');
}


emtoggle = function(id)
{
	var src = $(id+'-img').attr('src').match(/delete/) ? 'add_7' : 'delete_5'; 
	$(id+'-img').attr('src','../images/buttons/'+src+'.gif');
	$(id).toggle();
}

$(document).ready(function()
{
	if($('#message-wall').length > 0)
	{
		$(".collapsible").hide();
		if($.browser.msie)
		{
			$("textarea#message-wall-text").css('height','0px');
		}
		else
		{
			$("textarea#message-wall-text").autoResize();
		}
			
//		$("#message-wall-messages").jScrollPane({ scrollbarWidth:11, scrollbarMargin:0, arrowSize:0 });
		
/*		jQuery("a.openField").click(function(){
		 $(this).parent().parent().next(".collapsible").slideToggle("slow");
	   });

		$('textarea.autogrow').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});
*/		
	}
});

mwautosize = function(id)
{
	if($.browser.msie)
	{
		$("textarea#"+id).css('height','0px');
	}
	else
	{
		$("textarea#"+id).autoResize();
	}
}

/*
$(function()
	{
	$('#messagewall-content').bind('click',
		function()
		{
			$("#message-wall-messages").jScrollPane({ scrollbarWidth:11, scrollbarMargin:0, arrowSize:0 });
		}
	);
	$('.openField').bind('click',
		function()
		{
			$("#message-wall-messages").jScrollPane({ scrollbarWidth:11, scrollbarMargin:0, arrowSize:0 });
		}
	);
});
*/


/**
* Datepicker
*/

$(function() {
	$('.datepicker').datepicker({yearRange: '-5:+10', dateFormat: 'D dd.mm.y',firstDay: 1, numberOfMonths: [1,2], showAnim:'blind', duration: 100, stepMonths:1, changeYear: true, changeMonth: true});
	$('.datepicker2').datepicker({yearRange: '-5:+10', dateFormat: 'dd.mm.y',firstDay: 1,numberOfMonths: [1,2], showAnim:'blind', duration: 100, stepMonths:1, changeYear: true, changeMonth: true});
	});
activatedp = function(str)
{
	for(var i=0;i<activatedp.arguments.length;i++)
	{
		$(activatedp.arguments[i]).datepicker({yearRange: '-5:+10', dateFormat: 'D dd.mm.y',firstDay: 1, numberOfMonths: [1,2], showAnim:'blind', duration: 100, stepMonths:2, changeYear: true, changeMonth: true});
	}
}
activatedp2 = function(str)
{
	for(var i=0;i<activatedp2.arguments.length;i++)
	{
		$(activatedp2.arguments[i]).datepicker({yearRange: '-5:+10', dateFormat: 'dd.mm.y',firstDay: 1,numberOfMonths: [1,2], showAnim:'blind', duration: 100, stepMonths:2, changeYear: true, changeMonth: true});
	}
}
activatedpinl = function(str)
{
	$('#dp_emdesk_calendar').datepicker2({yearRange: '-5:+10', showOtherMonths: true, dateFormat: 'dd.mm.y',firstDay: 1,numberOfMonths: [2,1], stepMonths:1, changeYear: true, changeMonth: true});
}