// kitty.html & kittyframe.html & kitty.js 0.2 - Coffee Kitty
// Copyright (C) 2004-2007, Thomas Günther (toms-cafe.de)

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


// Interface functions for kittyframe.html:
//   LoadKitty
//   InputBeans
//   InputEmail
//   SubmitKitty
//   ViewHistory

// Required frames in kitty.html:
//   main           kittyframe.html
//   tinc           empty (filled in kittyframe.html)

// Required tags in kittyframe.html:
// Form             kitty
//   Input field    kitty.beans
//   Output field   kitty.total
//   Input field    kitty.name
//   Input field    kitty.email
// Form             tinc
//   Hidden field   tinc.reverse
//   Hidden field   tinc.subject
//   Hidden field   tinc.name
//   Hidden field   tinc.email
//   Input field    tinc.body
//   Hidden field   tinc.no
//   Hidden field   tinc.entrykey
//   Hidden field   tinc.key
//   Hidden field   tinc.adminkey


// Global variables
var kitty_url   = "http://www.toms-cafe.de/test-kitty";
var submit_key  = "MtKnBNAZ";
var history_key = "3wzrcrLC";
var history_url = "/tinc?key=" + history_key + "&start=-1&epp=10&reverse=1";
var kitty_beans = 0;
var kitty_total = 0;

// External functions
function LoadKitty()
{
  if (!parent)
    location.href = kitty_url;
  else if (!parent.tinc)
    parent.location.href = kitty_url;
  else
    LoadReadBeans(10);
  document.kitty.beans.focus();
}
function InputBeans()
{
  kitty_beans = parseInt(document.kitty.beans.value);
  if (!(kitty_beans >= 0 && kitty_beans <= 42))
  {
    alert(_("Input a number between 0 and 42!"));
    if (kitty_beans > 42)
      kitty_beans = 42;
    else
      kitty_beans = 0;
  }
  document.kitty.beans.value = kitty_beans;
  document.kitty.total.value = kitty_total + kitty_beans;
}
function InputEmail()
{
  var email = Strip(document.kitty.email.value);
  if (email.length > 0)
  {
    var new_email = AdjustEmail(email);
    if (new_email != email)
      alert(_("This is not a correct e-mail address!"));
    email = new_email;
  }
  document.kitty.email.value = email;
}
function SubmitKitty()
{
  SubmitReadBeans(5);
}
function ViewHistory()
{
  parent.location.href = history_url;
}

// Internal functions
function LoadReadBeans(retry)
{
  parent.tinc.location.href = history_url;
  if (!ReadBeans() && retry > 0)
    return setTimeout("LoadReadBeans(" + (retry - 1) + ")", 500);
  parent.tinc.location.href = "about:blank";
}
function SubmitReadBeans(retry)
{
  parent.tinc.location.href = history_url;
  if (!ReadBeans() && retry > 0)
    return setTimeout("SubmitReadBeans(" + (retry - 1) + ")", 500);
  parent.tinc.location.href = "about:blank";
  SubmitSendBeans();
}
function SubmitSendBeans()
{
  document.tinc.reverse.value = "1";
  document.tinc.name.value = AdjustName(document.kitty.name.value);
  document.tinc.email.value = AdjustEmail(document.kitty.email.value);
  document.tinc.subject.value =
    _("I drop ") + kitty_beans + _(" beans into the kitty ") +
    "[total: " + (kitty_total + kitty_beans) + "]";
  if (!document.tinc.body.value)
    document.tinc.body.value = " ";
  document.tinc.no.value = "-1";
  document.tinc.entrykey.value = "-1";
  document.tinc.key.value = submit_key;
  document.tinc.adminkey.value = "-1";
  document.tinc.submit();
}
function ReadBeans()
{
  var beans = parseInt(document.kitty.beans.value);
  var total = 0;
  var doc = parent.tinc.document;
  var lng = doc.getElementsByTagName("td").length;
  var pattern = "[total: ";
  for (var idx = 0; !total && idx < lng; idx++)
  {
    var node = doc.getElementsByTagName("td")[idx];
    while (!node.data && node.firstChild)
      node = node.firstChild;
    if (node.data && node.data.indexOf(pattern) >= 0)
      total = parseInt(node.data.substring(node.data.indexOf(pattern) +
                                           pattern.length));
  }
  if (!total)
    total = 0;
  if (total > 0)
    kitty_total = total;
  if (!beans)
    beans = 0;
  if (beans > 0)
    kitty_beans = beans;
  if (total > 0)
    document.kitty.total.value = kitty_total + kitty_beans;
  return total > 0;
}
function AdjustName(name)
{
  name = Strip(name);
  if (name.length == 0)
    name = "anonymous";
  return name;
}
function AdjustEmail(email)
{
  email = Strip(email);
  var at_pos = email.indexOf("@");
  var err = at_pos <= 0 || email.indexOf("@", at_pos + 1) >= 0 ||
            email.indexOf(".", at_pos + 1) <= at_pos + 1 ||
            email.charAt(email.length - 1) == ".";
  if (err)
  {
    if (at_pos >= 0)
      email = email.substring(0, at_pos);
    if (email.length > 0)
      email += "@unknown.xx";
    else
      email = "anonymous@unknown.xx";
  }
  return email;
}
function Strip(str)
{
  var first = 0, last = str.length - 1;
  while (str.charAt(first) == " ")
    ++first;
  while (str.charAt(last) == " " && last >= first)
    --last;
  return str.substring(first, last + 1);
}

// Translation table and translation function for localized versions
var trans_tab = new Array();

function _(s)
{
  var t = trans_tab[s];
  if (t)
    s = t;
  return s;
}

// Fill the translation table
function fill_trans_tab(trans_data)
{
  for (var i = 0; i < trans_data.length / 2; ++i)
    trans_tab[trans_data[2 * i]] = trans_data[2 * i + 1];
}

// Test functions
function TestKitty()
{
  alert("kitty.beans  = '" + document.kitty.beans.value  + "'\n" +
        "kitty.total  = '" + document.kitty.total.value  + "'\n" +
        "kitty.name   = '" + document.kitty.name.value   + "'\n" +
        "kitty.email  = '" + document.kitty.email.value  + "'\n" +
        "tinc.subject = '" + document.tinc.subject.value + "'\n" +
        "tinc.name    = '" + document.tinc.name.value    + "'\n" +
        "tinc.email   = '" + document.tinc.email.value   + "'");
}
function TestEmail()
{
  alert("''       -> '" + AdjustEmail("")       + "'\n" +
        "'@'      -> '" + AdjustEmail("@")      + "'\n" +
        "'@y.z'   -> '" + AdjustEmail("@x.y")   + "'\n" +
        "'x.y@z'  -> '" + AdjustEmail("x.y@z")  + "'\n" +
        "'x@y@z'  -> '" + AdjustEmail("x@y@z")  + "'\n" +
        "'x@.yz'  -> '" + AdjustEmail("x@.yz")  + "'\n" +
        "'x@yz.'  -> '" + AdjustEmail("x@yz.")  + "'\n" +
        "'x@y.z.' -> '" + AdjustEmail("x@y.z.") + "'\n" +
        "'x@y.z'  -> '" + AdjustEmail("x@y.z")  + "'\n");
}
