var loc = window.location;
var mis;
var Start_end = 50;
var Perebor_alert = 256;
var m_begin;
var m_end;
var ex  = 0;

var full;
nN = navigator.appName; 

function TrimString(sInString){
sInString = sInString.replace('/ /g',' ');
return sInString.replace('/(^\s+)|(\s+$)/g', '');
}


 function find_sel_context(chars){
  var 
    sel=window.getSelection(),
    text=[],
    pos=[],
    len=0;
	
	var st_fin = 3;
	
	

  function traverse(node){
    if(node.nodeType==3){
      if(node==sel.focusNode)
        pos.push(len+sel.focusOffset);
      if(node==sel.anchorNode)
        pos.push(len+sel.anchorOffset);
      text.push(node.nodeValue);
      len+=node.nodeValue.length;
    } else {
      var cn=node.childNodes;
      for(var i=0;cn&&i<cn.length;i++)
        traverse(cn[i]);
    }
  }
  traverse(document.body);
  pos.sort();	
	
	for (var i = 0; i <= text.length-1; i++)		
	{				
															  
			if (text[i].search(text.join('').substring(pos[0]-10,pos[1])) != -1) {
					st_fin = true;
					break;		
			}
					
			if (text[i].search(text.join('').substring(pos[0],pos[1]+10)) != -1) {	
				st_fin = false;
				break;
			}
			
			/*if (text[i].search(mis) != -1) {	
				st_fin = 4;				
				break;
			}*/
				
								
	}
	
	m_begin = text[i];
	
	/*if (st_fin == 4)
		{
			m_begin = ' ';
			m_end = ' ';
			return;
		}*/
	
	
	if (st_fin == 3)
	{m_begin = ' ';
			m_end = ' ';
		alert('Регистрация ошибки возможна непосредственно в тексе материала');
		window.refresh();
				ex = 1;
				return false;
	}
	else
	{
	
	if (st_fin)
	{
		bred = text.join('').substring(pos[0]-10,pos[1]-mis.length);
		//to4nobred =  text.join('').substring(pos[0]-10,pos[1]);
		m_begin = m_begin.substring(0,m_begin.search(bred)+bred.length);
		
		
		
	}
		else 
		{
			
			m_begin = m_begin.substring(0,m_begin.search(text.join('').substring(pos[0],pos[1]+10)));
		}	}
	
	
	m_end = text[i].slice( m_begin.length,text[i].length );
	
	//text.join('').substring(pos[0]-chars,pos[1])[0];
 //	m_end=text.join('').substring(pos[0],pos[1]+chars);
 
 	
  return true;//text.join('').substring(pos[0]-chars,pos[1]+chars);
}



function hideFlash(){
    f = document.getElementsByName("flash_!");
    for(var i=0; i<f.length; i++){
       f[i].style.display='none';
    }
}


function ProcessMdWin()
{
	
		if (mis.length == 0)
			return;
			
		if (mis.length >= Perebor_alert)
		{
			alert('Введен слишком длинный текст!');
			return;
		}
		
		
		if(find_sel_context(Start_end));
		{
		if (ex != 0) return;
		m_end = m_end.slice(mis.length);
		m_end = m_end.slice(0,m_end.lastIndexOf(' '));
		
		winop();
		}	
}

function strip(html) {
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent||tmp.innerText;
}

function winop()
{
hideFlash();
mb_load();
}

function getText(e) 
{	
	if (!e) e= window.event; 
	if((e.ctrlKey) && ((e.keyCode==10)||(e.keyCode==13))) 
	{ 
		if(nN == 'Microsoft Internet Explorer')
			{if(document.selection.createRange())
					{var range = document.selection.createRange();
       						mis = range.text;	
							if (mis.length == 0)
									return;
							if (mis.length >> Perebor_alert)
								{
									alert('Введен слишком длинный текст!');
									window.refresh();
									return;
								}
							range.moveStart("character",-Start_end);				 
							m_begin = range.text; 
							m_begin = m_begin.slice(0,  Start_end)
													
													
							range.moveEnd("character",Start_end*2 ); 
							m_end = range.text;
							m_end = m_end.slice(Start_end + mis.length-1);
							winop();
					}
			} 
else 
{if (window.getSelection()) 
{mis = window.getSelection().toString();
	ProcessMdWin();


} 
else if(document.getSelection())
{mis = document.getSelection();
	ProcessMdWin();
}}	
    return true;} 
    return true;}

document.onkeypress = getText;








