//_URL_="/pub/xdb?hits=0:16&skin=8050.html&web3=../dictionary.x&xgap=24";
// INPUT and PRINT

// DOCUMENTATION: 
// Create an AJAX Handler. This hander will support xdb query to TM using 
// the designed interface. Global search variables are defined by caller

var _OUT_=document.getElementById("PRINT");
var _INP_=document.getElementById("INPUT");

var _PGX_=new Array(); 
var _PGY_=new Array(); var _END_=1; 


var xmlxhttp = false;
if(navigator.appName == "Microsoft Internet Explorer")
   xmlxhttp = new ActiveXObject("Microsoft.XMLHTTP");
else
   xmlxhttp = new XMLHttpRequest();


var _KEY_=""; 
var _COL_= 1;
// DOCUMENTATION: (fullfind)
// The idea behind the following functions is to enable an effectivepaging 
// and caching mechanism on the browser. Control of of entries are processed: 
// For delete keys and no keyword: return to main For CR/LF reset variables 
// (global) containing history of search and go 100%. RunQuery with a zero

function fullfind(obj) {
  if(obj.keyCode!=13 && obj.keyCode!=10)
    return;

  _KEY_=_INP_.value;

  _END_=0; _PGX_.length=0; 
           _PGY_.length=0;  runquery(0);  return 1;
}

// DOCUMENTATION: (runquery)
// Check if index is at end; load a query, render into the browser. Follow 
// a check on a possible cache request; cache next one and fix paging on the 
// browser. Define lookahead pages as a constant. Perform an XDB query on 
// database and copy ouput into history _PGY_ ind is the index to the array. 
// On status 200, compute last udri. Do run routine if previous UDRI is 
// negative meaning page contained no hit. Do no increase _PGY_ and _PGX_ 
// on empty returns. Manage _END_=1 for termination.

function runquery(ind) { 
  if(ind<_PGY_.length-0)
    printit(ind), showpage(ind); if(_END_==1) return;
   
  if(ind<_PGY_.length-1) 
    return; 
 
  xmlxhttp.open("GET", 
    _URL_+"&data="+_KEY_+"&udri="+_PGX_[_PGX_.length-1], false);   

  xmlxhttp.send(null);

  if(xmlxhttp.status==200) 
     return UDRIload(ind);

  document.location=_URI_;
}

// DOCUMENTATION: (UDRIload)
// Parseout udri and augment +1 from hex to integer back to hex. This routine 
// guarantees an entry increase. Must terminate _END_ to 1 when no udrx found 
// in output. This prevents run away paging increase.

function UDRIload(ind) { 
  if(ind==_PGY_.length) 
    _OUT_.innerHTML=unescape(xmlxhttp.responseText); 

  var _uri_=xmlxhttp.responseText.lastIndexOf("udrx");
  if(_uri_<0) 
     return  showpage(ind), _END_=1; _END_=0;  _uri_=xmlxhttp.responseText.substr(_uri_+6, 24); 

  _PGY_[_PGY_.length]=unescape(xmlxhttp.responseText); 

  var _hex_="00000000"+(parseInt(_uri_.substr(17), 16)+1).toString(16); 
     _PGX_[_PGX_.length]=_uri_.substr(0, 16)+_hex_.substr(_hex_.length-8);  showpage(ind);  

  if(ind==0) runquery(0); 
}

// DOCUMENTATION: (showpage)
// The idea is that the skin has produced a div with a PAGING header in it. 
// and such, rendering the paging of what is cached is in the div. This allow 
// skin to take a stronger rendering ability

function showpage(ind) {
  var _pgn_=""; if(_PGX_.length==0) 
    _pgn_="<div style=\"font-size:20px; color:#AAA; text-align:center\">No Records Found</div>";

  for(i=0; i<_PGX_.length; i++)
    _pgn_=_pgn_+"<a onclick='runquery(\""+i+"\")'>"+(i+1)+"&nbsp;</a>";

  document.getElementById("PAGING").innerHTML=_pgn_+"&nbsp;&nbsp;";
}

// Below are support function generic for all purspoes along the the 
// interface definition. May share resources as part the optimization  
// need to make run time better and faster


// DOCUMENTATION (printit)
// This function has the ability to add additional pages to print out. 
// simply add to the output the next page already loaded. Presume paging
// is well done and caching is exceptional. Paging back should work

function columns(col) {
  _COL_=parseInt(col);

  if(_PGY_.length) printit(0);
}

function printit(ind) {
  var a='<li style="width:'+90/_COL_+'%">'; 
      b="</li>";

  _OUT_.innerHTML=a+_PGY_[ind]+b;

  ind=parseInt(ind);

  for(i=1; i<_COL_ && 
	ind+i<_PGY_.length; i++)

    _OUT_.innerHTML+=a+_PGY_[ind+i]+b;

  if(_COL_>1 && 
     i==1) _OUT_.innerHTML+="<li></li>";
}

// DOCUMENTATION (findpath)
// A pseudo xpath process needed to build the string of a pair tags to do
// string find and get the first one inline with index return the content 
// of in tags only. Tags are discarded and unused/not returned.

function findpath(obj, 
                  tag) { var z=tag.length+2;
                  
  a= (obj.indexOf("<" +tag+">")+z);
  b= (obj.indexOf("</"+tag+">")-0); return obj.substr(a, b-a);
}

// DOCUMENTATION: (logmeout)
// The ability to log someone out is very importan. A user must logout
// Perhaps an auto logout is conceivable for future releases such lock
// and security measures. THis functions is reusable all over tigerme

function logmeout(obj) {
  xmlxhttp.open("GET", "/", false, 
		"login", "password"); xmlxhttp.send(null); 

  document.cookie="username=;";
}

function logmexin(obj) {
  xmlxhttp.open("GET", "/xdb?");  

  xmlxhttp.onreadystatechange = function() {

    if(xmlxhttp.readyState==4 && xmlxhttp.status==200) 
       document.cookie="username="+findpath(xmlxhttp.responseText, "user"); 
  };

// Reject user from application if he is not recognized by a server
// Important to synchronize applications with server end authority
// .toUpperCase();

  xmlxhttp.send(null);
}

// DOCUMENTATION (cookiexy)
// A routine to parse out the cookie in resident memory. This role
// is generic and important overall to understand browser persist
// varialbles needed such as login user name and other MAC address

function cookiexy(obj) {
  var c=document.cookie.split(";"); 

  for(i=0; i<c.length; i++)
    if(c[i].indexOf(obj)>=0)
      return c[i].substr(c[i].indexOf(obj)+obj.length+1);

  return "";
}

// DOCUMENTATION: (loadxorg)
// A mechanism to superset the browser with a frame where the control
// is still set to tigerme including authentication windows hiding
// behind. This mechanism is way to control web hosting  clever way

function loadxorg(url) {

  for (i = document.body.firstChild; i; i = i.nextSibling) 
    if (i.tagName) {
      i._orig_ = i.style.display;
      i._flip_ = true;
      i.style.display = 'none';
    }

//  for (i = document.body.firstChild; i; i = i.nextSibling)
//    if (i.tagName && i._flip_) i.style.display = i._orig_;

// This is a process to call out an iframe an attach to it the caller
// id. A method to possible toggle multiple windows in a single shot

  _org_ = document.createElement("iframe");

  _org_.name=url;

  _org_.style.position="fixed";
  _org_.style.width="100%";
  _org_.style.height="100%";
  _org_.style.borderWidth="0px";
  _org_.src=url;

  document.body.appendChild(_org_); return _org_;
}


// DOCUMENTATION: (loadxobj)
// load a primary package as specified by obj. Load specifically a 
// java base code. This function is naturally important for real time
// operation and loading. Minimize oneshot runtime loading. 

function loadxobj(obj) {
  _ELM_=document.createElement("script"); 

  _ELM_.type="text/javascript", 
    _ELM_.src=obj;  document.getElementsByTagName("head")[0].appendChild(_ELM_);

  return;
}

// DOCUMENTATION: (loadxcss)
// load a primary package as specified by obj. Load specifically a 
// java base code. This function is naturally important for real time
// operation and loading. Minimize oneshot runtime loading. 

function loadxcss(obj) {
  _ELM_=document.createElement("link"); 

  _ELM_.rel="stylesheet";  _ELM_.media="screen";

  _ELM_.type="text/css",
    _ELM_.href=obj;  document.getElementsByTagName("head")[0].appendChild(_ELM_);

  return;
}
