

function field_background_image (element, background) {
	element.style.backgroundImage = 'url('+background+')';
	return true;
}

function login_name_onfocus () {
	var element = document.getElementById ('rpg2usrname');
	if (element == null) {
		return false;
	}

	field_background_image (element, '/images/rpg2/inputbg1_b.jpg');
}

function login_passwd_onfocus () {
	var element = document.getElementById ('rpg2usrpw');
	if (element == null) {
		return false;
	}

	field_background_image (element, '/images/rpg2/inputbg2_b.jpg');
}

function login_name_onblur () {
	var element = document.getElementById ('rpg2usrname');
	if (element == null) {
		return false;
	}

	var background = (element.value == '')? '/images/rpg2/inputbg1_a.jpg' : '/images/rpg2/inputbg1_b.jpg';
	field_background_image (element, background);
}

function login_passwd_onblur () {
	var element = document.getElementById ('rpg2usrpw');
	if (element == null) {
		return false;
	}

	var background = (element.value == '')? '/images/rpg2/inputbg2_a.jpg' : '/images/rpg2/inputbg2_b.jpg';
	field_background_image (element, background);
}

function login_onload () {
	var usrpw = document.getElementById ('rpg2usrpw');
	if (usrpw == null) {
		return false;
	}

	var usrname = document.getElementById ('rpg2usrname');
	if (usrname == null) {
		return false;
	}

	if (usrname.value == '') {
		field_background_image (usrname, '/images/rpg2/inputbg1_a.jpg');
	}

	if (usrpw.value == '') {
		field_background_image (usrpw, '/images/rpg2/inputbg2_a.jpg');
	}
}




function submit_form (id) {
	document.forms[id].submit();
}


function hide_notification_area () {
	if (document.getElementById) {
		document.getElementById('notificationbox').style.visibility = 'hidden';
	} else {
		if (document.layers) {
			document.hideShow.visibility = 'hidden';
		} else {
			document.all.hideShow.style.visibility = 'hidden';
		}
	}

	return true;
}

function show_notification (errmsg) {
	var msg = document.getElementById('notificationbox-message');
	var box = document.getElementById('notificationbox');

	if (box == null || msg == null) {
		alert (msg);
		return ;
	}

	var div = document.createElement('div');
	div.setAttribute ('id', 'notificationbox-message');
	div.appendChild (document.createTextNode(errmsg));
	$("#notificationbox-message").replaceWith (div);

	box.style.visibility = 'visible';
	box.style.display = 'block';
}

function goto_news_page (self, maxpage) {
	var value = document.getElementById(self).value;

	if (isNaN(parseInt (value))) {
		show_notification ('Bitte geben sie eine valide Seitenzahl ein!');
		return false;
	}

	if (value < 0 || value > maxpage) {
		show_notification ('Diese Seite existiert nicht.');
		return false;
	}

	window.location = '/news/page/'+value+'.html';
}

function goto_gal_page (self, maxpage, game, pagename) {
	var value = document.getElementById(self).value;

	if (isNaN(parseInt (value))) {
		show_notification ('Bitte geben sie eine valide Seitenzahl ein!');
		return false;
	}

	if (value < 0 || value > maxpage) {
		show_notification ('Diese Seite existiert nicht.');
		return false;
	}

	window.location = '/games/'+game+'/'+pagename+'/page/'+value+'.html';
}


function random_quote (uname) {
	var randquote = '...';

	if (uname == 'Mog') {
		var quotes = Array ("Ich mag dir keine Briefe schreiben, kupo!",
							"Rate mal welcher Finger das ist, kupo!",
							"Verschwinde, kupo!");
		randquote = quotes[Math.floor (Math.random() * quotes.length)];
	} else if (uname == 'Kaktor') {
		randquote = '...';
	} else {
		var quotes = Array ("Chupon!",
							"Muscle heads? Hate 'em!",
							"I'm nothing but a...stupid octpus!",
							"Yaaaouch! Seafood soup!",
							"Mwa ha ha",
							"I owe you one, so I'm gunna jam up your opera!",
							"I'll pretend to be Setzer, and foil their little plan!",
							"Let's see if Maria can shrug THIS off!",
							"N'ghaa!",
							"Rats!",
							"Silence! You're in the presence of octopus royalty!",
							"I have more lives than I do arms!"
							);
		randquote = quotes[Math.floor (Math.random() * quotes.length)];
	}

	var newdiv = document.createElement('div');
	newdiv.appendChild (document.createTextNode(randquote));
	newdiv.setAttribute ('class', 'ultrosblase');
	newdiv.setAttribute ('id', 'ultrosblase');
	$('#ultrosblase').replaceWith (newdiv);
}





function texteditor_insert (obj, starttag, endtag, linestart, lineend, singleline, block) {
	if (obj.type != 'textarea') {
		return ;
	}

	var selectionstart = obj.selectionStart;
	var selectionend = obj.selectionEnd;
	var content = obj.value;


	if (document.currentFocus == obj) {
		var foo = (linestart)? "\n" : "";
		var bar = (lineend)? "\n" : "";
		obj.value = foo + starttag + endtag + bar;
	}
	else if (selectionstart == selectionend) {
		var endstr = content.substring(selectionend);
		var startstr = content.substring(0, selectionstart);

		if (startstr[startstr.length-1] != "\n" && linestart ) {
			startstr = startstr + "\n";
		}

		if (endstr[endstr.length-1] != '\n' && lineend) {
			endstr = "\n" + endstr;
		}

		obj.value = startstr + starttag + endtag + endstr;
	}
	else if (selectionstart != selectionend) {
		var newcontent = obj.value.substring(selectionstart, selectionend);

		var foo = "";
		var bar = "";

		if (linestart == true) {
			if (newcontent[0] != "\n") {
				foo = "\n";
			}
		}

		if (lineend == true) {
			if (newcontent[newcontent.length-1] != "\n") {
				bar = "\n";
			}
		}


		if (block == true) {
			newcontent = newcontent.replace (/\n/g, "\n   ");
		}

		if (singleline == true) {
			if (newcontent[0] == "\n" && newcontent[newcontent.length-1] == "\n") {
				newcontent = newcontent.substring (1, newcontent.length-1);
				newcontent = "\n"+starttag+newcontent.replace(/\n/g, endtag+"\n"+starttag)+endtag+"\n";
			}
			else if (newcontent[0] == "\n") {
				newcontent = newcontent.substring (1);
				newcontent = "\n"+starttag+newcontent.replace(/\n/g, endtag+"\n"+starttag)+endtag;
			}
			else if (newcontent[newcontent.length-1] == "\n") {
				newcontent = newcontent.substring (0, newcontent.length-1);
				newcontent = starttag+newcontent.replace(/\n/g, endtag+"\n"+starttag)+endtag+"\n";
			}
			else {
				newcontent = starttag+newcontent.replace(/\n/g, endtag+"\n"+starttag)+endtag;
			}
		}
		else {
			newcontent = starttag+newcontent+endtag;
		}

		obj.value = content.substring(0, selectionstart) + foo + newcontent + bar + content.substring(selectionend);
	}
}

function texteditor_insert_italic (fieldid) {
	texteditor_insert (document.getElementById(fieldid), "[I]", "[/I]", false, false, false, false);
}

function texteditor_insert_bold (fieldid) {
	texteditor_insert (document.getElementById(fieldid), "[B]", "[/B]", false, false, false, false);
}

function texteditor_insert_underline (fieldid) {
	texteditor_insert (document.getElementById(fieldid), "[U]", "[/U]", false, false, false, false);
}

function texteditor_insert_quote (fieldid) {
	texteditor_insert (document.getElementById(fieldid), "[QUOTE]", "[/QUOTE]", false, false, false, false);
}

function texteditor_insert_url (fieldid) {
	var textfield = document.getElementById(fieldid);


	var selectionstart = textfield.selectionStart;
	var selectionend = textfield.selectionEnd;
	var link = null;

	var link = prompt("Bitte geben Sie die URL ein:", "http://");
	if (link == '' || link == 'http://') {
		return ;
	}

	if (selectionstart == selectionend) {
		desc = prompt("Bitte geben Sie die Beschreibung ein:", "");
		if (desc == '') {
			desc = link;
		}

		texteditor_insert (textfield, "[URL=\""+link+"\"]"+desc, "[/URL]", false, false, false, false);
	} else {
		texteditor_insert (textfield, "[URL=\""+link+"\"]", "[/URL]", false, false, false, false);
	}
}
function toggleSpoiler(block) {
    var s1 = block.parentNode.nextSibling.style;
    var s2 = block.parentNode.parentNode.style;
    s1.display = (s1.display == 'none') ? 'block' : 'none';
    s2.visibility = (s2.visibility == 'hidden') ? 'visible' : 'hidden';
    s2.border = '1px solid #c4c4c4';
}
function toggleCode(legend) {
    var sp = legend.nextSibling.nextSibling.style;
    var sf = legend.parentNode.style;
    if(sp.display != 'none') {
        sp.display = 'none';
        sf.visibility = 'hidden';
    } else {
        sp.display = '';
        sf.visibility = 'visible';
    }
}

