function charsLimit(textarea, counter,maxVal)
{
  if (textarea.value.length > maxVal)
    textarea.value = textarea.value.substring(0, maxVal);
  else 
    counter.value = maxVal - textarea.value.length;
}
function charCountSetup(desc,charsLeft,maxLength)
{
	desc.onkeydown=function(){charsLimit(desc,charsLeft,maxLength);};
	desc.onkeyup=function(){charsLimit(desc,charsLeft,maxLength);};
}
function initialize(el,time,frame,accel)
{
	el.frame=frame||50;
	el.time=time||800;
	el.each=Math.ceil(el.time/el.frame);
	el.accel=(accel/1000000)||(200/1000000);
	el.now=0;
	el.currentTime=0;
	if(!el.width) el.width=el.offsetWidth;
	if(!el.height) el.height=el.offsetHeight;
	if(!el.style.width) el.style.width=el.offsetWidth+'px';
	if(!el.style.height) el.style.height=el.offsetHeight+'px';
	if(!el.style.opacity && typeof el.style.opacity!='undefined')
	{
		el.style.opacity=0.99999;
		el.type='w3c';
	}
	else if(!el.style.filter && typeof el.style.filter!='undefined')
	{
		el.style.filter="alpha(opacity:100)";
		el.type='ie';
	}
	else if(!el.style.KhtmlOpacity && typeof el.style.KhtmlOpacity!='undefined')
	{
		el.style.KhtmlOpacity=1;
		el.type='safari';
	}
	else if(!el.style.MozOpacity && typeof el.style.MozOpacity!='undefined')
	{
		el.style.MozOpacity=0.99999;
		el.type='moz';
	}
	else el.type='opera';
	el.origin=getPosition(el);
}

function resetTimer(el)
{
	el.now=0;
}

function initDraggable(el)
{
	el.style.position="relative";
	attachEventListener(el, "mousedown", MDDragDrop, false);
}

function squish(el,w,h,start_x,final_x,start_y,final_y)
{
	if(el.interval) clearTimeout(el.interval);
	el.now+=el.each*2;
	if(!h)
	{
		var current=Math.round(Math.sin(el.now*Math.PI/el.time/2)*el.width);
		if(start_x>final_x)
		{
			if(el.width-current<=final_x) el.style.width=final_x+'px';
			else el.style.width=el.width-current+'px';
		}
		else
		{
			if(current>=final_x) el.style.width=final_x+'px';
			else el.style.width=current+'px';
		}
	}
	if(!w)
	{
		var current=Math.round(Math.sin(el.now*Math.PI/el.time/2)*el.height);
		if(start_y>final_y)
		{
			if(el.height-current<=final_y) el.style.height=final_y+'px';
			else el.style.height=el.height-current+'px';
		}
		else
		{
			if(current>=final_y) el.style.height=final_y+'px';
			else el.style.height=current+'px';
		}
	}
	if(!w && parseInt(el.style.height)!=final_y || !h && parseInt(el.style.width)!=final_x || parseInt(el.style.height)!=final_y && parseInt(el.style.width)!=final_x)
		el.interval=setTimeout(function(){squish(el,w,h,start_x,final_x,start_y,final_y);},el.each);
	else
	{
		if(parseInt(el.style.height)==0 || parseInt(el.style.width)==0) el.style.visibility="hidden";
		resetTimer(el);
	}
	return false;
}

function fade(el,to,stay,img)
{
	el.now+=el.each*2;
	var opacity = parseFloat(el.now/el.time);
	if(!img)
	{
		if(to==0)
		{
			if(1-opacity<0) changeOpacity(el,0);
			else changeOpacity(el,1-opacity);
		}
		else if(to==1 && opacity<1)	changeOpacity(el,opacity);
		else changeOpacity(el,1);
	}
	if(el.now<el.time) setTimeout(function(){fade(el,to,stay,img);},el.each);
	else resetTimer(el);
}

function changeOpacity(el,val)
{
	switch(el.type)
	{
		case 'safari': el.style.KhtmlOpacity=val; break;
		case 'moz': el.style.MozOpacity=val; break;
		case 'ie': el.style.filter="alpha(opacity:"+(val*100)+")"; break;
		default:el.style.opacity=(val==1?0.99999:val); break;
	}
}

function createDropSheet(x,y)
{
	var dropSheet = document.createElement("div");
	dropSheet.setAttribute("id", "dropSheet");
	dropSheet.style.position = "absolute";
	dropSheet.style.left = "0";
	dropSheet.style.top = "0";
	dropSheet.style.width = x + "px";
	dropSheet.style.height = y + "px";
	document.getElementsByTagName("body")[0].appendChild(dropSheet);
	initialize(dropSheet);
	changeOpacity(dropSheet,0.35);
	return dropSheet;
}

function toggle(divId)
{
	var d = document.getElementById(divId);
	if(d.style.display =='none')
		d.style.display = 'block';
	else
		d.style.display = 'none';
}

function fadeToggle(el,stay,img)
{
	if(el.style.opacity==0.99999 || el.style.filter=="alpha(opacity:100)") fade(el,0,stay,img);
	else if(el.style.opacity==0 || el.style.filter=="alpha(opacity:0)") fade(el,1,stay,img);
}

function toggleSquish(el,w,h)
{
	if(parseInt(el.style.width)>0 && parseInt(el.style.height)>0)
	{
		squish(el,w,h,el.offsetWidth,0,el.offsetHeight,0);
	}
	else
	{
		el.style.visibility="visible";
		squish(el,w,h,0,el.width,0,el.height);
	}
}

function addLoadingImg(id)
{
	var target=$(id);
	if(target)
	{
		var loading=document.createElement("img");
		loading.setAttribute("src","images/loading.gif");
		loading.setAttribute("id","loadingImg");
		loading.style.position="absolute";
		loading.style.left=Math.round(target.offsetWidth/2)-16+'px';
		loading.style.top=Math.round(target.offsetHeight/2)+20+'px';
		changeOpacity(loading,0.5);
		target.appendChild(loading);
		return true;
	}
	return false;
}

function removeLoadingImg()
{
	var loading=$("loadingImg");
	if(loading)
	{
		loading.parentNode.removeChild(loading);
		return true;
	}
	return false;
}

function mousemove(event)
{
	if(typeof event == "undefined")
		event = window.event;
	var cursorPosition = [0, 0];
	var scrollingPosition = getScrollingPosition();
//	if(typeof event.pageX == "undefined")
//	{
		cursorPosition[0] = event.clientX + scrollingPosition[0];
		cursorPosition[1] = event.clientY + scrollingPosition[1];
//	}
	var target = document.currentTarget;
	var viewportSize = getViewportSize();
	var tip=$("currentTooltip");
	if(tip)
	{
		positionTip(tip,cursorPosition,scrollingPosition,viewportSize);
	}
	else
	{
		target.style.left = cursorPosition[0] + target.differenceX + "px";
		target.style.top = cursorPosition[1] + target.differenceY + "px";
	}
	stopDefaultAction(event);
	return true;
}