/**
* OpenPHPNuke: Great Web Portal System
*
* Copyright (c) 2001-2010 by
* Heinz Hombergs heinz@hhombergs.de
* Stefan Kaletta stefan@kaletta.de
* Alexander Weber xweber@kamelfreunde.de
*
* This program is free software. You can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
* See LICENSE for details.
*
* This software is based on various code found in the internet
* See CREDITS for details
* If you are an author of a part of opn and not you are not mentioned
* SORRY for that ! We respect your rights to your code, so please send
* an eMail to devteam@openphpnuke.com we will include you to the CREDITS asap
*/

var oEditor = new Array();
var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf("gecko")!=-1) && (clientPC.indexOf("spoofer")==-1)
  && (clientPC.indexOf("khtml") == -1) && (clientPC.indexOf("netscape/7.0")==-1));
var is_safari = ((clientPC.indexOf("AppleWebKit")!=-1) && (clientPC.indexOf("spoofer")==-1));
var is_khtml = (navigator.vendor == "KDE" || ( document.childNodes && !document.all && !navigator.taintEnabled ));
var myAgent   = clientPC;
var myVersion = parseInt(navigator.appVersion);
var is_ie   = ((myAgent.indexOf("msie") != -1)  && (myAgent.indexOf("opera") == -1));
var is_win   =  ((myAgent.indexOf("win")!=-1) || (myAgent.indexOf("16bit")!=-1));
var is_opera = false;

if (clientPC.indexOf("opera")!=-1) {
	is_opera = true;
	var is_opera_preseven = (window.opera && !document.childNodes);
	var is_opera_seven = (window.opera && document.childNodes);
}

var opnurl = "";

var codeurlURL = "";
var codeurlTitle = "";
var codewikiTerm = "";
var codewikiLang = "";
var codewikiLangLang = "";
var codebookterm = "";
var codeImage = "";
var codeEmail = "";
var codeItem = "";

function storeCaret (textEl) {
	if (textEl.createTextRange) {
		textEl.caretPos =document.selection.createRange().duplicate();
	}
}

function insertAtCaret (textEl, text) {
	if (is_opera) {
		addText(text, "", false, textEl);
	} else if(document.selection  && !is_gecko) {
		IEWrap(textEl, text, "");
	} else if(textEl.selectionStart || textEl.selectionStart == "0") {
		mozWrap(textEl,text,"");
	} else {
		addText(text, "", false, textEl);
	}
	storeCaret(textEl);
}

function insertAroundCaret (textEl, textbefore, textafter) {
	if (is_opera) {
		addText(textbefore + textafter, "", false, textEl);
	} else if(document.selection  && !is_gecko) {
		IEWrap(textEl, textbefore, textafter);
	} else if(textEl.selectionStart || textEl.selectionStart == "0") {
		mozWrap(textEl,textbefore,textafter);
	} else {
		addText(textbefore + textafter, "", false, textEl);
	}
	storeCaret(textEl);
}

function bbc_seturl(url) {
	if (opnurl == "") {
		opnurl = url;
	}
}

function bbc_highlight(something, mode) {
	something.style.backgroundImage = "url("+opnurl+"/images/bbcode" + (mode ? "/bbc_hoverbg.gif)" : "/bbc_bg.gif)");
}

function mozWrap(textEl,textbefore,textafter) {
	var startPos = textEl.selectionStart;
	var endPos = textEl.selectionEnd;
	var scrollTop=textEl.scrollTop;
	var myText = (textEl.value).substring(startPos, endPos);
	var subst = "";
	if(!myText) { myText="";}
	if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
		subst = textbefore + myText.substring(0, (myText.length - 1)) + textafter + " ";
	} else {
		subst = textbefore + myText + textafter;
	}
	textEl.value = textEl.value.substring(0, startPos) + subst +
	textEl.value.substring(endPos, textEl.value.length);
	textEl.focus();
	var cPos=startPos+(textbefore.length+myText.length+textafter.length);
	textEl.selectionStart=cPos;
	textEl.selectionEnd=cPos;
	textEl.scrollTop=scrollTop;
}

function IEWrap(textEl, textbefore, textafter) {
	var theSelection = document.selection.createRange().text;
	if(!theSelection) {
		OtherAppend(textEl, textbefore, textafter);
	} else {
		document.selection.createRange().text =
		theSelection.charAt(theSelection.length - 1) == " " ?
		textbefore + theSelection.substring(0, theSelection.length - 1) + textafter + " " : textbefore + theSelection + textafter;
		textEl.focus();
	}
}

function OtherAppend(textEl, textbefore, textafter) {
	textEl.value  += textbefore + textafter;
	textEl.focus();
}

function CheckAll(name,element) {
	for (var i=0;i<document.forms[name].elements.length;i++) {
		var e = document.forms[name].elements[i];
		if ((e.name != element) && (e.type=="checkbox"))
			e.checked = document.forms[name].elements[element].checked;
	}
}

function CheckAllName(name,element, name1) {
	for (var i=0;i<document.forms[name].elements.length;i++) {
		var e = document.forms[name].elements[i];
		var ename = e.name.indexOf (name1);
		if ((e.name != element) && (e.type=="checkbox") && (ename > -1))
			e.checked = document.forms[name].elements[element].checked;
	}
}

function CheckCheckAll(name,element) {
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<document.forms[name].elements.length;i++) {
		var e = document.forms[name].elements[i];
		if ((e.name != element) && (e.type=="checkbox")) {
			TotalBoxes++;
			if (e.checked) {
				TotalOn++;
			}
		}
	}
	if (TotalBoxes==TotalOn) {
		document.forms[name].elements[element].checked=true;
	} else {
		document.forms[name].elements[element].checked=false;
	}
}

function CheckCheckAllName(name,element, name1) {
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<document.forms[name].elements.length;i++) {
		var e = document.forms[name].elements[i];
		var ename = e.name.indexOf (name1);
		if ((e.name != element) && (e.type=="checkbox") && (ename > -1)) {
			TotalBoxes++;
			if (e.checked) {
				TotalOn++;
			}
		}
	}
	if (TotalBoxes==TotalOn) {
		document.forms[name].elements[element].checked=true;
	} else {
		document.forms[name].elements[element].checked=false;
	}
}

function switch_display_inline(id){
	if (document.getElementById(id).style.display != 'inline') {
		document.getElementById(id).style.display = 'inline';
	} else {
		document.getElementById(id).style.display = 'block';
	}
}

function switch_display(id){
	if (document.getElementById(id).style.display != 'block') {
		document.getElementById(id).style.display = 'block';
	} else {
		document.getElementById(id).style.display = 'none';
	}
}

function hideTableColumn (colIndex, ftable) {
	var table = document.all ? 'document.all.' + ftable : document.getElementById(ftable);
	for (var r = 0; r < table.rows.length; r++) {
		table.rows[r].cells[colIndex].style.display = 'none';
	}
}

function showTableColumn (colIndex, ftable) {
	var table = document.all ? 'document.all.' + ftable : document.getElementById(ftable);
  for (var r = 0; r < table.rows.length; r++) {
		table.rows[r].cells[colIndex].style.display = '';
	}
}

function switchTableColumn (colIndex, ftable) {
	var table = document.all ? 'document.all.' + ftable : document.getElementById(ftable);
  for (var r = 0; r < table.rows.length; r++) {
		if (table.rows[r].cells[colIndex].style.display != '') {
			table.rows[r].cells[colIndex].style.display = '';
		}	else {
			table.rows[r].cells[colIndex].style.display = 'none';
		}
	}
}

function writeCookies(name, data){
	var cookieStr = name + "="+ data;
	document.cookie = cookieStr;
}

function blocking(name) {
	var current = '';
	if (document.layers) {
		current = (document.layers[name].display == 'none') ? 'block' : 'none';
		writeCookies(name, current);
		document.layers[name].display = current;
	} else if (document.all) {
		current = (document.all[name].style.display == 'none') ? 'block' : 'none';
		writeCookies(name, current);
		document.all[name].style.display = current;
	} else if (document.getElementById) {
		current = (document.getElementById(name).style.display == 'none') ? 'block' : 'none';
		writeCookies(name, current);
		document.getElementById(name).style.display = current;
	}
}

var win=null;
function NewWindow(mypage,myname,w,h){
	if (w == 0) {
		w = screen.width;
	}
	if (w < -1) {
		w = (screen.width * (w * -1) ) / 100;
	}
	if (h == 0) {
		h = screen.height;
	}
	if (h < -1) {
		h = (screen.height * (h * -1) ) / 100;
	}
	var LeftPosition=(screen.width)?(screen.width-w)/2:100;
	var TopPosition=(screen.height)?(screen.height-h)/2:100;
	var settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no, personalbar=no,status=no,directories=no,menubar=no,toolbar=no,resizable=yes';
	win=window.open(mypage,myname,settings);
	if(win.focus){
		win.focus();
	}
}

function NewEditWindow(mypage,myname,w,h){
	if (w == 0) {
		w = screen.width;
	}
	if (h == 0) {
		h = screen.height;
	}
	var LeftPosition=(screen.width)?(screen.width-w)/2:100;
	var TopPosition=(screen.height)?(screen.height-h)/2:100;
	var settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,toolbar=no,status=yes,resizable=yes';
	win=window.open(mypage,myname,settings);
	if(win.focus){
		win.focus();
	}
}

function CloseNewWin(){
	if(win!=null && win.open) {
		win.close();
	}
}

function setdiv(elem,text) {
	insertAroundCaret(elem, "<div align=\""+text+"\">","<\/div>");
	elem.focus();
	return;
}

function setspan(elem,text, text1) {
	insertAroundCaret(elem, "<span style=\""+text+": "+text1+";\">","<\/span>");;
	elem.focus();
	return;
}

function setfontcolor(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	insertAroundCaret(elem, "<span style=\"color: "+thisItem+";\">","<\/span>");
	elem.focus();
	return;
}

function setfont(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	insertAroundCaret(elem, "<span style=\"font-family: "+thisItem+";\">","<\/span>");
	elem.focus();
	return;
}

function setfontsize(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	insertAroundCaret(elem, "<span style=\"font-size: "+thisItem+"px;\">","<\/span>");
	elem.focus();
	return;
}

function setchar(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	insertAtCaret(elem, "&"+thisItem+";");
	elem.focus();
	return;
}

function setbbcode(elem,bbcode) {
	if (bbcode == "hr") {
		insertbbcode(elem,"["+bbcode+"]","");
	} else {
		insertbbcode(elem,"["+bbcode+"]","[/"+bbcode+"]");
	}
	return;
}

function setbbcodespan(elem,text, text1) {
	insertbbcode(elem,"["+text+"="+text1+"]","[/"+text+"]");
	return;
}

function setbbcodecolor(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "[color="+thisItem+"]";
	insertbbcode(elem,itemBBCode,"[/color]");
	return;
}

function setbbcodesearch(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "["+thisItem+"]";
	var itemBBCodeEnd = "[/"+thisItem+"]";
	insertbbcode(elem,itemBBCode,itemBBCodeEnd);
	return;
}

function setbbcodeslogan(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "[slogan="+thisItem+"]";
	insertbbcode(elem,itemBBCode,"[/slogan]");
	return;
}

function setbbcodefont(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "[font="+thisItem+"]";
	insertbbcode(elem,itemBBCode,"[/font]");
	return;
}

function setbbcodefontsize(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "[size="+thisItem+"px]";
	insertbbcode(elem,itemBBCode,"[/size]");
	return;
}

function setbbcodechar(elem,elem1) {
	var thisItem = elem1.options[elem1.selectedIndex].value;
	var itemBBCode = "["+thisItem+"]";
	insertbbcode(elem,itemBBCode,"");
	return;
}

function insertbbcode(elem,tagOpen,tagClose) {
	insertAroundCaret(elem, tagOpen,tagClose);
	elem.focus();
}

function getSelectedText(message) {
	var selected = '';

	if(navigator.appName=="Netscape" &&  message.textLength>=0 && message.selectionStart!=message.selectionEnd )
  		selected=message.value.substring(message.selectionStart,message.selectionEnd);	

	else if( (myVersion >= 4) && is_ie && is_win ) {
		if(message.isTextEdit){
			message.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;

			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(rng.text.length > 0) selected = rng.text;
			}
		}
	}

  	return selected;
}

function addText(theTag, theClsTag, isSingle, message) {
	var isClose = false;
	var set=false;
  	var old=false;
  	var selected="";

  	if(message.textLength>=0 ) { // mozilla, firebird, netscape
  		if(theClsTag!="" && message.selectionStart!=message.selectionEnd) {
  			selected=message.value.substring(message.selectionStart,message.selectionEnd);
  			str=theTag + selected+ theClsTag;
  			old=true;
  			isClose = true;
  		}
		else {
			str=theTag;
		}

		message.focus();
		start=message.selectionStart;
		end=message.textLength;
		endtext=message.value.substring(message.selectionEnd,end);
		starttext=message.value.substring(0,start);
		message.value=starttext + str + endtext;
		message.selectionStart=start;
		message.selectionEnd=start;

		message.selectionStart = message.selectionStart + str.length;

		if(old) { return false; }

		set=true;

		if(isSingle) {
			isClose = false;
		}
	}
	if ( (myVersion >= 4) && is_ie && is_win) {  // Internet Explorer
		if(message.isTextEdit) {
			message.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null){
				if(theClsTag != "" && rng.text.length > 0)
					theTag += rng.text + theClsTag;
				else if(isSingle)
					isClose = true;

				rng.text = theTag;
			}
		} else {
			if(isSingle) isClose = true;

			if(!set) {
					  message.value += theTag;
				  }
		}
	} else {
		if(isSingle) isClose = true;

		if(!set) {
			message.value += theTag;
		}
	}

	message.focus();

	return isClose;
}

function fckeditor_OnComplete( editorInstance ) {
	oEditor[oEditor.length] = editorInstance ;
}

function InsertFCK(instance,text) {
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(instance);
	oEditor.InsertHtml( text ) ;
}

function x () {
	return;
}

function submitForm(s,newtext) {
	s.value = newtext;
}


function setCodeVars(url, title, term, lang, langlang, bookterm, image, email, listitem) {
	codeurlURL = url;
	codeurlTitle = title;
	codewikiTerm = term;
	codewikiLang = lang;
	codewikiLangLang = langlang;
	codebookterm = bookterm;
	codeImage = image;
	codeEmail = email;
	codeItem = listitem;
}

function DoPromptBBcode(elem,action) {
	var BBCode = "";
	if (action == "url") {
		var thisURL = prompt(codeurlURL, "http://");
		var thisTitle = prompt(codeurlTitle, "Page Title");
		if ((thisURL != "") && (thisURL) && (typeof thisURL != "undefined") && (thisTitle != "") && (thisTitle) && (typeof thisTitle != "undefined")){
			BBCode = "[url="+thisURL+"]"+thisTitle+"[/url]";
		}
	}
	if (action == "wikipedia") {
		var thisTerm = prompt(codewikiTerm);
		var thisLang = prompt(codewikiLang, codewikiLangLang);
		if ((thisTerm != "") && (thisTerm) && (typeof thisTerm != "undefined") && (thisLang != "") && (thisLang) && (typeof thisLang != "undefined")){
			BBCode = "[wikipedia="+thisLang+"]"+thisTerm+"[/wikipedia]";
		}
	}
	if (action == "wikibook") {
		var thisTerm1 = prompt(codebookterm);
		var thisLang1 = prompt(codewikiLang, codewikiLangLang);
		if ((thisTerm1 != "") && (thisTerm1) && (typeof thisTerm1 != "undefined") && (thisLang1 != "") && (thisLang1) && (typeof thisLang1 != "undefined")){
			BBCode = "[wikibook="+thisLang1+"]"+thisTerm1+"[/wikibook]";
		}
	}
	if (action == "image") {
		var thisImage = prompt(codeImage, "http://");
		if ((thisImage != "") && (thisImage) && (typeof thisImage != "undefined")){
			BBCode = "[img]"+thisImage+"[/img]";
		}
	}
	if (action == "email") {
		var thisEmail = prompt(codeEmail);
		if ((thisEmail != "") && (thisEmail) && (typeof thisEmail != "undefined")){
			BBCode = "[email]"+thisEmail+"[/email]";
		}
	}
	if (action == "listitem") {
		var thisItem = prompt(codeItem);
		if ((thisItem != "") && (thisItem) && (typeof thisItem != "undefined")){
			BBCode = "[*]"+thisItem+"[/*]";
		}
	}
	if ((BBCode != "") && (BBCode) && (typeof BBCode != "undefined")){
		insertAtCaret(elem, BBCode);
	}
	elem.focus();
	return;
}

function DoPrompt(elem, action) {
	var HTMLCode = "";
	if (action == "url") {
		var thisURL = prompt(codeurlURL, "http://");
		var thisTitle = prompt(codeurlTitle, "Page Title");
		if ((thisURL != "") && (thisURL) && (typeof thisURL != "undefined") && (thisTitle != "") && (thisTitle) && (typeof thisTitle != "undefined")){
			HTMLCode = "<a href=\"" + thisURL + "\" target=\"_blank\">" + thisTitle + "<\/a>";
		}
	}
	if (action == "wikipedia") {
		var thisTerm = prompt(codewikiTerm);
		var thisLang = prompt(codewikiLang, codewikiLangLang);
		if ((thisTerm != "") && (thisTerm) && (typeof thisTerm != "undefined") && (thisLang != "") && (thisLang) && (typeof thisLang != "undefined")){
			HTMLCode = "<a class=\"wiki\" href=\"http://" + thisLang + ".wikipedia.org/wiki/" + thisTerm + "\" target=\"_blank\">" + thisTerm + "<\/a>";
		}
	}
	if (action == "wikibook") {
		var thisTerm1 = prompt(codebookterm);
		var thisLang1 = prompt(codewikiLang, codewikiLangLang);
		if ((thisTerm1 != "") && (thisTerm1) && (typeof thisTerm1 != "undefined") && (thisLang1 != "") && (thisLang1) && (typeof thisLang1 != "undefined")){
			HTMLCode = "<a class=\"wiki\" href=\"http://" + thisLang1 + ".wikibooks.org/wiki/" + thisTerm1 + "\" target=\"_blank\">" + thisTerm1 + "<\/a>";
		}
	}
	if (action == "image") {
		var thisImage = prompt(codeImage, "http://");
		if ((thisImage != "") && (thisImage) && (typeof thisImage != "undefined")){
			HTMLCode = "<img src=\""+thisImage+"\" alt=\"\" />";
		}
	}
	if (action == "email") {
		var thisEmail = prompt(codeEmail);
		if ((thisEmail != "") && (thisEmail) && (typeof thisEmail != "undefined")){
			HTMLCode = "<a href=\"mailto:"+thisEmail+"\">"+thisEmail+"<\/a>";
		}
	}
	if (action == "listitem") {
		var thisItem = prompt(codeItem);
		if ((thisItem != "") && (thisItem) && (typeof thisItem != "undefined")){
			HTMLCode = "<li>"+thisItem+"<\/li>";
		}
	}
	if ((HTMLCode != "") && (HTMLCode) && (typeof HTMLCode != "undefined")){
		insertAtCaret(elem, HTMLCode);
	}
	elem.focus();
	return;
}

function opnFadeout (id, geschwindigkeit) {
	var fps = Math.round (geschwindigkeit / 100); 
	var tmp = 0;
    for(i = 100; i >= 0; i--) {
        setTimeout("opnFadeout_fade('" + id + "'," + i + ")", (tmp * fps));
        tmp++;
    }
}
function opnFadeout_fade (id, pas) {
	var heurix = document.getElementById(id).style;
	if(pas > 0) {
		heurix.opacity = (pas / 100);
		heurix.MozOpacity = (pas / 100);
		heurix.KhtmlOpacity = (pas / 100);
		heurix.filter = "alpha(opacity=" + pas + ")"; 
	} else {
		heurix.display = "none";
	}
}