
// Sec 1.0 - variables
var upldAA = new Array;
var bulkCnt = 0

var upldA = new Array;
var tableTitleA = new Array;
var tableCommentA = new Array;

var lastUpdate = ""
var lastUpdateA = new Array;
var enableEdit = false

//Sec 2.0 Display function. This is the function that displays the data. 

function Show( bc ){

   titlesA= upldA[ bc ][0].split(",")
   opStr = "<H1> Table "+ bc +" "+ tableTitleA[ bc ] + " </H1>"+ tableCommentA[ bc ]
   opStr = opStr + "<table border=0><TR>"
   if ( enableEdit ) {
     opStr = opStr + "<TH><A href='javascript:pop( "+bc+",0 )'><B>edit..</B></A></TH>"
   }
   for ( I= 0; I < titlesA.length ; I++ ){
     opStr = opStr+"<TH>"+titlesA[ I ]+"</TH>"
   }	
   opStr = opStr+"</TR>"

   for ( I= 1; I < upldA[ bc ].length ; I++ ){

     //window.status= bc + " " + I 
     //alert( window.status )
     row = upldA[ bc ][I]+"" // copes with an update that causes a hole in the array.
     titlesA= row.split(",")

     opStr = opStr+"<TR>"
     if ( enableEdit ) {
       opStr = opStr+"<TD><A href='javascript:pop( "+bc+","+ I +")'><B>"+I+": edit..</B></A></TD>"
     }

     for ( J= 0; J < titlesA.length; J ++ ){
       // colourcode numbers, dates or text 
       if ( isNaN( titlesA[ J ]*1 )  ){
         var date = new Date( titlesA[ J ] ) 
         if (! isNaN( date )  ){
           opStr = opStr+"<TD class='date' >"+titlesA[ J ]+"</TD>"
         } else {
           opStr = opStr+"<TD>"+	titlesA[ J ]+"</TD>"
         }
       } else {
         opStr = opStr+"<TD class='num' >"+titlesA[ J ]+"</TD>"
       }
     } 
     opStr = opStr+"</TR>"
   }	
   opStr = opStr+"</table>"+"<HR>"+lastUpdateA[ bc ]+"<HR>"
   self.document.write( opStr )
}

//Sec 3.0 Dataload function. 
function gbF( name,email,postedOn,IPaddress,userfield1,userfield2,userfield3,userfield4,comments){

 // this function is called once per form entry and is used to load the data.
 lastUpdate = "Last Update by:<BR>"+name+","+email+","+postedOn+"<BR>update type:"+userfield1+","+userfield2+"<BR>"

 if ( userfield1 == "bulk"){
   // userfield2 contains the (table,row) to be updated, Only the table is used.

   var tableCnt = -1
   var row = -1
   uf1A = userfield2.split(",") // table to be uploaded.

   if ( uf1A.length > 1 ) {
     tableCnt  = uf1A[ 0 ]*1
     row       = uf1A[ 1 ]*1
   }

   if ( ( tableCnt < 0 ) || ( row < 0 ) ) {
     window.status= " Error in data posted on " + postedOn 
     return
   }


   upldA[ tableCnt ] = comments.split(/<BR>|\r/gi) // split up a row at a time.
   //titlesA= upldA[ tableCnt][0].split(",")

   tableTitleA[   tableCnt ] = userfield3
   tableCommentA[ tableCnt ] = userfield4
   lastUpdateA[   tableCnt ] = lastUpdate
 }

 if ( userfield1 == "update"){

 // userfield2 contains the (table,row) to be updated.
 var tableCnt = -1
 var row = -1

 uf1A = userfield2.split(",") // table,row,X
 if ( uf1A.length > 1 ) {
    tableCnt  = uf1A[ 0 ]*1
    row = uf1A[ 1 ]*1
 }

 if ( ( tableCnt < 0 ) || ( row < 0 ) ) {
   window.status= " Error in data posted on " + postedOn 
   return
 }

 // update a row
 if ( tableCnt == upldA.length ) {
   upldA[ tableCnt ] = new Array()
 }

 upldA[ tableCnt ][ row ] = comments

 tableTitleA[   tableCnt ] = userfield3
 tableCommentA[ tableCnt ] = userfield4
 lastUpdateA[   tableCnt ] = lastUpdate
}

 lastUpdate = "Last Update by:<BR>"+name+","+email+","+postedOn+"<BR>update type:"+userfield1+","+userfield2+"<BR>"

}

// Sec 3.0 - data dump function. Pops up a window and fills it with csv data.

function dump(  ){
  var upld="<HR>\n"
  for ( cnt = 1 ; cnt < upldA.length ; cnt++ ){
    upld = upld+ "<HR>"+tableTitleA[ cnt ] +"\n\n"+ tableCommentA[ cnt ] +"\n\n" +(upldA[ cnt ]).join("\n")
  }
  win = open( "blank.htm" )
  win.document.write( "<html><body><H1>copy this text into a text file</H1><PRE>"+upld+"</PRE></BODY></HTML>" )
}

