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 cmnSwitch_class( eOn, sClass_name, sInstead ){
	if( cmnMatch_class( eOn, sClass_name ) ){
		cmnSet_class( eOn, sInstead, sClass_name );
	}else{
		cmnSet_class( eOn, sClass_name, sInstead );
	}
}

function cmnRemove_class( eOn, sClass_name ){
	cmnSet_class( eOn, "", sClass_name );
}

function cmnSet_class( eOn, sClass_name, sInstead ){
	if( eOn ){
		sClass_name = ( sClass_name.length ) ? sClass_name.replace( /(^\s+|\s+$)/, "" ) : "";
		if( eOn.className.length ){
			var sOld = sClass_name;
			if( sInstead && sInstead.length ){
				sInstead = sInstead.replace( /\s+(\S)/g, "|$1" );
				if( sOld ){
					sOld += "|";
				}
				sOld += sInstead;
			}
			eOn.className = eOn.className.replace( new RegExp("(^|\\s+)(" + sOld +")($|\\s+)", "g"), "$1" );
		}
		eOn.className += ( eOn.className.length && sClass_name ? " " : "" ) + sClass_name;
	}
}

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 cmnAdd_hover( eOn ){
	cmnAdd_event( eOn, "mouseover", function(){ cmnSet_class( eOn,    "hover" ) } );
	cmnAdd_event( eOn, "mouseout",  function(){ cmnRemove_class( eOn, "hover" ) } );
}

function cmnAdd_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.addEventListener ){
		eOn.addEventListener( sEvent_type, ptrFunction, false );
	}else{
		if( !ptrFunction.$$guid ){ ptrFunction.$$guid = cmnAdd_event.guid++; }
		if( !eOn.events ){ eOn.events = {}; }
		var aHandler = eOn.events[sEvent_type];
		if( !aHandler ){
			aHandler = eOn.events[sEvent_type] = {};
			if( eOn['on' + sEvent_type] ){ aHandler[0] = eOn['on' + sEvent_type]; }
			eOn['on' + sEvent_type] = cmnHandle_event;
		}
		aHandler[ptrFunction.$$guid] = ptrFunction;
	}
}
cmnAdd_event.guid = 1;

function cmnRemove_event( eOn, sEvent_type, ptrFunction ){
	if( eOn.removeEventListener ){
		eOn.removeEventListener( sEvent_type, ptrFunction, false );
	}else if( eOn.events && eOn.events[sEvent_type] && ptrFunction.$$guid ){
		delete eOn.events[sEvent_type][ptrFunction.$$guid];
	}
}

function cmnHandle_event( event ){
	event = event || cmnFixe_event( window.event );
	var returnValue = true;
	var aHandler = this.events[event.type];
	for( var i in aHandler ){
		if( !Object.prototype[i] ){
			this.$$ptrFunction = aHandler[i];
			if( this.$$ptrFunction(event) === false ){ returnValue = false; }
		}
	}
	if( this.$$ptrFunction ){ this.$$ptrFunction = null; }

	return returnValue;
}

function cmnFixe_event( event ){
	event.preventDefault = cmnFixe_event.preventDefault;
	event.stopPropagation = cmnFixe_event.stopPropagation;
	return event;
}

cmnFixe_event.preventDefault = function(){
	this.returnValue = false;
};

cmnFixe_event.stopPropagation = function(){
	this.cancelBubble = true;
};

function cmnNextSibling(obj){
	obj	= obj.nextSibling;
	if(obj){
		while(obj.nodeType == 3){
			obj	= obj.nextSibling;
			if(obj == null) break;
		}
	}
	if(obj)	if(obj.nodeType == 3) return null;
	return obj;
}

function cmnPrevSibling(obj){
	obj	= obj.previousSibling;
	if(obj){
		while(obj.nodeType == 3){
			obj	= obj.previousSibling;
			if(obj == null) break;
		}
	}
	if(obj)	if(obj.nodeType == 3) return null;
	return obj;
}

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 getAttribute(oNode, sAttr){
	return (oNode.attributes) ? getAttributeIE(oNode, sAttr) : oNode.getAttribute(sAttr);
} 

function getAttributeIE(oNode, sAttr){
	for(var i=0; i<oNode.attributes.length; i++){
		if(oNode.attributes[i].nodeName == sAttr)
			return oNode.attributes[i].nodeValue;
	}
} 

// #####################################
// Функция удаляет все текстовые ноды
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++;
				}
			}
		}
	}
}
