isDOM = document.getElementById; //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
isOpera = isOpera5 = window.opera && isDOM; //Opera 5+
isOpera6 = isOpera && window.print; //Opera 6+
isOpera7 = isOpera && document.readyState; //Opera 7+
isMSIE = document.all && document.all.item && !isOpera; //Microsoft Internet Explorer 4+
isMSIE5 = isDOM && isMSIE; //MSIE 5+
isNetscape4 = document.layers; //Netscape 4.*
isMozilla = isDOM && navigator.appName == "Netscape"; //Mozilla или Netscape 6.*

 function addElement(btn){

        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;


                var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
                thisChilds = tr.getElementsByTagName('td');
                thisNew = newTr.getElementsByTagName('td');

                for (var i = 0; i < thisChilds.length; i++){

                        if(thisChilds[i].className != 'tmbutton' && thisChilds[i].className != 'tmcomments')
                        {
                          //alert(thisChilds[i].innerHTML);
                          thisNew[i].innerHTML=thisChilds[i].innerHTML;
                        }

                        if (thisChilds[i].className == 'tmcomments') thisChilds[i].innerHTML = '';
                        if (thisChilds[i].className == 'tmbutton') 
                        {
                         thisChilds[i].innerHTML = '<input type="button" name="add" value="Удалить" onClick=dropElement(this)>';
                        }


                }

                thisChildsInp = tr.getElementsByTagName('input');
                thisNewInp = newTr.getElementsByTagName('input');

                for (var i = 0; i < thisChildsInp.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp[i].type!='button') thisNewInp[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp[i].id=thisChildsInp[i].id;
                 thisChildsInp[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }


                thisChildsInp2 = tr.getElementsByTagName('textarea');
                thisNewInp2 = newTr.getElementsByTagName('textarea');

                for (var i = 0; i < thisChildsInp2.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp2[i].type!='button') thisNewInp2[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp2[i].id=thisChildsInp2[i].id;
                 thisChildsInp2[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }

        }
}




function dropElement(btn){
        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr.parentNode.removeChild(tr);
        }
}

function deleteprop(id, name)
{
  //удаление свойства. пробуем удалять подгрузкой изображения...
//  alert("/deleteprop.html?id="+id+"&name="+name+"&type=img0");
  var tmpimg=new Image();

 // tmpimg.src="/deleteprop.html?id="+id+"&name="+name+"&type=img0";
  window.open("/deleteprop.html?id="+id+"&name="+name+"&type=newwin",'','width=10,height=10,title=no,resizable=no,scrollbars=no');

}


function gotomypage(mylink)
{
  window.location.href="/users/"+mylink+".html";
}

//navigator

imgplus= new Image();
imgminus= new Image();
imgplus.src='/fig/plus.jpg';
imgminus.src='/fig/minus.jpg';

function setCookie(name, value, expires, path, domain, secure) 
{
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ("; path=/" ) +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");

                document.cookie = curCookie;
}


function TX_swapstyle(elementid, imgid, cid)
{

   if(document.getElementById(elementid).style.display!="none")
   {
     document.getElementById(elementid).style.display="none";
     document.getElementById(imgid).src=imgplus.src;

     setCookie(cid, '0');
   }
   else
   {
     document.getElementById(elementid).style.display="block";
     document.getElementById(imgid).src=imgminus.src;
     setCookie(cid, '1');

   }
   return false;

}

function insertvalue(elid, val)
{
 window.opener.document.getElementById(elid).value=val;
 window.close();
}

function setselect(sel, val)
{
 for(i=0; i<sel.options.length; i++)
 {
  if(sel.options[i].value == val) sel.selectedIndex=i;

 }
}

function insertuservalue(elid, val1, val2, val3, val4, val9)
{
 window.opener.document.getElementById(elid).value=val1;
 window.opener.document.getElementById(elid+'_d').value=val2;

 setselect(window.opener.document.getElementById(elid+'_d2'), val4);
 setselect(window.opener.document.getElementById(elid+'_d3'), val3);


 window.opener.document.getElementById(elid+'_d9').value=val9;

 window.close();
}

function insertuservalue3(elid, val1, val9)
{

 window.opener.document.getElementById(elid).value=val1;

 window.opener.document.getElementById(elid+'_d9').value=val9;

 window.close();
}


function cllogins()
{

 var c1;
 var c2;
 c1=document.getElementById('logins_1');
 c2=document.getElementById('logins_2');
 if(c1.value=='логин' && c2.value=='пароль')
 {
  c1.value='';
  c2.value='';
 }

}







function addElement2(btn){

        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr = tr.parentNode;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                

                var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
                thisChilds = tr.getElementsByTagName('td');
                thisNew = newTr.getElementsByTagName('td');

                for (var i = 0; i < thisChilds.length; i++){

                        if(thisChilds[i].className != 'tmbutton' && thisChilds[i].className != 'tmcomments')
                        {
                          //alert(thisChilds[i].innerHTML);
                          thisNew[i].innerHTML=thisChilds[i].innerHTML;
                        }

                        if (thisChilds[i].className == 'tmcomments') thisChilds[i].innerHTML = '';
                        if (thisChilds[i].className == 'tmdashed') thisChilds[i].innerHTML = '<div id=dashed1>&nbsp;</dbv>';
                        
                      //  if (thisChilds[i].className == 'tmbutton') 
                      //  {
                      //   thisChilds[i].innerHTML = '<input type="button" name="add" value="Удалить" onClick=dropElement(this)>';
                      //  }


                }



                thisChildsInp = tr.getElementsByTagName('input');
                thisNewInp = newTr.getElementsByTagName('input');

                for (var i = 0; i < thisChildsInp.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp[i].type!='button') thisNewInp[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp[i].id=thisChildsInp[i].id;
                 thisChildsInp[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }


                thisChildsInp2 = tr.getElementsByTagName('textarea');
                thisNewInp2 = newTr.getElementsByTagName('textarea');

                for (var i = 0; i < thisChildsInp2.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp2[i].type!='button') thisNewInp2[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp2[i].id=thisChildsInp2[i].id;
                 thisChildsInp2[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }

                btns = tr.getElementsByTagName('td');

                
                //alert(btns.length);
                for (var i = 0; i < btns.length; i++)
                {

                      if (btns[i].className == 'tmbutton') 
                      {
                       btns[i].innerHTML = '<input type="button" name="add" value="Удалить" onClick=dropElement2(this)>';
                      }

                }



        }
}

function dropElement2(btn){
        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr = tr.parentNode;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr.parentNode.removeChild(tr);
        }
}

function addElement3(btn){

        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr = tr.parentNode;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                

                var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
                thisChilds = tr.getElementsByTagName('td');
                thisNew = newTr.getElementsByTagName('td');

                for (var i = 0; i < thisChilds.length; i++){

                        if(thisChilds[i].className != 'tmbutton' && thisChilds[i].className != 'tmcomments')
                        {
    //                      alert(thisChilds[i].className+'-'+thisChilds[i].innerHTML);
                          thisNew[i].innerHTML=thisChilds[i].innerHTML;
                        }

                        if (thisChilds[i].className == 'tmcomments') thisChilds[i].innerHTML = '';
                        if (thisChilds[i].className == 'tmdashed') thisChilds[i].innerHTML = '<div id=dashed1>&nbsp;</dbv>';
                        
                      //  if (thisChilds[i].className == 'tmbutton') 
                      //  {
                      //   thisChilds[i].innerHTML = '<input type="button" name="add" value="Удалить" onClick=dropElement(this)>';
                      //  }


                }



                thisChildsInp = tr.getElementsByTagName('input');
                thisNewInp = newTr.getElementsByTagName('input');

                for (var i = 0; i < thisChildsInp.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp[i].type!='button') thisNewInp[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp[i].id=thisChildsInp[i].id;
                 thisChildsInp[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }

                thisChildsInp3 = tr.getElementsByTagName('select');
                thisNewInp3 = newTr.getElementsByTagName('select');

                for (var i = 0; i < thisChildsInp3.length; i++)
                {
                 var id="Npr"+Math.random();
  //                alert("ok");
               if(thisNewInp3[i].type!='button') thisNewInp3[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp3[i].id=thisChildsInp3[i].id;
                 thisChildsInp3[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }



                thisChildsInp2 = tr.getElementsByTagName('textarea');
                thisNewInp2 = newTr.getElementsByTagName('textarea');

                for (var i = 0; i < thisChildsInp2.length; i++)
                {
                 var id="Npr"+Math.random();

                 if(thisNewInp2[i].type!='button') thisNewInp2[i].value='';

                 //alert("B "+thisNew[i].id+' - '+thisChilds[i].id);
                 //alert("VB "+thisNew[i].value+' - '+thisChilds[i].value);
                 //thisNew[i].value='ЯЯЯ';
                 //thisChilds[i].value='ZZZZ';

                 thisNewInp2[i].id=thisChildsInp2[i].id;
                 thisChildsInp2[i].id=id;
                 //alert("A "+thisNew[i].id+' - '+thisChilds[i].id);
                }

                btns = tr.getElementsByTagName('td');

                
                //alert(btns.length);
                for (var i = 0; i < btns.length; i++)
                {

                      if (btns[i].className == 'tmbutton') 
                      {
                       btns[i].innerHTML = '<input type="button" name="add" value="Удалить" onClick=dropElement3(this)>';
                      }

                }



        }
}

function dropElement3(btn){
        if(document.getElementById) {
                tr = btn;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr = tr.parentNode;
                while (tr.tagName != 'TR') tr = tr.parentNode;
                tr.parentNode.removeChild(tr);
        }
}



////
////	NEW
////

var stack = new Array(1000);
var size = 0;
var tags = new Array(13);
tags['B'] = 0;
tags['I'] = 0;
tags['U'] = 0;
tags['SUB'] = 0;
tags['SUP'] = 0;

tags['S'] = 0;
tags['QUOTE'] = 0;
tags['SMALL'] = 0;
tags['URL'] = 0;
tags['EMAIL'] = 0;
tags['LIST'] = 0;
tags['CENTER'] = 0;
tags['LEFT'] = 0;
tags['RIGHT'] = 0;
tags['JUSTIFY'] = 0;

function push(val)
{
	stack[size++] = val;
}

function pop()
{
	return stack[--size];
}

function insertsmile(val, elid)
{
	appendEditor(val, elid);
	focusEditor(elid);    
}










function myinsertegs(txtstart,txtend, elid)
{
	var post=document.getElementById(elid);
	post.focus();
	if (document.selection)
	{
		// ie & may be opera 8
		var rng = document.selection.createRange();
		if (rng.text)
		{
			document.selection.createRange().text = txtstart + rng.text + txtend;
		}
		else
		{
			post.value += txtstart + txtend;
		}
		post.focus();
	}
	else
	if (post.selectionStart || post.selectionStart == '0')
	{
		// mozilla: intellegent bcodes support
		var selStart = post.selectionStart;
		var selEnd = post.selectionEnd;

		var s = post.value;
		s = s.substring(0, selStart) + txtstart + s.substring(selStart, selEnd)
				+ txtend + s.substring(selEnd, s.length);
		post.value = s;

		if (selEnd != selStart)
		{
			post.selectionStart = selStart;
			post.selectionEnd = selEnd + txtstart.length +
					txtend.length;
		}
		else
		{
			post.selectionStart = selStart + txtstart.length;
			post.selectionEnd =	post.selectionStart;
		}
	}
	else
	{
		// other browsers
		post.value += txtstart + txtend;
	}
}







function inserttag2(tag, elid)
{
		
    if(tag=='URL')
        return inserturl(elid);
    if(tag=='EMAIL')
        return insertemail(elid);
	if(tag=='QUOTE')
		return quote(elid);
	ta = document.getElementById(elid);
	var cltag = '';
	if(tag == 'B' || tag == 'I' || tag == 'U' || tag == 'S' || tag == 'QUOTE' ||
	    tag == 'SMALL' || tag == 'URL' || tag == 'EMAIL' || tag == 'LIST' || tag == 'CENTER' ||
	    tag == 'LEFT' || tag == 'RIGHT' || tag == 'JUSTIFY' || tag=='SUB' || tag=='SUP')
		cltag = '/'+tag;

	var single = true;
	/*
	alert(document.selection);
	if (document.selection && document.selection.createRange)
	{
		ta.focus();
		sel1 = ta.value.substr(0, ta.selectionStart);

		var sel = document.selection;
		var text = '';
		var rng = sel.createRange();

		rng.colapse;

		if ( (sel.type == "Text" || sel.type == "None") && rng != null )
		{			
			if ( cltag != "" && rng.text.length > 0)
			{
				text = '['+tag+']' + rng.text + '[/'+tag+']';
				single = false;
			}
				
			rng.text = text;
		}
	}
	*/
	myinsertegs('['+tag+']', '['+cltag+']', elid);
	/*
	if(single == true)
	{
		if(tags[tag] == 0)
		{
			appendEditor('['+tag+']', elid);
			push(tag);
			tags[tag] = 1;
		}
		else
		{
			var tag2;
			do
			{
				tag2 = pop();
				appendEditor('[/'+tag2+']', elid);
				tags[tag2] = 0;
			}while(tag2 != tag);
		}
	}
	*/
	focusEditor(elid);
}

function quote(elid)
{
	txt = '';
	if (document.getSelection) { txt = document.getSelection(); } else if (document.selection) { txt = document.selection.createRange().text; }
	if ( txt ) txt='[QUOTE]'+txt+'[/QUOTE]\n';
	appendEditor(txt);
	focusSubmit();
	focusEditor();
}

function answer(login)
{
	txt='[B]'+login+'[/B]\n';
	appendEditor(txt);
	focusSubmit();
	focusEditor();
}

function appendEditor(val, elid)
{
	//document.getElementById(elid).value = document.getElementById(elid).value + val;
	
	myinsertegs(val, '', elid)
}
function focusEditor(elid)
{
	document.getElementById(elid).focus();
}
function focusSubmit(elid)
{
    document.getElementById(elid).focus();
}

function opensmiles()
{
    window.open('smiles.html', 'fsmiles', 'height=300, width=300, menubar=no,status=no,toolbar=no,titlebar=no');
}
var win_ie_ver;
function changerows()
{
    if(win_ie_ver > 4)
    {
        obj = document.getElementById('editor');
        if(obj.scrollHeight)
        {
            height = obj.scrollHeight;
            if(height < 160)
                height = 160;
            if(height > 480)
            {
                height = 480;
                obj.style.overflow='auto';
            }
            else
            {
                obj.style.overflow='hidden';
            }
            obj.style.height = height;
            old = height;
        }
    }
}

function changerowsstart()
{
    win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
    if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
    if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
    if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
    window.setInterval('changerows()', 100);
}

function inserturl(elid)
{
    var url = window.prompt('Введите адрес сайта', '');
    if(url == '' || url == null)
        return;
        
    var inserted = false;
    
	ta = document.getElementById(elid);
    
    if (document.selection && document.selection.createRange)
	{
		ta.focus();
		sel1 = ta.value.substr(0, ta.selectionStart);

		var sel = document.selection;
		var text = '';
		var rng = sel.createRange();

		rng.colapse;

		if ( (sel.type == "Text" || sel.type == "None") && rng != null )
		{			
			if (rng.text.length > 0)
			{
	            inserted = true;
				text = rng.text;
				rng.text = '[URL='+url+']'+text+'[/URL]';
			}
		}
	}
	if(inserted == false)
	{
        text = window.prompt('Введите текст ссылки', '');
        if(text == '' || text == null)
            text = url;
        appendEditor('[URL='+url+']'+text+'[/URL]', elid);
    }    
}

function insertemail(elid)
{
    var url = window.prompt('Введите адрес электронного почтового ящика', '');
    if(url == '' || url == null)
        return;
    
    var inserted = false;
    
	ta = document.getElementById(elid);
    
    if (document.selection && document.selection.createRange)
	{
		ta.focus();
		sel1 = ta.value.substr(0, ta.selectionStart);

		var sel = document.selection;
		var text = '';
		var rng = sel.createRange();

		rng.colapse;

		if ( (sel.type == "Text" || sel.type == "None") && rng != null )
		{
			if (rng.text.length > 0)
			{
			    inserted = true;
				text = rng.text;
			    rng.text = '[EMAIL='+url+']'+text+'[/EMAIL]';
			}
		}
	}
	if(inserted == false)
	{
        text = window.prompt('Введите описание ссылки', '');
        if(text == '' || text == null)
            text = url;
        appendEditor('[EMAIL='+url+']'+text+'[/EMAIL]', elid);
    }    
}

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;
}

function setHome(ob)
{

  if (isMSIE) 
 { 
    ob.style.behavior='url(#default#homepage)'; 
    ob.setHomePage(document.location);
 }
  else
 {
   alert('Ваш браузер не поддерживает автоматическую установку стартовой страницы.');
 }
}

function GetThis(T, C, U, L) 
{ 
var targetUrl = 'http://www.myspace.com/Modules/PostTo/Pages/?' + 't=' + encodeURIComponent(T) + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L; 
window.open(targetUrl); 
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

