var dhWindow = null;       // id of the Help Window
var allTopics = ''; // handy 'wildcard' constant
var allTopicsLabel = '(Show all topics)';
var pageCount = 0;
var keywordCount = 0;
var pageList = new pageListObject;
var keywordList = new keywordListObject;

function pageObject(title,htmlPage,keywords,menuSection){
  this.title = title;
  this.htmlPage = htmlPage;
  this.keywords = keywords;
  this.processedKeywords = ','+keywords.toLowerCase()+',';
  this.menuSection = menuSection;
};

function iconCall(menuSection){
  var call = '';
  if (menuSection == 'Discus'){call = '<img src="_images/msidiscus.gif" width="13" height="11">'};
  if (menuSection == 'Account'){call = '<img src="_images/msiaccount.gif" width="11" height="11">'};
  if (menuSection == 'Information'){call = '<img src="_images/msiinfo.gif" width="11" height="11">'};
  if (menuSection == 'Tools'){call = '<img src="_images/msitools.gif" width="11" height="11">'};
  if (menuSection == 'Help'){call = '<img src="_images/msihelp.gif" width="11" height="11">'};
  return call;
};

function browserQuaint(){
  // is it Netscape or something equally horrific?
  return (!((navigator.appName == 'Microsoft Internet Explorer') && (navigator.appVersion.indexOf('MSIE 5') != -1)));
};

function keywordListObject(){
  var transfer = '';
  var i;
  var j;
  keywordCount = 0;
  for (i = 0; i < pageCount; i++){
    for (j = 0; j <= pageList[i].keywords.length; j++){
      if (pageList[i].keywords.charAt(j) != ','){
        transfer += pageList[i].keywords.charAt(j);
      } else {
        if (transfer != '') {this[keywordCount] = transfer;  keywordCount++;};  
        transfer = '';
      };
    }
    if (transfer != '') {this[keywordCount] = transfer;  keywordCount++;};  
    transfer = '';
  };
};

function pageListObject(){
  pageCount = 0;
  this[pageCount] = new pageObject('Help Contents','contents.html','help,contents,topics','Help'); pageCount++;
  this[pageCount] = new pageObject('Introduction to <i>Discus Online</i>','intro.html','Discus Online,introduction','Help'); pageCount++;
  this[pageCount] = new pageObject('Using help','help.html','help,using help,topics,searching,index','Help'); pageCount++;
  this[pageCount] = new pageObject('Registering','reg.html','registering,setting up an account,account creation','Discus'); pageCount++;
  this[pageCount] = new pageObject('Logging in','login.html','Logging in,password','Discus'); pageCount++;
  this[pageCount] = new pageObject('Results: Results Selector','rselect.html','results,selector,Results Selector,choosing results,selecting results,editing,deleting,downloading data','Discus'); pageCount++;
  this[pageCount] = new pageObject('Results: Candidate Report','rreport.html','results,Candidate Report,report,viewing a report,DISC Graphs','Discus'); pageCount++;
  this[pageCount] = new pageObject('Editing Candidate Details','edit.html','candidate details,editing candidate details','Discus'); pageCount++;
  this[pageCount] = new pageObject('New Profile: Candidate Details','ncanddet.html','candidate details,New Profile,entering Candidate Details','Discus'); pageCount++;
  this[pageCount] = new pageObject('New Profile: Phrase-based Questionnaire','nphraseq.html','questionnaires,phrase-based questionnaire','Discus'); pageCount++;
  this[pageCount] = new pageObject('New Profile: Adjective-based Questionnaire','nadjectq.html','questionnaires,adjective-based questionnaire','Discus'); pageCount++;
  this[pageCount] = new pageObject('New Profile: Remote Questionnaire','nremoteq.html','questionnaires,remote questionnaire','Discus'); pageCount++;
  this[pageCount] = new pageObject('Account Status','status.html','statements,Discus Online account,account status,account details','Account'); pageCount++;
  this[pageCount] = new pageObject('Account Details','details.html','Discus Online account,account details,editing,changing account details','Account'); pageCount++;
  this[pageCount] = new pageObject('FAQ','faq.html','FAQ,Frequently Asked Questions','Information'); pageCount++;
  this[pageCount] = new pageObject('Discus Range','discus.html','Discus,Discus Range,Axiom Software','Information'); pageCount++;
  this[pageCount] = new pageObject('About DISC','aboutdisc.html','DISC,Dominance,Influence,Steadiness,Compliance,Axiom Software','Information'); pageCount++;
  this[pageCount] = new pageObject('News','news.html','News,Latest updates,updates','Information'); pageCount++;
  this[pageCount] = new pageObject('PDF Configuration','pdf.html','PDF,downloads,working offline,offline tools','Tools'); pageCount++;
  this[pageCount] = new pageObject('Downloads','downloads.html','downloads,working offline,offline tools,adobe acrobat','Tools'); pageCount++;
  this[pageCount] = new pageObject('Preferences','prefs.html','Preferences,options,settings,personal preferences','Tools'); pageCount++;
  this[pageCount] = new pageObject('Remote Access','remote.html','Remote Access','Tools'); pageCount++;
  this[pageCount] = new pageObject('Profile Credits','credits.html','Profile Credits,profile batches,batch profiling,credits','Tools'); pageCount++;
  this[pageCount] = new pageObject('Job Match Credits','credits.html','Job Match Credits,credits,Job Matching','Tools'); pageCount++;
  this[pageCount] = new pageObject('Job Matching','jmatch.html','Job Matching,matching','Discus'); pageCount++;
  this[pageCount] = new pageObject('Publishing','jpublish.html','Profile publishing,publishing','Discus'); pageCount++;  
};

function sortPageList(){
  var transfer;
  var i;
  var j;
  for (i = 0; i < pageCount; i++){
    for (j = 0; j < i; j++){
      if (pageList[i].title.toLowerCase() < pageList[j].title.toLowerCase()){
        transfer = pageList[i];
        pageList[i] = pageList[j];
        pageList[j] = transfer;
      };
    };
  };
};

function extractQuery(name){
// extract a value from the query string for 'name'
  value = '';
  loc = location.search.indexOf('&'+name+'=',0);
  if (loc == -1) {loc = location.search.indexOf('?'+name+'=',0)};
  if (loc != -1){
    loc = loc + name.length + 2;
    value = '';
    i = loc;
    while ((location.search.charAt(i) != '&') && (i < location.search.length)){
      value += location.search.charAt(i);
      i += 1;
    };
  };
  return(value);
};

function formatKeywords(){
  for (i = 0; i < keywordCount; i++){
    initial = keywordList[i].charAt(0).toUpperCase();
    remainder = keywordList[i].substring(1,keywordList[i].length);
    keywordList[i] = initial + remainder;
  };
};

function sortKeywords(){
  var transfer;
  var i;
  var j;
  for (i = 0; i < keywordCount; i++){
    for (j = 0; j < i; j++){
      if (keywordList[i] < keywordList[j]){
        transfer = keywordList[i];
        keywordList[i] = keywordList[j];
        keywordList[j] = transfer;
      };
    };
  };
};

function weedKeywords(){
  var index = 0;               
  var j;
  while (index < keywordCount){
    if (keywordList[index] == keywordList[index+1]){
      for (j = index+1; j < keywordCount; j++){
        keywordList[j] = keywordList[j+1];
      };
      keywordCount--;
    } else {
      index++;
    };
  };
};

function renderKeywords(){
  topicForm.topicSelect.options[0] = new Option(allTopicsLabel);
  topicForm.topicSelect.options[0].selected = true;
  for (i = 0; i < keywordCount; i++){
    topicForm.topicSelect.options[i+1] = new Option(keywordList[i]);
  };
}; 

function buildOptionList(){
  if (topicForm.topicSelect.length == 0){
    formatKeywords();
    sortKeywords();
    weedKeywords()
    renderKeywords();
  };
};

function buildEasyTopicList(keyword){
  // an 'easy' version of buildTopicList for browsers that are functionally challenged
  var HTML = '';
  var i;
  sortPageList();
  HTML += '<table>';
  for (i = 0; i < pageCount; i++){
    HTML += '<tr>';
    HTML += '<td valign="top">'+iconCall(pageList[i].menuSection)+'</td>';
    HTML += '<td valign="top"><font face="arial,helvetica" size="1" style="font-size:8pt"><a href="dhtopic.html?topic='+pageList[i].htmlPage+'" target="topic">'+pageList[i].title+'</a></font></td>';
    HTML += '</tr>';
  };
  HTML += '</table>';
  document.write(HTML);
};

function buildTopicList(keyword){
  var HTML = '';
  var i;
  sortPageList();
  if (keyword == allTopicsLabel) {keyword = allTopics}; // compensate for the two 'all topics' conditions
  if (keyword == allTopics){
    searchHead.innerHTML = 'All topics listed'
  } else {
    searchHead.innerHTML = 'Topics for \''+keyword+'\''
  };
  HTML += '<table>';
  for (i = 0; i < pageCount; i++){
    if ((keyword == allTopics) || (pageList[i].processedKeywords.indexOf(','+keyword.toLowerCase()+',') != -1)){
      HTML += '<tr>';
      HTML += '<td valign="top">'+iconCall(pageList[i].menuSection)+'</td>';
      HTML += '<td valign="top"><font face="arial,helvetica" size="1" style="font-size:8pt"><a href="dhtopic.html?topic='+pageList[i].htmlPage+'" target="topic">'+pageList[i].title+'</a></font></td>';
      HTML += '</tr>';
    };
  };
  HTML += '</table>';
  topicList.innerHTML = HTML;
};

function spawnHelp(root,helpPage){
  window.open(root+'helpframe.html?topic='+root+helpPage,dhWindow,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=500,height=400,top=10,left=10')
};

