function $( id ) { return document.getElementById( id ); }

function ConfirmAct( num )
{
  if( num && num > 0 )
  {
    var mess = new Array('выключить', 'включить', 'обнулить', 'удалить', 'внести изменения', 'это сделать');
    return confirm('Вы действительно хотите ' + mess[num] + '?');
  }
  return true
}

function trim(str) 
{
  while (str.substring(str.length-1,str.length)==' ')
    str = str.substring(0, str.length-1);
  return str;
}

function strip(str) 
{
  str = str.split('"').join('&quot;')
  str = str.split('<').join('&lt;')
  str = str.split('>').join('&gt;')
  return str
}

function astrip(str)
{
  str = str.replace(/&lt;/ig, "<")
  str = str.replace(/&gt;/ig, ">")
  str = str.replace(/&quot;/ig, '"')
  str = str.replace(/&#039;/ig, "'")
  str = str.replace(/&amp;/ig, "&")
  return str
}

function GlSetLoad( id , item , inp )
{
	var iLoad = false;
	f = document.forms['f' + item + 'set']
	if ( f[item + '_id'].value != id )
	{
		f[item + '_id'].value = id
		iLoad = true;
		f.bt_set.disabled = true
	}
	f[inp].focus();
	return iLoad;
}

function GlAct( item, id , act , confirm )
{
	if (!ConfirmAct(confirm)) return
	f = document.forms['fmenu']
	inp_id = document.createElement("input")
	inp_id.setAttribute("type", 'hidden')
	inp_id.setAttribute("name", item + '_id')
	inp_id.setAttribute("value", id)
	f.appendChild(inp_id)
	inp = document.createElement("input")
	inp.setAttribute("type", 'hidden')
	inp.setAttribute("name", item + '_' + act)
	f.appendChild(inp)
	f.submit()
}


function createRequestObject()
{
	var r;
	var browser = navigator.appName ;
	if( browser == "Microsoft Internet Explorer" )
	{
		r = new ActiveXObject( "Microsoft.XMLHTTP" );
	} else
	{
		r = new XMLHttpRequest();
	}
	return r;
}


function r( id , r )
{
	var http = createRequestObject() ;
	var ms = new Array('gdr','zvz','ban');
	if( !ms[r] ) return false;	
	http.open( "get", '/bash/?rate&id=' + id + '&act=' + ms[r] );
	http.onreadystatechange = function()
	{
		if( http.readyState == 4 )
		{
			var m = http.responseText ;
			r = $('r'+id) ;
			if( m != '' ) r.innerHTML = m ;
		}
	}
	http.send( null ) ;
}

function cntTComm( m )
{
	$('cnt_tcomm_bar').style.backgroundColor = document.fcommset.text.value.length > m ? '#FF6347' : '' ;
	$('cnt_tcomm_bar').style.width = document.fcommset.text.value.length >= m ? 500 : 500 * document.fcommset.text.value.length / m ;
	$('cnt_tcomm_cnt').innerHTML = document.fcommset.text.value.length ;
}
function insText( text )
{
	var ta = document.fcommset.text ;
	if( ta.createTextRange && ta.caretPos )
	{
		var cPos = ta.caretPos;
		cPos.text = cPos.text.charAt( cPos.text.length - 1 ) == ' ' ? cPos.text + text + ' ' : cPos.text + text ;
		ta.focus();
	} else
	{
		ta.value  += text;
		ta.focus();
	}
}
var txt_quote='' ;
function getSel()
{
	txt_quote='';
	if( document.getSelection )
	{
		txt_quote = document.getSelection();
	} else if( document.selection )
	{
		txt_quote = document.selection.createRange().text;
	}
}
function insQuote( name )
{
	if( txt_quote.length < 1) 
	{
		alert( 'Выделите часть текста и нажмите Цитата.' );
	} else
	{
		document.fcommset.text.value += '[quote=' + name + ']' + txt_quote + '[/quote]' ;
	}
}

function insSm( id )
{
	var f = $( 'text' ) ;
	var sm = ' :' + ( 1000 + parseInt( id ) ) + ': ' ;
	if( document.selection )
	{
		f.focus();
		var s = document.selection.createRange();
		s.text = sm ;
		f.focus();
	} else if( f.selectionStart || f.selectionStart == '0' )
	{
		var sP = f.selectionStart;
		var eP = f.selectionEnd;
		var cP = eP;
		f.value = f.value.substring( 0, sP ) + sm + f.value.substring( eP , f.value.length);
		cP = sP + sT.length + eT.length;
		f.focus();
		f.selectionStart = cP ;
		f.selectionEnd = cP ;
	} else
	{
		f.value += sm ;
		f.focus();
	}
}

function insTag( i )
{
	var f = $( 'text' );
	var sT = '[' + i + ']' ;
	var eT = '[/' + i + ']' ;
	if( document.selection )
	{
		f.focus();
		var s = document.selection.createRange();
		if( s.text.length > 0 )
		{
			s.text = sT + s.text + eT ;
		} else
		{
			s.text = sT + eT ;
		}
		f.focus();
	} else if( f.selectionStart || f.selectionStart == '0' )
	{
		var sP = f.selectionStart;
		var eP = f.selectionEnd;
		var cP = eP;
		if( sP != eP )
		{
			f.value = f.value.substring( 0, sP ) + sT + f.value.substring( sP , eP) + eT + f.value.substring( eP , f.value.length ) ;
			cP += sT.length + eT.length;
		} else
		{
			f.value = f.value.substring( 0, sP ) + sT + eT + f.value.substring( eP , f.value.length);
 			cP = sP + sT.length + eT.length;
		}
		f.focus();
		f.selectionStart = cP ;
		f.selectionEnd = cP ;
	} else
	{
		f.value += sT + eT ;
		f.focus();
	}
}
