  ///////////////////////////////////////////////////
  //Phpclanwebsite 1.20 BBCode Javascript Functions//
  ///////////////////////////////////////////////////
  var BBCimage = false;
  var BBCselected = false;

  // Check for Browser & Platform for PC & IE specific bits
  var clientPC = navigator.userAgent.toLowerCase();
  var clientVer = parseInt(navigator.appVersion);
  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);

// Specify the BBCstyle code tags
  BBCode = new Array();
  BBCtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[flash]','[/flash]','[s]','[/s]','[img]','[/img]','[url]','[/url]');
  BBCimage = false;
  
function BBCfontstyle(bbopen, bbclose){
  if (!window.txtarea) var txtarea = document.addnews.details;
  else var txtarea = window.txtarea
  if ((clientVer >= 4) && is_ie && is_win){
    BBCselected = document.selection.createRange().text;
    if (!BBCselected){
      txtarea.value += bbopen + bbclose;
      txtarea.focus();
      return;
    }
    document.selection.createRange().text = bbopen + BBCselected + bbclose;
    txtarea.focus();
    return;
  }else if (txtarea.selectionEnd){
    mozWrap(txtarea, bbopen, bbclose);
    return;
  }else{
    txtarea.value += bbopen + bbclose;
    txtarea.focus();
  }
  storeCaret(txtarea);
  }


function BBCstyle(bbnumber) {
  if (!window.txtarea) var txtarea = document.addnews.details;
  else var txtarea = window.txtarea
  if (!window.BBCode[txtarea]) BBCode[txtarea] = new Array();
  txtarea.focus();
  dontadd = false;
  BBCselected = false;
  bblast = 0;
  if (bbnumber == -1) {
    while (BBCode[txtarea][0]) {
      butnumber = arraypop(BBCode[txtarea]) - 1;
      txtarea.value += BBCtags[butnumber + 1];
      buttext = eval('document.addnews.BBC' + butnumber + '.value');
      eval('document.addnews.BBC' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    }
    BBCimage = false;
    return;
  }
  if ((clientVer >= 4) && is_ie && is_win){
    BBCselected = document.selection.createRange().text;
    if (BBCselected) {
      document.selection.createRange().text = BBCtags[bbnumber] + BBCselected + BBCtags[bbnumber+1];
      txtarea.focus();
      BBCselected = '';
      return;
    }
  }else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)){
    mozWrap(txtarea, BBCtags[bbnumber], BBCtags[bbnumber+1]);
    return;
  }
  for (i = 0; i < BBCode[txtarea].length; i++) {
    if (BBCode[txtarea][i] == bbnumber+1) {
      bblast = i;
      dontadd = true;
    }
  }
  if (dontadd) {
    while (BBCode[txtarea][bblast]) {
      butnumber = arraypop(BBCode[txtarea]) - 1;
      BBCemoticon(BBCtags[butnumber + 1]);
      buttext = eval('document.addnews.BBC' + butnumber + '.value');
      eval('document.addnews.BBC' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
      BBCimage = false;
    }
    txtarea.focus();
    return;
  } else {
    if (BBCimage && (bbnumber != 14)) {
      BBCemoticon(BBCtags[15]);
      lastValue = arraypop(BBCode[txtarea]) - 1;
      document.addnews.BBC14.value = "Img";
      BBCimage = false;
    }
    BBCemoticon(BBCtags[bbnumber]);
    if ((bbnumber == 14) && (BBCimage == false)) BBCimage = 1;
    arraypush(BBCode[txtarea],bbnumber+1);
    eval('document.addnews.BBC'+bbnumber+'.value += "*"');
    txtarea.focus();
    return;
  }
  }

  // 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;
  }

  // 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();
  else if (textEl.selectionStart){
    //Remove from browsers that don't support, such as FX. Alternate method used.
    textEl.caretPos = null;
    textEl.onclick = null;
    textEl.onselect = null;
    textEl.onkeyup = null;
  }
  }

function BBCemoticon(text) {
  if (!window.txtarea) var txtarea = document.addnews.details;
  else var txtarea = window.txtarea
  if (text.substring(0,1) != "[") 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;
    storeCaret(txtarea);
  } else if (txtarea.selectionStart || txtarea.selectionStart==0){
    var start = txtarea.selectionStart;
    var end = txtarea.selectionEnd;
    txtarea.value= txtarea.value.substring(0, start) + text + txtarea.value.substring(end,txtarea.value.length);
    txtarea.selectionStart = txtarea.selectionEnd = start + text.length;
  } else {
    txtarea.value  += text;
  }
  txtarea.focus();
  }

function BBCurl() {
  var FoundErrors = '';
  var enterURL   = prompt("Enter your URL", "http://");
  var enterTITLE = prompt("Enter the webpage title", "Webpage Title");
  if (!enterURL)    {
    FoundErrors += " You have not entered the URL yet!";
  }
  if (!enterTITLE)  {
    FoundErrors += " You have not entered the title yet!";
  }
  if (FoundErrors)  {
    alert("Error!"+FoundErrors);
    return;
  }
  var ToAdd = "[url="+enterURL+"]"+enterTITLE+"[/url]";
  BBCemoticon(ToAdd);
  }

function BBCimg() {
  var FoundErrors = '';
  var enterURL   = prompt("Enter your image URL","http://");
  if (!enterURL) {
    FoundErrors += " You have not entered the URL yet!";
  }
  if (FoundErrors) {
    alert("Error!"+FoundErrors);
    return;
  }
  var ToAdd = "[img]"+enterURL+"[/img]";
  BBCemoticon(ToAdd);
  }

function BBCflash() {
  var FoundErrors = '';
  var enterURL   = prompt("Enter the URL to the flash SWF File","http://");
  var enterWIDTH   = prompt("Enter the Width of the Flash Image","300");
  var enterHEIGHT   = prompt("Enter the Height of the Flash Image","50");
  if (!enterURL) {
    FoundErrors += " You have not entered the SWF URL yet!";
  }
  if (FoundErrors) {
    alert("Error!"+FoundErrors);
    return;
  }
  var ToAdd = "[flash="+enterWIDTH+"x"+enterHEIGHT+"]"+enterURL+"[/flash]";
  BBCemoticon(ToAdd);
  }

//Code for these javascript functions are a combination of code by Adam Annen (C), and
//code sourced from a freeware mod file "BBCode_faster.zip" by http://smartor.is-root.com
//Source of the above is available on request

// 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 changebox(fullid){
  if (!window.txtarea) var txtarea = document.addnews.details;
  else txtarea=window.txtarea;
  if (!window.BBCode[txtarea]) BBCode[txtarea] = new Array();
  if (fullid!=txtarea){
    while (BBCode[txtarea][0]) {
      butnumber = arraypop(BBCode[txtarea]) - 1;
      txtarea.value += BBCtags[butnumber + 1];
      buttext = eval('document.addnews.BBC' + butnumber + '.value');
      eval('document.addnews.BBC' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    }
    window.txtarea=fullid;
  }
}
