var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);
var theSelection = false;


function b_code(theform, fname, vbcode) 
{
  b_wrap(theform[fname], "["+vbcode+"]", "[/"+vbcode+"]");
}

function b_wrap(txtarea, open, close, wnd) 
{
  if(wnd == null) 
    wnd = window;
    
  txtarea.focus();

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = wnd.document.selection.createRange().text; // Get text selection
		//if (theSelection) {
			// Add tags around selection
			wnd.document.selection.createRange().text = open + theSelection + close;
			txtarea.focus();
			theSelection = '';
		//}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart >= 0))
	{
		mozWrap(txtarea, open, close);
		return;
	} else {
	  txtarea.value += open + close;
	}
}
function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	var currentScroll = txtarea.scrollTop;
	//if (selEnd == 1 || selEnd == 2) 
	//	selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;

	txtarea.scrollTop = currentScroll;
	var addLength = open.length + close.length;
	txtarea.setSelectionRange(selEnd + addLength, selEnd + addLength);
	txtarea.focus();

	return;
}




function b_img(theform,fname)
{
 var w = window.open('/cgi-bin/main.cgi?act=photogl.upload&insert=' + theform.name + '.' + fname,'','toolbar=no,location=no,status=no,menubar=no,height=350,width=500,resizable=yes,scrollbars=yes');
 w.focus();
// var w = '/cgi-bin/main.cgi?act=photogl.upload&insert=' + theform.name + '.' + fname;
// lightMedia(w);
}

function b_url(theform,fname,tag)
{
  var url = prompt('URL ссылки:','http://');
  var text = prompt('Текст ссылки:','');
  if(tag == null)
  	tag='URL';
  	
  b_wrap(theform[fname], '', "[" + tag + "=" + url + "]" + text + "[/" + tag + "]");
}  

function b_getSelection(){ 
   if      (window.getSelection)   return window.getSelection().toString();      
   else if (document.getSelection) return document.getSelection();                
   else if (document.selection)   return document.selection.createRange().text;  
}

function b_quote(theform, fname) 
{
  var txtarea = theform[fname];
  txtarea.value+= '[QUOTE]' + b_getSelection() + '[/QUOTE]';
  txtarea.focus();
}


function b_insert_image(fname,src, alt, preview)
{
 eval("b_wrap(window.opener.document.forms." + fname + ", '[IMG=' + src + (alt != '' ? ' ALT=\"' + alt + '\"' : '') + (preview != '' ? ' PREVIEW=\"' + preview + '\"' : '') + ']', '', window.opener)");
 //eval("window.opener." + fname + ".value += '[IMG=' + src + (alt != '' ? ' ALT=' + alt : '') + ']'");
 window.close();
 return false;
}

function b_insert_album(fname, albumid)
{
 eval("b_wrap(window.opener.document.forms." + fname + ", '[ALBUM=' + albumid + ']', '', window.opener)");
 window.close();
 return false;
}

function b_video(theform,fname)
{
  var code = prompt('Код для вставки (YouTube, GoogleVideo, video.mail.ru и др.) с пометкой embed:','');
  b_wrap(theform[fname],'',code);
}  

function b_toggle(id)
{
   if(document.getElementById(id).style.display=="none"){
      document.getElementById(id).style.display="block";
    }else{
      document.getElementById(id).style.display="none";
    }
}

function b_openWindow(url,width,height)
{
  window.open(url,'','resizable=yes, menubar=no, status=no, scrollbars=yes, statusbar=no, toolbar=no, width=' + width + ', height=' + height).focus();
  return false;
}


var last_com_id = null;
var form_html = null;
function b_reply(id) 
{
  var div_com = document.getElementById('b_com-' + id);
  var base_form = document.getElementById('b_form-base');
  if(null != div_com && null != base_form) {
    // get form code
    if(form_html == null) {
      form_html =  base_form.innerHTML;
       base_form.innerHTML = '';
    }
    
    // hide prev
    var show=1;
    if(last_com_id != null) {
      document.getElementById('b_com-' + last_com_id).removeChild(document.getElementById('b_form-' + last_com_id));
      if(last_com_id == id){
        last_com_id = null;
        show = 0;
      }
    }

    // show form
    if(show) {
      var div_form = document.createElement("div");
      div_form.setAttribute("id", "b_form-" + id);
      div_form.setAttribute("class", "b-formcomment");
      div_form.innerHTML = form_html;
      div_com.appendChild(div_form);
      if(document.getElementsByName('commentid').length>0) {
      	document.getElementsByName('commentid')[0].removeAttribute("value");
      	document.getElementsByName('commentid')[0].setAttribute("value", id);
      	div_form.getElementsByTagName('textarea')[0].focus();
      }
      last_com_id = id;
    }
  } else {
   alert("Добавление комментария невозможно");
  }
}    
    
    
// Отображает форму извещения админа
function b_notify_form(type, id, show) 
{
  var el = document.getElementById('notify' + type + id);
  if(null != el) {
    if(show) {
     el.style.left = 500;
     el.innerHTML = '<form action="/cgi-bin/main.cgi" method=POST>' +
      '<input type="hidden" name="act" value="blog.donotify">' +
      '<input type="hidden" name="type" value="' + type + '">' +
      '<input type="hidden" name="id" value="' + id + '">' +
    	'<textarea class="forminput" name="message" cols=35 rows=5 wrap=virtual>текст сообщения</textarea><br>' +
    	'<input type="submit" value="Отправить" class="formsubmit"> <input type="button" onClick="return b_notify_form(\'' + type + '\',' + id + ',0)" class="formsubmit" value="Отмена">' +
    	'</form>';

      el.style.visibility = 'visible';
    } else {
     el.innerHTML = '';
     el.style.visibility = 'hidden';
    }
  }
  return false;
}
     

var show_state = new Array;
function toggle_visible(id, initial_state)
{
	if(show_state[id] != null && show_state[id] == 1 || show_state[id] == null && initial_state) {
		document.getElementById(id).style.display = 'none';
		show_state[id] = 0;
	} else {
		document.getElementById(id).style.display = 'block';
		show_state[id] = 1;
	}
	return false;
}

//var is_rated  = new Array;
function b_rate(object, id, rating, container, check)
{
  //if(!is_rated[object + id]) {
  	//is_rated[object + id] = 1;
  	M_loadScript('/cgi-bin/main.cgi?act=rating.dorating&object=' + object + '&id=' + id + '&rating=' + rating + '&container=' + container + '&check=' + check + '&rnd=' + Math.random());
  //} 
  return false;
}

function M_loadScript(link)
{
     var script = document.createElement('script');
     script.defer = false;
     script.src = link;
     document.getElementsByTagName('body')[0].appendChild(script);
} 

function b_alert(text) {

  /*** Fine alert ***/
	/*
	var div3 = document.createElement('div');
  div3.className = 'b-alert';
	div3.style.top = document.body.scrollTop;
	div3.style.display = 'block';
	div3.innerHTML = text;
  document.body.appendChild(div3);

	setTimeout( function () { div3.style.display = 'none'; }, 5000);
	*/


	/*** Normal alert (popup) ***/
	//alert(text);
	

	return false;
}
  

