// The value for TestStr in setRelativeRoot needs to be changed for each language or new site using this script.
// It should be set to the subdirectory on the server that will hold the site for that language.
// The portion of the header() function that switches among languages also needs modification.

function setRelativeRoot()
{
  var TestStr = "/faithful/";
  TestLen = TestStr.length;
  var Pos = 0;
  var PathLen = 0;
  var LPos = 0;
  var LLen = 0;
  var CPath = "";
  CPath = location.href;
  PathLen = CPath.length;
  Pos = CPath.indexOf(TestStr);
  if (Pos < 0)
    { URL = CPath.substr(7, PathLen);
      LPos = URL.indexOf('/') + 8;
    }
  else
    { LPos = Pos + TestLen; }  
  LLen = PathLen - LPos + 1;
  FPath = CPath.substr(LPos, LLen);
  FLen = FPath.length;
  Pos = 1;
  var SlashCnt = 0;
  while (Pos <= FLen)
    {
    if (FPath.substr(Pos, 1) == "/")
      SlashCnt++;
    Pos++;
    }
  if (SlashCnt == 0)
    relativeRoot = "./";
  else
    {
    relativeRoot = "";
    for (LoopCnt = 1; LoopCnt <= SlashCnt; LoopCnt++)
      relativeRoot += "../";
    }
}


function openDir( form ) { 
  var newIndex = form.fieldname.selectedIndex; 
  cururl = form.fieldname.options[ newIndex ].value; 
  window.location.assign( cururl ); 
} 

function header( ExtraLangs )
{
  docURL = document.URL;
  if (relativeRoot == "./")
    { slashCnt = 1 }
  else
    { slashCnt = (relativeRoot.length / 3) + 1; }
  pos = docURL.length;
  while (slashCnt > 0)
    { docChar = docURL.substr(pos, 1);
      if (docChar == "/" || docChar == "\\")
      { slashCnt--;
        if (slashCnt == 0)
          { break; }
      }
      pos--;
    }
  relURL = docURL.substring(pos + 1, docURL.length);

  //general background and text settings
  document.write('<img src="' + relativeRoot + 'graphics/titlebar.gif"><br><br>');
  document.write('<table width="750" border="1" bgcolor="#FFFFE0"><tr>');
  document.write('<td width="625"><p class="leadRcV">Titus 1:9 - Holding to the faithful word, which is according to the teaching of the apostles, that he may be able both to exhort by the healthy teaching and to convict those who oppose.</p></td>');

//Values for ExtraLangs
// P - Portuguese, E - English, S - Spanish, T - Traditional Chinese, F - French, K - Korean, C - Simplified Chinese, J - Japanese, R - Russian
  if (!(ExtraLangs == null))
  {
  document.write('</td><td align="center">');
  lenExtra = ExtraLangs.length;
  ColBrk = parseInt(String((lenExtra) / 2));
  for (LoopCnt = 1; LoopCnt <= lenExtra; LoopCnt++)
    {
    Lang = ExtraLangs.substr(LoopCnt - 1, 1);
    switch (Lang)
      {
        case "P":
          document.write('<a href="' + relativeRoot + 'portuguese/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/portuguese.jpg" border="0"></a>');
          break;
        case "E":
          document.write('<a href="' + relativeRoot + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/english.jpg" border="0"></a>');
          break;
        case "S":
          document.write('<a href="' + relativeRoot + 'spanish/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/spanish.jpg" border="0"></a>');
          break;
        case "T":
          document.write('<a href="' + relativeRoot + 'chinese/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/trad_chin.jpg" border="0"></a>');
          break;
        case "F":
          document.write('<a href="' + relativeRoot + 'french/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/french.jpg" border="0"></a>');
          break;
        case "K":
          document.write('<a href="' + relativeRoot + 'korean/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/korean.jpg" border="0"></a>');
          break;
        case "C":
          document.write('<a href="' + relativeRoot + 'simpchin/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/sim_chin.jpg" border="0"></a>');
          break;
        case "J":
          document.write('<a href="' + relativeRoot + 'japanese/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/japanese.jpg" border="0"></a>');
          break;
        case "R":
          document.write('<a href="' + relativeRoot + 'russian/' + relURL + '" border="0"><img src="' + relativeRoot + 'graphics/russian.jpg" border="0"></a>');
          break;
      }
    if (ColBrk == LoopCnt && lenExtra > 4)
      document.write('</td><td valign="center">')
    else
      document.write("<br>");
    }  
  }
  if ((ExtraLangs == null) || (ExtraLangs.substr(1, 1) != "X"))
      document.write('</td></tr></table><br>');
}

function tablestart()
{
  //size table to screen resolution
  document.write('<table width="750" frame="void" border="2" cellpadding="4">');
//  document.write('<tr><td colspan="2"></td></tr>');
//  document.write('<tr><td valign="top" width="200" bgcolor="#FFE0FF">');
//  document.write('<tr><td></td></tr>');
}

function tablebody()
{
//  document.write('</td>');
  document.write('<td bgcolor="#FFFFFF">');
}

function construct()
{
  document.write('<center><img src=' + relativeRoot + 'graphics/construct.gif></center>');
  document.write('<p>This site is currently under construction. ');
  document.write('Please check the <a href=' + relativeRoot + 'whatsnew.html>What&#39s New</a> page to find out what has been added.</p>');
}

function acrobat()
{
document.write('<a href="http://www.adobe.com/products/acrobat/readstep2.html" target="_blank">');
document.write('<img src="' + relativeRoot + 'graphics/get_adobe_reader.gif"></a>');
}

// This function inserts a copyright statement with "DCP" as the holder.

function endtable()
{
  document.write('</td></tr></table>');
}

function mailURL()
{ 
  HTMLtitle = document.title;
  docutitle = "";
  pNode = document.getElementById("doctitle");
  if (pNode != null)
    { cNodes = pNode.childNodes;
      for (var i = 0; i < cNodes.length; i++) 
        {
        var child = cNodes[i];
        docutitle = docutitle + child.nodeValue;
        }
    }
  if (HTMLtitle.length > docutitle.length)
    { docutitle = HTMLtitle; }
  docutitle = docutitle.replace(/"/gi, '%22');
  docutitle = docutitle.replace(/'/gi, "%27");
  docutitle = docutitle.replace("?", "%3F");
  docutitle = docutitle.replace("?", "%3F");
  docutitle = docutitle.replace(/&mdash;/gi, "--");
  docutitle = docutitle.replace(/&amp;/gi, "%26");
  docutitle = docutitle.replace(/& /gi, "%26 ");
  docutitle = docutitle.replace(/null/gi, " ");
  docutitle = docutitle.replace("<u>/", "");
  docutitle = docutitle.replace("</u>", "");
  docutitle = docutitle.replace(/Say%3F/gi, "Really Say%3F");
  
  titlepos = docutitle.length;
  while (titlepos >= 0)
    { TestPos = docutitle.substr(titlepos, 1);
      escTestPos = escape(TestPos);
      pt1 = docutitle.substring(0, titlepos);
      pt2 = docutitle.substring(titlepos + 1, docutitle.length);
      if (escTestPos == "%u2014")
        { docutitle = pt1 + "--" + pt2; 
        }
      if (escTestPos == "%0A")
        { docutitle = pt1 + " " + pt2; 
        }

      titlepos--;
    }
  docutitle = docutitle.replace(/  /gi, " ");
  pageloc = escape(location.href);
  mailToURL = "mailto:&subject=A page from afaithfulword.org&body=" + docutitle + " (" + pageloc + ")";
//  document.write(docutitle + "<br>");
  window.location = mailToURL;
}

function webml()
{
m1 = "lt";
m2 = "mai";
m3 = "o:";
a1 = "o@af";
a2 = "ord.";
a3 = "fulw";
a4 = "org";
a5 = "inf";
a6 = "aith";
ml = m2 + m1 + m3 + a5 + a1 + a6 + a3 + a2 + a4;
document.write('<a href="' + ml + '">');
document.write('<center><img src="' + relativeRoot + 'graphics/up_info.jpg" border="0"></a></center>');
}


// This function inserts a copyright statement with "DCP" as the holder.
function footer(EMailButton)
{
  document.write('<table width="750">');
  document.write('<tr><td valign="middle">');
  document.write('<FORM method=GET action="http://www.google.com/search">');
  document.write('<input type=hidden name=ie value=UTF-8>');
  document.write('<input type=hidden name=oe value=UTF-8>');
  document.write('&nbsp;&nbsp;<A HREF="http://www.google.com/"><IMG SRC="' + relativeRoot + 'graphics/Google.gif" border="0" ALT="Google"></A>');
  document.write('</td>');
  document.write('<td valign="middle">');
  document.write('<INPUT TYPE=text name=q size=40 maxlength=255 value="">');
  document.write('</td>');
  document.write('<td valign="middle">');
  document.write('<INPUT type=image name=btnG SRC="' + relativeRoot + 'graphics/search.jpg">');
//  document.write('<INPUT type=submit name=btnG VALUE="Search afaithfulword.org">');
  document.write('<input type=hidden name=domains value="afaithfulword.org">');
  document.write('<input type=hidden name=sitesearch value="afaithfulword.org"><span class="textbody">&nbsp;</span>');
  document.write('<input type=hidden name=lr value="lang_en"><span class="textbody">&nbsp;</span>');
  document.write('</FORM>');
  document.write('</td></tr></table>');
  document.write('<table width="750">');
  document.write('<tr><td rowspan="2"><span class="copyright">&copy; 2006-2007 DCP.  All Rights Reserved.<br>');
  document.write('DCP is a project to defend and confirm the New Testament ministry of Watchman Nee and Witness Lee and the practice of the local churches.');
  document.write('</span></td>');
  if (EMailButton == "No")
    { document.write('<td colspan="3">&nbsp;</td></tr>'); 
	}
  else
    { document.write('<td colspan="3" align="center"><a onclick="mailURL()"><img border="0" src="' + relativeRoot + 'graphics/up_email.jpg"></a></td></tr>'); 
	}
  document.write('<tr>');
  document.write('<td align="center"><a href=' + relativeRoot + 'index.html><img border="0" src="' + relativeRoot + 'graphics/up_homepage.jpg"></a></td>');
  document.write('<td align="center"><a href=' + relativeRoot + 'whatsnew.html><img border="0" src="' + relativeRoot + 'graphics/up_whatsnew.jpg"></a></td>');
  document.write('<td align="center"><a href=' + relativeRoot + 'sitemap.html><img border="0" src="' + relativeRoot + 'graphics/up_sitemap.jpg"></a></td>');
  document.write('</tr>');
  document.write('<tr><td colspan="5"><p class="copyright">');
  document.write('All verses and footnotes are from the Holy Bible, Recovery Version. Excerpts from the Recovery Version and the ministry of Watchman Nee and Witness Lee are copyrighted by Living Stream Ministry and used by permission. Witness Lee is the author of quoted materials unless otherwise noted.</p></td></tr></table>');
}
