function cmnSet_cookie( sName, vValue, dExpires ){
   document.cookie = sName + "=" + escape( vValue )
	   + ( dExpires == null ? "" : ( "; expires=" + dExpires.toGMTString() ) )
	   + "; path=/";
}

function cmnGet_cookie( sName ){
	return cmnPairs_string_get_value( document.cookie, sName );
}

function cmnMatch_class( eOn, sClass_name ){
	return ( sClass_name && eOn.className && eOn.className.length && eOn.className.match( new RegExp("(^|\\s+)(" + sClass_name +")($|\\s+)") ) );
}

function cmnPairs_string_get_value( 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;
}

function cmnGetAbsolutePos(oElem){
	var _x=0;
	var _y=0;
	if(oElem){
		do{
			_x+=oElem.offsetLeft;
			_y+=oElem.offsetTop;
		}while( (oElem=oElem.offsetParent) )
	}
	
	return {x: _x, y: _y};
}

function unescape_opera(text) {
	if(typeof(RegExp) == 'function') {
		re = /quot;/g;  
		newstr=text.replace(re, ''); 
		re = /&/g; 
		return newstr.replace(re, '"');  
	} 
	else return text;
}
var arrStdPopupFeatures = [
	['menubar', 'no'],
	['toolbar', 'no'],
	['resizable', 'yes'],
	['scrollbars', 'yes'],
	['status', 'yes']
]; 

function getPopupSizeRelatedFeatures( iWinW, iWinH, strUserFeatures ) {
	var strSizeRelatedFeatures = '';
	var iWinWidth = ( iWinW )? iWinW : 540;
	var iWinHeight = ( iWinH )? iWinH : 600;
	if ( screen ) {
		var iScrWidth = ( screen.width )? screen.width : 0;
		var iScrHeight = ( screen.height )? screen.height : 0;
		var bNeedScroll = false;
		if ( iScrWidth < iWinWidth + 50 ) { bNeedScroll = true; iWinWidth = iScrWidth - 50; }
		if ( iScrHeight < iWinHeight + 100 ) { bNeedScroll = true; iWinHeight = iScrHeight - 100; }
		if ( !(strUserFeatures && strUserFeatures.indexOf('scrollbars') >= 0 ) ) {
			strSizeRelatedFeatures += ( bNeedScroll )? ',scrollbars=yes' : ',scrollbars=no';
		}
		var iPosX = Math.round( ( iScrWidth - iWinWidth ) / 2 );
		var iPosY = Math.round( ( ( iScrHeight - 70 ) - iWinHeight ) / 2);
		strSizeRelatedFeatures += ( document.all )? ',left=' + iPosX + ',top=' + iPosY : ',screenX=' + iPosX + ',screenY=' + iPosY;
	}
	strSizeRelatedFeatures += ',width=' + iWinWidth + ',height=' + iWinHeight;

	return strSizeRelatedFeatures;
} 

function getStdFeatures( strUserFeatures ) {
	var strStdFeatures = '';
	for ( var i = 0; i < arrStdPopupFeatures.length; i++) {
		if ( !(strUserFeatures && strUserFeatures.indexOf( arrStdPopupFeatures[i][0] ) >= 0 ) ) {
			strStdFeatures += ',' + arrStdPopupFeatures[i][0] + '=' + arrStdPopupFeatures[i][1];
		}
	}
	return strStdFeatures;
}

function cmnPopup(strFileUrl, strUserWinName, iWinW, iWinH, strUserFeatures) {
	var strAllFeatures = strUserFeatures + getPopupSizeRelatedFeatures( iWinW, iWinH, strUserFeatures );
	strAllFeatures += getStdFeatures( strAllFeatures );
	var strWinName = ( strUserWinName )? strUserWinName : 'popupWin';
	var popupWin = window.open(strFileUrl, strWinName, strAllFeatures);
	if (popupWin) popupWin.focus();
}

// #################################################
// Класс управлением параметрами CSS классов
function iStyle(file){
	var b = navigator.userAgent.toLowerCase();
	this.browser = {
		safari: /webkit/.test(b),
		opera: /opera/.test(b),
		msie: /msie/.test(b) && !/opera/.test(b),
		mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
	};
	this.setFile(file);
}

iStyle.prototype.style = function(name){
	var ret  = null;
	if(this.rules){
		for(var i=0, len=this.rules.length; i<len; i++){
			if(this.rules[i].selectorText == name){
				ret = this.rules[i].style;
				break;
			}
		}
	}
	return ret;
};

iStyle.prototype.setFile = function(file){
	this.rules = null;
	var re = new RegExp('.*?' + file);
	for(var i=0, len = document.styleSheets.length; i<len; i++){
		if(re.test(document.styleSheets[i].href)){
			this.rules = this.browser.msie ? document.styleSheets[i].rules : document.styleSheets[i].cssRules;
			break;
		}
	}	
};

// #####################################
// Функция удаляет все текстовые ноды
function removeAllTextNodes(oNode){
	if(oNode){
		var len = oNode.childNodes.length;
		if(len > 0){
			var pos	= 0;
			for(var i=0; i<len; i++){
				if(oNode.childNodes[pos].nodeType == 3){
					var str	= oNode.childNodes[pos].nodeValue;
					if(str){
						if (str.match('/^(\s|\n|\r)$/')){
							oNode.removeChild(oNode.childNodes[pos]);
						}
					}else{
						oNode.removeChild(oNode.childNodes[pos]);
					}
				}else{
					if(oNode.childNodes[pos].childNodes){
						if(oNode.childNodes[pos].childNodes.length > 0) removeAllTextNodes(oNode.childNodes[pos]);
					}
					pos++;
				}
			}
		}
	}
}
