// 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_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);


//Instanz festlegen
var instanz = document.pfform.hrcbox;

//Startwerte
opentags = '';

//Tags
hrcbold   = new Array('[b]','[/b]');
hrcitalic = new Array('[i]','[/i]');
hrcul     = new Array('[u]','[/u]');
hrcquote  = new Array('[quote]','[/quote]');












//Selektierten Text taggen
function phrc(hrc) {
    if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = hrc[0] + theSelection + hrc[1];
			instanz.focus();
			theSelection = '';
			return;
	    } else {
	       opentag(hrc);
	    }
    } else if (instanz.selectionEnd && (instanz.selectionEnd - instanz.selectionStart > 0)) {
		    mozWrap(instanz, hrc[0], hrc[1]);
		    return;
	} else  {
	    opentag(hrc);
	}
	instanz.focus();
}

// Tag einzeln öffnen/schliessen
function opentag(hrc) {
    if ( opentags.indexOf(hrc[1]) != -1 ) {
        AddCode = hrc[1];
        opentags = opentags.replace(hrc[1],'');
    } else {
        AddCode = hrc[0];
        opentags = hrc[1] + opentags;
    }
    AddToField(AddCode);
}

//Tags schliessen
function closetags () {
	AddToField(opentags);
    opentags = '';
    lasttag = '';
}

//Caret Store
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
	document.pfform.txtlen.value = textEl.value.length;
}

//Tag adden
function AddToField(AddCode) {
   	if ( instanz.createTextRange && instanz.caretPos )
	{
		var caretPos = instanz.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? AddCode + ' ' : AddCode;
	}
	else
	{
		instanz.value += AddCode;
	}
    instanz.focus();
}

//Bild
function hrcimg() {
    imgurl = prompt("URL zum Bild eingeben:","http://");
	if ((imgurl != null) && (imgurl != "")) {
	    AddToField("[img]"+imgurl+"[/img] ");
    }
}

//Hyperlink
function hrclink() {
    linktext = prompt("Titel des Links:","");
    linkurl = prompt("URL des Links:","http://");
 	if ((linkurl != null) && (linkurl != "")) {
		if ((linktext != null) && (linktext != ""))
			AddToField("[URL="+linkurl+"]"+linktext+"[/URL] ");
		else
			AddToField("[URL]"+linkurl+"[/URL] ");
    }
}

//E-Mail
function hrcemail() {
    linkemail = prompt("EMail Adresse eingeben:","");
	if ((linkemail != null) && (linkemail != "")) {
		AddToField("[EMAIL]"+linkemail+"[/EMAIL] ");
    }
}

//textarea Grösse
function zoomarea(size) {
    if ( instanz.rows < 5 && size < 0 ) {
        size = 0;
    }
    if ( instanz.rows > 25 && size > 0 ) {
        size = 0;
    }
    instanz.rows = instanz.rows + size;
}

// From http://www.massless.org/mozedit/
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;
}

//Farbcode
function hrccolor(color) {
    code = new Array('[color=' + color + ']','[/color]');
    phrc(code);
}

//Textgrösse
function hrcsize(size) {
    code = new Array('[size=' + size + ']','[/size]');
    phrc(code);
}

//HoverClass
function pmouseover(el) {
	el.className = "editraise";
}

function pmouseout(el) {
	el.className = "editbutton";
}

function pmousedown(el) {
	el.className = "editpress";
}

function pmouseup(el) {
	el.className = "editraise";
}
