// FILENAME IS QLGLOBAL.JS Sep 7/2000
// QL Browser Object
// macintosh/dom testing added jan 22/2001
// mods for safari browser  - Feb 5, 2003
// Oct. 14/2005 - Position persistence - mtm
// Dec 8, 2005 - fix to get_context and get_doc_name
//             - allow numerics in DB code - mtm


function ConstructQLBrowser() {

    // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();
    var ver=navigator.appVersion.toLowerCase();

    this.mac   = (agt.indexOf('macintosh') != -1) ||
                 (ver.indexOf('macintosh') != -1);

    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);

    this.ns  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1)
                && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1));
    this.ns4 = (this.ns && (this.major == 4));
    this.ns5 = (this.ns && (this.major == 5));
    this.ns5up = (this.ns && (this.major >= 5));

    this.ie   = (agt.indexOf("msie") != -1);
    this.ie4  = (this.ie && (this.major == 4)
                && (agt.indexOf("msie 5.")==-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5  = (this.ie && (this.major == 4) &&
                (agt.indexOf("msie 5.")!=-1) );
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie6  = (this.ie && (agt.indexOf("msie 6.")!=-1) );
    this.ie5up = (this.ie5up || this.ie6);
    this.ie5mac = (this.ie5up && this.mac);

    this.dom = (this.ie5up || this.ns5up);
    this.safari = (agt.indexOf('safari')!=-1) && this.dom;
}

QLBrowser = new ConstructQLBrowser()

function nullfunc() {}

function initg()
{
  QLG.name="globalobj";
  if (!QLG.rsz || QLBrowser.ie)
    {if (top.RIGHT) QLG.rsz=top.RIGHT.resizewin; else QLG.rsz=nullfunc;}
  if (!QLG.hdc || QLBrowser.ie)
    {if (top.RIGHT) QLG.hdc=top.RIGHT.hidecommands; else QLG.hdc=nullfunc;}
  if (!QLG.lnext || QLBrowser.ie)
    {if (top.RIGHT) QLG.lnext=top.RIGHT.lnext; else QLG.lnext=nullfunc;}
  if (!QLG.lprev || QLBrowser.ie)
    {if (top.RIGHT) QLG.lprev=top.RIGHT.lprev; else QLG.lprev=nullfunc;}
  QLG.first = 1;
}
QLG = new Object();

function SetTitle() {
 if (QLBrowser.dom)
  if (top.RIGHT.document) top.document.title = top.RIGHT.document.title; }

  
// new mods for window positon persistence follow - Murray McDonald - October 14, 2005
  
// reverse string passed
function strRev(o) {var s="";
  for (var j = o.length-1; j > -1; j--)
    s = s+o.charAt(j);
  return s;
}


// extract and return the saved positon list from cookie
//
// positon list format is: @doc_name#pos repeated "n" times
// eg @hitlist-1#250@ojre-2217#4267#ojre-32544#467
//
function get_plist() {
	var cv = document.cookie;
//  alert('cookie is ' + cv);
	var plist = '';
	if (cv != "") {
		var b = cv.indexOf("PLIST=");
    if (b != -1) {
	    plist = cv.slice(b+6);
	    var e = plist.indexOf(';');
	    if (e != -1)
	      plist = plist.slice(0,e);
    }    
  } // if cv not null
  return plist;
} // get_plist


// write saved positon list to cookie
function set_plist(plist) {
    var cv = "PLIST=" + plist + ';path=/';	 
    document.cookie = cv;
} // set_plist


// extract and return "position list context" from cookie
function get_plcontext() {
	var cv = document.cookie;
	var ctxt = '';
	if (cv != "") {
		var b = cv.indexOf("PLCTX=");
    if (b != -1) {
	    ctxt = cv.slice(b+6);
	    var e = ctxt.indexOf(';');
	    if (e != -1)
	      ctxt = ctxt.slice(0,e);
    }    
  } // if cv not null
  return ctxt;
} // get_plcontext


// write "position list context" to cookie
function set_plcontext(cn) {
    var cv = "PLCTX=" + cn + ';path=/';	 
    document.cookie = cv;
} // set_plcontext


// given "context", "document name" and "positon" add or replace
// the recorded position for this document to
// if current context is not the saved context then re-initialize
// the saved position list
//
function add_pos(cn, nm, pos) {
	// wrap document name with "@" and "#"
	var lf = '@' + nm + '#'; // what to look for

	// get current plist context
	var plctx = get_plcontext();
	
	// still same context?
	if (plctx != cn) {
	  var plist='';       // no -- empty out plist
		set_plcontext(cn);  // and save the new context
	}
	else {  
	  var plist=get_plist(); // yes -- get plist from cookie
	  
		// see if we already have a saved pos for this doc  
		var b = plist.indexOf(lf);  // go look for it
		
		// got a saved pos for this doc already?
		if (b != -1)  { // yes - get rid of "old" saved position
			var repRG=new RegExp(lf + "\\d+","g")
			repRG.exec(plist);
	    // alert("removing old entry")
	    // alert("old plist is " + plist);
			plist = RegExp.leftContext + RegExp.rightContext; // drop it
		}
	} // if same context
	
	
	// now save this doc/pos unless pos is "0"
	if (pos != '0') {
		plist += lf + pos;  // append at end
	  set_plist(plist);	  // and save it in a cookie
  }
  // alert("new plist is " + plist);
	
} // add_pos


// given "window" extract the context and session ID
// by examining the location.href value for the window
// typical value will be something like: /qltemp/C1KYyvMAFkvLTEkg/00003hitlist-0001.htm
// in this example extracted context and session ID would be: 3C1KYyvMAFkvLTEkg
//
function get_context(w) {
	var tc = '';
	var sid ='';
	var pn = w.location.pathname;
	
	// first get the session ID
	var b = pn.indexOf('qltemp/');
	if (b != -1)
	  sid = pn.slice(b+7,b+7+16);
	  
	// reverse the path name and look for first "/"  
  pn = strRev(pn);
  var e = pn.indexOf('/');
  if (e != -1)
    pn = pn.slice(0,e);
  pn = strRev(pn);
  
  // have isolated context and document name
//  var np=pn.match(/(\d*)(\D*-\d*)\.htm/i); **OLD**
  var np=pn.match(/(\d*)(\D[^\-]*-\d*)\.htm/i);
  // get the context and drop leading zeroes
  if (np && np.length > 2) { 
    var cn=np[1].match(/0*(\d*)/i);
    if (cn && cn.length > 1)
      tc=parseInt(cn[1]).toString();
  } // if np.length > 2

  if (tc !='' && sid !='') // must have both context and session ID
    return tc+sid;
  else
    return '';  
} // get_context


// given "window" extract and "compress" the document name by examining
// the location.href value for the window
// typical value will be something like: /qltemp/C1KYyvMAFkvLTEkg/00003ojre-00030407.htm
// in this example extracted name would be: ojre-30407
//
function get_doc_name(w) {
	var pn = w.location.pathname;
	
	// reverse the path name and look for first "/"  
  pn = strRev(pn);
  var e = pn.indexOf('/');
  if (e != -1)
    pn = pn.slice(0,e);
  pn = strRev(pn);
  
  // have isolated context and document name
//  var np=pn.match(/(\d*)(\D*-\d*)\.htm/i); **OLD**
  var np=pn.match(/(\d*)(\D[^\-]*-\d*)\.htm/i);
  if (np && np.length > 2) { // got a match?
    // docname will be xxxxxx-ddddd.htm
    // compress it to xxxxx-dd with leading zeroes of "ddddd" removed
    pn = np[2];  //just the docname
    np = pn.match(/(.*-)(\d*)/i);
    if (np && np.length > 2) { // got a match?
      var dn=np[2].match(/0*(\d*)/i);
      if (dn && dn.length > 1)
        pn = np[1] + parseInt(dn[1]).toString();
    } // if np.length > 2
  } // if np.length > 2
  else pn ='';

  return pn;
} // get_doc_name


// every event that would replace either the LEFT or RIGHT panel
// should call this routine FIRST to record the current "scroll" position
// of both windows
//
// record the current "position" of the LEFT and RIGHT frame documents
//
// The LEFT panel document must be a "hitlist" document
// The RIGHT panel document must be a "database" document
//
function record_pos() {
//	alert('record_pos has been called');
	
	// first deal with the "LEFT" frame
	var lw='';
	var lwdn='';
	var lwpos='';
	var lwcn='';
	
	if (top.QLBrowser.dom)
	  lw = top.LEFT;
	else if (top.QLBrowser.ns4)
	  lw =parent.top.LEFT;
	  
	if (lw) { 
		lwcn=get_context(lw);   // get the context/session ID
		lwdn=get_doc_name(lw);  // get the document name
		
		// get the "position" to record
		if (top.QLBrowser.dom) { // dom support?
		  var hldiv = lw.document.getElementById('HLIST');
		  if (hldiv) {
			  if (hldiv.style.overflow == "visible")
			    lwpos=document.body.scrollTop;
			  else
			    lwpos=hldiv.scrollTop;  
		  } // if hldiv
	  } // dom
	  else if (top.QLBrowser.ns4) {
		  lwpos=lw.document.body.scrollTop;
	  } // ns4
	  
	  // got a name and a position to record
    // alert('left window: dn is ' + lwdn + ", pos is " + lwpos + ", context=" + lwcn);
  }  // lw
  
	// and now the RIGHT frame
	var rw='';
	var rwdn='';
	var rwpos='';
	var rwcn='';
	
	if (top.QLBrowser.dom)
	  rw = top.RIGHT.window;
	else if (top.QLBrowser.ns4)
	  rw =parent.top.RIGHT.window;
	  
	if (rw) {    
		rwcn=get_context(rw);   // get the context/session ID
		rwdn=get_doc_name(rw);  // get the document name
		
		// get the "position" to record
		if (top.QLBrowser.dom || top.QLBrowser.ns4)
		  rwpos=rw.document.body.scrollTop;
	  
	  // got a name and a position to record
    // alert('right window: dn is ' + rwdn + ", pos is " + rwpos + ", context=" + rwcn);
  } // rw  
  
  // now figure out what to record - get the "highest" context
  var tc='';
  if (lwcn>rwcn)
    tc = lwcn;
   else
    tc = rwcn;

  // nothing to do -- bail out now      
  if (!tc) return;
  
  // something to record for LEFT window?
  if (lwcn != '' && lwdn != '' && lwpos !='') {
	  add_pos(tc, lwdn, lwpos)
  }
    
  // something to record for RIGHT window?
  if (rwcn != '' && rwdn !='' && rwpos != '') {
	  add_pos(tc, rwdn, rwpos)
  }
  
  return true;
  
} // record_pos


// called from "onload" event of either LEFT or RIGHT panel documents
// to determine if they should "scroll" the document viewport window
//
// given "window" get the current document name and context/session id
// look up context/document in current position persistence list to see
// if we have a saved positon -- if so return it to caller else return false
//
function has_pos(w) {

	var dn='';
	var cn='';
  var pos='';
		
	  
	if (w) { 
		cn=get_context(w);   // get window context/session ID   
		dn=get_doc_name(w);  // get current document name
		if (cn && dn) {
      var plctx = get_plcontext();
      if (plctx == cn) { // same context?
    	  var plist=get_plist(); // yes -- get plist from cookie
    	  
    	  // see if we have a saved pos for this document  
	      var lf = '@' + dn + '#'; // what to look for
	      var b = plist.indexOf(lf);  // go look for it
	
	      // got a saved pos for this doc?
	      if (b != -1)  {
					var repRG=new RegExp(lf + "(\\d+)","g")
					repRG.exec(plist);
					pos = RegExp.lastParen;  // extract the saved position
//					alert("has_pos is returning : " + pos);
					return pos;
	      }
	      else
	        return false;
      } // same context
      else
        return false;
		} // if cn && dn
  } // if w	
	else
	  return false;	
	
} // has_pos  
