/*****************************
**   Misc functions
******************************/

function fnIsNaN(iStr){
	return (isNaN(iStr) || iStr == Number.NaN || iStr=='NaN');
}

function CreateNodeWithText(sNodeName, sText){
	var oNode=document.createElement(sNodeName);
	var oText=document.createTextNode(sText);
	oNode.appendChild(oText);
	return oNode;
}

function getAttribute(oNode, sAttr){
	//return (oNode.attributes) ? oNode.attributes[sAttr].nodeValue : oNode.getAttribute(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;
	}
}

/* функция возвращает правильное слово для числа, пример использования: GetCorrectWord(10, ['осел','осла','ослов']) */
function GetCorrectWord(iNum, aWords, bSkipNum){
	var iLast=Number(String(iNum).substr(-1));
	var sResult;
	
	if(iNum.between(5,20))
		sResult=aWords[2];
	else{
		switch(iLast){
			case 1:  sResult=aWords[0]; break;
			case 2:
			case 3:
			case 4:  sResult=aWords[1]; break;
			default: sResult=aWords[2]; break;
		}
	}
	
	return (bSkipNum) ? sResult : iNum+' '+sResult;
}

/*****************************
**   Prototypes
******************************/

Array.prototype.push=function(){
	for(var i=0; i<arguments.length; i++){
		this[this.length]=arguments[i];
	}
}

Array.prototype.remove=function(sData){
	for(var i=0; i < this.length; i++){
		if(this[i] == sData){
			this.splice(i,1);
			break;
		}
	}
}

Array.prototype.contains=function(sData){
	var iFound=-1;
	for(var i=0; i<this.length; i++){
		if(this[i] == sData){
			iFound=i;
			break;
		}
	}
	return iFound;
}

Array.prototype.last=function(){
	return this[this.length];
}

Number.prototype.between=function(iStart, iEnd, bNoInclude){
	var iVal=this.valueOf();
	if(bNoInclude)
		return ( iVal > iStart && iVal < iEnd );
	else
		return ( iVal >= iStart && iVal <= iEnd );
}

Number.prototype.nice=function(iRoundBase){
	var re=/^(\d+)([\.,](\d+))?$/
	var iNum=Number(this);
	var sNum=String(iNum);
	var aMatches;
	var sDecPart='';
	var sTSeparator=' ';
	if((aMatches = sNum.match(re))){
		var sIntPart=aMatches[1];
		var iDecPart=(aMatches[3]) ? Number('0.'+aMatches[3]) : 0;
		if(iDecPart){
			var iRF=Math.pow(10, (iRoundBase) ? iRoundBase : 2);
			iDecPart=Math.round(iDecPart*iRF);
			sDecPart=(iDecPart) ? ','+iDecPart : '';
		}
		if(Number(sIntPart) < 10000)
			return sIntPart+sDecPart;
		else{
			var sNewNum='';
			var i;
			for(i=1; i*3<sIntPart.length; i++){
				//~ alert(sIntPart.substr(-i*3, 3));
				//~ ($iLen - $i*3;$iLen - ($i - 1)*3 - 1)
				//~ alert(sIntPart.substring(sIntPart.length - i*3, sIntPart.length - (i-1)*3 ));
				//~ sNewNum=sTSeparator+sIntPart.substr(-i*3, 3)+sNewNum;
				sNewNum=sTSeparator+sIntPart.substring(sIntPart.length - i*3, sIntPart.length - (i-1)*3)+sNewNum;
			}
			return sIntPart.substr(0, 3 - i*3 + sIntPart.length)+sNewNum+sDecPart;
		}
	}
	else{    //нам что-то не то подсунули
		return sNum;
	}
}

String.prototype.toNumber=function(){
	return Number(this.replace(/[^\d\.]/g,''));
}

/*****************************
**   Event listeners
******************************/

function checkEvent(oEvt){
	oEvt=(oEvt) ? oEvt : ( (window.event) ? window.event : null );
	if(oEvt && oEvt.srcElement)
		oEvt.target=oEvt.srcElement;
	return oEvt;
}

function addEvent(objElement, strEventType, ptrEventFunc) {
	if (objElement.addEventListener) objElement.addEventListener(strEventType, ptrEventFunc, false);
		else if (objElement.attachEvent) objElement.attachEvent('on' + strEventType, ptrEventFunc);
}

function removeEvent(objElement, strEventType, ptrEventFunc) {
	if (objElement.removeEventListener) objElement.removeEventListener(strEventType, ptrEventFunc, false);
		else if (objElement.detachEvent) objElement.detachEvent('on' + strEventType, ptrEventFunc);
}

/*****************************
**   Common cookie methods
******************************/

// Functions from Netscape's JavaScript Guide
// http://developer.netscape.com/docs/manuals/js/client/jsguide/

function setCookie(name, value, expire, path) {
	document.cookie = name + '=' + escape(value)
		+ ((expire == null)? '' : ('; expires=' + expire.toUTCString()))
		+ ((path == null)? '' : ('; path=' + path));
}

function getCookie(Name) {
	var search = Name + '='
	if (document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search) 
		if (offset != -1) { // if cookie exists 
			offset += search.length 
			// set index of beginning of value
			end = document.cookie.indexOf(';', offset) 
			// set index of end of cookie value
			if (end == -1) 
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		}
	}
	return '';
}


/*****************************
**   Common class methods
******************************/

function switchClass( objNode, strCurrClass, strNewClass ) {
	if ( matchClass( objNode, strNewClass ) ) replaceClass( objNode, strCurrClass, strNewClass );
		else replaceClass( objNode, strNewClass, strCurrClass );
}

function removeClass( objNode, strCurrClass ) {
	replaceClass( objNode, '', strCurrClass );
}

function addClass( objNode, strNewClass ) {
	replaceClass( objNode, strNewClass, '' );
}

function replaceClass( objNode, strNewClass, strCurrClass ) {
	var strOldClass = strNewClass;
	if ( strCurrClass && strCurrClass.length ){
		strCurrClass = strCurrClass.replace( /\s+(\S)/g, '|$1' );
		if ( strOldClass.length ) strOldClass += '|';
		strOldClass += strCurrClass;
	}
	objNode.className = objNode.className.replace( new RegExp('(^|\\s+)(' + strOldClass + ')($|\\s+)', 'g'), '$1' );
	objNode.className += ( (objNode.className.length)? ' ' : '' ) + strNewClass;
}

function matchClass( objNode, strCurrClass ) {
	return ( objNode && objNode.className.length && objNode.className.match( new RegExp('(^|\\s+)(' + strCurrClass + ')($|\\s+)') ) );
}

var pw;
var strg;
var was=false;
function Big_foto(filename,ww,hh,name)
{
	if(was && (!pw.closed)) pw.close();
	ww=ww+0;
	hh=hh+0;
	strg="";
	strg="toolbar=no,scrollbars=no,height="+hh+",width="+ww+",top=40,left=200";
	pw=window.open("", "zoom", strg);
	pw.document.write("<html><head><title>"+name+"</title></head><body bgcolor=#000000 topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 marginwidth=0 marginheight=0><center><a href='javascript:window.close();'><img src='"+filename+"' border='0'></a></center></body></html>");
	if(!was) was=true;
}

function cmnPlaceholder( eThis, sText, sClass_on_empty ){
	eThis.onfocus = function(){ if( eThis.value.length && eThis.value == sText ){ eThis.value = ''; } cmnRemove_class( this, sClass_on_empty ); eThis.select(); }
	eThis.onblur = function(){ if( !this.value.length ){ cmnSet_class( this, sClass_on_empty ); this.value = sText; } }
	if( !eThis.value.length ){ eThis.onblur(); }
	if( !eThis.getAttribute( 'placeholder' ) ){ eThis.setAttribute( 'placeholder', sText ); }
}
var cmn_aPopup_defaults = [
	["width", 720],
	["height", 650],
	["location", "no"],
	["menubar", "no"],
	["toolbar", "no"],
	["resizable", "yes"],
	["scrollbars", "yes"],
	["status", "yes"]
]

var cmn_aPopup = new Array();
function cmnPopup( sURL, sName, sFeatures, bReplace ){
	var sTarget = sName;
	if( !sURL.length ){
		if( sURL && sURL.tagName.toLowerCase() == "a" ){
			if( !sName ){ sName = ( sURL.id ) ? sURL.id : "_blank"; }
			sURL = sURL.href;
		}else{
			return false;
		}
	}else if( !sName ){
		sName = "_blank";
	}
	for( var i = 0 ; i < cmn_aPopup_defaults.length ; i++ ){
		if( !cmnPairs_string_get_value( sFeatures, cmn_aPopup_defaults[i][0], "=", "," ) ){
			sFeatures = cmnPairs_string_set_value( sFeatures, cmn_aPopup_defaults[i][0], cmn_aPopup_defaults[i][1], "=", "," );
		}
	}

	var iWidth = cmnPairs_string_get_value( sFeatures, "width", "=", "," );
	var iHeight = cmnPairs_string_get_value( sFeatures, "height", "=", "," );
	if( screen ){
		var iScreen_height = screen.height ? screen.height - 150 : 0;
		var iScreen_width = screen.width ? screen.width - 100 : 0;
		var bScroll = false;
		if( iScreen_height < iHeight ){ bScroll = true; iHeight = iScreen_height; sFeatures = cmnPairs_string_set_value( sFeatures, "height", iHeight, "=", "," ); }
		if( iScreen_width < iWidth ){ bScroll = true; iWidth = iScreen_width; sFeatures = cmnPairs_string_set_value( sFeatures, "width", iWidth, "=", "," ); }
		if( bScroll ) {
			sFeatures = cmnPairs_string_set_value( sFeatures, "scrollbars", "yes", "=", "," );
		}
		sFeatures = cmnPairs_string_set_value( sFeatures, "top", Math.round( ( iScreen_height - iHeight ) / 2 ), "=", "," );
		sFeatures = cmnPairs_string_set_value( sFeatures, "left", Math.round( ( iScreen_width - iWidth ) / 2 ), "=", "," );
	}

	if( sURL.match(/\.(gif|jpe?g|png)$/i) ){
		cmn_aPopup[sName] = window.open( "", sName, sFeatures );
		if( cmn_aPopup[sName] ){
			var sTitle = unescape( cmnPairs_string_get_value( sFeatures, "title", "=", "," ) );
			sTitle = sTitle.replace( /<\/?\w[^>]*>/g, " " ).replace( /</g, "&lt;" ).replace( /</g, "&lt;" ).replace( /"/g, "&quot;" );
			cmn_aPopup[sName].document.open();
			cmn_aPopup[sName].document.write('<html><head><title>'
				+ sTitle
				+ '</title></head><body bgcolor="white" style="margin: 0px; padding: 0px;"><table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center"><img src="'
				+ sURL + '" alt="'
				+ sTitle + '" /></td></tr></table></body></html>');
			cmn_aPopup[sName].document.close();
		}
	}else{
		var bWas_open = false;
		try{ bWas_open = ( cmn_aPopup[sName] && cmn_aPopup[sName].location.href != sURL ) }catch(e){}
		if( !cmn_aPopup[sName] || cmn_aPopup[sName].closed || !bWas_open ){
			if( sTarget && sTarget.match( /^_/ ) ){
				cmn_aPopup[sName] = window.open( sURL, sTarget );
			}else{
				cmn_aPopup[sName] = window.open( sURL, sName, sFeatures, bReplace );
			}
		}
	}
	if( sName != "_blank" ){
		cmn_aPopup[sName].focus();
	}

	return false;
}
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+)") ) );
}
