String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;

function Menu(id, startX, startY, operationMode, leverSize)
{
  this.menu = new Animation(id);
  this.collapsed = true;
  this.toggle = toggle;
  this.mode = (operationMode ? operationMode : 'L');
  this.pullSize = (leverSize ? leverSize : 16);
  
  startX = (startX ? startX : 0);
  startY = (startY ? startY : 0);
  this.menu.moveTo(startX, startY);
  
  switch(this.mode)
  {
    case 'T':
      this.menu.slideBy(0, 0-this.menu.height()+this.pullSize, 20, 10);  
      break;
    default:
      this.menu.slideBy(0-this.menu.width()+this.pullSize, 0, 20, 10);  
  }
  
  function toggle()
  {
    if (this.collapsed)
    {
      if (this.mode=='T')
        this.menu.slideBy(0, this.menu.height()-this.pullSize, 20, 10);
      else
        this.menu.slideBy(this.menu.width()-this.pullSize, 0, 20, 10);
      this.collapsed = false;
    }
    else
    {
      if (this.mode=='T')
        this.menu.slideBy(0, 0-this.menu.height()+this.pullSize, 20, 10);
      else
        this.menu.slideBy(0-this.menu.width()+this.pullSize, 0, 20, 10);
      this.collapsed = true;
    }
	return this.collapsed;
  }
  
}

function Animation(id) {
  this.element = (NS4) ? document[id] : document.all[id];
  this.styleElement = (NS4) ? document[id] : document.all[id].style;
  this.active = 0;
  this.timer = null;
  this.path = null;
  this.num = null;

  this.name = id + "Var";
  eval(this.name + " = this");

  this.animate = animate;
  this.step = step;
  this.show = show;
  this.hide = hide;
  this.left = left;
  this.top = top;
  this.moveTo = moveTo;
  this.slideBy = slideBy;
  this.slideTo = slideTo;
  this.circle = circle;
  this.width = width;
  this.height = height;
  this.grow = grow;
}

function pos(x, y, w, h) {
  this.x = Math.round(x);
  this.y = Math.round(y);
  this.w = w ? Math.round(w) : -1;
  this.h = h ? Math.round(w) : -1;
}

function show() {
  this.styleElement.visibility = (NS4) ? "show" : "visible";
}

function hide() {
  this.styleElement.visibility = (NS4) ? "hide" : "hidden";
}

function left() {
  var l = parseInt(this.styleElement.left);
  if (isNaN(l))
  {
    l = findPosX(this.element);
  }
  return l;
}

function top() {
  var t = parseInt(this.styleElement.top);
  if (isNaN(t))
  {
    t = findPosY(this.element);
  }
  return t;
}

function width() {
  return parseInt(this.element.offsetWidth);
}

function height() {
  return parseInt(this.element.offsetHeight);
}


function moveTo(x, y) {
  this.styleElement.left = x;
  this.styleElement.top = y;
}

function step() {
  this.moveTo(this.path[this.num].x, this.path[this.num].y);
  if (this.path[this.num].w > -1) this.styleElement.width  = this.path[this.num].w;
  if (this.path[this.num].h > -1) this.styleElement.height  = this.path[this.num].h;
  if (this.num >= this.path.length - 1) {
    clearInterval(this.timer);
    this.active = 0;
    if (this.statement)
      eval(this.statement);
  } else {
    this.num++;
  }
}

function animate(interval) {
  if (this.active) return;
  this.num = 0;
  this.active = 1;
  this.timer = setInterval(this.name + ".step()", interval);
}

function slideBy(dx, dy, steps, interval, statement) {
  var fx = this.left();
  var fy = this.top();
  var tx = fx + dx;
  var ty = fy + dy;
  this.slideTo(tx, ty, steps, interval, statement);
}

function slideTo(tx, ty, steps, interval, statement) {
  var fx = this.left();
  var fy = this.top();
  var dx = tx - fx;
  var dy = ty - fy;
  var sx = dx / steps;
  var sy = dy / steps;

  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    fx += sx;
    fy += sy;
    ar[i] = new pos(fx, fy);
  }
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}

function grow(tw, th, steps, interval, statement)
{
  var fw = this.width();
  var fh = this.height();
  var dw = (tw > fw) ? tw - fw : fw - tw;
  var dh = (th > fh) ? th - fh : fh - th;
  var sw = dw / steps;
  var sh = dh / steps;
  var tx = this.left();
  var ty = this.top();
  
  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    fw += sw;
    fh += sh;
    ar[i] = new pos(tx, ty, fw, fh);
  }
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}

function circle(radius, angle0, angle1, steps, interval, statement) {
  var dangle = angle1 - angle0;
  var sangle = dangle / steps;
  var x = this.left();
  var y = this.top();
  var cx = x - radius * Math.cos(angle0 * Math.PI / 180);
  var cy = y + radius * Math.sin(angle0 * Math.PI / 180);

  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    angle0 += sangle;
    x = cx + radius * Math.cos(angle0 * Math.PI / 180);
    y = cy - radius * Math.sin(angle0 * Math.PI / 180);
    ar[i] = new pos(x, y);
  }
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}




function ToggleView(id, count){
	var control=null;
	control=Helper.GetElementByID('T'+id);
	if (control){
	  if(control.className == 'groupClosed') control.className = 'groupOpen';
	  else  control.className = 'groupClosed';
	//	if (control.style.backgroundColor=='lightblue') control.style.backgroundColor='#EEEEEE'; 
	//	else control.style.backgroundColor='lightblue';
	}
	for(var i=0;i<count;i++){ 
		control=Helper.GetElementByID(id+i);
		if (control){
			if (control.style.display==''){ control.style.display='none'; } else { control.style.display= ''; }
		}
	}
}

function SelectGroup(chk, id, count){
	for(var i=0;i<count;i++){ 
		control=Helper.GetElementByID('C'+id+i);
		if (control){
			control.checked = chk.checked;
		}
	}
}

function Callback(obj)
{
	var o = eval(obj);
	alert(typeof( window[ 'callbackObject' ] ));
	if ( typeof( window[ 'callbackObject' ] ) != "undefined" )
	{
			if(callbackObject.Execute) callbackObject.Execute(o);
	}
}
// End Animation 

// Helper 

var Helper = new helper();

function helper()
{
	this.GetElementByID = function(id) {
      if(document.getElementById) {
          return document.getElementById(id);
      } else if(document.all) {
          return document.all[id];
      }
      return;
    }
	this.Ranges = new Range();
	this.Selection = new Selection();
	this.Request = new request();

	function Range()
	{
		this.Create =  function() {
	      if(document.body && document.body.createTextRange) {
	        return document.body.createTextRange(); 
	      } else if(document.createRange) {
	        return document.createRange();
	      }
	      return;
	    }
		this.SelectNode = function(node) {
	      var rng = this.Create(), method = '';
	      if(rng.moveToElementText) { method = 'moveToElementText'; }
	      else if(rng.selectNode) { method = 'selectNode'; }
	      if(method){
	          rng = rng || Helper.Ranges.Create();
	          rng[method](node);
	          return rng;
	        }
	      return;
	    }
	}
	function Selection()
	{
		this.Clear = function() {
	      if( typeof document.selection !== 'undefined' ) {
	          if(document.selection && document.selection.empty) {
	             document.selection.empty(); 
				}
	        
	      } else if(window.getSelection) {
	        var sel = window.getSelection();
	        if(sel.removeAllRanges) {
				sel.removeAllRanges();
	          }
			sel = null;
	      }
		  return;    
	    }
		
	    this.Add = function(range) {
			var selection;
			if (range)
			{
		      if(range.select) {
		        range.select(); 
		      } else if(selection = window.getSelection) {
		        if(selection.addRange) {
		          selection.addRange(range); 
		        }
		      }
			}
			selection = null;
			return;
	    }  
	}
	function request()
	{
		this.xmlhttp=false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				this.xmlhttp = false;
			}
		}
		@end @*/
		if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
			try {
				this.xmlhttp = new XMLHttpRequest();
			} catch (e) {
				this.xmlhttp=false;
			}
		}
		if (!this.xmlhttp && window.createRequest) {
			try {
				this.xmlhttp = window.createRequest();
			} catch (e) {
				this.xmlhttp=false;
			}
		}
		this.GET = function(url, callback){
			var httpObj = this.xmlhttp;
			var responseHandler = this.HandleResponse;
			if (this.xmlhttp)
			{
				this.xmlhttp.open("GET", url, true);
				this.xmlhttp.onreadystatechange = function() {
					if(responseHandler) responseHandler(httpObj, callback);
				}
				//xmlhttp.setRequestHeader('Accept','message/x-jl-formresult')
				this.xmlhttp.setRequestHeader('Accept','object/js-result')
				this.xmlhttp.send();
			}
			else if (callback)
			{
				callback(null);
			}
		}
		this.HandleResponse = function(xmlhttp, callback)
		{
			if (xmlhttp && xmlhttp.readyState==4) {
				if (callback) callback(new Response(xmlhttp.responseText, 
								xmlhttp.getAllResponseHeaders(), xmlhttp.status)
							);
			}
		}
		this.POST = function(url, data, callback){
			var httpObj = this.xmlhttp;
			var responseHandler = this.HandleResponse;
			
			if (this.xmlhttp)
			{
				this.xmlhttp.open("POST", url, true);
				this.xmlhttp.onreadystatechange=function() {
					if(responseHandler) responseHandler(httpObj, callback);
				}
				this.xmlhttp.setRequestHeader('Accept','object/js-result')
				this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
				this.xmlhttp.setRequestHeader("Content-length", data.length); 
				this.xmlhttp.setRequestHeader("Connection", "close"); 
				this.xmlhttp.send(data);
			}
			else if (callback)
			{
				callback(null);
			}
		}
	}

	function Response(responseText, headers, status)
	{
		this.responseText = responseText;
		this.headers = headers;
		this.status = status;
		this.GetHeader = function(header){
			var result = '';
			var start = this.headers.indexOf(header);
			if (start > -1)
			{
				if (start == 0 || (start > 0 && (this.headers.substr(start-1,1) == '\n')))
				{
					if (this.headers.substr(start + header.length,1) == ':')
					{
						result = this.headers.substring(start + header.length + 1, this.headers.indexOf('\n', start)-1).trim();
					}
				}
			}
			return result;
		}
	}
}


// End Helper 
  
//Summary: Used to get the X coordinate of an element 
 
function findPosX(obj) 
{ 
    var curleft = 0; 
     
    if (obj.offsetParent) 
    { 
        while (obj.offsetParent) 
        { 
            curleft += obj.offsetLeft; 
            obj = obj.offsetParent; 
        } 
    } 
    else if (obj.x) 
        curleft += obj.x; 
         
    return curleft; 
} 

// Summary: Used to get the Y coordinate of an element 

function findPosY(obj) 
{ 
    var curtop = 0; 
     
    if (obj.offsetParent) 
    { 
        while (obj.offsetParent) 
        { 
            curtop += obj.offsetTop 
            obj = obj.offsetParent; 
        } 
    } 
    else if (obj.y) 
        curtop += obj.y; 
     
    return curtop; 
}

//alert('here ' + Helper)
function SetInnerHTML(id, val)
{
	if (Animation)
	{
		var obj = new Animation(id);
		if (obj && obj.element)
		{
			obj.element.innerHTML = val;
		}
	}	
}

function Hide(id)
{
	if (Animation)
	{
		var obj = new Animation(id);
		if (obj)
		{
			obj.hide();
		}
	}	
}
