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_url(theform,fname)
{
  var url = prompt('URL ссылки:','http://');
  var text = prompt('Текст ссылки:','');
  b_wrap(theform[fname], '', "[URL=" + url + "]" + text + "[/URL]");
}  

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();
}



isDOM=document.getElementById //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
isMSIE=document.all && document.all.item //Microsoft Internet Explorer 4+
isNetscape4=document.layers //Netscape 4.*
isOpera=window.opera //Opera
isOpera5=isOpera && isDOM //Opera 5+
isMSIE5=isDOM && isMSIE && !isOpera //MSIE 5+
isMozilla=isNetscape6=isDOM && !isMSIE && !isOpera


function ShowMedia(parent,id,mediaid) 
{
  window.open('/cgi-bin/main.cgi?module=media&act=show&parent='+parent+'&id=' + id + '&mediaid=' + mediaid,'','resizable=yes, menubar=no, status=no, scrollbars=yes, statusbar=no, toolbar=no, width=550, height=540');
}


function ShowDiv(id)
{
  if (document.getElementById) {
    var obj = document.getElementById(id);
    if(obj.style.display != 'block')
     obj.style.display = 'block';
    else 
     obj.style.display = 'none';    
    return false;
  }
  return true;
}

function CloseDiv(id)
{
  if (document.getElementById) {
    document.getElementById(id).style.display = 'none';    
  }
  return false;
}



//------ Функции магазина -------

function add_cart(itemid)
{
var num =  document.getElementById('num' + itemid) != null ? document.getElementById('num' + itemid).value : 1;
 cart_window = window.open('/cgi-bin/main.cgi?module=shop&act=addcart&itemid='+itemid+'&num='+num,'cart','toolbar=no,location=no,status=no,menubar=no,height=350,width=450,resizable=yes,scrollbars=yes'); 
 cart_window.focus();
 
 return false;
}

function add_cart_multi(ids)
{
  var url = '/cgi-bin/main.cgi?module=shop&act=addcart&num=1';
  for(var i=0; i<ids.length; i++) {
    url+= '&itemid=' + ids[i];
  }
  cart_window = window.open(url,'cart','toolbar=no,location=no,status=no,menubar=no,height=350,width=450,resizable=yes,scrollbars=yes'); 
  cart_window.focus();
 
  return false;
}
