var PG_BROWSER_CHROME = 'Chrome';
var PG_BROWSER_OPERA = 'Opera';
var PG_BROWSER_INTERNET_EXPLORER = 'Internet Explorer';
var PG_BROWSER_FIREFOX = 'Firefox';
var PG_BROWSER_PHOENIX = 'Phoenix';
var PG_BROWSER_MOZILLA = 'Mozilla';
var PG_BROWSER_NETSCAPE = 'Netscape';
var PG_BROWSER_SAFARI = 'Safari';

var PG_BROWSER_OS_MAC = 'Mac';
var PG_BROWSER_OS_WINDOWS = 'Windows';
var PG_BROWSER_OS_LINUX = 'Linux';

function classPG_Browser()
{
	this.oWindow = window;
	this.oDocument = document;

	this.fOnSelectStart = null;
	this.fOnMouseDown = null;
	this.fOnClick = null;
	
	this.oFocusedElement = null;
	
	this.bRunOnceOnLoad = true;

	this.destroy = function()
	{
		// set all properties to null...
		this.oWindow = null;
		this.oDocument = null;
		this.fOnSelectStart = null;
		this.fOnMouseDown = null;
		this.fOnClick = null;
		this.bRunOnceOnLoad = null;

		// cleanup all properties...
		delete this.oWindow;
		delete this.oDocument;
		delete this.fOnSelectStart;
		delete this.fOnMouseDown;
		delete this.fOnClick;
		delete this.bRunOnceOnLoad;

		// cleanup all methods...
		delete this.create;
		delete this.getBrowserVersion;
		delete this.getBrowserName;
		delete this.disableOnMouseDown;
		delete this.enableOnClick;
		delete this.initSelect;
		delete this.enableSelect;
		delete this.disableSelect;
		delete this.getDocumentOffsetX;
		delete this.getDocumentOffsetY;
		delete this.onLoad;
		delete this.destroy;
	}
	
	/*
	this.getIframeWindow = function(_sIframeID)
	{
		var _oWindow = null;
		var _oIframe = window.getElementById(_sIframeID);
		if (_oIframe) {_oWindow = (_oIframe.contentWindow || _oIframe.window);}
		return _oWindow;
	}
	
	this.getIframeDocument = function(_sIframeID)
	{
		var _oDocument = null;
		var _oIframe = window.getElementById(_sIframeID);
		if (_oIframe)
		{
			_oDocument = (_oIframe.contentWindow || _oIframe.contentDocument);
			if (_oDocument.document) {_oDocument = _oDocument.document;}
		}
		return _oDocument;
	}
	
	this.overwriteFrame = function(_sWindow, _sFrameToOverwrite, _sTargetFrame)
	{
		if (typeof(window.defineGetter) != 'undefined')
		{
alert(_sWindow+'.defineGetter("'+_sFrameToOverwrite+'", function() {return '+_sTargetFrame+'});');
			eval(_sWindow+'.defineGetter("'+_sFrameToOverwrite+'", function() {return '+_sTargetFrame+'});');
			return;
		}
		
		if (window.HTMLElement)
		{
			if (typeof(window.HTMLElement.prototype.__defineGetter__) != 'undefined')
			{
alert(_sWindow+'.prototype.HTMLElement.__defineGetter__("'+_sFrameToOverwrite+'",function() {return '+_sTargetFrame+';});');
				eval(_sWindow+'.prototype.HTMLElement.__defineGetter__("'+_sFrameToOverwrite+'",function() {return '+_sTargetFrame+';});');
				return;
			}
		}
		
		if (typeof(window.prototype) != 'undefined')
		{
alert(_sWindow+'.prototype.'+_sFrameToOverwrite+' = '+_sTargetFrame+';');
			eval(_sWindow+'.prototype.'+_sFrameToOverwrite+' = '+_sTargetFrame+';');
			return;
		}
		
alert(_sWindow+'.'+_sFrameToOverwrite+' = '+_sTargetFrame+';');
		// eval(_sWindow+'.'+_sFrameToOverwrite+' = window.self;');
		eval('top = self;');
	}
	*/
	
	this.create = function(_oWindow, _oDocument)
	{
		if (_oWindow) {this.oWindow = _oWindow;}
		if (_oDocument) {this.oDocument = _oDocument;}
	}
	
	this.getBrowserInfo = function() {return navigator.userAgent;}
	
	this.getVersion = function(_sBrowserInfo) {return this.getBrowserVersion(_sBrowserInfo);}
	this.getBrowserVersion = function(_sBrowserInfo)
	{
		_sBrowserVersion = '';
		if (_sBrowserInfo == null) {_sBrowserInfo = this.getBrowserInfo();}
		
		// Chrome...
		if (_sBrowserInfo.search(/\sChrome\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sChrome\s([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/\sChrome\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sChrome\/([0-9\.\,]+).*/i, "$1");}

		// Opera...
		else if (_sBrowserInfo.search(/(\s|^)Opera\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/(.*\s|^)Opera\/([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/Opera\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/Opera\/([0-9\.\,]+).*/i, "$1");}

		// MSIE...
		else if (_sBrowserInfo.search(/\sMSIE\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sMSIE\s([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/\sMSIE\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sMSIE\/([0-9\.\,]+).*/i, "$1");}
		
		// Safari...
		else if (_sBrowserInfo.search(/\Safari\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*Version\/([0-9\.\,]+)\sSafari\/.*/i, "$1");}
		
		// Netscape...
		else if (_sBrowserInfo.search(/\sNavigator\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sNavigator\/([0-9\.\,]+)/i, "$1");}
		else if (_sBrowserInfo.search(/\sNetscape\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sNetscape\/([0-9\.\,]+)/i, "$1");}
		
		// Firefox...
		else if (_sBrowserInfo.search(/\sFirefox\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sFirefox\/([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/\sFirefox\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sFirefox\s([0-9\.\,]+).*/i, "$1");}
		
		// Mozilla...
		else if (_sBrowserInfo.search(/\sMozilla\//i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sMozilla\/([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/\sMozilla\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sMozilla\s([0-9\.\,]+).*/i, "$1");}

		// Internet Explorer...
		else if (_sBrowserInfo.search(/\sIE\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sIE\s([0-9\.\,]+).*/i, "$1");}
		else if (_sBrowserInfo.search(/\sExplorer\s/i) != -1) {_sBrowserVersion = _sBrowserInfo.replace(/.*\sExplorer\s([0-9\.\,]+).*/i, "$1");}

		return _sBrowserVersion;
	}
	
	this.getName = function() {return this.getBrowserName();}
	this.getBrowserName = function()
	{
		var _sBrowserInfo = this.getBrowserInfo();
		if ((navigator.appName.indexOf("Chrome") != -1) || (navigator.appCodeName.indexOf("Chrome") != -1)) {return PG_BROWSER_CHROME;}
		else if ((_sBrowserInfo.search(/\sChrome\s/i) != -1) || (_sBrowserInfo.search(/\sChrome\//i) != -1)) {return PG_BROWSER_CHROME;}
		else if ((navigator.appName.indexOf("Opera") != -1) || (navigator.appCodeName.indexOf("Opera") != -1)) {return PG_BROWSER_OPERA;}
		else if ((navigator.appName.indexOf("MSIE") != -1) || (navigator.appCodeName.indexOf("MSIE") != -1)) {return PG_BROWSER_INTERNET_EXPLORER;}
		else if ((navigator.appName.indexOf("Internet Explorer") != -1) || (navigator.appCodeName.indexOf("Internet Explorer") != -1)) {return PG_BROWSER_INTERNET_EXPLORER;}
		else if ((navigator.appName.indexOf("Safari") != -1) || (navigator.appCodeName.indexOf("Safari") != -1)) {return PG_BROWSER_SAFARI;}
		else if (_sBrowserInfo.search(/\Safari\//i) != -1) {return PG_BROWSER_SAFARI;}
		else if ((navigator.appName.indexOf("Navigator") != -1) || (navigator.appCodeName.indexOf("Navigator") != -1)) {return PG_BROWSER_NETSCAPE;}
		else if ((navigator.appName.indexOf("Netscape") != -1) || (navigator.appCodeName.indexOf("Netscape") != -1)) {return PG_BROWSER_NETSCAPE;}
		else if ((navigator.appName.indexOf("Firefox") != -1) || (navigator.appCodeName.indexOf("Firefox") != -1)) {return PG_BROWSER_FIREFOX;}
		else if ((navigator.appName.indexOf("Phoenix") != -1) || (navigator.appCodeName.indexOf("Phoenix") != -1)) {return PG_BROWSER_PHOENIX;}
		else if ((navigator.appName.indexOf("Mozilla") != -1) || (navigator.appCodeName.indexOf("Mozilla") != -1)) {return PG_BROWSER_MOZILLA;}
		else if ((navigator.appName.indexOf("IE") != -1) || (navigator.appCodeName.indexOf("IE") != -1)) {return PG_BROWSER_INTERNET_EXPLORER;}
		else if ((navigator.appName.indexOf("Explorer") != -1) || (navigator.appCodeName.indexOf("Explorer") != -1)) {return PG_BROWSER_INTERNET_EXPLORER;}
		return '';
	}
	
	this.getOSName = function()
	{
		var _sBrowserInfo = this.getBrowserInfo();
		if ((_sBrowserInfo.search(/\Mac\//i) != -1) || (_sBrowserInfo.search(/\sMac\s/i) != -1)) {return PG_BROWSER_OS_MAC;}
		else if ((_sBrowserInfo.search(/\Windows\//i) != -1) || (_sBrowserInfo.search(/\sWindows\s/i) != -1)) {return PG_BROWSER_OS_WINDOWS;}
		else if ((_sBrowserInfo.search(/\Linux\//i) != -1) || (_sBrowserInfo.search(/\sLinux\s/i) != -1)) {return PG_BROWSER_OS_LINUX;}
		return '';
	}

	this.disableOnSelectStart = function(_eEvent) {return false;}
	this.disableOnMouseDown = function(_eEvent) {return false;}
	this.enableOnClick = function(_eEvent) {return true;}
	
	this.initSelect = function()
	{
		if (typeof(this.oDocument.onselectstart) != 'undefined') {this.fOnSelectStart = this.oDocument.onselectstart;}
		if (typeof(this.oDocument.onmousedown) != 'undefined') {this.fOnMouseDown = this.oDocument.onmousedown;}
		if (typeof(this.oDocument.onclick) != 'undefined') {this.fOnClick = this.oDocument.onclick;}
		/*
		if (this.oWindow.sidebar)
		{
			// alert(this.oWindow.sidebar);
			this.fOnMouseDown = this.oDocument.onmousedown;
			this.fOnClick = this.oDocument.onclick;
		}
		*/
		// alert(this.fOnSelectStart+" ; "+this.fOnMouseDown+" ; "+this.fOnClick);
	}
	
	this.enableSelect = function()
	{
		/*
		if (this.oWindow.sidebar)
		{
			this.oDocument.onmousedown = this.fOnMouseDown;
			this.oDocument.onclick = this.fOnClick;
		}
		*/
		if (typeof(this.oDocument.onselectstart) != 'undefined') {this.oDocument.onselectstart = this.fOnSelectStart;}
		if (typeof(this.oDocument.onmousedown) != 'undefined') {this.oDocument.onmousedown = this.fOnMouseDown;}
		if (typeof(this.oDocument.onclick) != 'undefined') {this.oDocument.onclick = this.fOnClick;}
	
		if (typeof(event) != "undefined")
		{
			if (event.preventDefault)
			{
			}
			else
			{
				event.returnValue = true;
				event.cancelBubble = false;
			}
		}
	}
	
	this.disableSelect = function()
	{
		/*
		if (this.oWindow.sidebar)
		{
			this.oDocument.onmousedown = this.disableOnMouseDown;
			this.oDocument.onclick = this.enableOnClick;
		}
		*/
		if (typeof(this.oDocument.onselectstart) != 'undefined') {this.oDocument.onselectstart = this.disableOnSelectStart;}
		if (typeof(this.oDocument.onmousedown) != 'undefined') {this.oDocument.onmousedown = this.disableOnMouseDown;}
		if (typeof(this.oDocument.onclick) != 'undefined') {this.oDocument.onclick = this.enableOnClick;}
	
		if (typeof(event) != "undefined")
		{
			if (event.preventDefault)
			{
				event.preventDefault();
				event.stopPropagation();
			}
			else
			{
				event.returnValue = false;
				event.cancelBubble = true;
			}
		}
	}
	
	this.getScreenSize = function()
	{
		if ((this.oWindow) && (this.oDocument))
		{
			var _bIsNetscape = (navigator.appName.indexOf("Netscape") != -1);
			var _oSize = new Object();
			if (typeof(this.oDocument.documentElement) != 'undefined')
			{
				_oSize.x = _bIsNetscape ? this.oWindow.innerWidth : this.oDocument.documentElement.clientWidth;
				_oSize.y = _bIsNetscape ? this.oWindow.innerHeight : this.oDocument.documentElement.clientHeight;
			}
			else
			{
				_oSize.x = _bIsNetscape ? this.oWindow.innerWidth : this.oDocument.body.clientWidth;
				_oSize.y = _bIsNetscape ? this.oWindow.innerHeight : this.oDocument.body.clientHeight;
			}
			return _oSize;
		}
		return null;
	}
	
	this.getScreenSizeX = function()
	{
		var _iSizeX = 0;
		if ((this.oWindow) && (this.oDocument))
		{
			if (typeof(this.oDocument.documentElement) != 'undefined')
			{
				if (typeof(this.oDocument.documentElement.clientWidth) != 'undefined')
				{
					_iSizeX = this.oDocument.documentElement.clientWidth;
				}
			}
			if ((typeof(this.oWindow.innerWidth) != 'undefined') && (_iSizeX == 0)) {_iSizeX = this.oWindow.innerWidth;}
			if ((typeof(this.oDocument.body.clientWidth) != 'undefined') && (_iSizeX == 0)) {_iSizeX = this.oDocument.body.clientWidth;}
			if ((typeof(this.oDocument.body.offsetWidth) != 'undefined') && (_iSizeX == 0)) {_iSizeX = this.oDocument.body.offsetWidth;}
		}
		return _iSizeX;
	}
	
	this.getScreenSizeY = function()
	{
		var _iSizeY = 0;
		if ((this.oWindow) && (this.oDocument))
		{
			if (typeof(this.oDocument.documentElement) != 'undefined')
			{
				if (typeof(this.oDocument.documentElement.clientHeight) != 'undefined')
				{
					_iSizeY = this.oDocument.documentElement.clientHeight;
				}
			}
			if ((typeof(this.oWindow.innerHeight) != 'undefined') && (_iSizeY == 0)) {_iSizeY = this.oWindow.innerHeight;}
			if ((typeof(this.oDocument.body.clientHeight) != 'undefined') && (_iSizeY == 0)) {_iSizeY = this.oDocument.body.clientHeight;}
			if ((typeof(this.oDocument.body.offsetHeight) != 'undefined') && (_iSizeY == 0)) {_iSizeY = this.oDocument.body.offsetHeight;}
		}
		return _iSizeY;
	}
	
	this.getScrollPos = function()
	{
		if ((this.oWindow) && (this.oDocument))
		{
			var _oScrollPos = new Object();
			/*
			var _bIsNetscape = (navigator.appName.indexOf("Netscape") != -1);
			_oScrollPos.x = _bIsNetscape ? pageXOffset : this.oDocument.body.scrollLeft;
			_oScrollPos.y = _bIsNetscape ? pageYOffset : this.oDocument.body.scrollTop;
			*/

			if (this.oDocument.documentElement && this.oDocument.documentElement.scrollTop)	// Explorer 6 Strict
			{
				_oScrollPos.x = this.oDocument.documentElement.scrollLeft;
				_oScrollPos.y = this.oDocument.documentElement.scrollTop;
			}
			else if (this.oDocument.body) // all other Explorers
			{
				_oScrollPos.x = this.oDocument.body.scrollLeft;
				_oScrollPos.y = this.oDocument.body.scrollTop;
			}
			else if (self.pageXOffset) // all except Explorer
			{
				_oScrollPos.x = self.pageXOffset;
				_oScrollPos.y = self.pageYOffset;
			}

			return _oScrollPos;
		}
		return null;
	}
	
	this.popup3 = function(_sUrl, _iSizeX, _iSizeY) {return this.popup(_sUrl, '', null, null, _iSizeX, _iSizeY, true, true);}
	this.popup2 = function(_sUrl, _iSizeX, _iSizeY, _bScrollbars, _bResizable) {return this.popup(_sUrl, '', null, null, _iSizeX, _iSizeY, _bScrollbars, _bResizable);}
	this.popup = function(_sUrl, _sWinName, _iPosX, _iPosY, _iSizeX, _iSizeY, _bScrollbars, _bResizable)
	{
		if (_iSizeX > screen.width) {_iSizeX = screen.width;}
		if (_iSizeY > screen.height) {_iSizeX = screen.height;}
		
		if (_iPosX == null) {_iPosX = Math.floor((screen.width-_iSizeX)/2);}
		if (_iPosY == null) {_iPosY = Math.floor((screen.height-_iSizeY)/2);}

		var _sProperties = "scrollbars=";
		if (_bScrollbars == true) {_sProperties += "yes";} else {_sProperties += "no";}
		_sProperties += ",resizable=";
		if (_bResizable == true) {_sProperties += "yes";} else {_sProperties += "no";}
		_sProperties = "top="+_iPosY+",left="+_iPosX+",width="+_iSizeX+",height="+_iSizeY+",location=no,toolbar=no,menubar=no,status=no,"+_sProperties;
		return this.oWindow.open(_sUrl, _sWinName, _sProperties);
	}
	
	this.getDocumentOffsetX = function(_oElement)
	{
		var _vPosX = 0;
		if (typeof(_oElement) != 'undefined')
		{
			while ((typeof(_oElement) == 'object') && (typeof(_oElement.tagName) != 'undefined'))
			{
				_vPosX += parseInt(_oElement.offsetLeft);
				if (_oElement.tagName.toUpperCase() == 'BODY') {_oElement = 0; return _vPosX;}
				else if (_oElement.tagName.toUpperCase() == 'DIV') {_vPosX -= _oElement.scrollLeft;}
				if (typeof(_oElement) == 'object') {if (typeof(_oElement.offsetParent) == 'object') {_oElement = _oElement.offsetParent;}}
			}
		}
		return _vPosX;
	}

	this.getDocumentOffsetY = function(_oElement)
	{
		var _vPosY = 0;
		if (typeof(_oElement) != 'undefined')
		{
			while ((typeof(_oElement) == 'object') && (typeof(_oElement.tagName) != 'undefined'))
			{
				_vPosY += parseInt(_oElement.offsetTop);
				if (_oElement.tagName.toUpperCase() == 'BODY') {_oElement = 0; return _vPosY;}
				else if (_oElement.tagName.toUpperCase() == 'DIV') {_vPosY -= _oElement.scrollTop;}
				if (typeof(_oElement) == 'object') {if (typeof(_oElement.offsetParent) == 'object') {_oElement = _oElement.offsetParent;}}
			}
		}
		return _vPosY;
	}
	
	/*
	this.setHTML = function(_sContainerID, _sHTML, _sJsToExecuteOnLoading, _sJsToExecuteOnDone)
	{
		var _oContainer = this.oDocument.getElementById(_sContainerID);
		if ((_oContainer) && (typeof(oPGStringTool) != 'undefined'))
		{
			_oContainer.innerHTML = _sHTML;
			alert(oPGStringTool.addSlashes(_sHTML));
			this.checkHTMLIsSet(_sContainerID, oPGStringTool.stringToUtf8(_sHTML), oPGStringTool.stringToUtf8(_sJsToExecuteOnLoading), oPGStringTool.stringToUtf8(_sJsToExecuteOnDone));
		}
	}
	
	this.checkHTMLIsSet = function(_sContainerID, _sHTML, _sJsToExecuteOnLoading, _sJsToExecuteOnDone)
	{
		var _oContainer = this.oDocument.getElementById(_sContainerID);
		if (_oContainer)
		{
			alert(_oContainer.innerHTML+" != "+oPGStringTool.stripSlashes(oPGStringTool.utf8ToString(_sHTML)));
			if (_oContainer.innerHTML != oPGStringTool.stripSlashes(oPGStringTool.utf8ToString(_sHTML)))
			{
				if ((typeof(oPGStringTool) != 'undefined') && (_sJsToExecuteOnLoading != '')) {eval(oPGStringTool.utf8ToString(oPGStringTool.stripSlashes(_sJsToExecuteOnLoading)));}
				this.oWindow.setTimeout("oPGBrowser.checkHTMLIsSet('"+_sContainerID+"', '"+oPGStringTool.addSlashes(_sHTML)+"', '"+oPGStringTool.addSlashes(_sJsToExecuteOnLoading)+"', '"+oPGStringTool.addSlashes(_sJsToExecuteOnDone)+"')", 100);
			}
			else
			{
				if ((typeof(oPGStringTool) != 'undefined') && (_sJsToExecuteOnDone != '')) {eval(oPGStringTool.utf8ToString(oPGStringTool.stripSlashes(_sJsToExecuteOnDone)));}
			}
		}
	}
	*/
	
	/*
	this.getFocusedElement = function() {return this.oFocusedElement;}
	
	this.onFocus = function(_eEvent)
	{
		if (!_eEvent) {_eEvent = window.event;} // For IE.
		this.oFocusedElement = arguments[0].target || _eEvent.srcElement;
	}
	*/
	
	this.onLoad = function()
	{
		if (this.bRunOnceOnLoad == true)
		{
			this.initSelect();
			this.bRunOnceOnLoad = false;
		}
	}
}

var oPGBrowser = new classPG_Browser();

