//---------------------------------------------------------------------
// JavaScript Utilities
// Copyright 1999-2003, Juerg von Kaenel, All Rights Reserved
//
// 2005-06-27  fixed dateString to work with Firefox and IE
//---------------------------------------------------------------------
// initialize some common constants
isNS   = (navigator.appName.indexOf("Netscape")!=-1) ? true : false;
isIE   = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;
isMac  = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
isFF   = (navigator.userAgent.indexOf("Firefox")!=-1) ? true : false;
//alert("appName: "+navigator.appName+"\nappVersion: "+navigator.appVersion+"\nuserAgent: "+navigator.userAgent)

isNS4  = (document.layers) ? true : false;
isIE4  = (document.all) ? true : false;
isVer4 = isNS4 || isIE4;
isIE5  = false;
isIE6  = false;
isNS5  = false;
isNS6  = false;
isPrintable = false;
root   = "";
msiel = navigator.appVersion.indexOf('MSIE');
if (msiel > 0) {
  msiev = parseFloat(navigator.appVersion.substring(msiel+4));
  if (msiev >= 5.0) {
    isIE5 = true;
  }
  if (msiev >= 6.0) {
    isIE6 = true;
  }
} else if (isNS) {
  nsv = parseFloat(navigator.appVersion);
  if (nsv >= 5.0) {
    isNS5 = true;
  }
  if (nsv >= 6.0) {
    isNS6 = true;
    // alert("using Netscape 6");
  }
}
isVer5 = isNS5 || isIE5;

// include the correct style sheet
document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\"woodpuzzles.css\">");


// include the correct style sheet and set the root
function setRoot(r)
{
  root = r;
  document.writeln("<link rel=\"stylesheet\" type=\"text/css\" href=\""+root+"woodpuzzles.css\">");
}

//---------------------------------------------------------------------
// initial initialisation complete
//---------------------------------------------------------------------

function dateString(objDate)
{
  //alert(objDate);
  current_month = objDate.getMonth() +1;
  current_day = objDate.getDate();
  current_year = objDate.getYear();
  if (current_year < 1000) {
    current_year = current_year + 1900  // compensate for NS returning year -1900.
  }
  s = current_year + "-" + current_month + "-" + current_day;
  //alert(s);
  return(s);
}

function displayStatusMsg(msgStr)
{
  status=msgStr;
  document.util_returnValue = true;
}

function setCookie(name, value, expires) {
  document.cookie = name + "=" + escape(value) + "; path=/" +
    ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

function getCookie(name){
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
  begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
      return unescape(dc.substring(begin, end));
    }
  }
  return null;
}

function enQuote(text, useChar)
{
  if (text) {
    var li = 0;
    var rTxt = "";
    //alert("enQuote: "+text);
    while ((li = text.indexOf("'", 0)) > 0) {
      rTxt = rTxt + text.substring(0, li)+(useChar ? "&#39;" : "\\'");
      text = text.substring(li+1, text.length);
    }
    text = rTxt + text;
    rTxt = "";
    while ((li = text.indexOf('"', 0)) > 0) {
      rTxt = rTxt + text.substring(0, li)+(useChar ? "&#34;" : '\\"');
      text = text.substring(li+1, text.length);
    }
    rTxt = rTxt + text;
    //alert("enQuote result: "+rTxt);
    return(rTxt)
  } else return null;
}

function writePic(name, wl, hl, typl, w, h, typ, align, title, map) {
  if (title != null) {
    qtitle = enQuote(title);
    //alert("writePic title="+title+", "+qtitle);
    document.writeln("<a href=\"javascript:showPic('"+name+"."+wl+"x"+hl+"."+typl+"', "+wl+", "+hl+", '"+qtitle+"')\"");
    document.writeln("  onMouseOver=\"window.status='"+qtitle+"'; return true\"");
    document.writeln("  onMouseOut=\"window.status=' '\">");
  }
  if (isPrintable) {
    document.write("  <img src=\""+name+"."+wl+"x"+hl+"."+typl+"\" width="+w+" height="+h+" border=0");
  } else {
    document.write("  <img src=\""+name+"."+w+"x"+h+"."+typ+"\" width="+w+" height="+h+" border=0");
    if (map != null) {
      document.write(" usemap=\"#"+map+"\"");
    }
  }
  if (title != null) {
    document.write(" alt='"+enQuote(title,true)+"'");
  } else {
    document.write(" alt=\"\"");
  }
  if (align) {
    document.write(" align="+align);
    if (align == "left") {
      document.write(" style=\"margin-right: 8px\"");
    } else if (align == "right") {
      document.write(" style=\"margin-left: 8px\"");
    }
  }
  if (title != null) {
    document.writeln("></a>");
  } else {
    document.writeln(">");
  }
}

function writeButtonRoll(name, w, h, typ, align, title, id, url, urlw, urlh) {
  qtitle = enQuote(title);
  document.write("<a href=\"Javascript:popURL('"+url+"', "+urlw+", "+urlh+")\">");
  document.write("  <img id="+id+" name="+id+" src=\""+name+"Up."+w+"x"+h+"."+typ+"\" width="+w+" height="+h+" alt='"+enQuote(title,true)+"' border=0");
  if (align) {
    document.write(" align="+align);
  }
  document.write("\n");
  document.writeln("    onMouseOver=\"window.status='"+qtitle+"'; flipImage('"+id+"', '"+name+"Down."+w+"x"+h+"."+typ+"', null);return true\"");
  document.writeln("    onMouseOut=\"window.status=' '; flipImage('"+id+"', '"+name+"Up."+w+"x"+h+"."+typ+"', null);return true\"");
  document.writeln("  ></a>");
}

function showPic(name, w, h, title) {
  //alert("showPic("+name+", "+w+", "+h+", "+title+")");
  var head  = "<!doctype html public \"html3.0\">\n";
  head += "<html>\n";
  head += "<head>\n";
  head += "<title>"+title+"</title>\n";
  //head += "<base href=\""+this.window.location.href.substring(0, this.window.location.href.lastIndexOf("/"))+"/\">\n";
  if (isMac) {
    head += "  <link rel=\"stylesheet\" type=\"text/css\" href=\"ipp20mac.css\">\n";
  } else if (isNS && !isNS5) {
    head += "  <link rel=\"stylesheet\" type=\"text/css\" href=\"ipp20NS.css\">\n";
  } else {
    head += "  <link rel=\"stylesheet\" type=\"text/css\" href=\"ipp20.css\">\n";
  }
  head += "</script>\n";
  head += "<script language=\"JavaScript\">\n";
  head += "d = new Date();\n";
  head += "FocusTime = d.getTime();\n";
  head += "function activateWindow() {\n";
  head += "  d = new Date();\n";
  head += "  FocusTime = d.getTime();\n";
  head += "}\n";
  head += "function activateOrClose() {\n";
  head += "  if (navigator.appName.indexOf(\"Microsoft\")==-1) window.close();\n";
  head += "  a = new Date();\n";
  head += "  t = a.getTime();\n";
  head += "  if ((t - FocusTime) > 750) {\n";
  head += "    window.close();\n";
  head += "  } else {\n";
  head += "    window.focus();\n";
  head += "  }\n";
  head += "}\n";
  head += "</script>\n";
  head += "</head>\n";
  var body  = "<body bgcolor=\"#FFFFFF\" onFocus=\"activateWindow();\">\n";
  body += "<a href=\"JavaScript:activateOrClose();\"><img src=\""+name+"\" width="+w+" height="+h+" border=0 onMouseDown='activateOrClose()' alt='click to close'></a>\n";
  body += "<br /><font size=1 face=\"Arial, Helvetica, sans-serif\"><span class=small>click on image to close window</span></font>\n";
  body += "</body>\n";
  body += "</html>\n";
  controls = "resizable,"
  // alert("sw="+screen.width+", sh="+screen.height);
  if ((w > 640) || (h > 640)) {
    controls += "scrollbars=yes,"
  }
  w = w+45;
  if (w > screen.width-80) w = screen.width-80;
  h = h+50;
  if (h > screen.height-80) h = screen.height-80;

  w = window.open("", "picture", controls+"width="+w+",height="+h);
  w.document.open("text/html");
  w.document.writeln(head);
  w.document.writeln(body);
  w.document.close();
  w.focus();
}

function activateOrClosePic() {
  //alert("just testing");
  window.close();
}

function popURL(url, w, h) {
  controls = "resizable,status,"
  if ((w > 640) || (h > 640)) {
    controls += "scrollbars=yes,"
  }
  if (w > 640) {
    w = w+40;
    if (w > 1000) w = 1000;
  } else {
    w = w+35;
  }
  if (h > 640) {
    h = h+40;
    if (h > 768) h = 768;
  } else {
    h = h+35;
  }
  newName = "popup";
  if (window.name == "popup") {
    newName = "popup2";
  }
  myPopWindow = window.open(url, newName, controls+"width="+w+",height="+h);
  myPopWindow.focus();
}

function flipImage(id, name, link)
{
  // alert("flipImage: "+id+", "+name+", "+link);
  var obj = eval("document."+id);
  obj.src = name;
  if (link) {
     // need to something here
  }
}

function writeHeader(section)
{
  //alert("writing header for section: '"+section+"'");
  sHead = "";
  sHead += "<table width=\"640\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
  sHead += "  <tr valign=\"top\">\n";
  sHead += "    <td><img src=\""+root+"masthead.640x70.jpg\" width=\"640\" height=\"70\" alt=\"www.woodpuzzles.com\"></td>\n";
  sHead += "  </tr>\n";
  sHead += "</table>\n";
  sHead += "<table width=\"640\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
  sHead += "  <tr valign=\"top\">\n";
  sHead += "    <td background=\""+root+"nav.141x1.gif\" class=\"pageNav\"><img src=\""+root+"nav_"+section+".141x170.jpg\" width=\"141\" height=\"170\" alt=\"\"><br />\n";

  cimg = root+"choice"; if (section == "home") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"index.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Home</a><p>\n";
  cimg = root+"choice"; if (section == "puzzles") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"puzzles.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Puzzles</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesxmas") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesXMas.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>XMas</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesipp") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesIPP.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>IPP Exchange</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesHC") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesHC.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Hexahedron<br /><img src=\""+root+"dot.gif\" hspace=\"18\" border=0>Collection</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesDes") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesDes.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Designed by:</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesDesK") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"12\"><a class='black' href='"+root+"puzzlesDesK.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Karin</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesDesJ") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"12\"><a class='black' href='"+root+"puzzlesDesJ.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>J&uuml;rg</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesDesO") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"12\"><a class='black' href='"+root+"puzzlesDesO.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>other</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesDesC") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"12\"><a class='black' href='"+root+"puzzlesDesC.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>classic</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesLC") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesLC.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Laser Plans</a><br />\n";
  cimg = root+"choice"; if (section == "puzzlesclass") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <img src=\""+root+"dot.gif\" hspace=\"5\"><a class='black' href='"+root+"puzzlesclass.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Classification</a><p>\n";
  cimg = root+"choice"; if (section == "faq") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"faq.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>FAQ</a><br />\n";
  cimg = root+"choice"; if (section == "wood") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"woodworking.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Woodworking</a><br />\n";
  cimg = root+"choice"; if (section == "bibliography") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"bibliography.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Bibliography</a><br />\n";
  cimg = root+"choice"; if (section == "links") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"links.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Links</a><br />\n";
  cimg = root+"choice"; if (section == "sitemap") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"sitemap.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Site Map</a><br />\n";
  cimg = root+"choice"; if (section == "personal") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"personal.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Personal</a><br />\n";
  cimg = root+"choice"; if (section == "new") { cimg += "1" } else { cimg += "0"}; cimg += ".16x16.gif";
  sHead += "  <a class='black' href='"+root+"new.html'><img src="+cimg+" width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>New</a><p>\n";
  sHead += "  <a class='black' href='mailto:jvk@woodpuzzles.com?subject=WoodPuzzles%20Website%20Feedback'><img src='"+root+"choice0.16x16.gif' width=\"16\" height=\"16\" hspace=\"5\" border=\"0\" alt=\"\" align='texttop'>Feedback</a><br /><br />\n";

  sHead += "    </td>\n";
  sHead += "    <td><img src=\""+root+"dot.gif\" width=\"14\" height=\"1\" alt=\"\"></td>\n";
  sHead += "    <td width=\"485\" class=\"pageBody\"><br />\n";
  //alert(sHead);
  document.writeln(sHead);
}

function writeFooter()
{
  //alert("writing footer");
  sFoot = "";
  sFoot += "  </td>\n";
  sFoot += "</tr>\n";
  sFoot += "</table>\n";
  sFoot += "<table width=\"640\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
  sFoot += "<tr valign=\"top\">\n";
  sFoot += "  <td background=\""+root+"nav.141x1.gif\"><img src=\""+root+"dot.gif\" width=\"141\" height=\"1\" alt=\"\"></td>\n";
  sFoot += "  <td background=\""+root+"footer.499x16.jpg\" width=\"499\" class=\"pageFoot\">\n";
  sFoot += "    <table width=\"499\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
  sFoot += "      <tr valign=\"top\">\n";
  sFoot += "        <td width=\"400\" background=\""+root+"dot.gif\" class=\"pageFoot\">\n";
  sFoot += "          &nbsp;&nbsp;&nbsp;&nbsp;&copy; All Rigths Reserved, 1998-2009 Karin &amp; J&uuml;rg von K&auml;nel\n";
  sFoot += "        </td>\n";
  sFoot += "        <td width=\"99\" background=\""+root+"dot.gif\" class=\"pageFoot\" align=right>\n";
  sFoot += "          "+dateString(new Date(Date.parse(document.lastModified)))+"\n";
  sFoot += "        </td>\n";
  sFoot += "    </tr>\n";
  sFoot += "    </table>\n";
  sFoot += "  </td>\n";
  sFoot += "</tr>\n";
  sFoot += "</table>\n";

  //alert(sFoot);
  document.writeln(sFoot);
}
