function BaseFun() {}

BaseFun.last_status = null;
BaseFun.curr_status = null;
BaseFun.draggerLeft = 0;
BaseFun.draggerTop  = 0;


BaseFun.setStatus = function(_status)
{
  BaseFun.last_status = BaseFun.curr_status;
  BaseFun.curr_status = _status;
}

BaseFun.getStatus = function()
{
  return BaseFun.curr_status;
}

BaseFun.getLastStatus = function()
{
  return BaseFun.last_status;
}

BaseFun.beMoving = function()
{
  return (BaseFun.curr_status != null && BaseFun.curr_status == "dragger") ;
}

BaseFun.getAssignDiv = function(DivID)
{
  var div = document.getElementById (DivID);
  
  if(div == null)
  {
    div = document.createElement ("div");
    div.id = DivID;
  }
  
  return div;
}

BaseFun.setAssignDivForMove = function (DivID)
{
  var mainDiv = document.getElementById(DivID);
  
  if(mainDiv)
  {
    var div = BaseFun.getAssignDiv(DivID+"_moveDiv");
    div.style.backgroundColor = "#cccccc";
    div.style.width = mainDiv.style.width;
    div.style.height = 20;
    div.innerHTML = "&nbsp;";
    
    div.onmousedown = function ()
  	{
  	  if(event.button == 1)
  	  {
    		BaseFun.setStatus ("dragger");
    		BaseFun.draggerLeft = parseInt (event.clientX);
    		BaseFun.draggerTop  = parseInt (event.clientY);
    		this.setCapture ();
    		this.style.cursor = "move";
    	}
  	}
	  
  	div.onmouseup = function ()
  	{
  	  if(event.button == 1)
  	  {
    		BaseFun.setStatus (null);
    		this.releaseCapture ();
    		this.style.cursor = "default";
    	}
  	}
  	
  	div.onmousemove = function ()
  	{
  		if (BaseFun.beMoving() && event.button == 1)
  		{
  			var x = parseInt(this.parentNode.style.left) + parseInt (event.clientX) - BaseFun.draggerLeft;
  			var y = parseInt(this.parentNode.style.top)  + parseInt (event.clientY) - BaseFun.draggerTop;
  			
  			BaseFun.draggerLeft = parseInt (event.clientX);
  			BaseFun.draggerTop  = parseInt (event.clientY);
  			
  			if (x < 0)
  				x = 0;
  			else if (x +this.parentNode.offsetWidth/2 > document.body.clientWidth)
  				x = document.body.clientWidth - this.parentNode.offsetWidth/2;
  			
  			if (y < 0)
  				y = 0;
  			else if (y + this.parentNode.offsetHeight/2 > document.body.clientHeight + document.body.scrollTop)
  				y = document.body.clientHeight + document.body.scrollTop - this.parentNode.offsetHeight/2;
  			
  			this.parentNode.style.left = x;
  			this.parentNode.style.top  = y;
  		}
  	}
  	
    mainDiv.appendChild (div);
  }
}

BaseFun.hideAssignDiv = function(DivID)
{
  var div = document.getElementById (DivID);
  
  if(div)
  {
    div.style.display = "none";
    div.innerHTML = "";
  }
  
  return div;
}

BaseFun.replaceAll = function(str,oldstr,newstr)
{
  return str.replace(new RegExp(oldstr,"gm"),newstr);
}

BaseFun.trim = function(str)
{
  return str.replace(/(^\s*)|(\s*$)/g,  "");
}

function StringBuffer()
{
  this.sb = new Array;
  
  this.append = function(str)
  {
    this.sb[this.sb.length] = str;
  }
  
  this.toString = function()
  {
    return this.sb.join ("");
  }
}

if(typeof(Number.prototype.toFixed)!="function") 
{ 
	Number.prototype.toFixed = function(d) 
	{ 
		var s=this+"";if(!d)d=0; 
		if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0"); 
		if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s)) 
		{ 
		var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true; 
		if (a==d+2){a=s.match(/\d/g); if (parseInt(a[a.length-1])>4) 
		{ 
		for(var i=a.length-2; i>=0; i--) {a[i] = parseInt(a[i])+1; 
		if(a[i]==10){a[i]=0; b=i!=1;} else break;} 
		} 
		s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2"); 
		}if(b)s=s.substr(1);return (pm+s).replace(/\.$/, "");} return this+""; 
	}; 
} 

if (![].push) Array.prototype.push = function() {
    for (var i = 0; i < arguments.length; i++) {
      this[this.length] = arguments[i];
    }
    return this.length;
  };
 
if (![].pop) Array.prototype.pop = function() {
    var $item = this[this.length - 1];
    this.length--;
    return $item;
  };
