

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);

style_code = new Array();
style_tags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]','[/list]','[list=]','[/list]','[img]','[/img]','[url]','[/url]');


function style_diary(style_number) {
	var txtarea = document.post.f_text;

	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (style_number == -1) { // Close all open tags & default button names
		while (style_code[0]) {
			butnumber = arraypop(style_code) - 1;
			txtarea.value += style_tags[butnumber + 1];
			buttext = eval('document.post.addstyle_code' + butnumber + '.value');
			eval('document.post.addstyle_code' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		txtarea.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = style_tags[style_number] + theSelection + style_tags[style_number+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, style_tags[style_number], style_tags[style_number+1]);
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < style_code.length; i++) {
		if (style_code[i] == style_number+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (style_code[bblast]) {
				butnumber = arraypop(style_code) - 1;
				txtarea.value += style_tags[butnumber + 1];
				buttext = eval('document.post.addstyle_code' + butnumber + '.value');
				eval('document.post.addstyle_code' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags

		if (imageTag && (style_number != 14)) {		// Close image tag before adding another
			txtarea.value += style_tags[15];
			lastValue = arraypop(style_code) - 1;	// Remove the close image tag from the list
			document.post.addstyle_code14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		txtarea.value += style_tags[style_number];
		if ((style_number == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(style_code,style_number+1);
		eval('document.post.addstyle_code'+style_number+'.value += "*"');
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

function mozWrap(txtarea, open, close)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	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;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}


function doInsert(res)  //insert_image_into_diary
{
	var txtarea = document.post.f_text;
	//res='[img]' + res + '[/img]';

	//IE support
	if (document.selection) {
	txtarea.focus();
	sel = document.selection.createRange();
	sel.text = res;
	}
	//MOZILLA/NETSCAPE support
	else if (txtarea.selectionStart || txtarea.selectionStart == '0')
	{
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		txtarea.value = txtarea.value.substring(0, startPos) + res + txtarea.value.substring(endPos, txtarea.value.length);
	}
	else
	{
		txtarea.value += res;
	}

}

function insert_image_into_diary(res)
{
	res='[img]' + res + '[/img]';
	doInsert(res);
}


function tag_url()
{
	var txtarea = document.post.f_text;

	var seltext = '';

	//IE support
	if (document.selection)
	{
		txtarea.focus();
		sel = document.selection.createRange();
		seltext = sel.text;// = res;
	}
	//MOZILLA/NETSCAPE support
	else if (txtarea.selectionStart || txtarea.selectionStart == '0')
	{
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		seltext = txtarea.value.substring(startPos, endPos);
	}

	if (seltext=='')
	{
		seltext = 'My Webpage';
	}

	var FoundErrors = '';
    var enterURL   = prompt('Įveskite nuoruodą', "http://");
    var enterTITLE = prompt('Įveskite nuoruodos pavadinimą', seltext);

    if (!enterURL)
	{
        FoundErrors += "\n" + 'Neįvedėte nuoruodos.';
    }
    if (!enterTITLE)
	{
        FoundErrors += "\n" + 'Neįvedėte nuoruodos pavadinimo.';
    }

    if (FoundErrors) {
        alert("Klaida: " + FoundErrors);
        return;
    }

	doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]" );
}


function popas(id)
{
    url = 'http://www.glimstedt.ee/rss/pop_text.php?id=' + id;
    kas = window.open(url,'pop','width=650, height=340,top='+(screen.height-170)/4+',left='+(screen.width-700)/2+',location=no,directories=no,status=no,menuBar=no,scrollBars=no,resizable=no');
}
