//Table generator replacing table button v2 with editor
//by frufru

var textcolor="FF0000"; //Text Color of popup window
var bgcolor="F1F1F1"; //background color of popup window

// do not edit below
var genwinopen;
var editwinopen;
var thewindow;
var theotherwindow;
var thehtml;
var editorheader;
var editorfooter;

function closeall(){
thewindow.close();
theotherwindow.close();
}

if (document.postForm){
thehtml="<html><head><title>Generate Table</title></head><body bgColor='" + bgcolor + "' text='" + textcolor + "'>Generate Table<br><form name='asdf' onSubmit='return false'>Rows: <input name=rows size=4> <br> Columns: <input name=columns size=4><br><input type=button name='gobutton' value='Add Table' onClick='opener.addtable(document.asdf.rows.value,document.asdf.columns.value)'> <input type=button name='editbutton' value='Edit the table' onClick='opener.edittable(document.asdf.rows.value,document.asdf.columns.value)'><br><b>Note: Entering extremely large numbers will slow down your browser, not the board.</b></body></html>";
editorheader="<html><head><title>Edit Table</title></head><body bgColor='" + bgcolor + "' text='" + textcolor + "'><form name='editform'>Use these textboxes to edit the table contents, then click the button at the bottom. Note: Closing the other windows before closing this one may cause errors.<p>";
editorfooter="<p><input type=button value='Insert to Post' onClick='opener.inserttopost(document.editform,rows,cols);'></form></body></html>";

window.onunload=closeall;
}

function blah(){
alert("Please note that your popup blocker must be disabled for this to work properly.");
thewindow=window.open("","GenerateTable","width=250,height=250");
thewindow.location="about:blank";
thewindow.document.write(thehtml);
}

function edittable(rows,columns){
theotherwindow=window.open("","EditTable","width=600,height=500, scrollbars=yes");
theotherwindow.location="about:blank";
theotherwindow.document.write(editorheader);

var t="<scr" + "ipt>var rows=" + rows + ";var cols=" + columns + ";</scri" + "pt>";
if (IsNumeric(rows) && IsNumeric(columns)){
t+="<table border='1'>";
for (x=0;x<rows;x++){
t+="<tr>";
for (y=0;y<columns;y++){
t+="<td><textarea name='xr" + (x+1) + "c" + (y+1) + "' rows='3' cols='12'>Row " + (x+1) + " Column " + (y+1) + "</textarea></td>";
}
t+="</tr>";
}
t+="</table>";
theotherwindow.document.write(t);
theotherwindow.document.write(editorfooter);

}else{
alert("Rows or columns are invalid");
thewindow.focus();
theotherwindow.close();
}
}

function inserttopost(theform,rows,columns){
var t="";
t+="[table]";
for (x=0;x<rows;x++){
t+="[tr]";
for (y=0;y<columns;y++){
t+="[td]" + eval("theform.xr" + (x+1) + "c" + (y+1) + ".value") + "[/td]";
}
t+="[/tr]";
}
t+="[/table]";
add(t);
closeall();
}


function addtable(rows,columns){
var t="";
if (IsNumeric(rows) && IsNumeric(columns)){
thewindow.close();
genwinopen=false;
t+="[table]";
for (x=0;x<rows;x++){
t+="[tr]";
for (y=0;y<columns;y++){
t+="[td]Row " + (x+1) + " Column " + (y+1) + "[/td]";
}
t+="[/tr]";
}
t+="[/table]";
add(t);
}else{
alert("Rows or columns are invalid");
thewindow.focus();
}
}

var a=document.getElementsByTagName("a");
for (x=0;x<a.length;x++){
if (a[x].href=='javascript:add("[table][tr][td]","[/td][/tr][/table]")'){
a[x].href='javascript:blah()';
}
}

//the below code is a validation code taken from http://www.codetoad.com/javascript/isnumeric.asp

function IsNumeric(sText)
{
if (sText==""){
return false;
}else{
var ValidChars = "0123456789";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
}