/*
Common functions
@author Filatov Dmitry
@date   22.08.2006
*/

var Common = {

	// Common classes methods
	
	Class : {
		
		match : function(	
			oElement,
			sClassName
			) {
	
			return oElement.className.match(new RegExp('(^|\\s+)' + sClassName + '($|\\s+)'));	
		
		},
		
		add : function(
			oElement,
			sClassName
			) {

			if(!Common.Class.match(oElement, sClassName)) {
				oElement.className += ' ' + sClassName;
			}
			
		},
		
		replace : function(
			oElement,
			sClassNameFrom,
			sClassNameTo
			) {

			oElement.className = ( oElement.className.replace( new RegExp("(^|\\s+)(" + sClassNameFrom + "|" + sClassNameTo + ")($|\\s+)", "g"), "$1" ) + ' ' + sClassNameTo ).replace( /^\s+/, '' );
			
		},
		
		remove : function(
			oElement,
			sClassName
			) {
	
			oElement.className = oElement.className.replace(new RegExp('(.*)(^|\\s+)(' + sClassName + ')($|\\s+)(.*)'), '$1$4$5').replace(/(^)\s/, '$1');	
			
		}
		
	},
		
	
	// Common event's methods
	
	Event : {
	
		add : function(
			oElement,
			sEventType,
			fEventFunc,
			bCapture
			) {
					
			if(oElement.addEventListener) {
				oElement.addEventListener(
					sEventType,
					fEventFunc,
					bCapture? true : false
					);
			}
			else if(oElement.attachEvent) {
				oElement.attachEvent(
					'on' + sEventType,
					fEventFunc
					);
			}
	
		},		

		remove : function(
			oElement,
			sEventType,
			fEventFunc,
			bCapture
			) {
	
			if(oElement.removeEventListener) {
				oElement.removeEventListener(
					sEventType,
					fEventFunc,
					bCapture? true : false
					);
			}
			else if(oElement.detachEvent) {
				oElement.detachEvent(
					'on' + sEventType,
					fEventFunc
					);
			}
			
		},
		
		getAbsoluteCoords : function(oEvent) {

			var coords = {
				left: 0,
				top: 0
			};

			if(oEvent.pageX) {
  
				coords.left = oEvent.pageX;
				coords.top = oEvent.pageY;
		
			}
			else {
	
				coords.left = oEvent.clientX + document.body.scrollLeft - document.body.clientLeft;
				coords.top = oEvent.clientY + document.body.scrollTop - document.body.clientTop;

				if(document.body.parentElement && document.body.parentElement.clientLeft) {
		
					var bodyParent = document.body.parentElement;
		
					coords.left += bodyParent.scrollLeft - bodyParent.clientLeft;
					coords.top += bodyParent.scrollTop - bodyParent.clientTop;  
			
				}
		
			}

			return coords;
			
		}
		
	},	
		
	Object : {
				
		extend : function(
			oSource,
			oDestination
			) {				
		
			for(var i in oSource) {		
				oDestination[i] = oSource[i];
			}
		
			return oDestination;
		
		}
		
	},
	
	Utils : {
	
		oPopupDefaults : {	
	
			iWidth      : 540,
			iHeight     : 600,
			sToolbar    : 'no',
			sMenubar    : 'no',
			sResizeable : 'yes',
			sScrollbars : 'yes',
			sStatus     : 'yes'
	
		},
	
		popup : function(
			sUrl,
			sName,
			oOptions,
			bReplace
			) {
		
			oOptions = this.extend(
				oOptions,
				this.oPopupDefaults
				);
		
			var iLeftOffset = screen.availWidth / 2 - oOptions.iWidth / 2;
			var iTopOffset = screen.availHeight / 2 - oOptions.iHeight / 2;			
		
			oNewWindow = window.open(
				sUrl,
				sName,
				'left=' + iLeftOffset + ', ' +
				'top = ' + iTopOffset + ', ' +
				'width=' + oOptions.iWidth + ', ' +
				'height=' + oOptions.iHeight + ', ' +
				'resizable=' + oOptions.sResizeable + ', ' +
				'toolbar=' + oOptions.sToolbar + ', ' +
				'scrollbars=' + oOptions.sScrollbars + ', ' +
				'status=' + oOptions.sStatus
				);
			
			if(sUrl.match(/\.(gif|jpe?g|png)$/i)) {
		
				oNewWindow.document.open();
			
				oNewWindow.document.write('<html><head></head>' +
					'<body style="background: #FFF; margin: 0px; padding: 0px;">' +
					'<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center">' + 
					'<img src="' + sUrl + '" </td></tr></table></body></html>'
					);
				
				oNewWindow.document.close();
		
			}
			
			oNewWindow.focus();				

			return false;
			
		}
		
	},
	
	HTMLObject : {
		
		getParentByTagName : function ( oObj, sTagName ) {
			while (oObj.tagName.toUpperCase() != sTagName.toUpperCase()) {
				oObj = oObj.parentNode;
			}
			return oObj;
		}
		
	},

	Cookie : {
		
		set : function ( sName, vValue, dExpires ) {
			document.cookie = sName + "=" + escape( vValue )
			+ ( ( dExpires == null ) ? "" : ( "; expires=" + dExpires.toGMTString() ) )
			+ "; path=/";
			},

		get : function( sName ) {
			return Common.Strings.get_value( document.cookie, sName );
		}
		
	},
	
	Strings : {
		get_value : function ( sText, sName, sFrom, sBefore ){
			var sValue = "";
			if( sText ){
				if( !sFrom ) sFrom = "=";
				if( !sBefore ) sBefore = ";";
				sText = sText.replace( new RegExp( "(" + sBefore + ")\\s+", "g" ), "$1" );
				var iStart = sText.indexOf( sBefore + sName + sFrom );
				if( iStart >= 0 ){
					iStart += ( sBefore.length + sName.length + sFrom.length );
				}else{
					iStart = sText.indexOf( sName + sFrom );
					if( iStart == 0 ){
						iStart += ( sName.length + sFrom.length );
					}else{
						iStart = -1;
					}
				}
				if( iStart >= 0 ){
					var iEnd = sText.indexOf( sBefore, iStart );
					if( iEnd < 0 ){
						iEnd = sText.length;
					}
					sValue = sText.substring( iStart, iEnd );
				}
			}
			return sValue;
		},

		set_value : function ( sText, sName, sValue, sFrom, sBefore ){
			if( !sFrom ) sFrom = "=";
			if( !sBefore ) sBefore = ";";
			var iStart = -1;
			if( sText ){
				sText = sText.replace( new RegExp( "\\s+(" + sBefore + "|" + sFrom + ")\\s+", "g" ), "$1" );
				iStart = sText.indexOf( sBefore + sName + sFrom );
				if( iStart >= 0 ){
					iStart += ( sBefore.length + sName.length + sFrom.length );
				}else{
					iStart = sText.indexOf( sName + sFrom );
					if( iStart == 0 ){
						iStart += ( sName.length + sFrom.length );
					}else{
						iStart = -1;
					}
				}
				if( iStart >= 0 ){
					var iEnd = sText.indexOf( sBefore, iStart );
					if( iEnd < 0 ){
						iEnd = sText.length;
					}
					sText = sText.substring( 0, iStart ) + sValue + sText.substr( iEnd );
				}
			}
			if( iStart < 0 ){
				if( sText && sText.lastIndexOf( sBefore ) != ( sText.length - sBefore.length ) ){
					sText += sBefore;
				}
				sText += sName + sFrom + sValue + sBefore;
			}
			return sText;
		}
		
	}
};



function unescape_opera(text) {
	if(typeof(RegExp) == 'function') {
		re = /quot;/g;  
		newstr=text.replace(re, ''); 
		re = /&/g; 
		return newstr.replace(re, '"');  
	} 
	else return text;
}

function unescape_nbsp(text) {
	if(typeof(RegExp) == 'function') {
		re = /&nbsp;/g;  
		newstr=text.replace(re, ' '); 
		return newstr;  
	} 
	else return text;
}