/**
 @package 	Linso
 @subpackage 	Common
 @copyright 	ООО "Портал" 2008 г.
 @autor : Маклыгин Дмитрий 
*/

function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		
		obj.addEventListener(evType, fn, false);
		return true;
	} 
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} 
	else return false;
}

function removeEvent(obj, evType, fn, useCapture)
{
	if (obj.removeEventListener)
	{
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} 
	else if (obj.detachEvent)
	{
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} 
	else alert("Handler could not be removed");
}

function getViewportHeight() 
{
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}

function getViewportWidth() 
{
	var offset = 17;
	var width = null;
	
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

function getScrollTop() 
{
	if (self.pageYOffset) 
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) 
	{
		return document.body.scrollTop;
	}
}

function getScrollLeft() 
{
	if (self.pageXOffset) 
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)		
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) 
	{
		return document.body.scrollLeft;
	}
}

urlEvent = false;

function gotoUrl(url)
{
	if (urlEvent == null)
	{
		if(url == 'none' || url == '') return;
		else document.location = url;		

		urlEvent = true;		
	}
	
	setTimeout("urlEvent = null;", 100);
}
	
function $(e)
{
	return document.getElementById(e);
}

function $$(e)
{
	return document.getElementsByTagName(e);
} 

function include(url) { 
      // Простая проверка переданной переменной 
      if (typeof(url) == "undefined" || url.length == 0) { 
            alert("URL is not correct"); 
            return; 
      } 
      url = url.toString(); 
      // Создаем элемент SCRIPT 
      var script = document.createElement("SCRIPT"); 
      script.type = "text/javascript"; 
      // Присваиваем передаваемый URL 
      script.src = url; 
      // Добавляем в HEAD, если таковой имеется 
      if (document.getElementsByTagName("head")[0]) { 
            var head = document.getElementsByTagName("head")[0]; 
            head.appendChild(script); 
      } else { 
            alert("HTML-document is not correct."); 
            return; 
      } 
}

// Убирание левых пробелов
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
// Убирание правых пробелов
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

// Округление до XXX


// Показывает \ скрывает DIV
function Show_modal(name)
 {
	$(name).className = ( $(name).className == "hide") ? "show" : "hide";
 }
 
 
 

	/**
	*	Function   : Формирует строку к файлу
	*
	*	@param     : string id_user
	*	@param     : string path
	*/
	
	function getPathToFile(id_user , path)
	 {
		
		if(id_user != undefined)
		  {
			 			 
			 for (var i = (id_user.toString().length - 1); i >= 0; i--)
			   { 
			     path = path + id_user.toString().substr(i,1) + '/';
			   }
			 
			 return path;			  
		  }
		 else 
		   return false;
	 }
	
	
	/**
	*	Function   : Формирует строку модального окна к юзеру
	*
	*	@param     : string id_user
	*	@param     : string path
	*/
	
	function getStringForUser( id_user , nick ,  cssclass , t_tag)
	 {
		if((id_user == undefined)||(nick == undefined)) 
		  return false;
		
		var str = '<'+t_tag+' class="'+cssclass+'" onclick="CustomAjaxDialog(event, \'/-_user-ajax?userid='+id_user+'\')">'+nick+'</'+t_tag+'>';
		
		return str;
	 }
	
	function UrlEncodeCust(s) {
		
		s = encodeURIComponent(s);		
		return s.replace('%','%25','g').replace('&','%26','g').replace('+','%2B','g').replace('?','%3F','g');
	}
	
	function UrlDecodeCust(s) {
		
		s = decodeURIComponent(s);	
		
		return s.replace('+',' ','g').replace('%25','%','g').replace('%26','&','g').replace('%2B','+','g').replace('%3F','?','g');
	}
	
	 
	/**
	*	СОздание Диалогового окна. 
	*   Yes \ No
	*
	*/
	function DialogYesNo(message , header , onsubmit ,  text_ok  , text_cancel )
	 {
		
		var str_dialog = '';
		 
		 str_dialog   = '<form method="post" class="global_form">\n';
		 str_dialog  += '<div class="global_pubmainz" style="color:#FF0000">' + header + '</div>\n'; 
		 str_dialog  += '<div class="global_h15px"></div>\n'; 
		 str_dialog  += '<div class="global_pubsubz">' + message + '</div>\n';
		 str_dialog  += '<div class="global_h15px"></div>\n'; 
		 str_dialog  += '<div align="right" class="global_buttonstl">\n';
		 str_dialog  += '<input name="dialog_yes" type="button" onClick="'+onsubmit+'" value="'+ text_ok +'" />\n';
		 str_dialog  += '</div>\n';
		 str_dialog  += '<div align="right" class="global_buttonstl">\n';
		 str_dialog  += '<input name="dialog_no" type="button" onClick="hidePopWin();" value="'+ text_cancel +'" />\n';
		 str_dialog  += '</div>\n';
		 str_dialog  += '</form>\n';
		 
		 return str_dialog;
		 
	 }
	
	/**
	*	СОздание Диалогового окна. 
	*   Okey (alert)
	*
	*/
	function DialogAlert(message , header , text_ok , callback)
	 {
		
		 
		 var div = jQuery('<div></div>');
		 
		 var div_h = jQuery('<div></div>');
	  	  	 div_h.attr("class", "global_pubmainz" );
	  	  	 div_h.css("color", "#00FF00" );
	  	  	 div_h.html(header);
	  	 
	  	 var br = jQuery('<br/>');
	  	 
	  	 var div_m = jQuery('<div></div>');
	  	  	 div_m.attr("class", "global_pubsubz" );	  	  	
	  	  	 div_m.html(message);
	  	 
	  	 var div_t = jQuery('<div></div>');
	  	  	 div_t.attr("class", "global_buttonstl" );	
	  	  	 div_t.attr("align", "right" );  
	  	 
	  	 var btn = jQuery('<input type="button" />');	  	  	 
	  	  	 btn.attr("value", text_ok );  	  	  	 
	  	  	 if(!callback)
	  	  	  	btn.attr("onclick", "hidePopWin()" );  	
	  	  	 else
	  	  	 	btn.click(callback); 
	  	  	 	 	  	  	 
	  	  	 btn.attr("onfocus" , "this.blur()");  
	  	 
	  	 div_t.append(btn);
	  	 
	  	 div.append(div_h);
	  	 div.append(br);
	  	 div.append(div_m);	  	 
	  	 div.append(br);
	  	 div.append(div_t);	
	  	 
	  	 /*
		 str_dialog   = '<div class="global_pubmainz" style="color:#00FF00">'+header+'</div><br />\n';
		 str_dialog  += '<div class="global_pubsubz">'+message+'</div>\n'; 
		 str_dialog  += '<br />\n'; 
		 str_dialog  += '<div align="right" class="global_buttonstl">\n';
		 str_dialog  += '<input type="button" value="'+text_ok+'" onclick="hidePopWin()" onfocus="this.blur();" />\n'; 
		 str_dialog  += '</div>\n';		*/
		 
		 if(!callback) return div.html();
		 else return div;		 
	 }
	
	
	
	function addBookmark(url, title)
	{
	  if (!url) url = location.href;
	  if (!title) title = document.title;
	  
	  //Gecko
	  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
	  //IE4+
	  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
	  //Opera7+
	  else if (window.opera && document.createElement)
	  {
		var a = document.createElement('A');
		if (!a) return false; //IF Opera 6
		a.setAttribute('rel','sidebar');
		a.setAttribute('href',url);
		a.setAttribute('title',title);
		a.click();
	  }
	  else return false;
	  
	  return true;
	}	
		
	
			
var ExTitle = {
	rendered : false,
	render:function() {
		if(this.rendered) return;
		this.rendered = true;
		this.tooltipDiv = document.createElement("DIV");
		this.tooltipDiv.className = 'global_tooltip';
		this.tooltipDiv.style.position = "absolute";	
		document.body.appendChild(this.tooltipDiv);
	},
	
	moveToCursor:function(e) {
		this.tooltipDiv.style.top = e.clientY + document.body.scrollTop+23 + "px";
		this.tooltipDiv.style.left = e.clientX + "px";
	},
	
	show:function(elem) {	
		this.tooltipDiv.innerHTML = elem.title;
		elem.title = '';
		this.tooltipDiv.style.display = "block";
	},
	
	hide:function(elem) {
		elem.title = this.tooltipDiv.innerHTML;
		this.tooltipDiv.style.display = "none";
	},
	
	init:function(jqueryman)
	{
		this.render();
		jqueryman.mouseover( function() { ExTitle.show(this) } );
		jqueryman.mouseout( function() { ExTitle.hide(this) } );
		jqueryman.mousemove( function(e) { ExTitle.moveToCursor(e); } );
	}
};	

	
/*
* Кодирует Кирилицу в Латиницу
*/
function parseLatin(text){
	var outtext = text;
	var lat1 = ["yo","zh","kh","ts","ch","sch","shch","sh","eh","yu","ya","YO","ZH","KH","TS","CH","SCH","SHCH","SH","EH","YU","YA","'"];
	var rus1 = ["ё", "ж", "х", "ц", "ч", "щ",  "щ",   "ш", "э", "ю", "я", "Ё", "Ж", "Х", "Ц", "Ч", "Щ",  "Щ",   "Ш", "Э", "Ю", "Я", "ь"];
	for(var i=0;i<lat1.length;i++){
		outtext = outtext.split(lat1[i]).join(rus1[i]);
	}
	var lat2 = "abvgdezijklmnoprstufhcyABVGDEZIJKLMNOPRSTUFHCY";
	var rus2 = "абвгдезийклмнопрстуфхцыАБВГДЕЗИЙКЛМНОПРСТУФХЦЫ";
	for(var i=0;i<lat2.length;i++){
		outtext = outtext.split(lat2[i]).join(rus2[i]);
	}

	return (outtext==text)?null:outtext;
}	
			
		
function protectmail(login,domen,text)
{
document.write('<a href="mailto:'+login+'@'+domen+'" class="headerlink">'+text+'</a>');
}	