var xpos, ypos;

var ie = (document.getElementById && document.all);
var ns = (document.getElementById && !document.all);

function MoveHandler(e)
{
	xpos = e.pageX;
	ypos = e.pageY;
	return true;
}

function MoveHandlerIE() {
	xpos = window.event.x + document.body.scrollLeft;
	ypos = window.event.y + document.body.scrollTop;
}

function mouseMove(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	xpos = posx;
	ypos = posy;
}

document.onmousemove = mouseMove;
function gr_move(a,b)
{
    ea = document.getElementById(a);
    eb = document.getElementById(b);

    var y = 0;
    var to_mv = new Array();
    for(x=0;x < ea.options.length;x++)
    {
        //
        if (ea.options[x].selected)
        {
            to_mv[y++] = ea.options[x];
        }
    }
    for(x=0;x<y;x++)
    {
        eb.appendChild(to_mv[x]);
    }

}

function selectAll(a)
{
    ea = document.getElementById(a);
    for(x=0;x < ea.options.length;x++)
    {
        ea.options[x].selected = true;
    }
}


function ajaxinfo_on( str ) {

	document.getElementById('ajaxinfo').innerHTML = str;
	document.getElementById('ajaxinfo').style.left = xpos+10+'px' ;
	document.getElementById('ajaxinfo').style.top = ypos+10+'px';
	document.getElementById('ajaxinfo').style.visibility = 'visible';
}


function ajaxinfo_off() {
	document.getElementById('ajaxinfo').style.visibility = 'hidden';
}

    function smoke_on( filename ) {
        // informacja o pobieraniu danych
        ajaxinfo_on("Pobieranie...");
        // ajaxujemy sie do serwera i pobieramy informacje
        advAJAX.get({
            url: "/filemanager.php",
            name : filename,
            action : "getinfo",
            onSuccess : function(obj)
            {
                if (obj.responseText)
                {
                        ajaxinfo_on(obj.responseText);
                }
                else
                {
                        ajaxinfo_on('Brak powiązań');
                }
            },
            onError : function(obj)
            {
                ajaxinfo_on("Błąd: " + obj.statusText);
            }
        });
    }


    function smoke_off() {
        ajaxinfo_off();
    }


// script zapożyczony z phpBB
// zmodyfikował do potrzeb Coyote - Marooned

/*** piechnat - zmiany - BEGIN ***/

	function closeTags(str) { // by Marooned
		var tags = {}, ret = '', sT = ['b', 'i', 'u', 'code', 'url', 'email', 'quote', 'image', 'delphi', 'cpp', 'php', 'asm', 'plain', 'wiki', 'ort', 'font']; // tagi do zamkniecia
		str.replace(/<(\/)?([\w]+)([\s=][^>]*)?>/g, function(a, b, c) {
			if (isNaN(tags[c = c.toLowerCase()])) tags[c] = 0;
			for (var i = 0; i < sT.length;) if (c == sT[i++]) {
				b != '/' ? tags[c]++ : tags[c] > 0 ? tags[c]-- : 0; break;
			}
		} );
		for (var v in tags) while (tags[v]--) ret += '</' + v + '>';
		return ret;
	}

	function selExist(elm) {
		if (elm.setSelectionRange) {
			return elm.selectionStart != elm.selectionEnd;
		}
		else if (document.selection) {
			elm.focus();
			var range = document.selection.createRange();
			return range.text != '';
		}
		return false;
	}

	function addToEnd(elm, text) {
		elm.value += text;
		if (elm.setSelectionRange) {
			var len = elm.value.length;
			elm.setSelectionRange(len, len);
		}
		elm.scrollTop = elm.scrollHeight;
	}

	function selReplace(elm, text1, text2) {
		elm.focus();
		if (elm.setSelectionRange) {
			if (typeof text2 != 'undefined') {
				text1 += elm.value.substring(elm.selectionStart, elm.selectionEnd) + text2;
			}
			var scrollPos = [elm.scrollLeft, elm.scrollTop];
			var begin = elm.value.substr(0, elm.selectionStart);
			var end = elm.value.substr(elm.selectionEnd);
			var curPos = elm.selectionStart + text1.length;
			elm.value = begin + text1 + end;
			elm.setSelectionRange(curPos, curPos);
			elm.scrollLeft = scrollPos[0];
			elm.scrollTop = scrollPos[1];
		}
		else if (document.selection) {
			var range = document.selection.createRange();
			if (typeof text2 != 'undefined') {
				text1 += range.text + text2;
			}
			range.text = text1;
			range.select();
		}
		else {
			if (typeof text2 != 'undefined') text1 += text2;
			addToEnd(elm, text1);
		}
	}

/*** piechnat - zmiany - END ***/

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
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_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);


// Helpline messages
b_help = "Tekst pogrubiony: <b>tekst</b>  (alt+b)";
i_help = "Tekst pochylony: <i>tekst</i>  (alt+i)";
u_help = "Tekst podkreślony: <u>tekst</u>  (alt+u)";
c_help = "Kod: <code>kod</code>  (alt+c)";
w_help = "Link: <url>http://adres</url> lub <url=http://adres>Tekst adresu</url> (alt+w)";
e_help = "E-mail: <email>adres@domena.pl</email> (alt+e)";
q_help = "Cytat: <quote>tekst</quote>  (alt+q)";
p_help = "Wstaw obrazek: <image>http://adres_obrazka</image>  (alt+p)";

d_help = "Delphi: <delphi>tu wstaw kod Pascala/Delphi</delphi> (alt+d)";
x_help = "C++: <cpp>tu wstaw kod C/C++/Java/JS</cpp> (alt+x)";
h_help = "PHP: <php>tu wstaw kod PHP</php> (alt+h)";
a_help = "Assembler: <asm>tu wstaw kod Assemblera</asm> (alt+a)";
l_help = "Plain: <plain>???</plain> (alt+l)";
k_help = "Wikipedia: <wiki>tu wstaw słowo-odnośnik do encyklopedii</wiki> (alt+k)";
o_help = "Ort!: <ort>tu wstaw błędny wyraz</ort> (alt+o)";

z_help = "Zamknij wszystkie otwarte tagi. (alt+z)";
s_help = "Kolor czcionki: <font color=red>tekst</font> | możesz także podać color=#FF0000";
f_help = "Rozmiar czcionki: <font size=12>tekst</font>";


h1_help = "Naglowek najwyzszego rzedu.";
h2_help = "Naglowek drugiego rzedu.";
h3_help = "Naglowek trzeciego rzedu.";
h4_help = "Naglowek czwartego rzedu.";
h5_help = "Naglowek piatego rzedu.";
h6_help = "Wycentruj";

ul_help = "Lista wyliczeniowa.";
ol_help = "Lista numerowana.";
li_help = "Element listy.";

// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('<b>','</b>','<i>','</i>','<u>','</u>','<code>','</code>','<url>','</url>','<email>','</email>','<quote>','</quote>','<image>','</image>','<delphi>','</delphi>','<cpp>','</cpp>','<php>','</php>','<asm>','</asm>','<plain>','</plain>','<wiki>','</wiki>','<ort>','</ort>','<h1>','</h1>','<h2>','</h2>','<h3>','</h3>','<h4>','</h4>','<h5>','</h5>','<h6>','</h6>','<ul>','</ul>','<li>','</li>','<ol>','</ol>');
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
	document.doc.helpbox.value = eval(help + "_help");
}


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function append_smile(smile)
{
	smile = ' ' + smile + ' ';

/*** piechnat - zmiany - BEGIN ***/

	selReplace(document.doc.text, smile);

/*
	if ( document.doc.content.createTextRange && document.doc.content.caretPos )
	{
		var caretPos = document.doc.content.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? smile + ' ' : smile;
		document.doc.content.focus();
	}
	else
	{
		document.doc.content.value  += smile;
		document.doc.content.focus();
	}
*/

/*** piechnat - zmiany - END ***/

}
/*
function emoticon(text) {
	var txtarea = document.doc.content;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}
*/
function bbfontstyle(bbopen, bbclose) {
	var txtarea = document.doc.text;

/*** piechnat - zmiany - BEGIN ***/

	if (selExist(txtarea)) {
		selReplace(txtarea, bbopen, bbclose)
	}
	else {
		selReplace(txtarea, bbopen + bbclose)
	}

/*
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			txtarea.value += bbopen + bbclose;
			txtarea.focus();
			document.doc.color.selectedIndex = 0;
			document.doc.size.selectedIndex = 0;
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		txtarea.focus();
		document.doc.color.selectedIndex = 0;
		document.doc.size.selectedIndex = 0;
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbopen, bbclose);
		document.doc.color.selectedIndex = 0;
		document.doc.size.selectedIndex = 0;
		return;
	}
	else
	{
		txtarea.value += bbopen + bbclose;
		txtarea.focus();
	}
*/

/*** piechnat - zmiany - END ***/

	document.doc.color.selectedIndex = 0;
	document.doc.size.selectedIndex = 0;

//	storeCaret(txtarea); // piechnat - zmiany

}


function bbstyle(bbnumber) {
	var txtarea = document.doc.text;

	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;

			addToEnd(txtarea, bbtags[butnumber + 1]);  // piechnat
//			txtarea.value += bbtags[butnumber + 1];    // zmiany

			buttext = eval('document.doc.addbbcode' + butnumber + '.value');
			eval('document.doc.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		addToEnd(txtarea, closeTags(txtarea.value))
		txtarea.focus();
		return;
	}

/*** piechnat - zmiany - BEGIN ***/

	if (selExist(txtarea)) {
		selReplace(txtarea, bbtags[bbnumber], bbtags[bbnumber + 1]);
		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 = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}
*/

/*** piechnat - zmiany - END ***/

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;

				selReplace(txtarea, bbtags[butnumber + 1]); //addToEnd(txtarea, bbtags[butnumber + 1]);  // piechnat
//				txtarea.value += bbtags[butnumber + 1];    // zmiany

				buttext = eval('document.doc.addbbcode' + butnumber + '.value');
				eval('document.doc.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another

			selReplace(txtarea, bbtags[15]); //addToEnd(txtarea, bbtags[15]);  // piechnat
//			txtarea.value += bbtags[15];    // zmiany

			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.doc.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag

/*** piechnat - zmiany - BEGIN ***/

		selReplace(txtarea, bbtags[bbnumber]);

/*
		txtarea.value += bbtags[bbnumber];
*/

/*** piechnat - zmiany - END ***/

		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('document.doc.addbbcode'+bbnumber+'.value += "*"');
		txtarea.focus();
		return;
	}

//	storeCaret(txtarea); // piechnat - zmiany

}



/*** piechnat - zmiany - END ***/


// 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(); // piechnat - zmiany
}

function CheckForm()
{

	if ( document.doc.title.value == "" )
	{
		alert("Nie wpisano tematu!");
		document.doc.subject.focus();
		return false;
	}

	if ( document.doc.text.value == "" )
	{
		alert("Nie wpisano treści!");
		document.doc.text.focus();
		return false;
	}

//	document.doc.Submit.disabled = true;	//to powoduje zle dzialanie skryptu
	bbstyle(-1);
	return true;
}

  function append_attachment(attachment_name)
  {
      attachment_name = '<image>' + escape(attachment_name) +'</image> ';

/*** piechnat - zmiany - BEGIN ***/

      selReplace(document.doc.text, attachment_name);


      if ( opener.document.doc.text.createTextRange && opener.document.doc.text.caretPos )
      {
          var caretPos = opener.document.doc.text.caretPos;

          caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? attachment_name + ' ' : attachment_name;
          opener.document.doc.text.focus();
      }
      else
      {
          opener.document.doc.text.value  += attachment_name;
          opener.document.doc.text.focus();
      }
/**/

/*** piechnat - zmiany - END ***/

  }
  function append_attachment2(attachment_name)
  {
      attachment_name = '<url>' + escape(attachment_name) +'</url> ';

/*** piechnat - zmiany - BEGIN ***/

      selReplace(document.doc.text, attachment_name);


      if ( opener.document.doc.text.createTextRange && opener.document.doc.text.caretPos )
      {
          var caretPos = opener.document.doc.text.caretPos;

          caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? attachment_name + ' ' : attachment_name;
          opener.document.doc.text.focus();
      }
      else
      {
          opener.document.doc.text.value  += attachment_name;
          opener.document.doc.text.focus();
      }
/**/

/*** piechnat - zmiany - END ***/

  }

