/********************************************************************************
Extention to JS 
********************************************************************************/
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(val, fromIndex) {
		if (typeof(fromIndex) != 'number') fromIndex = 0;
		for (var index = fromIndex,len = this.length; index < len; index++)
			if (this[index] == val) return index;
		return -1;
	}
}

/********************************************************************************
Class WSDomUtils
********************************************************************************/
var WSDomUtils = {
	addEvent: function(obj, evType, fn) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, false);
			return true;
		}
		else if (obj.attachEvent) {
			var r = obj.attachEvent("on" + evType, fn);
			return r;
		}
		return false;
	},
	//--------------------------------------------------------------------------------------
	removeEvent: function(obj, evType, fn) {
		if (obj.removeEventListener) {
			obj.removeEventListener(evType, fn, false);
			return true;
		} else if (obj.dispatchEvent) {
			var r = obj.detachEvent("on" + evType, fn);
			return r;
		} else {
			return false;
		}
	},
	//--------------------------------------------------------------------------------------
	escapeHTML: function(str) {
		var div = document.createElement('div');
		var text = document.createTextNode(str);
		div.appendChild(text);
		return div.innerHTML;
	},
	//--------------------------------------------------------------------------------------
	getStyle: function(elm,sStyle)
	{
		if (!elm) return false;
		var val = null;
		if (elm.currentStyle) { //IE
			sStyle = sStyle.replace(/-(\D)/g, function(a,b) { return b.toUpperCase(); } );
			val = elm.currentStyle[sStyle];
		}	
		else if (window.getComputedStyle && elm.ownerDocument.defaultView) { //Mozzila etc
			val = elm.ownerDocument.defaultView.getComputedStyle(elm,null).getPropertyValue(sStyle);
		}	
		return val;
	},
	//--------------------------------------------------------------------------------------	
	/*
	getStyle: function(elm, na) {
		if (!elm) return false;
		if (WSUtils.isGecko && elm.ownerDocument.defaultView) {
			try {
				return elm.ownerDocument.defaultView.getComputedStyle(elm, null).getPropertyValue(na);
			} catch(elm) {
				return null;
			}
		}
		na = na.replace(/-(\D)/g, function(a,b) { return b.toUpperCase(); } );
		if (elm.currentStyle) {
			return elm.currentStyle[na];
		}
		return false;
	},
	*/
	//--------------------------------------------------------------------------------------
	getElementMargin: function(elm)
	{
		var margin = {left: 0, right: 0, top: 0, bottom: 0};
		margin.left = parseInt(this.getStyle(elm,"margin-left"));
		margin.right = parseInt(this.getStyle(elm,"margin-right"));
		margin.top = parseInt(this.getStyle(elm,"margin-top"));
		margin.bottom = parseInt(this.getStyle(elm,"margin-bottom"));
		return margin;
	},
	//--------------------------------------------------------------------------------------
	hideElement: function(oElement) {
		if (oElement) {
			oElement.style.display = "none";
			oElement.style.visibility = "hidden";
			//oElement.innerHTML = "";
		}
	},
	//--------------------------------------------------------------------------------------
	showElement: function(oElement) {
		if (oElement) {
			oElement.style.display = "block";
			oElement.style.visibility = "visible";
		}
	},
	//--------------------------------------------------------------------------------------
	isVisible:  function(oElement) {
		return ((oElement.style.display!="" && oElement.style.display!="none") || 
				(oElement.style.visibility!="" && oElement.style.visibility!="hidden"));
	},
	//--------------------------------------------------------------------------------------
	toggleVisibility:  function(oElement) {
		if (this.isVisible(oElement)) {
			this.hideElement(oElement);
		}	
		else {
			this.showElement(oElement);
		}
	},
	//--------------------------------------------------------------------------------------	
	getEventElement: function(e) {
		var targ = null;
		if (!e) {
			e = window.event;
			if (!e) {
				return null;
			}
		}
		if (e.target) {
			targ = e.target;
		} else if (e.srcElement) {
			targ = e.srcElement;
		}
		if (targ.nodeType == 3) {
			targ = targ.offsetParent;
		}
		return targ;
	},
	//--------------------------------------------------------------------------------------	
	/*
	getElementValueById:  function(sMessage, tagID) {
		var tempMsg = "";

		var start_idx = sMessage.indexOf('"');
		var end_idx  = sMessage.lastIndexOf('"');
		if (start_idx>0 && end_idx>0) {
			++start_idx;
			tempMsg = sMessage.substr(start_idx, end_idx - start_idx);
		}
		else {
			tempMsg = sMessage;
		}
		var tmp_elm = document.createElement('div');
		tmp_elm.innerHTML = tempMsg;
		var nodes = tmp_elm.childNodes;
		for (var i=0; i<nodes.length; ++i) {
			alert("nodes["+i+"]=" + nodes[i].id);
			if (nodes[i].id == tagID) { 
				tempMsg = nodes[i].innerHTML;
				break;
			}
		}
		delete tmp_elm;
		//alert("tempMsg = " + tempMsg);
		return tempMsg;
	}*/
	//--------------------------------------------------------------------------------------	
	getElementValueById:  function(sMessage, tagName, tagID) {
		var tempMsg = "";

		var start_idx = sMessage.indexOf('"');
		var end_idx  = sMessage.lastIndexOf('"');
		if (start_idx>0 && end_idx>0) {
			++start_idx;
			tempMsg = sMessage.substr(start_idx, end_idx - start_idx);
		}
		else {
			tempMsg = sMessage;
		}
		var tmp_elm = document.createElement('div');
		tmp_elm.innerHTML = tempMsg;
		var nodes = tmp_elm.getElementsByTagName(tagName);
		for (var i=0; i<nodes.length; ++i) {
			//alert("nodes["+i+"]=" + nodes[i].id);
			//if (nodes[i].className == tagClassName) { 
			if (nodes[i].id == tagID) { 
				tempMsg = nodes[i].innerHTML;
				break;
			}
		}
		delete tmp_elm;
		//alert("tempMsg = " + tempMsg);
		return tempMsg;
	}
	//--------------------------------------------------------------------------------------		
}

/********************************************************************************
Class WSUtils
********************************************************************************/
var WSUtils = {
	//ua: navigator.userAgent,
	isMSIE: (navigator.appName == "Microsoft Internet Explorer"),
	isMSIE5: (this.isMSIE && (navigator.userAgent.indexOf('MSIE 5') != -1)),
	isMSIE5_0: (this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1)),
	isMSIE7: (this.isMSIE && (navigator.userAgent.indexOf('MSIE 7') != -1)),
	isMSIE8: (this.isMSIE && (navigator.userAgent.indexOf('MSIE 8') != -1)),
	isGecko: (navigator.userAgent.indexOf('Gecko') != -1),
	isSafari: (navigator.userAgent.indexOf('Chrome') != -1),
	isChrome: (navigator.userAgent.indexOf('Safari') != -1),
	isOpera: (window['opera'] && opera.buildNumber ? true: false),
	isMac: (navigator.userAgent.indexOf('Mac') != -1),
	isNS7: (navigator.userAgent.indexOf('Netscape/7') != -1),
	isNS71: (navigator.userAgent.indexOf('Netscape/7.1') != -1),
	//--------------------------------------------------------------------------------------
	zeroBounds: function() {
		return {x: 0, y: 0,	w: 0, h: 0};
	},
	//--------------------------------------------------------------------------------------	
	zeroPoint: function() {
		return {x: 0, y: 0};
	},
	//--------------------------------------------------------------------------------------	
	absolutize: function(element) {
		if (element && element.style) {
			if (element.style.display == 'none') {
				element.style.display = '';
			}
		}
	},
	//--------------------------------------------------------------------------------------
	html2Text: function(sHTML)
	{
		var sText = "";
		var nPos = sHTML.indexOf("<body");
		if (!nPos || nPos<0)
		{
			nPos = sHTML.indexOf("<BODY");
			if (!nPos || nPos<0)
			{
				nPos = 0;
			}
		}
		sText = sHTML.substr(nPos);
		sText = sText.replace(/\r\n|\n/ig,"");
		sText = sText.replace(/<p>/ig,"\r\n\r\n");
		//sText = sText.replace(/<br>|<div>|<\/p>/ig,"\r\n");
		sText = sText.replace(/<br>/ig,"\r\n");
		sText = sText.replace(/\&amp;/ig,"&");
		sText = sText.replace(/\&quot;/ig,"\"");
		sText = sText.replace(/\&nbsp;/ig," ");
		//removes all tags
		//sText = sText.replace(/<\/?\w+[^>]*>/ig, ""); //w=start with alpha or num
		sText = sText.replace(/<\/?[^>]+>/ig, "");
		//sText = sText.replace(/<[\w\/g]+[^<>]*>/ig, ""); //replace(/<&#91;^>&#93;*>/g, "");
		//alert("after removing all tags\n" + sText);
		//alert("in html2Text:\n\nsHTML=[" + sHTML + "]\n\nsText=[" + sText + "]");
		return sText;
	},
	//--------------------------------------------------------------------------------------	
	arrayFind: function(arr, val, fromIndex) {
		if (typeof(fromIndex) != 'number') fromIndex = 0;
		for (var index = fromIndex,len = arr.length; index < len; index++)
			if (arr[index] == val) return index;
		return -1;
	},
	//--------------------------------------------------------------------------------------	
	hasFrameFinishedLoaded: function(oFrame) {
		var has_loaded = false;
		if (typeof(oFrame.readyState) != "undefined") { //For IE
			has_loaded = (oFrame.readyState=='complete');
		}
		else {
			has_loaded =
				oFrame && 
				oFrame.contentWindow &&
				oFrame.contentWindow.document &&
				oFrame.contentWindow.document.body &&
				typeof(oFrame.contentWindow.document.body.innerHTML)=="string";
		}
		return has_loaded;
	},
	//--------------------------------------------------------------------------------------
	pageWidth: function(oWindow) {
		if (oWindow.innerWidth != null) {
			return oWindow.innerWidth;
		}
		else if (oWindow.document.documentElement && 
				 oWindow.document.documentElement.clientWidth) {
			return oWindow.document.documentElement.clientWidth;
		}	
		else if (oWindow.document.body != null) {
			return oWindow.document.body.clientWidth;
		}
		return null;
	},	
	//--------------------------------------------------------------------------------------
	pageHeight: function(oWindow) {
		if (oWindow.innerHeight != null) {
			return oWindow.innerHeight;
		}
		else if (oWindow.document.documentElement && 
				 oWindow.document.documentElement.clientHeight) {
			return oWindow.document.documentElement.clientHeight;
		}	
		else if (oWindow.document.body != null) {
			return oWindow.document.body.clientHeight;
		}
		return null;
	},
	//--------------------------------------------------------------------------------------
	posLeft: function(oWindow) {
		if (typeof oWindow.pageXOffset != 'undefined') {
			return oWindow.pageXOffset;
		}
		else if (oWindow.document.documentElement && 
				 oWindow.document.documentElement.scrollLeft) {
			return oWindow.document.documentElement.scrollLeft;
		}
		else if (oWindow.document.body.scrollLeft) {
			return oWindow.document.body.scrollLeft;
		}
		return 0;
	},
	//--------------------------------------------------------------------------------------
	posTop: function(oWindow) {
		if (typeof oWindow.pageYOffset != 'undefined') {
			return oWindow.pageYOffset;
		}
		else if (oWindow.document.documentElement && 
				 oWindow.document.documentElement.scrollTop) {
			return oWindow.document.documentElement.scrollTop;
		}
		else if (oWindow.document.body.scrollTop) {
			return oWindow.document.body.scrollTop;
		}
		return 0;
	},
 	//--------------------------------------------------------------------------------------
	getWindowBounds2: function(oWindow) {
		var b = this.zeroBounds();
		b.x = this.posLeft(oWindow);
		b.y = this.posTop(oWindow);
		b.w = this.pageWidth(oWindow);
		b.h = this.pageHeight(oWindow);
		return b;
	},
	//--------------------------------------------------------------------------------------		
	getScrollXY: function(oWindow) {
		var scrOfX = 0, scrOfY = 0;
		if (oWindow.document.body && 
				  (oWindow.document.body.scrollLeft || oWindow.document.body.scrollTop)) {
			//DOM compliant
			scrOfY = oWindow.document.body.scrollTop;
			scrOfX = oWindow.document.body.scrollLeft;
		} else if (typeof(oWindow.pageYOffset) == 'number') {
			//Netscape compliant
			scrOfY = oWindow.pageYOffset;
			scrOfX = oWindow.pageXOffset;
		} else if (oWindow.document.documentElement && 
				   (oWindow.document.documentElement.scrollLeft || oWindow.document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
			scrOfY = oWindow.document.documentElement.scrollTop;
			scrOfX = oWindow.document.documentElement.scrollLeft;
		}
		return {x: scrOfX, y: scrOfY};
	},
	//--------------------------------------------------------------------------------------			
	getWindowSize: function(oWindow) {
		var myWidth = 0, myHeight = 0;
		if (typeof(oWindow.innerWidth) == 'number') {
			//Non-IE
			myWidth = oWindow.innerWidth;
			myHeight = oWindow.innerHeight;
		} else if (oWindow.document.documentElement && 
				   (oWindow.document.documentElement.clientWidth || oWindow.document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			myWidth = oWindow.document.documentElement.clientWidth;
			myHeight = oWindow.document.documentElement.clientHeight;
		} else if (oWindow.document.body && 
				   (oWindow.document.body.clientWidth || oWindow.document.body.clientHeight)) {
			//IE 4 compatible
			myWidth = oWindow.document.body.clientWidth;
			myHeight = oWindow.document.body.clientHeight;
		}
		return {w: myWidth, h: myHeight};
	},
	//--------------------------------------------------------------------------------------
	getWindowBounds: function(oWindow) {
		var b = this.zeroBounds();
		if (oWindow.innerHeight) {
			b.x = parseInt(oWindow.pageXOffset);
			b.y = parseInt(oWindow.pageYOffset);
			b.w = parseInt(oWindow.innerWidth);
			b.h = parseInt(oWindow.innerHeight);
		}
		else if (oWindow.document.documentElement && 
				 oWindow.document.documentElement.clientHeight) {
			b.w = parseInt(oWindow.document.documentElement.clientWidth);
			b.h = parseInt(oWindow.document.documentElement.clientHeight);
			b.x = parseInt(oWindow.document.documentElement.scrollLeft);
			b.y = parseInt(oWindow.document.documentElement.scrollTop);
		}
		else if (oWindow.document.body) {
			b.w = parseInt(oWindow.document.body.clientWidth);
			b.h = parseInt(oWindow.document.body.clientHeight);
			b.x = parseInt(oWindow.document.body.scrollLeft);
			b.y = parseInt(oWindow.document.body.scrollTop);
		}
		return b;
	},
	//--------------------------------------------------------------------------------------
	getElementBounds_old: function(element, safe) {
		if (element) {
			var orig_element = element;
			this.absolutize(element);
			var bounds = this.zeroBounds();
			bounds.x = parseInt(element.offsetLeft);
			bounds.y = parseInt(element.offsetTop);
			bounds.w = parseInt(element.offsetWidth);
			bounds.h = parseInt(element.offsetHeight);
			if (element.tagName == "IFRAME") {
				var obj = element;
				var curleft = 0;
				var curtop = 0;
				if (obj.offsetParent) {
					while (obj.offsetParent) {
						curleft += obj.offsetLeft;
						curtop += obj.offsetTop;
						obj = obj.offsetParent;
					}
				}
				else {
					if (obj.x) {
						curleft += obj.x;
					}	
					if (obj.y) {
						curtop += obj.y;
					}	
				}
				bounds.x = curleft;
				bounds.y = curtop;
			}
			if (safe && /Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
				var valueT = 0,
				valueL = 0;
				var obj = element;
				do {
					valueT += obj.offsetTop || 0;
					valueL += obj.offsetLeft || 0;
					if (obj.offsetParent == document.body) {
						if (WSDomUtils.getStyle(obj,'position') == 'absolute')
							break;
					}
					obj = obj.offsetParent;
				} while ( obj );
				bounds.x = valueL;
				bounds.y = valueT;
			}
			//IE consider the margin when calculating element bounds, others not	
			if (! this.isMSIE) {
				var margin = WSDomUtils.getElementMargin(orig_element.ownerDocument.body);
				bounds.x += margin.left;
				bounds.y += margin.top;
				//bounds.w += margin.left + margin.right;
				//bounds.h += margin.top + margin.bottom;
			}
			return bounds;
		}
		return zeroBounds();
	},
	//--------------------------------------------------------------------------------------
	getElementPos: function(elm) {
		var finalPoint = this.zeroPoint();
		this.absolutize(elm);
		var obj = elm;
		if (obj.offsetParent) {
			do {
				finalPoint.x += parseInt(obj.offsetLeft);
				finalPoint.y += parseInt(obj.offsetTop);
				/*
				if (obj.offsetParent == document.body && 
					WSDomUtils.getStyle(obj,'position') == 'absolute')
						break;
				}
				*/
			} while (obj = obj.offsetParent)
		}
		else {
			if (obj.x)  finalPoint.x += parseInt(obj.x);
			if (obj.y) finalPoint.y += parseInt(obj.y);
		}
		/*
		if (! this.isMSIE) {
			var margin = WSDomUtils.getElementMargin(elm.ownerDocument.body);
			finalPoint.x += parseInt(margin.left);
			finalPoint.y += parseInt(margin.top);
		}
		*/
		return finalPoint;
	},
	//--------------------------------------------------------------------------------------
	getElementBounds: function(elm, safe) {
		//return this.getElementBounds2(elm, safe);
		var bounds = this.zeroBounds();
		var finalPoint = this.getElementPos(elm);
		bounds.x = parseInt(finalPoint.x);
		bounds.y = parseInt(finalPoint.y);
		bounds.w = parseInt(elm.offsetWidth);
		bounds.h = parseInt(elm.offsetHeight);

		return bounds;
	},
	//--------------------------------------------------------------------------------------
	calcCenteredPosition: function(oElement, oWindow) {
		var finalPoint   = this.zeroPoint();
		var windowBounds = this.getWindowBounds(oWindow, true);
		var elmBounds    = this.getElementBounds(oElement, true);

		finalPoint.x = windowBounds.x + (windowBounds.w - elmBounds.w)/2;
		finalPoint.y = windowBounds.y + (windowBounds.h - elmBounds.h)/2;
		//finalPoint.w = elmBounds.w;
		//finalPoint.h = elmBounds.h;

		return finalPoint;	
	},
	//--------------------------------------------------------------------------------------
	alert: function(obj) {
		if (obj) {
			var str = '{';
			for (var item in obj) {
				str += item + ": " + obj[item] + ", ";
			}
			alert(str + "}");
		}
		alert(obj);
	},
	//--------------------------------------------------------------------------------------
	isRightClick: function(e) {
		if (!e) e = window.event;
		var bRightClick = false;
		if (e.which) {
			bRightClick = (e.which == 2 || e.which == 3);
		} else if (e.button) {
			bRightClick = (e.button == 2)
		}
		return (bRightClick);
	},
	//--------------------------------------------------------------------------------------
	deprecationWarning: function() {
		alert("the called : \n " + this.deprecationWarning.caller + "\n is depricated; \n It is called from: \n" + this.deprecationWarning.caller.caller);
	},
	//--------------------------------------------------------------------------------------
	getMousePoint: function(e) {
		var p = this.zeroPoint();
		if (!e) var e = window.event;
		if (!e) {
			throw ("Error!!");
		}
		if (e.pageX || e.pageY) {
			p.x = e.pageX;
			p.y = e.pageY;
		} else if (e.clientX || e.clientY) {
			p.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			p.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return p;
	},
	//--------------------------------------------------------------------------------------
	getEscapedWord: function(sWord) {
		return sWord.replace(/\'/g, "\\'");
	},
	//--------------------------------------------------------------------------------------
	centerElement: function(oElement,oWindow) {
		var targetPos       = this.calcCenteredPosition(oElement,oWindow);
		oElement.style.left = (targetPos.x) + "px";
		oElement.style.top  = (targetPos.y) + "px";
		//oElement.style.width   = targetPos.w+"px";
		//oElement.style.height  = targetPos.h+"px";
	},
	//--------------------------------------------------------------------------------------	
	getScrollWidth: function() {
		//alert("window.pageXOffset = " + window.pageXOffset);
		//alert("document.body.scrollLeft = " + document.body.scrollLeft);
		//alert("document.documentElement.scrollLeft = " + document.documentElement.scrollLeft);
		
		var w = window.pageXOffset ||
				document.body.scrollLeft ||
				document.documentElement.scrollLeft;
		return w ? w : 0;
	},
	//--------------------------------------------------------------------------------------	
	getScrollHeight: function()	{
		alert("window.pageYOffset = " + window.pageYOffset);
		alert("document.body.scrollTop = " + document.body.scrollTop);
		alert("document.documentElement.scrollTop = " + document.documentElement.scrollTop);

		var h = window.pageYOffset ||
			   document.body.scrollTop ||
			   document.documentElement.scrollTop;
			   
	   return h ? h : 0;
	},
	//--------------------------------------------------------------------------------------
	calcScrollbarSize: function() {
		//This function calculates window.scrollbarWidth and window.scrollbarHeight
		//This must be called “onload” to work correctly (or on “DOM ready”, if you're using
		//a framework that provides such an event)

		var i = document.createElement('p');
		i.style.width = '100%';
		i.style.height = '200px';

		var o = document.createElement('div');
		o.style.position = 'absolute';
		o.style.top = '0px';
		o.style.left = '0px';
		o.style.visibility = 'hidden';
		o.style.width = '200px';
		o.style.height = '150px';
		o.style.overflow = 'hidden';
		o.appendChild(i);

		document.body.appendChild(o);
		var w1 = i.offsetWidth;
		var h1 = i.offsetHeight;
		o.style.overflow = 'scroll';
		var w2 = i.offsetWidth;
		var h2 = i.offsetHeight;
		if (w1 == w2) w2 = o.clientWidth;
		if (h1 == h2) h2 = o.clientWidth;

		document.body.removeChild(o);

		//window.scrollbarWidth = w1-w2;
		//window.scrollbarHeight = h1-h2;
		return {w: w1-w2, h: h1-h2};
	},
	//--------------------------------------------------------------------------------------
	isAlpha: function(sWord) {
		for (var i = 0; i < sWord.length; ++i) {
			var ch = sWord.charCodeAt(i);
			if ((ch < 65 || ch > 90) && (ch < 97 || ch > 122)) {
				return false;
			}
		}
		return true;
	},
	//--------------------------------------------------------------------------------------	
	//Stop propagation of the event to other registered functions.
	stopPropagation: function(evnt) {
		if (typeof(evnt.stopPropagation) == "function") { // FF
			evnt.stopPropagation();
		}
		else if (typeof(evnt.preventDefault) == "function") { // FF
			evnt.preventDefault();
		}
		else if (typeof(evnt.cancelBubble) == "boolean") { //FF and IE
			evnt.cancelBubble = true;
			try {
				evnt.returnValue = false;
			}
			catch (err) {}
		}
		return false;
	}
}
