getElementsByClassName = function(cl) 
{
   var retnode = [];
   var myclass = new RegExp('\\b'+cl+'\\b');
   var elem = this.getElementsByTagName('*');

   for (var i = 0; i < elem.length; i++)
   {
      var classes = elem[i].className;
      if (myclass.test(classes)) retnode.push(elem[i]);
   }
   return retnode;
};

function getElementsByClassNameEx(elem,cl)
{
   elem.getElementsByClassName = getElementsByClassName;
   return elem.getElementsByClassName(cl);
}

document.getElementsByClassName = getElementsByClassName;



//////////////////////
////////////////////// newRequest
//////////////////////

function newRequest()
{
   var xmlhttp;                                                                                 // Pretty standard code to get an http request object

   if (window.XMLHttpRequest)
   {  // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
   }
   else
   {  // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
   return xmlhttp;

} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// newRequest


//////////////////////
////////////////////// backendRequest
//////////////////////

function backendRequest(params,url,async)
{
   return _postRequest(params,url,false); 

   var xmlhttp = newRequest();          							// get a request handle
   var txt = '';

   xmlhttp.open('POST', url, false);                                                        // make a SYNCHRONOUS request
   xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlhttp.send(params);
   txt = xmlhttp.responseText;                                                                  // FIX: snarf the response for debugging

   return txt;                                                                                  // return the raw response (a form in HTML)

} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// getRequest

function asyncRequest(params,url,cb)
{
   return _postRequest(params,url,cl);
}

function _postRequest(params,url,cb)				// standard function to post an httpRequest ... 
{
   var r = newRequest();
   if (cb)
   {
      r.onreadystatechange=function()
      {
         if (r.readyState == 4 && r.status == 200)
         {
            cb(r);
         }
      }
   }

   r.open("POST",url,cb);

   r.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  // r.setRequestHeader("Content-Length",params.length);
  // r.setRequestHeader("Connection", "close");
 
   r.send(params);

   if (cb)
      return '';
   else
      return r.responseText;
}


//////////////////////
////////////////////// tableToArray
//////////////////////

function tableToArray(variableName,html,vectoronly)
{
   var r,k;									// index variables
   var handle;									// index to the 'handle' column

   var arrayof = new Array();							// create the object that will be returned
   var container = document.createElement('div');				// get a temporary container
       container.style.display = 'none';
       document.body.appendChild(container);
       container.innerHTML = html.replace(/~q/g,"\'").replace(/~Q/g,'\"');	// populate the container with the html given

   //container.getElementsByClassName = document.getElementsByClassName;
   //var rows=container.getElementsByClassName('TABLEDATAROW');
   //var keys=container.getElementsByClassName('TABLEKEY');

   var first_row = 0;
//   var rows=container.getElementsByTagName('tr');				// get all the rows in the given table
//   var keys=container.getElementsByTagName('th');				// get all the column headers (these are the associative names of the columns)

   var rows=getElementsByClassNameEx(container,'TABLEDATAROW');
   var keys=getElementsByClassNameEx(container,'TABLEKEY');

   if (vectoronly) 								// if instructed to retrieve only a vector ... get ready to do so
      handle = 'NONE';
   else										// otherwise try to find the index of the 'handle' column
   {
      for (k=0;k<keys.length;k++) { if (keys[k].innerHTML == 'handle') break;}
      handle = (k < keys.length) ? k : 'INCR';					// if we find the handle column we'll use it, otherwise we'll simply index the array
   } 
   for (r=first_row;r<rows.length;r++)						// now we create an array for each row in the table
   {
      var thisitem = new Array();

    //  var vals=rows[r].getElementsByClassName('TABLEVALUE');
      var vals=getElementsByClassNameEx(rows[r],'TABLEVALUE');

    //  var vals=rows[r].getElementsByTagName('td');				// we get the values in the 'td' elements

      for (k=0;k<keys.length;k++)
      {
         thisitem[keys[k].innerHTML] = vals[k].innerHTML; 			// and associate the values with the keys
      }  
      switch (handle)
      {
         case 'NONE': arrayof				= thisitem; break;	// the vectory only case
         case 'INCR': arrayof[r - first_row]		= thisitem; break;	// the case where the 'handle' column wasn't found: index by row number
         default:     arrayof[vals[handle].innerHTML]	= thisitem; break;	// the case where we have the index of the 'handle' column: assoc index by handle (better be unique!)
      }
   }
   container.parentNode.removeChild(container);					// take the garbage out

   return arrayof;

} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// tableToArray


function tableSchema(variableName,html,vectoronly)
{
   var schema = '';
   var r,k;
   var container = document.createElement('div');
       container.innerHTML = html;

   //var keys=container.getElementsByClassName('TABLEKEY');
   var keys=container.getElementsByTagName('th');				// get all the column headers (these are the associative names of the columns)

   for (k=0;k<keys.length;k++) 
      schema += ((schema == '') ? '' : ',') + keys[k].innerText;

   //dddddddddddd
   return schema;
} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// tableSchema

function setCache(variableName,arrayof)
{
   eval (variableName + "_loaded = new Date();");
   eval (variableName + "_cache  = arrayof;");
}

function getTable(variableName,vectoronly,flushcache)
{
   var f;

   if (vectoronly == undefined) vectoronly = false;
   if (flushcache == undefined) flushcache = true;

   try{ f = eval(variableName + "_loaded");} catch (e) { f = undefined; }

   var arrayof;

   if (f == undefined || flushcache)
   {
      arrayof = new Array();

      var params = "op=getarray&table=" + variableName;
      txt = postRequest("db.php",params);

      arrayof = tableToArray(variableName,txt,vectoronly)
      
      eval (variableName + "_loaded = new Date();");
      eval (variableName + "_cache  = arrayof;");
   }
   else // use cache
   {
      eval( "arrayof = " + variableName + "_cache;");
   }
   return arrayof;
}


