var doSync = true;

function getPath(url) {
  var path = '';
  var folders = url.split("/");
  for (var i = folders.length - 2; i >= folders.length - 3 ; i--) {
    path = '/' + path;
    if (folders[i] != 'index.htm')
      path = folders[i] + path;
  }
  return path;
}

function FindDoc(doc) {
  var ii = 1;
  while (docList[ii] != "END") {
    re = new RegExp(docList[ii]);
    if (re.test(doc)) {
      return ii;
    } else {
      ii += 2;
    }
  }
  return -1;
}

function prevDoc() {
  var docIndex = FindDoc(parent.frames.main.document.location);
  if (docIndex > 1) {
    docIndex -= 2;
    parent.frames.main.document.location = docList[docIndex];
  }
}

function nextDoc() {
  var docIndex = FindDoc(parent.frames.main.document.location);
  if (docIndex >= 1) {
    docIndex += 2;
    if (docList[docIndex] != "END") {
      parent.frames.main.document.location = docList[docIndex];
    }
  }
}

function getDocFilename() {
  var docIndex = FindDoc(frames.main.document.location);
  return docList[docIndex];
}

function delaySync() {
  if (doSync) {
    var docIndex = FindDoc(frames.main.document.location);
    if (parent.frames.toc) {
      if (parent.frames.toc.syncToc)
        parent.frames.toc.syncToc(docList[docIndex]);
    }
  } else {
    doSync = true;
  }
}

var isHighlighted = false

function highlight(keywords) {
  var oColl;
  var key;
  var searchStr;
  var anchorMatch = new Array();
  var n;

  if (document.getElementsByTagName) {
    var arKeywords = keywords.split(' ');
    oColl = parent.frames.main.document.getElementsByTagName('p');
    if (oColl && oColl[0].innerHTML) {
      for (var i = 0; i < oColl.length; i++) {
        str = oColl[i].innerHTML;
        for (var j = 0; j < arKeywords.length; j++) {
          re = /\*$/;
          if (re.test(arKeywords[j])) {
            key = arKeywords[j].replace(re, '');
            searchStr = '\\b(' + key + ')';
          } else {
            key = arKeywords[j];
            searchStr = '\\b(' + key + ')\\b';
          }
          re = new RegExp(searchStr, 'gi');

//          re2 = new RegExp('(popup\\((.*?)\\))', 'i');
          re2 = new RegExp('(<a(.*?)>)', 'i');
          n = 0;
          while (re2.test(str)) {
            anchorMatch[n++] = RegExp.$1;
            str = str.replace(re2, 'ZZZZZZ');
          }

          newstr = str.replace(re, '<span style="background-color:aqua">$1</span>');

          n = 0;
          re3 = new RegExp('ZZZZZZ');
          while (re3.test(newstr)) {
            newstr = newstr.replace(re3, anchorMatch[n++]);
          }

          str = newstr;
        }
        oColl[i].innerHTML = newstr;
      }
    }
  }
}

function highlightPhrase(keywords) {
  if (document.getElementsByTagName) {
    var oColl = parent.frames.main.document.getElementsByTagName('p')
    for (var i = 0; i < oColl.length; i++) {
      str = oColl[i].innerHTML
      re = new RegExp("(" + keywords + ")", "gi")
      newstr = str.replace(re, '<span style="background-color:aqua">$1</span>')
      oColl[i].innerHTML = newstr
    }
  }
}

function highlightdoc() {
  if (isHighlighted) {
    if (parent.frames.searchbar.searchType == 'phrase')
      highlightPhrase(parent.frames.searchbar.keywordStr)
    else
      highlight(parent.frames.searchbar.keywordStr)

    isHighlighted = false
  }
}

function showDoc(doc) {
  var newDoc = '.';
  var parts = doc.split(' ');
  newDoc += parts[0];
  for (var i = 1; i < parts.length; i++) {
    newDoc += '%20' + parts[i];
  }
  isHighlighted = true;
  frames.main.location = newDoc;
}

var printFolder = false;
var docs;
var docIndex = 0;

function writeToDoc() {
  top.frames.printFrame.document.write(top.frames.main.document.body.innerHTML);
  docIndex++;
  if (docIndex < docs.length) {
    top.frames.main.location = docs[docIndex];
  } else {
    printFolder = false;
    if (document.all) {
      top.frames.printFrame.focus();
      top.print();
    } else {
      top.frames.printFrame.print();
    }
    top.frames.main.location = docs[0];
  }
}

function printDoc() {
  if (window.print) {
    // Clear out array from last use.
    docs = new Array();

    var doc = getDocFilename();


    if (frames.toc.isList(doc) && top.confirm('To print all the subdocuments as well ' +
                                         'as the current one "click" [OK].\n' +
                                         '"Clicking" [Cancel] will print only the ' +
                                         'current document.')) {
      docs[0] = doc;
      alert('Please be patient. Collating many subdocuments will take some time.\n' +
            '"Click" [OK] to continue.');
      frames.toc.getDocList(docs);

      printFolder = true;
      top.frames.printFrame.document.open();
      top.frames.printFrame.document.writeln('<html>');
      top.frames.printFrame.document.writeln('<head>');
      top.frames.printFrame.document.writeln('<link rel=stylesheet type="text/css" href="../../webcodes.css" title="Web Codes">');
      top.frames.printFrame.document.writeln('</head>');
      top.frames.printFrame.document.writeln('<body>');
      top.frames.printFrame.document.write(top.frames.main.document.body.innerHTML);
      docIndex = 1;
      top.frames.main.location = docs[docIndex];
    } else {
//      top.frames.printFrame.focus();
//      top.frames.printFrame.print();
      top.frames.main.focus();
      top.frames.main.print();
    }
  } else {
    alert('Your browser does not support printing from JavaScript.\n' +
          'You will have to use your browser\'s print feature.');
  }
}

var debugWin = null;
var debugDoc = null;
function MyDebugMsg(str) {
  if (debugWin == null) {
    debugWin = window.open("", "DEBUG", "height=200,width=500,menubar, toolbar,scrollbars, resizable");
    debugDoc = debugWin.document;
  }
  debugDoc.writeln(str);
}

function FramesLoaded() {
  delaySync();
}


