// (c)1999-2000 Open Objects Software
// Displaying documents.

var docID;
var pageID=0;
var tempLocation;
var inHL0;


function zeroPageID(){
  pageID=0;
}


function loadWebromTocHit(location){
  var highlightingResults = isShowingHighlights();

  setShowingHighlights(false);
  loadWebromSearchHit(location);
  setShowingHighlights(highlightingResults);
}


function loadWebromSearchHit(location){
  if (!checkED()){
    showIsExpiredPage();
  } else if (AU && !authorizeUser()){
    showIsUnauthorizedPage();
  } else if (isHtmlDoc(location) && isShowingHighlights()) {
    displayDocumentHighlighted(location);
  } else if (isHtmlDoc(location) && !isShowingHighlights()) {
    displayDocumentUnHighlighted(location);
  } else if (isTextDoc(location)) {
    displayDocumentUnHighlighted(location);
  } else if (isPdfDoc(location)) {
    displayDocumentUnHighlighted(location);    
  } else {
    //It's not an HTML, TXT, or PDF document - so open the document in a window whose name is 'otherSearchHitWindow'
    window.open(location, 'otherSearchHitWindow');
  }
}


function displayDocumentHighlighted(loc){
  if (isMicrosoftInternetExplorerFivePointFive()) {
    displayDocumentUnHighlighted(loc);
    return;
  }

  tempLocation = loc;
  locString = "";

  if (inHL0){
    locString = locString + "wr_hl1.html";
  } else{
    locString = locString + "wr_hl0.html";
  }

  if (isShowingHighlights()){
    locString = locString + "#qa_anchor_1";
  }

  var searchHitWindow = findNamedWindow(top, getSearchHitWindowName());

  searchHitWindow.document.location = locString;
}



function displayDocumentUnHighlighted(location){
  var searchHitWindow = findNamedWindow(top, getSearchHitWindowName());
  searchHitWindow.document.location = location;
}


function openEncDoc(id, location){
  pageID = id;
  highlightAndDecryptDocument(location, true);
}


function highlightAndDecryptDocument(location, decrypt){
  var words = "";

  if (isShowingHighlights()) {
    words=getWordsToHighlight();
  }

  if( navigator.appName.indexOf( "Netscape" ) != -1 && navigator.appVersion >= "4" ){
    parent.code.document.highlight.setUseCallbacks(false);
  }

  var dID = parent.code.document.highlight.openDocument(location, words, isShowingNavigationLinks(), backLink, nextLink, decrypt);

  if (!checkED()){
    showIsExpiredPage();
  } else if (AU && !authorizeUser()){
    showIsUnauthorizedPage();
  } else {
    var searchHitWindow = findNamedWindow(top, getSearchHitWindowName());

    searchHitWindow.document.open();
    while( true ) { 
      var page = parent.code.document.highlight.readPage( dID );

      if( page == null ) {
        break;
      }
      
      var jspage = "" + page;

      if( jspage.length == 0 ) {
        break;
      }
        
      searchHitWindow.document.write( jspage );
    }
    searchHitWindow.document.close();
  }
}



function displayDocumentHighlightedAux(bln){
  inHL0 = bln;
  highlightAndDecryptDocument(tempLocation, false);
}



function isHtmlDoc(location){
  var lclocation = location.toLowerCase();

  return (endsWith(lclocation, ".html") || endsWith(lclocation, ".htm"));
}


function isTextDoc(location){
  var lclocation = location.toLowerCase();

  return endsWith(lclocation, ".txt");
}


function isPdfDoc(location){
  var lclocation = location.toLowerCase();

  return endsWith(lclocation, ".pdf");
}



function endsWith(searchString, matchString){
  return ((searchString.indexOf(matchString) + matchString.length) == searchString.length);
}



// Authentication

function authorizeUser(){
  if (!sessionStarted){
    var validUser = parent.GetCookie(cID);
    if (validUser!="1"){
      var userId=prompt("Enter User Id:","");
      if (userId != null){
        var userPass=prompt("Enter Password:","");
        if (userPass != null){
          idValid=parent.code.document.auser.authorize(userId,userPass,ucdi);
          if (idValid){
            var expDate = new Date;
            expDate.setTime (expDate.getTime() + (30 * 24 * 60 * 60 * 1000));
            parent.SetCookie(cID, "1", expDate);
          }
          sessionStarted=true;
        }
      }
    }
    else{
      idValid=true;
    }
  }
  return idValid;
}



function checkED(){
  var expDate = new Date;
  var nowDate = new Date;
  nowDate.setTime(expDate.getTime());
  if ((eDy == 0) || (eDm == 0) || (eDd == 0)){
    return true;
  }
  else{
    expDate.setUTCFullYear(eDy, eDm - 1 , eDd);
    return (nowDate < expDate)
  }
}



function isMicrosoftInternetExplorerFivePointFive() {
  return ((navigator.appName.indexOf("Microsoft Internet Explorer") != -1) && 
        (navigator.appVersion.indexOf("MSIE 5.5") != -1));
}



function scrollTo(name, doc){
  if(isMicrosoftInternetExplorerFivePointFive())
    doc.anchors.item(name).scrollIntoView();
  else
    doc.location.hash=name;
}


// Eof
