window.addNamespace("MS");
Object.extend(MS, {
	setText: function(ele, text) {
		if(ele == null) { return; }
		if(document.all) {
			ele.innerText = text;
		} else {
			ele.textContent = text;
		}
	},
	setHtml: function(ele, html) {
		if(ele == null) { return; }
		ele.innerHTML = html;
	},
	cancelEvent: function(e) {
		e = MS.getEvent(e);
		if(window.event) {
			e.returnValue = false;
		} else if(e) {
			e.preventDefault();
			e.stopPropagation();
		}
	},
	getEvent: function(e) {
		if(window.event) { return window.event; }
		if(e) { return e; }
		return null;
	},
	getTarget: function(e) {
		e = MS.getEvent(e);
		if(window.event) { return e.srcElement; }
		if(e) { return e.currentTarget; }
	}
}, false);

window.addNamespace("MS.Browser");
Object.extend(MS.Browser, {
	isIE: navigator.userAgent.indexOf('MSIE') != -1,
	isFirefox: navigator.userAgent.indexOf('Firefox') != -1,
	isOpera: window.opera != null
}, false);

window.addNamespace("MS.Position");
Object.extend(MS.Position, {
	getLocation: function(ele) {
		var x = 0;
		var y = 0;
		var p;
		for(p=ele; p; p=p.offsetParent) {
			//if(p.style.position == "relative" || p.style.position == "absolute") break;
			if(p.offsetLeft) { x += p.offsetLeft; }
			if(p.offsetTop) { y += p.offsetTop;}
		}
		return {left:x,top:y};
	},
	getParent: function(ele) {
		var p;
		for(p=ele; p; p=p.offsetParent) {
			if (p.tagName.toLowerCase() == "div" || p.tagName.toLowerCase() == "td" || p.tagName.toLowerCase() == "th") {
				return p;
			}		
		}
	},
	getBounds: function(ele) {
		var offset = MS.Position.getLocation(ele);
		var width = ele.offsetWidth;
		var height = ele.offsetHeight;
		return {left:offset.left,top:offset.top,width:width,height:height};
	},
	setLocation: function(ele, loc) {
		ele.style.position = "absolute";
		ele.style.left = loc.left + "px";
		ele.style.top = loc.top + "px";
	},
	setBounds: function(ele, rect) {
		if(rect.left && rect.top) {
			MS.Position.setLocation(ele, rect);
		}
		ele.style.width = rect.width + "px";
		ele.style.height = rect.height + "px";
	},
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var iwindowWidth, iwindowHeight;
		if (self.innerHeight) {	// all except Explorer
			iwindowWidth = self.innerWidth;
			iwindowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			iwindowWidth = document.documentElement.clientWidth;
			iwindowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			iwindowWidth = document.body.clientWidth;
			iwindowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < iwindowHeight){
			ipageHeight = iwindowHeight;
		} else { 
			ipageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < iwindowWidth){	
			ipageWidth = iwindowWidth;
		} else {
			ipageWidth = xScroll;
		}

		return {pageWidth:ipageWidth,pageHeight:ipageHeight,windowWidth:iwindowWidth,windowHeight:iwindowHeight};
	},
	getPageScroll:function (){
		var yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		return yScroll;
	},
	getTop: function(ele) {
	   if (!ele && this) ele = this; 
	   
	   var bIE = document.all ? true : false; 
	   var nTopPos = ele.offsetTop;         
	   var eParElement = ele.offsetParent;  

	   while (eParElement != null) {                                         
		  if(bIE) {
			 if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") ) {                                   
				nTopPos += eParElement.clientTop; 
			 }
		  }
		  else {
			 if(eParElement.tagName == "TABLE") {                                   
				var nParBorder = parseInt(eParElement.border);
				if(isNaN(nParBorder)) {                                
				   var nParFrame = eParElement.getAttribute('frame');
				   if(nParFrame != null) {
					  nTopPos += 1;              
				   }
				}
				else if(nParBorder > 0) {
				   nTopPos += nParBorder;        
				}
			 }
		  }

		  nTopPos += eParElement.offsetTop;      
		  eParElement = eParElement.offsetParent; 
	   }                                         
	   return nTopPos;                           
	},
	getLeft: function(ele) {
	   if (!ele && this) ele = this;
	   
	   var bIE = document.all ? true : false; 
	   var nLeftPos = ele.offsetLeft;       
	   var eParElement = ele.offsetParent;  

	   while (eParElement != null) {                                         
		  if (bIE) {
			 if( (eParElement.tagName != "TABLE") && (eParElement.tagName != "BODY") ) {                                   
				nLeftPos += eParElement.clientLeft; 
			 }
		  }
		  else {
			 if(eParElement.tagName == "TABLE") {                                   
				var nParBorder = parseInt(eParElement.border);
				if(isNaN(nParBorder)) {                                
				   var nParFrame = eParElement.getAttribute('frame');
				   if(nParFrame != null) {
					  nLeftPos += 1;             
				   }
				}
				else if(nParBorder > 0) {
				   nLeftPos += nParBorder;       
				}
			 }
		  }
		  nLeftPos += eParElement.offsetLeft;    
		  eParElement = eParElement.offsetParent; 
	   }                                         
	   return nLeftPos; 
	}
}, false);

window.addNamespace("MS.Keys");
Object.extend(MS.Keys, {
	TAB: 9,
	ESC: 27,
	KEYUP: 38,
	KEYDOWN: 40,
	KEYLEFT: 37,
	KEYRIGHT: 39,
	SHIFT: 16,
	CTRL: 17,
	ALT: 18,
	ENTER: 13,
	BACKSPACE: 8,
	DEL: 46,
	getCode: function(e) {
		e = MS.getEvent(e);
		if(e != null) { return e.keyCode; }
		return -1;
	}
}, false);

window.addNamespace("StringBuilder");
StringBuilder = window.Class.create();
Object.extend(StringBuilder, {
	v:[],
	initialize:function(){
	}
}, true);

Object.extend(StringBuilder.prototype, {
	append: function(s) {
		this.v.push(s);
	},
	appendLine: function(s) {
		this.v.push(s + "\r\n");
	},
	clear: function() {
		this.v.clear();
	},
	toString: function() {
		return this.v.join("");
	}
}, true);
