/*
Invision Free style UBBC (cross-browser)
Coded by NCGI (Uchiha Sasuke)
This code may not be posted with out express permission from it's creator
*/
AllTags = new Array('b','i','u','s','glow','shadow','move','pre','left','hr','center','right','size','font','url','ftp','img','email','td','tr','table','sup','sub','tt','code','quote','list','color');
function add(Material,fInfo) {
if (fInfo=="colors") {
Material = "[color="+Material+"][/color]";}
if (Material.match(/TEXT/)) {
Material = Material.replace(/TEXT/,"");}
if (Material.match(/URL/)) {
Material = Material.replace(/URL/,"");}
document.postmodify.message.focus(true);
if (Material=="[list][*][*][*][/list]") {
document.postmodify.message.value = document.postmodify.message.value+"[list]\r[*]\r[*]\r[*]\r[/list]";}
else if (Material=="/me ") {
document.postmodify.message.value = document.postmodify.message.value+"/me";}
else if (window.navigator.userAgent.match(/Opera\/7.54/)) {
OpenClose(Material);}
else if (window.getSelection || document.getSelection) {
var startAmount = Math.floor(document.postmodify.message.selectionStart);
var endAmount = Math.floor(document.postmodify.message.selectionEnd);
var totalAmount = Math.floor(document.postmodify.message.value.length);
if (startAmount!=endAmount) {
if (Material=="[table][tr][td][/td][/tr][/table]") {
document.postmodify.message.value = document.postmodify.message.value.substring(0,startAmount)+"[table][tr][td]"+document.postmodify.message.value.substring(startAmount,endAmount)+"[/td][/tr][/table]"+document.postmodify.message.value.substring(endAmount,totalAmount);}
else if (Material.match(/\[\//i)) {
document.postmodify.message.value = document.postmodify.message.value.substring(0,startAmount)+Material.split("][")[0]+"]"+document.postmodify.message.value.substring(startAmount,endAmount)+"["+Material.split("][")[1]+document.postmodify.message.value.substring(endAmount,totalAmount);}
else {
OpenClose(Material);}}
else {
OpenClose(Material);}
}
else if (document.selection && document.selection.createRange) {
document.postmodify.message.caretPos = document.selection.createRange();
if (document.postmodify.message.caretPos.text!="") {
if (Material=="[table][tr][td][/td][/tr][/table]") {
document.postmodify.message.caretPos.text = "[table][tr][td]"+document.postmodify.message.caretPos.text+"[/td][/tr][/table]";}
else if (Material.match(/\[\//i)) {
document.postmodify.message.caretPos.text = Material.split("][")[0]+"]"+document.postmodify.message.caretPos.text+"["+Material.split("][")[1];}
else {
OpenClose(Material);}}
else {
OpenClose(Material);}
}
else {
OpenClose(Material);
}}

function DoTagFind(Item) {
if (Item=="color" || Item=="font") {
GlobalOpenItem = new RegExp("\\["+Item+"=\\w+\\]","gi");}
else if (Item=="size") {
GlobalOpenItem = new RegExp("\\["+Item+"=\\d\\]","gi");}
else if (Item=="glow" || Item=="shadow") {
GlobalOpenItem = new RegExp("\\["+Item+"=\\w*,\\w*,\\d*\\]","gi");}
else {
GlobalOpenItem = new RegExp("\\["+Item+"\\]","gi");}
GlobalCloseItem = new RegExp("\\[\\/"+Item+"\\]","gi");
if (document.postmodify.message.value.match(GlobalOpenItem)) {
ItemOpenAmount = Math.floor(document.postmodify.message.value.match(GlobalOpenItem).length);}
else {
ItemOpenAmount = 0;}
if (document.postmodify.message.value.match(GlobalCloseItem)) {
ItemCloseAmount = Math.floor(document.postmodify.message.value.match(GlobalCloseItem).length);}
else {
ItemCloseAmount = 0;}
return ItemCloseAmount;
return ItemOpenAmount;
return Item;
}

function OpenClose(Item) {
ItemGet = "";
if (Item=="[hr]") {
ItemStrip = Item.split("]")[0].split("[")[1];}
else if (Item.match(/=/)) {
ItemGet = Item.split("][")[0].split("=")[1]
ItemStrip = Item.split("="+ItemGet)[0].split("[")[1];}
else {
ItemStrip = Item.split("][")[0].split("[")[1];}
DoTagFind(ItemStrip);
for (a=0;a<AllTags.length;a++) {
if (AllTags[a]==ItemStrip) {
if (Math.floor(ItemOpenAmount>ItemCloseAmount) && ItemStrip!="hr") {
if (window.navigator.userAgent.match(/Opera\/7.54/)) {
document.postmodify.message.value += "[/"+ItemStrip+"]";}
else if (window.getSelection || document.getSelection) {
document.postmodify.message.value = document.postmodify.message.value.substring(0,document.postmodify.message.selectionStart)+"[/"+ItemStrip+"]"+document.postmodify.message.value.substring(document.postmodify.message.selectionStart,document.postmodify.message.selectionEnd)+document.postmodify.message.value.substring(document.postmodify.message.selectionEnd,document.postmodify.message.value.length);}
else if (document.selection && document.selection.createRange) {
document.postmodify.message.caretPos.text = document.postmodify.message.caretPos.text+"[/"+ItemStrip+"]";}
else {
document.postmodify.message.value = document.postmodify.message.value+"[/"+ItemStrip+"]";}}
else {
if (window.navigator.userAgent.match(/Opera/)) {
if (ItemGet!="") {
document.postmodify.message.value += "["+ItemStrip+"="+ItemGet+"]";}
else {
document.postmodify.message.value += "["+ItemStrip+"]";}}
else if (window.getSelection || document.getSelection) {
if (ItemGet!="") {
document.postmodify.message.value = document.postmodify.message.value.substring(0,document.postmodify.message.selectionStart)+"["+ItemStrip+"="+ItemGet+"]"+document.postmodify.message.value.substring(document.postmodify.message.selectionStart,document.postmodify.message.selectionEnd)+document.postmodify.message.value.substring(document.postmodify.message.selectionEnd,document.postmodify.message.value.length);}
else {
document.postmodify.message.value = document.postmodify.message.value.substring(0,document.postmodify.message.selectionStart)+"["+ItemStrip+"]"+document.postmodify.message.value.substring(document.postmodify.message.selectionStart,document.postmodify.message.selectionEnd)+document.postmodify.message.value.substring(document.postmodify.message.selectionEnd,document.postmodify.message.value.length);}}
else if (document.selection && document.selection.createRange) {
if (ItemGet!="") {
document.postmodify.message.caretPos.text = document.postmodify.message.caretPos.text+"["+ItemStrip+"="+ItemGet+"]";}
else {
document.postmodify.message.caretPos.text = document.postmodify.message.caretPos.text+"["+ItemStrip+"]";}}
else {
if (ItemGet!="") {
document.postmodify.message.value = document.postmodify.message.value+"["+ItemStrip+"="+ItemGet+"]";}
else {
document.postmodify.message.value = document.postmodify.message.value+"["+ItemStrip+"]";}}}
}}}

function CloseAll() {
for (DoClose=0;DoClose<AllTags.length;DoClose++) {
if (AllTags[DoClose]!="hr") {
CloseOpened(AllTags[DoClose]);
}}}

function CloseOpened(Item) {
DoTagFind(Item);
if (Math.floor(ItemOpenAmount>ItemCloseAmount)) {
CloseTags = ItemCloseAmount;
while (CloseTags < ItemOpenAmount) {
document.postmodify.message.value = document.postmodify.message.value+"[/"+Item+"]";
CloseTags++;}}}

function adds(Item) {
document.postmodify.message.value = document.postmodify.message.value+" "+Item
}

function colors() {
add(document.postmodify.color.options[document.postmodify.color.options.selectedIndex].value,'colors')
}

iTextarea = document.getElementsByTagName('textarea');
for (t=0;t<iTextarea.length;t++) {
if (iTextarea[t].name=="message") {
createInput = document.createElement('input');
createInput.type = "button"; createInput.onclick=CloseAll; createInput.onkeypress=CloseAll; createInput.value="Close All Tags";
iTextarea[t].parentNode.parentNode.insertBefore(createInput,iTextarea[t].parentNode.parentNode.lastChild);
}}
