////////////////////////////////////////////////////////////////////////
//                                                                    //
// BSYS (c) BNI 2001-8                                                //
// Developed by Jeremy Kahn Associates                                //
//                                                                    //
// File: http/bsys.js                                                 //
// History:                                                           //
//  16-Feb-01  RWB  Initial creation                                  //
//  24-Jan-05  JIK  dependLib functions                               //
//  15-Jun-05  JIK  dependLib/getList functions                       //
//  07-Jul-05  JIK  make getList work for multi                       //
//  28-Jul-05  JIK  ` delimiter for getList                           //
//  06-Dec-05  JIK  pass on cv parameter in doChildQuery              //
//     Apr-07  JIK  support for new interface                         //
//  18-May-07  RWB  simple/advanced search stuff                      //
//  27-Sep-07  JIK  helpWin                                           //
//  05-Mar-08  RWB  submitButton                                      //
//  31-Mar-08  JIK  toggleThreadSections                              //
//  02-Apr-08  RWB  pass childType to getList                         //
//  07-Apr-08  JIK  tidy up helpwin, winOpen4 -> winOpen              //
//  08-Apr-08  JIK  toggleElect                                       //
//  12-Jul-08  JIK  simpleShow                                        //
//  19-Aug-08  RWB  copy cq parm to getList to defeat browser cache   //
//                                                                    //
// Function:                                                          //
//  Common JavaScript functions available to all pages                //
//                                                                    //
////////////////////////////////////////////////////////////////////////


function showObj(where, pParam)
// function for debugging
{
var msg = where + ' ' + pParam + ': ';
for (var propName in pParam)
   msg += propName + '<' + pParam[propName] + '>';
window.alert(msg);
} // showObj


function verifyField(pField, pSuppressAlert)
// attach as <INPUT ... onChange="verifyField(this)">
// as handler, return value ignored, but when invoked
// from verifyForm (pSuppressAlert true), returns msg
// instead of doing alert
{
return; // !!! disable
} // verifyField


function verifyForm(pForm)
// attach as <FORM ... onSubmit="return verifyForm(this)">
{
return true; // !!! disable
} // verifyForm


function selectUrl(pField)
// jump to URL option value from select list
// attach as <SELECT ... onChange="selectUrl(this)">
{
var newUrl = pField.options[pField.selectedIndex].value;
window.top.location.href = newUrl;
pField.selectedIndex = 0; // reset list to first element
} // selectUrl


function printIt()
// nicked from http://www.lawsentinel.com/java.js
{
if (window.print)
   window.print();
else
   {
   var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
   document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
   WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
   }
} // printIt


function printStart()
// Print window then close
// attach as <BODY ... onLoad="printStart">
// The new page will try to print itself
{
printIt();
self.close();
} // printStart


function backButton()
{
history.back();
} // backButton


function submitButton(pFormInx, pFldInx, pFldName, pFldValue)
{
var theForm = document.forms[pFormInx];
// showObj('theForm', theForm);
if (pFldValue) {
   var hdnFld = theForm.elements[pFldInx];
// showObj('submitButtonHdn', hdnFld);
   hdnFld.value = pFldValue;
   }
theForm.submit();
} // submitButton


function doConfirm(pMsg)
{
var agree = confirm(pMsg);
if (agree)
   return true;
else
   return false;
} // doConfirm


function setList(formInx, pParentName, pChildName, pOptList, pChildVal)
// Dependent pop-list function
// To use this, set up an array which contains sets of three elements
// URN of parent element
// desc of allowed child element
// URN of allowed child element
{
var parentList = document.forms[formInx].elements[pParentName];
var childList = document.forms[formInx].elements[pChildName];

// clear child list
childList.options.length = 0;
childList.selectedIndex = -1;

if (parentList.selectedIndex == -1)
   parentList.selectedIndex = 0; // assumes first elem means all/empty str
var parentVal = parentList[parentList.selectedIndex].value;

// !!! pOptList may have a trailing undef. element. -1 ok as work in mult of 3
var optListLength = pOptList.length - 1;
for (var i = 0; i < optListLength; i += 3)
   if (parentVal == "" || pOptList[i] == parentVal || pOptList[i] == 0) {
      childList.options[childList.options.length] =
         new Option(pOptList[i + 1], pOptList[i + 2]);
      if (pOptList[i + 2] == pChildVal)
         childList.selectedIndex = childList.options.length - 1;
   }

return true;
} // setList


// don't seem to be able to pass variables into event handler functions so ...
var gFormInx;
var gChild;
var gChildType;

//%%JIK I prefer the layout below

function processReqChange()
{
   var childList = document.forms[gFormInx].elements[gChild];
   var delimiter = "`";
   if (gChildType == 'multi') {
      childList = document.forms[gFormInx].elements[gChild][1];
   }
   childList.options.length = 0;
   childList.selectedIndex = -1;
   // only if req shows "complete"
   if (req.readyState == 4) {
      if (req.status == 200) {
         // ...processing statements go here...
         response = req.responseText;
      // need to xlate the following tags so move to back end
      // if (response){
      //    childList.options[childList.options.length] =
      //       new Option('<choose>', "");
      // } else{
      //    childList.options[childList.options.length] =
      //       new Option('<no matches>', "");
      // }
         selected = false;
         while(response) {
            pos = response.indexOf(delimiter);
            urn = response.substr(0, pos);
            response = response.substr(pos+1);
            pos = response.indexOf(delimiter);
            name = response.substr(0, pos);
            if (gChildType == 'multi') {
               response = response.substr(pos+1);
               pos = response.indexOf(delimiter);
               selected = response.substr(0, pos) == 'T';
            }
            response = response.substr(pos+2); // lose delimiter and CR
            childList.options[childList.options.length] =
               new Option(name, urn, selected);
               // 3rd param doesn't have any effect in ie6
            childList.options[childList.options.length-1].selected = selected;
         }
      }
   }
} //processReqChange


function doChildQuery(formInx, parent, child, childType)
{
   var pos;
   var endPos;
   gFormInx = formInx;
   gChild = child;
   gChildType = childType;
   var parentList = document.forms[formInx].elements[parent];
   var parentVal = parentList[parentList.selectedIndex].value;
   var inUrl = document.URL;
       pos = inUrl.indexOf("doIt.php?");  // ??? what if bfe.php
   var base = inUrl.substr(0, pos);
   //
       pos = inUrl.indexOf("cq="); // normally first, ie ?cq
   var cq = inUrl.substr(pos+3);
       endPos = cq.indexOf("&");
       cq = cq.substr(0, endPos);
   //
       pos = inUrl.indexOf("&cv=");
   var cv = inUrl.substr(pos+4);
       endPos = cv.indexOf("&");
       cv = cv.substr(0, endPos);
   //
   var outUrl = base + 'getList.php?cookie=' + document.cookie
                     + '&cq=' + cq
                     + '&cv=' + cv
                     + '&parentFld=' + parent
                     + '&childFld=' + child
                     + '&parentUrn=' + parentVal
                     + '&childType=' + childType;
// using childType for other than list/multi is a big bodge - sorry
   // branch for native XMLHttpRequest object
   if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
      req.onreadystatechange = processReqChange;
      req.open("GET", outUrl, true);
      req.send(null);
   // branch for IE/Windows ActiveX version
   }
   else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
         req.onreadystatechange = processReqChange;
         req.open("GET", outUrl, true);
         req.send();
      }
   }
} // doChildQuery


function hide(id)
{
   el = document.getElementById(id);
   el.style.display = 'none';
} // hide


function hideAll()
{
   hide('findMenu');
   hide('reportMenu');
   hide('otherMenu');
// hide('mainStuff');
} // hideAll


function show(id)
{
   hideAll();
   el = document.getElementById(id);
   el.style.display = 'block';
} // show

function simpleShow(id)
{
   el = document.getElementById(id);
   el.style.display = 'block';
} // simpleShow

function toggle(id)
{
   el = document.getElementById(id);
   if (el.style.display == 'block')
      hide(id);
   else
      show(id);
} // toggle


function toggleThreadSections(index)
{
   show('threadSection1-' + index);
   show('threadSection3-' + index);
   hide('revealUrl-' + index);
} // toggleThreadSections


function toggleSection(sectionId, mode)
{
   div = sectionId + 'Details';
   showButton = 'show' + sectionId.substr(0,1).toUpperCase() +
                   sectionId.substr(1);
   hideButton = 'hide' + sectionId.substr(0,1).toUpperCase() +
                   sectionId.substr(1);
   div = document.getElementById(div);
   showButton = document.getElementById(showButton);
   hideButton = document.getElementById(hideButton);
// !!! beware, div.style.display isn't set initially
   if (mode == 'show' && div.style.display != 'inline') {
      div.style.display = 'inline';
      hideButton.style.display = 'inline';
      showButton.style.display = 'none';
      // bit of a hack for simple/advanced queries
      // !!! protected against infinite recursion by spotting d.s.d correct
      if (sectionId == 'simple') {
         toggleSection('advanced', 'hide');
// assume single/first form and name=searchType
         document.forms[0].searchType.value = sectionId;
      }
      else if (sectionId == 'advanced') {
         toggleSection('simple', 'hide');
         document.forms[0].searchType.value = sectionId;
      }
      // end of hack
      div.scrollIntoView(false);
   }
   else if (mode == 'hide' && div.style.display != 'none') {
      div.style.display = 'none';
      showButton.style.display = 'inline';
      hideButton.style.display = 'none';
      // bit of a hack for simple/advanced queries
      if (sectionId == 'simple')
         toggleSection('advanced', 'show');
      else if (sectionId == 'advanced')
         toggleSection('simple', 'show');
      // end of hack
//    showButton.scrollIntoView(true);
   }
} // toggle


function initialise(openMode, errMsg)
{
   if (document.forms.length) {
      try {
         document.forms[0].elements[0].focus();
         }
      catch(error) {
         }
   }
   if (openMode == 'displayPrint') {
      printStart();
   }
   if (errMsg) {
      alert(errMsg);
   }
} // initialise

function winOpen(pUrl, pWindName, width, height)
{
var newWin;
if (!width)
    width = 400;
if (!height)
    height = 400;

// defaults for options seem to be no
newWin=window.open(pUrl,pWindName,"width="+width+",height="+height+",status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,titlebar=yes,scrollbars=yes,fullscreen=no,top=0,left=0,directories=yes");
newWin.focus(); // if opening a previous one, bring to top
} // winOpen


function printPage(pUrl)
// display given URL in new window with minimal decoration
// The new window will try to print itself onLoad
{
    winOpen(pUrl, 'printWindow', 700, 1000);
} // printPage


// that's it
