var opens		=	[],
	isSel		= 0,
	bbtags		= [],
	myAgent		= navigator.userAgent.toLowerCase(),
	myVersion	= parseInt(navigator.appVersion);

var is_ie	= (myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1),
	is_nav	= (myAgent.indexOf('mozilla')!=-1) && (myAgent.indexOf('spoofer')==-1)
	&& (myAgent.indexOf('compatible') == -1) && (myAgent.indexOf('opera')==-1)
	&& (myAgent.indexOf('webtv') ==-1)       && (myAgent.indexOf('hotjava')==-1),

is_win	= (myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1),
is_mac	= myAgent.indexOf("mac")!=-1;

function hstat(msg){try{document.addform.helpbox.value = eval( "help_" + msg )} catch(e){}}

function cstat(fi,form){
	if (!fi)fi='';
	var c = stacksize(bbtags);

	if ( (c < 1) || (c == null) )
		c = 0;
	if ( !bbtags[0] )
		c = 0;
	if( typeof form=='undefined' || !form ) form = 'addform';
	document[form]['tagcount'+fi].value = c;
}

function stacksize(thearray){
	for (i = 0 ; i < thearray.length; i++ ) 
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') )
			return i;

	return thearray.length;
}

function pushstack(thearray,newval,fi){
	arraysize = stacksize(thearray);
	thearray[arraysize] = newval;
}

function popstack(thearray){
	arraysize = stacksize(thearray);
	theval = thearray[arraysize - 1];
	delete thearray[arraysize - 1];
	return theval;
}

function closeall(wh,fi,form){
	if (!fi)fi='';
	if (!wh)wh='message';
	wh = document.getElementById(wh);
	if( typeof form=='undefined' || !form ) form = 'addform';
	if (bbtags[0]){
		try{
			while (bbtags[0]) {
				tagRemove = popstack(bbtags);
				wh.value += "[/" + tagRemove + "]";
				if ( (tagRemove != 'font') && (tagRemove != 'size') && (tagRemove != 'color') ){
					if (tagRemove=='code')
						document[form]["codes"+fi].value = ' ' + tagRemove + ' ';
					else
						document[form][tagRemove+fi].value = ' ' + tagRemove + ' ';
					opens[tagRemove+fi]=0;
				}
			}
		}catch(e){}
	}

	document[form]['tagcount'+fi].value = 0;
	bbtags = new Array();
	wh.focus();
}


function emoticon(theSmilie,wh){
	doInsert(" " + theSmilie + " ","",false,wh)
}

function add_code(NewCode,wh){
	if (!wh) wh='message';
	wh = document.getElementById(wh);
	wh.value += NewCode;
	wh.focus();
}

function alterfont(theval,thetag,wh,fi,form){
	if (!fi)fi='';
	if( typeof form=='undefined' || !form ) form = 'addform';
	if (theval == 0)
		return;

	if( doInsert("[" + thetag + "=" + theval + "]", "[/" + thetag + "]",true,wh) )
		pushstack(bbtags,thetag);

	cstat(fi,form);
}

function simpletag(thetag,fid,chtxt,wh,fi,form){
	if(!fi) fi='';
	if(!fid) fid='';
	var tagOpen = opens[thetag+fid];
	if( typeof form=='undefined' || !form ) form = 'addform';

	if (!tagOpen){
		if( doInsert("[" + thetag + "]", "[/" + thetag + "]",true,wh) ){
			opens[thetag+fid] = 1;
/*			try{
				if (fid)
					document.getElementById(fid).value=chtxt+'*';
				else
					if (thetag=='code')
						document[form]["codes"+fi].value += '*';
					else
						document[form][thetag+fi].value += '*';
			} catch(e){};*/
			pushstack(bbtags,thetag,fi);
			cstat(fi, form);
			hstat('click_close');
		}
	} else {
		lastindex = 0;
		for (i = 0 ; i < bbtags.length; i++ )
			if(bbtags[i] == thetag )
				lastindex = i;
		while(bbtags[lastindex]){
			tagRemove = popstack(bbtags);
			doInsert("[/" + tagRemove + "]", "",false,wh);
			if ( (tagRemove != 'font') && (tagRemove != 'size') && (tagRemove != 'color') ){
/*				try{
					if (fid)
						document.getElementById(fid).value = chtxt;
					else
						if (thetag=='code')
							document[form]['codes'+fi].value = tagRemove;
						else
							document[form][tagRemove+fi].value = tagRemove;
				}
				catch(e){};*/
				opens[tagRemove+fid] = 0;
			}
		}
		cstat(fi, form);
	}
}

function tag_list(wh){
	var listvalue = "init";
	var thelist = "";

	while ( (listvalue != "") && (listvalue != null) ){
		listvalue = prompt('Введите элемент списка', "");
		if ( (listvalue != "") && (listvalue != null) )
			thelist = thelist+"[*]"+listvalue+"\n";
	}

	if ( thelist != "" )
		doInsert( "[list]\n" + thelist + "[/list]\n", "",false,wh);
}

function tag_url(wh){
	var enterURL  = prompt('Введите адресс', "http://");
	var enterTITLE=isSelected(wh);
	if (enterTITLE.length==0)
		enterTITLE = prompt('Введите название ссылки',"Моя веб-страница");
	if (!enterURL || enterURL=='http://' || !enterTITLE)
		return;

	doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]","",false,wh);	
}

function tag_image(wh){
	var FoundErrors = '';
	var enterURL   = prompt('Ссылка на изображение',"http://");

	if (!enterURL || enterURL=='http://' || enterURL.length<10)
		return;

	doInsert("[img]"+enterURL+"[/img]","",false,wh);
}

function tag_email(wh) {
	var emailAddress = prompt('E-mail',"");

	if (!emailAddress) return;
	var enterTITLE=isSelected(wh);
	if (enterTITLE.length>0)
		doInsert("[email="+emailAddress+"]"+enterTITLE+"[/email]","",false,wh);	
	else
		doInsert("[email]"+emailAddress+"[/email]","",false,wh);
}

function tag_music(wh){
	var url = prompt('Прямая ссылка на MP3 файл',"");
	if(url) doInsert("[music]"+url+"[/music]","",false,wh);
}

function tag_video(wh){
	var url = prompt('Ссылка на видео',"");
	if(url) doInsert("[video]"+url+"[/video]","",false,wh);
}

function tag_flash(wh){
	var url = prompt('Ссылка на флеш',"");
	if(url) doInsert("[flash]"+url+"[/flash]","",false,wh);
}

function doInsert(ibTag,ibClsTag,isSingle,wh){
	if(!wh)wh='message';
	var isClose = false,
		obj_ta = document.getElementById(wh);

	if ( (myVersion >= 4) && is_ie && is_win){
		if(obj_ta.isTextEdit){
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;

				rng.text = ibTag;
			}
		} else{
			if(isSingle)
				isClose = true;
			obj_ta.value += ibTag;
		}
	} else
		try{
			var txtStart = obj_ta.selectionStart;
			if(!(txtStart >= 0)) throw 1;
			var txtEnd   = obj_ta.selectionEnd;
			if(ibClsTag != "" && obj_ta.value.substring(txtStart,txtEnd).length>0) {
				obj_ta.value = obj_ta.value.substring(0,txtStart) + ibTag +
				obj_ta.value.substring(txtStart,txtEnd)+
				ibClsTag+
				obj_ta.value.substring(txtEnd,obj_ta.value.length);
			} else {
				if(isSingle) isClose = true;  
				if (isSel==1)
					obj_ta.value = obj_ta.value.substring(0,txtStart) + ibTag + obj_ta.value.substring(txtEnd,obj_ta.value.length);
				else obj_ta.value = obj_ta.value.substring(0,txtStart) + ibTag + obj_ta.value.substring(txtStart,obj_ta.value.length);
			}
		} catch(e){
			if(isSingle)
				isClose = true;
			obj_ta.value += ibTag;
		}

	obj_ta.focus();
	return isClose;
}



function isSelected(wh){
	if (!wh)wh='message';
	var obj_ta = document.getElementById(wh);

	if ( (myVersion >= 4) && is_ie && is_win){
		if(obj_ta.isTextEdit){
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null)
				if(rng.text.length > 0){
					isSel=1;
					return rng.text;
				}
		}
		return '';
	}
	try{
		var txtStart = obj_ta.selectionStart;
		if(!(txtStart >= 0)) throw 1;
		var txtEnd   = obj_ta.selectionEnd;
		if(obj_ta.value.substring(txtStart,txtEnd).length>0) {
			isSel=1;
			return obj_ta.value.substring(txtStart,txtEnd);
		}
	} catch(e) {}
	return '';
}