function $(elem)
{
	el =  document.getElementById(elem);
	el = (!el)? document.body : el;
	return el;
}

bodyonload = new Array();
bodymousemove = new Array();
bodymouseup = new Array();

doc_loaded = false;

function addToBodyOnLoad(code)
{
	bodyonload.push(code);
}

function addToBodyMouseMove(code)
{
	bodymousemove.push(code);
}

function addToBodyMouseUp(code)
{
	bodymouseup.push(code);
}

function Body_OnLoad(event)
{
	doc_loaded = true;
	for(__i=0; __i < bodyonload.length; __i++)
	{
		bol = bodyonload[__i]; 
		bol(event);
	}
}

function Body_OnMouseMove(event)
{
	if(!doc_loaded) return true;

	updateMousePos(event);
	for(i=0; i < bodymousemove.length; i++)
	{
		bodymousemove[i](event);
	}
}

function Body_OnMouseUp(event)
{
	if(!doc_loaded) return true;

	updateMousePos(event);
	for(i=0; i < bodymouseup.length; i++)
	{
		bodymouseup[i](event);
	}
}

var elem_z = 10000;

function ElementToFront(element)
{
	elem_z++;
	element.style.zIndex = elem_z;
}

var mouse_pos_x = 0;
var mouse_pos_y = 0;

function updateMousePos(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	mouse_pos_x = posx;
	mouse_pos_y = posy;
}

function ClientWindowWidth() {
	  var myWidth = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	  }
	  return myWidth;
}

function ClientWindowHeight() {
	  var myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	  }
	  return myHeight;
}


function moveToVisible(element) {
	
	scrollY = document.body.scrollTop
			+ document.documentElement.scrollTop;
	
	winHeight = ClientWindowHeight();
	
	var top = parseInt(element.style.top);
	
	if(top < scrollY || top > scrollY + winHeight)
	{
		element.style.top = (scrollY + 10) + 'px';
	} 				
}

function getEventTarget(e)
{
	var targ;
	if (!e)
  	{
  		var e=window.event;
  	}
	if (e.target)
  	{
  		targ=e.target;
  	}
	else if (e.srcElement)
  	{
  		targ=e.srcElement;
  	}
	if (targ.nodeType==3) // defeat Safari bug
  	{
  		targ = targ.parentNode;
  	}	
  	return targ;
}


function SetCookie(cname,value,expireD)
{
   var exd=new Date();
   exd.setDate(exd.getDate()+expireD);
   document.cookie=cname+ "=" +escape(value)+
   ((expireD==null) ? "" : ";expires="+exd.toGMTString());
}      
    
function GetCookie(cname)
{
   if (document.cookie.length>0)
   {
      cstart = document.cookie.indexOf(cname + "=");
      if (cstart!=-1)
      { 
          cstart = cstart + cname.length + 1; 
          cend = document.cookie.indexOf(";",cstart);
          if (cend == -1) cend=document.cookie.length;
          return unescape(document.cookie.substring(cstart,cend));
      } 
   }
   return "";
}

function SaveCompState(compname, compprop, compvalue)
{
   SetCookie(escape('CMPPersist-'+compname+'-prop-'+compprop),'='+compvalue,null);
}      

function LoadCompState(compname, compprop, defvalue)
{
   value=GetCookie(escape('CMPPersist-'+compname+'-prop-'+compprop));
   if(value==""){return defvalue;} else
   {
      return value.substring(1,value.length);
   }
}



