var num = 0;
var fcount = 1;

function change_src (src,fnum)
{
	local = document.getElementsByName('fileLocal[]')[fnum];
	remote = document.getElementsByName('fileRemote[]')[fnum];
	localspan = getByName('span','spanLocal')[fnum];
	if(src == 'remote')
	{
		if(remote.value != '' && local.value != '') {
			localspan.removeChild(local);
			remote_new = document.createElement('input');
			remote_new.setAttribute('onChange',"change_src('local',getnum(this))");
			remote_new.setAttribute('size','50');
			remote_new.setAttribute('name','fileLocal[]');
			remote_new.setAttribute('id','fileLocal');
			remote_new.setAttribute('type','file');
//			remote_new.setAttribute('class','txt');
			localspan.appendChild(remote_new);
		}
	}
	else if(src == 'local')
	{
		remote.value = '';
	}
	return;
}

function getnum(ref)
{
	return 0;
}

function getByName(tag,nme)
{
	var result = new Array();
	result = document.getElementsByName(nme);
	if(result.length)
	{
		return result;
	}
	else
	{
		var result = new Array();
		var  ObjList = document.getElementsByTagName(tag);
		var n = 0;
		for (i=0; i<ObjList.length; i++)
		{
			if(ObjList[i].name == nme)
			{
				result[n] = ObjList[i];
				n++;
			}
		}
		return result;
	}
}

var preview_window;

function display_preview (second)
{
	var fnum = 0;
	if (!second)
	{
		var nme = 'bbcode[]';
		var width_name = 'width[]';
		var height_name = 'height[]';
		var i_second = 0;
	}
	else
	{
		var nme = 'bbcode2[]';
		var width_name = 'width2[]';
		var height_name = 'height2[]';
		var i_second = 1;
	}

	var bbcode = document.getElementsByName(nme)[fnum];

	if (!bbcode)
	{
		alert('Preview failed due to internal error.');
		return;
	}
	if (!trim(bbcode.value))
	{
		alert('The message is empty.');
		return;
	}
	if (bbcode.value.length > 30000)
	{
		alert('The message is too big (' + bbcode.value.length + '/30000)');
		return;
	}

	var width = parseInt(document.getElementsByName(width_name)[fnum].value);
	var height = parseInt(document.getElementsByName(height_name)[fnum].value);

	if (width > 800)
	{
		alert('Window width (' + width + ' pixels) is too big. It must not exceed 800 pixels.');
		return;
	}
	if (height > 600)
	{
		alert('Window height (' + height + ' pixels) is too big. It must not exceed 600 pixels.');
		return;
	}

	if (preview_window && !preview_window.closed)
		preview_window.close()
	preview_window = window.open("", "preview", "location=0,scrollbars=1,width=" + width + ",height=" + height);

	var form = document.editform;
	var old_action = form.action;
	form.action = 'preview.php?fnum=0&second=' + i_second;
	form.target = 'preview';
	form.submit();
	form.action = old_action;
	form.target = '';

	preview_window.focus();
}

function show_smilies (second)
{
	var form = document.getElementById('smiles' + (second ? '2' : ''));
	form.style.display = form.style.display == 'none' ? '' : 'none';
}

// Editor

var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = (navigator.product == 'Gecko');
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

function tag(tag, second) 
{
	doInsert('['+tag+']','[/'+tag+']', true, second);
}

function doInsert (ibTag, ibClsTag, isSingle, second)
{
	var isClose = false;
	var obj_ta = document.editform['bbcode' + (second ? '2' : '') + '[]'];

	if ( (ua_vers >= 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;
				else if(isSingle)
					isClose = true;
				rng.text = ibTag+ibClsTag;
			} 
		}
		else
		{
			if(isSingle)
			{
				isClose = true;
			}
			
			obj_ta.value += ibTag+ibClsTag;
		}
	}
	else if ( obj_ta.selectionEnd )
	{ 
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;

		if (es <= 2)
			es = obj_ta.textLength;

		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);

		//-----------------------------------
		// text range?
		//-----------------------------------

		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
			middle = ibTag + middle + ibClsTag;
		else
		{
			middle = ibTag + middle + ibClsTag;
			if (isSingle)
				isClose = true;
		}

		obj_ta.value = start + middle + end;

		var cpos = ss + (middle.length);

		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;
	}
	else
	{
		if (isSingle)
			isClose = true;
		obj_ta.value += ibTag + ibClsTag;
	}
	
	obj_ta.focus();

	return isClose;
}

function tag_url (second) 
{
	var param1 = prompt('Link URL', 'http://');
	if (param1 == null)
		return;

	if (param1 == '') 
	{
		alert('You have to enter an URL.');
		return;
	}

	var param2 = prompt('Enter description', param1);
	if (param2 == null)
		return;

	if(param2 == '')  
		param2 = param1;

	doInsert('[url='+param1+']'+param2+'[/url]', '', false, second);
}

function tag_img (num,second) 
{
	var param1 = prompt('Image URL');

	if (param1 == null)
		return;

	if (param1 == '') 
	{
		alert('You have to enter the URL of an image.');
		return;
	}

	doInsert('[img]'+param1+'[/img]', '', false, second);
}

function tag_mail (second) 
{
	var param1 = prompt('Enter an e-mail');
	if (param1 == null)
		return;

	if (param1 == '') 
	{
		alert('You have to enter an e-mail address.');
		return;
	}

	param2 = prompt('Enter description', param1);
	if (param2 == null)
		return;
	else if(param2 == '') 
		param2 = param1;

	doInsert('[mail='+param1+']'+param2+'[/mail]', '', false, second);
}

function font(type, param, second) 
{
	doInsert('['+type+'='+param+']', '[/'+type+']', true, second);
}

function smilie (smilie, second) 
{
	doInsert(' '+smilie+' ', '', false, second);
}

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// fixPNG(); http://www.tigir.com/js/fixpng.js (author Tigirlas Igor)
function fixPNG(element)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
		var src;
		if (element.tagName == 'IMG')
		{
			if (/\/images\/([^\/]*)\.png$/.test(element.src))
			{
				src = element.src;
				element.src = "/images/blank.gif";
			}
		}
		else
		{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src = src[1];
				element.runtimeStyle.backgroundImage = "none";
			}
		}
		if (src)
			element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}
