function set_cookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Tue, 14 Dec 2010 23:59:59 GMT; path=/;";
}



function site_map(){	
	form = $('site_map');	
	if(form.style.display == 'none'){
		form.show();
	} else {
		form.hide();	
	}	
return false;
}


function popup(url, width, height)
{
  wx = (screen.availWidth - width)/2;
  wy = (screen.availHeight - height)/2;

  return window.open(url, "popup", "width="+width+",height="+height+",status=no,resizable=0,scrollbars=1,toolbar=no,menubar=no,left="+wx+",top="+wy);
}

function popupr(url, width, height)
{
  wx = (screen.availWidth - width)/2;
  wy = (screen.availHeight - height)/2;

  return window.open(url, "popup", "width="+width+",height="+height+",status=no,resizable=1,scrollbars=1,toolbar=no,menubar=no,left="+wx+",top="+wy);
}


function confirm_form(Query){
	
	var is_confirmed = confirm(Query);
	if (is_confirmed) {
		return true;        
	}
	return false;
} 

function expand(Item) { 
	dom = (document.getElementById); 
	ie4 = (document.all); 
	if (dom || ie4) { 
	  var currElement = (dom) ? document.getElementById(Item) : document.all[Item]; 
	  currElement.style.display = (currElement.style.display == 'none')? 'block' : 'none'; 
	  return false; 
	} 
	else return true; 
}


function autoAttachEvent(element, type, handler)
{
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else
	{
		if (!handler.$$guid) handler.$$guid = autoAttachEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers)
		{
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}

		handlers[handler.$$guid] = handler;
	}
}
autoAttachEvent.guid = 1;

function handleEvent(event)
{
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)
	{
		if (!Object.prototype[i])
		{
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}

	if (this.$$handler) this.$$handler = null;

	return returnValue;
}


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 tab(id){

	t1=(!t1)?"tab_1":t1; 
	d1=(!d1)?"div_1":d1;  

	var tab1 = 'tab_'+id; 
	
	if ( t1 == tab1) return false;

	if (t1) { $(t1+'_on').hide(); $(t1).show(); } 
	$(tab1+'_on').show(); $(tab1).hide();
	t1 = tab1;    

	if (d1) { $(d1).hide(); } 
	var div1 = 'div_'+id;
	$(div1).show();
	d1 = div1;    

	return false;
}


var t = d = '';


function tab_small(id){

	var tab = 'tab_'+id; 
	if ( t == tab) return false;
	$(tab).className='tab_on';
	if (t) { $(t).className = 'tab_off'; } t = tab;    
	if (d) { $(d).hide(); } 
	var div = 'div_'+id;
	$(div).show();
	d = div;    
	return false;
}



function tab1(id){

	
	t1=(!t1)?"tab_b_1":t1; 
	d1=(!d1)?"div_b_1":d1;  

	var tab1 = 'tab_'+id; 
	
	if ( t1 == tab1) return false;

	// alert (t1);
	
	if (t1) { $(t1+'_on').hide(); $(t1).show(); } 
	$(tab1+'_on').show(); $(tab1).hide();
	t1 = tab1;    


	if (d1) { $(d1).hide(); } 
	var div1 = 'div_'+id;
	$(div1).show();
	d1 = div1;    

	return false;
}


function tab2(id){

	t2 = (!t2)?"tab_t_1":t2; 
	d2 = (!d2)?"div_t_1":d2;  

	var tab2 = 'tab_'+id; 
	
	if ( t2 == tab2) return false;

	if (t2) { $(t2+'_on').hide(); $(t2).show(); } 
	$(tab2+'_on').show(); $(tab2).hide();
	t2 = tab2;    


	if (d2) { $(d2).hide(); } 
	var div2 = 'div_'+id;
	$(div2).show();
	d2 = div2;    

	return false;
}



function initHLTable(tId)
{
		if (!document.getElementById(tId)) return false;

	var cTable = document.getElementById(tId).tBodies[0];
	if (!cTable) return false;
	for (var i = 0; i < cTable.rows.length; i++)
	{
		if (cTable.rows[i].className.indexOf('head') == -1 && cTable.rows[i].className.indexOf('inputs') == -1)
		{
			autoAttachEvent(cTable.rows[i],'mouseover',function() {addClass(this,'hlRow');});
			autoAttachEvent(cTable.rows[i],'mouseout',function() {removeClass(this,'hlRow');});
		}
	}
}


function topmenu( id, state ) {

	if (!id) return;
	
	if ( state  == "on" ) { 
	
		$(id).style.display="block" ; 
		if ($("t_"+id)) $("t_"+id).addClassName('hovermenu');
		
		} else { 
			
			$(id).hide(); 
			if ($("t_"+id)) $("t_"+id).removeClassName('hovermenu');
			}

}


function _M( id, state ) {

	if (!id) return;
	
	if ( state  == "on" ) { 
								
		$("div#" + id).show();

		
		if ($("#t_"+id)) { 
			 
			 $("#t_"+id).addClass("hovermenu"); 
			 $("#t_"+id).corner("cc:#5E9799 top");

			 }

		} else { 
			
			$("div#" +id).hide(); 
			if ($("#t_"+id)) $("#t_"+id).removeClass("hovermenu");
			}

}


// Уставливаем в куки часовой пояс пользователя для новостей и календаря событий.
if ( !$.cookie('time_zone') ) { var d = new Date(); var gmtHours = -d.getTimezoneOffset()/60 ; $.cookie('time_zone', gmtHours);  }


// 
nn4 = (document.layers) ? true : false;
ie4 = (document.all) ? true : false;
dom = (document.createTextNode)? true : false;

function popupWindow(fileUrl, winW, winH, winN, scrollB) {
/* var winWidth = (winW)? winW : 740;
  var winHeight = (winH)? winH : 520;*/
  var winWidth = winW;
  var winHeight = winH;
  var winName = (winN)? winN : 'popupWin'
  var scrollBars = (scrollB)? scrollB : 'auto'
  if (nn4 || ie4 || dom) {
	if (screen.width < winWidth + 50) { winWidth = screen.width - 50; scrollbars = 'yes' }
	if (screen.height < winHeight + 100) { winHeight = screen.height - 100; scrollbars = 'yes' }
	posX = Math.round((screen.width - winWidth) / 2);
	posY = Math.round((screen.height - winHeight) / 2);
	posCode = (nn4 || dom)? "screenX="+posX+",screenY="+posY : "left="+posX+",top="+posY;
  } else {
	posCode = "";
  }
  var popupWin = window.open(fileUrl, winName,"menubar=yes,toolbar=no,scrollbars=" + scrollBars + ",status=yes,resizable=yes,width=" + winWidth + ",height=" + winHeight + "," + posCode);
  if (popupWin) popupWin.focus();
}




function validateEmail( emailValue ) {
var email = document.getElementById('emailaddress');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus
return false;
}
}


function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

	//check for all spaces
	if(objRegExp.test(strValue)) {
	   strValue = strValue.replace(objRegExp, '');
	   if( strValue.length == 0)
		  return strValue;
	}

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
	   //remove leading and trailing whitespace characters
	   strValue = strValue.replace(objRegExp, '$2');
	}
  return strValue;
}


function validateEmail( strValue) {
var pattern  = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/i;
  //check for valid email
  return pattern.test(strValue);
}


function validateLength( strValue, lenghtValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > lenghtValue){
	 return true;
   }
   return false;
}


function checkCommentForm () {

   if ( !validateLength ($('#f_User').val(), 2) ) {
	   alert ("Укажите свое имя\nИмя должно быть не менее 3 символов")  ;
	   $('#f_User').focus();
	   return false;
   }
   /*
   if ( !validateEmail ($('#f_Email').val()) ) {
	   alert ("Укажите правильный адрес электронной почты.\nМы не публикуем полученные адреса и продаем их спаммерам");
	   $('#f_Email').focus();
	   return false;

   }
*/
   if ( !validateLength ($('#f_Message').val(), 6) ) {
	   alert ("Текст комментария должен быть не менее 7 символов")  ;
	   $('#f_Message').focus();
	   return false;
   }

   if ( !validateLength ($('#nc_comment_captcha_code').val(), 1) ) {
	   alert ("Укажите защитный код, указанный на картинке")  ;
	   $('#nc_captcha_code').focus();
	   return false;
   }

   return true;

// if (!validateEmail(form))

}


function getStrRev ( string ) {

	var ret = '', i = 0;
	string += '';
	for ( i = string.length-1; i >= 0; i-- ){
	   ret += string.charAt(i);
	}
	return ret;
}


function fuckMailSpammers ( string ) { 

	document.write('<a href=mailto:' + getStrRev(string) + '>' + getStrRev(string) + '</a>');
}

