var vbmenu_usepopups = false;
var ignorequotechars = 0;

// #############################################################################
// lets define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// #############################################################################
// let's find out what DOM functions we can use
var vbDOMtype = '';
if (document.getElementById)
{
	vbDOMtype = "std";
}
else if (document.all)
{
	vbDOMtype = "ie4";
}
else if (document.layers)
{
	vbDOMtype = "ns4";
}
var vBobjects = new Array();


//******************************************************************************
function validatePrivMsg (aForm) {

    if (aForm.addr.value) {
    
        atmp = new Array();
        str = new String(aForm.addr.value);
        atmp = explodeArray(str, ';');
        
        if ( atmp.length > 5 ) {
            alert('Wiadomość możesz wysłać do najwyżej pięciu osób!');
            return false;
        }
    } else {
        alert('Nie wpisałeś adresata wiadomości');
        return false;
    }
    
    if (!aForm.subject.value) {
        alert('Podaj temat wiadomości');
        return false;
    }

    if (!aForm.message.value) {
        alert('Wpisz treść wiadomości');
        return false;
    }

    return true;
}

//******************************************************************************
function fetch_object(idname, forcefetch) {
	if (forcefetch || typeof(vBobjects[idname]) == "undefined") {
		switch (vbDOMtype) 		{
			case "std":
				vBobjects[idname] = document.getElementById(idname);
			break;

			case "ie4":
				vBobjects[idname] = document.all[idname];
			break;

			case "ns4":
				vBobjects[idname] = document.layers[idname];
			break;
		}
	}
	return vBobjects[idname];
}
//******************************************************************************


function resize_msgbox(boxid, pixelvalue) {
	var box = fetch_object(boxid);
	var boxheight = parseInt(box.style.height);
	var newheight = boxheight + pixelvalue;
	if (newheight > 0) {
		box.style.height = newheight + "px";
	}
	return false;
}
//-------------------------------------------------------
var vbphrase = {
	
		// standard only
		"enter_option_x_tag" : "Enter the option for the [%1$s] tag:",
		"enter_text_to_be_formatted" : "Wpisz tekst do sformatowania",
		"enter_link_text" : "Podaj tekst wyświetlany dla jako opis dla linku (opcjonalnie):",
		"enter_list_type" : "Wybierz rodzaj listy? Wpisz '1' dla numerycznej listy, wpisz 'a' dla alfabetycznej listy, lub pozostaw puste dla oznaczenia punktowego:",
		"enter_list_item" : "Wpisz listę elementów.\nZostaw pole puste lub wybierz 'Cancel' aby zamknąc listę:",
	
	// both
	"must_enter_subject" : "Musisz podać tytuł!",
	"message_too_short" : "Wpisana wiadomość jest za krótka. Minimalna długość wiadomości powinna mieć minimum %1$s znaków.",
	"enter_link_url" :  "Proszę podać adres URL linku:",
	"enter_image_url" : "Proszę podać adres URL obrazka:",
	"enter_email_link" : "Proszę podać adres emial do linku:"
};
// array which holds the currently opened tags
var tags = new Array();

// currently selected text
var text = "";

// text which is to be added to the post
var AddTxt = "";

// form object that has to be used
var theform = "";

theform = document.forms.edform;

// #############################################################################
function bbcode(vbcode, prompttext, displayoption)
{
	if (typeof theform != "object" && typeof must_click_message != "undefined")
	{ // QR check
		alert(must_click_message);
		return false;
	}

	var optioncompiled = "";
	if (displayoption)
	{
		optionvalue = prompt(construct_phrase(vbphrase["enter_option_x_tag"], vbcode), "");
		if (optionvalue != null)
		{
			optioncompiled = "=\"" + optionvalue + "\"";
		}
	}

	// lets call this when they try and use vbcode rather than on change
	getActiveText();

	if (text)
	{ // its IE to the rescue
		if (text.substring(0, vbcode.length + 2 ) == "[" + vbcode + "]" && text.substring(text.length - vbcode.length - 3, text.length) == "[/" + vbcode + "]")
		{
			AddTxt = text.substring(vbcode.length + 2, text.length - vbcode.length - 3);
		}
		else
		{
			AddTxt = "[" + vbcode + optioncompiled + "]" + text + "[/" + vbcode + "]";
		}
		AddText(AddTxt);
	}
	else if ((theform.message.selectionStart || theform.message.selectionStart == '0') && theform.message.selectionStart != theform.message.selectionEnd)
	{ // its mozilla and we'll need to re-write entire text
		var start_selection = theform.message.selectionStart;
		var end_selection = theform.message.selectionEnd;
            	// fetch everything from start of text area to selection start
		var start = (theform.message.value).substring(0, start_selection);
		// fetch everything from start of selection to end of selection
		var middle = (theform.message.value).substring(start_selection, end_selection);
		// fetch everything from end of selection to end of text area
		var end = (theform.message.value).substring(end_selection, theform.message.textLength);

		if (middle.substring(0, vbcode.length + 2 ) == "[" + vbcode + "]" && middle.substring(middle.length - vbcode.length - 3, middle.length) == "[/" + vbcode + "]")
		{
			middle = middle.substring(vbcode.length + 2, middle.length - vbcode.length - 3);
		}
		else
		{
			middle = "[" + vbcode + optioncompiled + "]" + middle + "[/" + vbcode + "]";
		}

		theform.message.value = start + middle + end;
		setfocus();
		theform.message.selectionStart = end_selection + middle.length;
		theform.message.selectionEnd = start_selection + middle.length;
		return false;
	}
	else
	{

		if (!normalmode)
		{
			var donotinsert = false;
			var thetag = 0;
			for (var i = 0; i < tags.length; i++)
			{
				if (typeof(tags[i]) != "undefined" && tags[i] == vbcode)
				{
					donotinsert = true;
					thetag = i;
				}
			}

			if (!donotinsert)
			{
				array_push(tags, vbcode);
				AddTxt = "[" + vbcode + optioncompiled + "]";
				AddText(AddTxt);
			}
			else
			{ // its already open
				var closedtag = "";
				while (typeof(tags[thetag]) != "undefined")
				{
					closedtag = array_pop(tags);
					AddTxt = "[/" + closedtag + "]";
					AddText(AddTxt);
					
					//if (vbcode!='FONT' && vbcode!='SIZE' && vbcode!='COLOR') {
                                            if (closedtag!='FONT' && closedtag!='SIZE' && closedtag!='COLOR' && closedtag!=vbcode)
 			                        pushButton(closedtag);
 			               // } else {
                                       //     if (closedtag!='FONT' && closedtag!='SIZE' && closedtag!='COLOR')
 			               //         pushButton(closedtag);
                                       // }
					
				}
			}
		}
		else
		{
			var inserttext = prompt(vbphrase["enter_text_to_be_formatted"] + "\n[" + vbcode + "]xxx[/" + vbcode + "]", "");
			if ((inserttext != null) && (inserttext != ""))
			{
				AddTxt = "[" + vbcode + optioncompiled + "]" + inserttext + "[/" + vbcode + "] ";
			}
			AddText(AddTxt);
		}
	}
	setfocus();
	return false;
}

//*******************************************************************
// function used to check that the value in the array we're using is valid
function thearrayisgood(thearray, i)
{
	if (typeof(thearray[i]) == "undefined" || (thearray[i] == "") || (thearray[i] == null))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//*******************************************************************
// emulation of PHP's sizeof function
function sizeof(thearray)
{
	for (var i = 0; i < thearray.length; i++)
	{
		if (!thearrayisgood(thearray, i))
		{
			return i;
		}
	}
	return thearray.length;
}

//*******************************************************************
// emulation of PHP's array_push function
function array_push(thearray, value)
{
	var thearraysize = sizeof(thearray);
	thearray[thearraysize] = value;
	return thearray[thearraysize];
}

//*******************************************************************
// emulation of PHP's array_pop function
function array_pop(thearray)
{
	var thearraysize = sizeof(thearray);
	var retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

//*******************************************************************
function pushButton(btnid) {

    var btn = fetch_object(btnid);

    if (!normalmode) {
    
      if(is_ns)
        if (btn.style.borderStyle == 'inset inset inset inset')
           btn.style.borderStyle = 'outset outset outset outset';
        else
           btn.style.borderStyle = 'inset inset inset inset';
           
      if(is_ie)
        if (btn.style.borderStyle == 'inset')
           btn.style.borderStyle = 'outset';

        else
           btn.style.borderStyle = 'inset';
    }
}
	
//*******************************************************************

function closetag()
{
	getActiveText();
	if (!normalmode)
	{
		if (typeof(tags[0]) != "undefined")
		{
			var Tag = array_pop(tags)
			AddTxt = "[/"+ Tag +"]";
			if (Tag!='FONT' && Tag!='SIZE' && Tag!='COLOR')
 			    pushButton(Tag);
			AddText(AddTxt);
		}
	}
	setfocus();
}
//*******************************************************************

function closeall()
{
	getActiveText();
	if (!normalmode)
	{
		var g = sizeof(tags);
		if (thearrayisgood(tags, g-1))
		{
			var Addtxt = "";
			var newtag = "";
			for (var h = 0; h < g; h++)
			{
				newtag = array_pop(tags);
				Addtxt += "[/" + newtag + "]";
				if (newtag!='FONT' && newtag!='SIZE' && newtag!='COLOR')
                                    pushButton(newtag);
			}
			AddText(Addtxt);
		}
	}
	setfocus();
}
//*******************************************************************

function setmode(modevalue)
{
        var mode = modevalue;
	closeall(theform);
	if (mode == 1)
	{
		normalmode = false;
	}
	else
	{
		normalmode = true;
	}
	document.cookie = "vbcodemode=" + mode + "; path=/; expires=Wed, 1 Jan 2020 00:00:00 GMT;";
}
//*******************************************************************


function setfocus()
{
        theform = document.forms.edform;
	theform.message.focus();
}

//********************************************************************
function getActiveText() {

        theform = document.forms.edform;

	setfocus();
	if (!is_ie || (is_ie && !document.selection)) 	{
		return false;
	}

	var sel = document.selection;
	var rng = sel.createRange();

	if (rng != null && (sel.type == "Text" || sel.type == "None")) 	{
		text = rng.text;
	}
	if (rng != null && theform.message.createTextRange) 	{
		theform.message.caretPos = rng.duplicate();
	}
	return true;
}

//**********************************************************************


function fontformat(thevalue, thetype)
{
	getActiveText();

	if (text)
	{ // its IE to the rescue
		AddTxt = "[" + thetype + "=" + thevalue + "]" + text + "[/" + thetype + "]";
		AddText(AddTxt);
	}
	else if (theform.message.selectionEnd && (theform.message.selectionEnd - theform.message.selectionStart > 0))
	{ // its mozilla and we'll need to re-write entire text
		var start_selection = theform.message.selectionStart;
		var end_selection = theform.message.selectionEnd;
		if (end_selection <= 2)
		{
			end_selection = theform.message.textLength;
		}

		// fetch everything from start of text area to selection start
		var start = (theform.message.value).substring(0, start_selection);
		// fetch everything from start of selection to end of selection
		var middle = (theform.message.value).substring(start_selection, end_selection);
		// fetch everything from end of selection to end of text area
		var end = (theform.message.value).substring(end_selection, theform.message.textLength);

		middle = "[" + thetype + "=" + thevalue + "]" + middle + "[/" + thetype + "]";

		theform.message.value = start + middle + end;
		theform.message.selectionStart = end_selection + middle.length;
		theform.message.selectionEnd = start_selection + middle.length;
	}
	else
	{
		if (!normalmode)
		{
			var donotinsert = false;
			var thetag = 0;
			for (var i = 0; i < tags.length; i++)
			{
				if (typeof(tags[i]) != "undefined" && tags[i] == thetype)
				{
					donotinsert = true;
					thetag = i;
				}
			}

			if (!donotinsert)
			{
				array_push(tags, thetype);
				AddTxt = "[" + thetype + "=" + thevalue + "]";
				AddText(AddTxt);
			}
			else
			{ // its already open
				var closedtag = "";
				while (tags[thetag])
				{
					closedtag = array_pop(tags);
					AddTxt = "[/" + closedtag + "]";
					AddText(AddTxt);
					if (closedtag!='FONT' && closedtag!='SIZE' && closedtag!='COLOR')
			                    pushButton(closedtag);

				}
			}
		}
		else
		{
			var inserttext = prompt(vbphrase["enter_text_to_be_formatted"] + "\n[" + thetype + "=" + thevalue + "]xxx[/" + thetype + "]", "");
			if ((inserttext != null) && (inserttext != ""))
			{
				AddTxt = "[" + thetype + "=" + thevalue + "]" + inserttext + "[/" + thetype + "]";
				AddText(AddTxt);
			}
		}
	}

	theform.sizeselect.selectedIndex = 0;
	theform.fontselect.selectedIndex = 0;
	theform.colorselect.selectedIndex = 0;
	setfocus();
	return false;
}


//**************************************************************************
function AddText(NewCode)
{
	if (typeof(theform.message.createTextRange) != "undefined" && theform.message.caretPos)
	{
		var caretPos = theform.message.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
		caretPos.select();
	}
	else if (theform.message.selectionStart || theform.message.selectionStart == '0')
	{ // its mozilla and we'll need to re-write entire text
		var start_selection = theform.message.selectionStart;
		var end_selection = theform.message.selectionEnd;

		// fetch everything from start of text area to selection start
		var start = (theform.message.value).substring(0, start_selection);
		// fetch everything from start of selection to end of selection
		var middle = NewCode;
		// fetch everything from end of selection to end of text area
		var end = (theform.message.value).substring(end_selection, theform.message.textLength);

		theform.message.value = start + middle + end;
		setfocus();
		theform.message.selectionStart = end_selection + middle.length;
		theform.message.selectionEnd = start_selection + middle.length;
		getActiveText();
		AddTxt = "";
		return;
	}
	else
	{
		theform.message.value += NewCode;
	}
	setfocus();
	getActiveText();
	AddTxt = "";
}


//**************************************************************************
function namedlink(thetype)
{
	var extraspace = "";

	getActiveText();
	var dtext = "";
	if (text)
	{
		dtext = text;
	}
	else
	{
		extraspace = " ";
	}
	linktext = prompt(vbphrase["enter_link_text"], dtext);
	var prompttext, prompt_contents;
	if (thetype == "URL")
	{
		prompt_text = vbphrase["enter_link_url"];
		prompt_contents = "http://";
	}
	else
	{
		prompt_text = vbphrase["enter_email_link"];
		prompt_contents = "";
	}
	var linkurl = prompt(prompt_text, prompt_contents);
	if ((linkurl != null) && (linkurl != ""))
	{
		if ((linktext != null) && (linktext != ""))
		{
			AddTxt = "[" + thetype + "=" + linkurl + "]" + linktext + "[/" + thetype + "]" + extraspace;
			AddText(AddTxt);
		}
		else
		{
			AddTxt = "[" + thetype + "]" + linkurl + "[/" + thetype + "]" + extraspace;
			AddText(AddTxt);
		}
	}
	
	pushButton(thetype);
	
	return false;
}

//**************************************************************************
function dolist()
{       theform = document.forms.edform;
	var listtype = prompt(vbphrase["enter_list_type"], "");

	if ((listtype == "a") || (listtype == "1") || (listtype == "i"))
	{
		thelist = "[LIST=" + listtype + "]\n";
	}
	else
	{
		thelist = "[LIST]\n";
	}
	var listentry = "initial";
	while ((listentry != "") && (listentry != null))
	{
		listentry = prompt(vbphrase["enter_list_item"], "");
		if ((listentry != "") && (listentry != null))
		{
			thelist = thelist + "[*]" + listentry + "\n";
		}
	}
	AddTxt = thelist + "[/LIST]";
	if (!text)
	{
		AddTxt = AddTxt + " ";
	}
	AddText(AddTxt);
	
	pushButton('LIST');
	
        return false;
}

//**************************************************************************
function smilie(thesmilie)
{
	getActiveText();
	var AddSmilie = " " + thesmilie + " ";
	AddText(AddSmilie);
}


