/**
 * /javascript/statform/stat.js
 * --------------------------------------------------------------------------
 * Check of forms in stat-services of the PIROBASE (R) System.
 * This code is developed by the PIRONET PRODUCT GROUP.
 * Do not make any changes.
 * --------------------------------------------------------------------------
 * Copyright (c) 2000 PIRONET AG.
 * All Rights Reserved.
 */

/*
 * 2000-11-22/clorz - created
 * 2000-11-28/clorz - real int addition with year counts (bevor: string + int)
 * 2000-12-12/clorz - testroutines for conection between frames (but problems with netscape)
 * 2000-12-13/clorz - deactivate submit-test
 * 2000-12-15/clorz - centralize images in /images/statistic
 *                  - new: functions for separat graph-applet window
 *                  - renamed graph to chart
 * 2000-12-20/clorz - new: quicknavigation with goToAnchor()
 * 2001-01-02/clorz - avoid Netscape-bug with 'formular.QUESTIONSELECT.value' is always null
 * 2001-01-10/clorz - new: closeStatisticFrame()
 * 2001-01-23/clorz - respect frames at close() or back()
 * 2001-02-05/clorz - fixed error on evalute day of VALID_TO
 */


  /*
   * insert a '<a href ...' tag in the HTML-document for statistic bar chart if javascript is enabled
   *
   * @param index - index of function to call
   */
  function getGraphCallCommand(index)
  {
    document.write("<a href=\"JavaScript:loadGraphWindow" + index + "()\"><img src=\"/images/statistic/chart_bar17x17.gif\" width=\"17\" height=\"17\" border=\"0\" alt=\"Grafik [" + index + "]\"></a>");
  }


  /*
   * insert a '<a href ...' tag in the HTML-document for statistic circle chart if javascript is enabled
   *
   * @param index - index of function to call
   */
  function getChartAppletCallCommand(index)
  {
    document.write("<a href=\"JavaScript:loadChartAppletWindow" + index + "()\"><img src=\"/images/statistic/chart_circle17x17.gif\" width=\"17\" height=\"17\" border=\"0\" alt=\"Grafik [" + index + "]\"></a>");
  }


  /*
   * writes the header for chart window
   *
   * @param doc - targetobject of document
   * @param title - title of document
   */
  function writeChartAppletWindowHeader(doc, title, headline)
  {
    doc.write("<html>");
    doc.write(" <head>");
    doc.write("  <title>" + title + "</title>");
    doc.write("  <link rel=\"stylesheet\" href=\"/definition.css\" type=\"text/css\">");
    doc.write("  <style><!--");
    doc.write("   h1 { font-family: \"Square 721 BT\",Arial,Helvetica,Verdana; font-size: 14pt; }");
    doc.write("   a { font-family: \"Square 721 BT\",Arial,Helvetica,Verdana; font-size: 12pt; }");
    doc.write("  --></style>");
    doc.write(" </head>");
    doc.write(" <body>");
    doc.write("  <center>");
    doc.write("   <h1>" + headline + "</h1>");
    doc.write("  </center>");
    doc.write("  <p>&nbsp;</p>");
  }


  /*
   * writes the footer for chart window
   *
   * @param doc - targetobject of document
   * @param closeLabel - label for closebutton
   */
  function writeChartAppletWindowFooter(doc, closeLabel)
  {
    doc.write("  <p align=\"CENTER\">");
    doc.write("   <a href=\"JavaScript:window.close();\">" + closeLabel + "</a>");
    doc.write("  </p>");
    doc.write(" </body>");
    doc.write("</html>");
  }


  /*
   * Check form bevor submit;<br>
   * sets textfields VALID_FROM and VALID_TO if necessary and possible
   *
   * @param formular - form-object
   * @return true and submit or false and not submit
   */
  function submitForm(formular)
  {
    if (formular.VALID_FROM.value != "")
    {
      var endOfYear = formular.VALID_FROM.value.indexOf("-");
      var endOfMonth = formular.VALID_FROM.value.lastIndexOf("-");
      if (endOfYear == 0 || endOfYear < 4 || endOfMonth == 0 || endOfMonth == (formular.VALID_FROM.value.length - 1) || endOfYear == endOfMonth)
      {
        alert("Datumsformat: JJJJ-MM-TT   - bitte Feld 'von:' prüfen -");
        return false;
      }

      var entryDate = formular.VALID_FROM.value;
      if (entryDate.substring(4, 5) == "-" && entryDate.substring(6, 7) == "-")
      {
        var newDate = entryDate.substring(0, 5) + "0" + entryDate.substring(5, entryDate.length)
        formular.VALID_FROM.value = newDate;
        entryDate = newDate;
      }
      if (entryDate.substring(4, 5) == "-" && entryDate.substring(7, 8) == "-" && entryDate.length < 10)
      {
        var newDate = entryDate.substring(0, 8) + "0" + entryDate.substring(8, entryDate.length)
        formular.VALID_FROM.value = newDate;
      }
    }

    if (formular.VALID_TO.value != "")
    {
      var endOfYear = formular.VALID_TO.value.indexOf("-");
      var endOfMonth = formular.VALID_TO.value.lastIndexOf("-");
      if (endOfYear == 0 || endOfYear < 4 || endOfMonth == 0 || endOfMonth == (formular.VALID_TO.value.length - 1) || endOfYear == endOfMonth)
      {
        alert("Datumsformat: JJJJ-MM-TT   - bitte Feld 'bis:' prüfen -");
        return false;
      }

      var entryDate = formular.VALID_TO.value;
      if (entryDate.substring(4, 5) == "-" && entryDate.substring(6, 7) == "-")
      {
        var newDate = entryDate.substring(0, 5) + "0" + entryDate.substring(5, entryDate.length)
        formular.VALID_TO.value = newDate;
        entryDate = newDate;
      }
      if (entryDate.substring(4, 5) == "-" && entryDate.substring(7, 8) == "-" && entryDate.length < 10)
      {
        var newDate = entryDate.substring(0, 8) + "0" + entryDate.substring(8, entryDate.length)
        formular.VALID_TO.value = newDate;
      }
    }
/*
    if (formular.VALID_FROM.value != "" && formular.VALID_TO.value == "")
    {
      setCurrentDate(formular.VALID_TO);
    }

    if (formular.VALID_FROM.value == "" && formular.VALID_TO.value != "")
    {
      setCurrentDate(formular.VALID_FROM);
    }
*/
    if (formular.VALID_FROM.value != "" && formular.VALID_TO.value != "" && formular.VALID_FROM.value > formular.VALID_TO.value)
    {
      alert("Das Datum im Feld 'von:' ist neuer als das im Feld 'bis:'!");
      return false;
    }

    var onlyOneDay = 0;
    if (formular.VALID_FROM.value != "" && formular.VALID_TO.value != "" && formular.VALID_FROM.value == formular.VALID_TO.value)
    {
      onlyOneDay = 1;
    }

    if (formular.VALID_FROM.value.length > 0)
    {
      var iYear = parseInt(formular.VALID_FROM.value.substring(0, 4));
      var iMonth = parseInt(formular.VALID_FROM.value.substring(5, 7));
      var iDay = parseInt(formular.VALID_FROM.value.substring(8, formular.VALID_FROM.value.length));

      var date = new Date();
      date.setYear(iYear);
      date.setMonth(iMonth - 1);
      date.setDate(iDay);
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);

      formular.LVALID_FROM.value = date.getTime();

      //DEBUG
      //alert("Year=" + iYear + "   Month=" + iMonth + "  Day=" + iDay + "   Sek=" + date.getTime());
    }

    if (formular.VALID_TO.value.length > 0)
    {
      var iYear = parseInt(formular.VALID_TO.value.substring(0, 4));
      var iMonth = parseInt(formular.VALID_TO.value.substring(5, 7));
      var iDay = parseInt(formular.VALID_TO.value.substring(8, formular.VALID_TO.value.length));

      var date = new Date();
      date.setYear(iYear);
      date.setMonth(iMonth - 1);
      date.setDate(iDay);
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      if (onlyOneDay == 1)
      {
        date.setHours(23);
        date.setMinutes(59);
        date.setSeconds(59);
      }

      formular.LVALID_TO.value = date.getTime();

      //DEBUG
      //alert("Year=" + iYear + "   Month=" + iMonth + "  Day=" + iDay + "   Sek=" + date.getTime());
    }

    // All fields ok: post
    formular.submit();

/*
    if (parent.STATRESULT)
    {
      var selectedID = formular.TOKEN_ID.options.selectedIndex;
      var searchStr = "TOKEN_ID=" + formular.TOKEN_ID.options[selectedID].value;
      searchStr += "&VALID_TO=" + formular.VALID_TO.value;
      searchStr += "&VALID_TO=" + formular.VALID_TO.value;
      parent.STATRESULT.location.search = searchStr;
    }
*/

    if (parent.STATQUEST)
    {
      parent.STATQUEST.location.search="TOKEN_ID=" + formular.TOKEN_ID.options[formular.TOKEN_ID.selectedIndex].value;
      //var curStatQuestUrl = parent.STATQUEST.location;
      //var newStatQuestUrl = curStatQuestUrl.protocol + "//" + curStatQuestUrl.host + curStatQuestUrl.pathname + "?TOKEN_ID=1&TOKEN_NAME=pirobase";
      //alert("statQuestUrl=" + statQuestUrl + "\nhref=" + statQuestUrl.href + "\nsearch=" + statQuestUrl.search);
      //alert("newStatQuestUrl=" + newStatQuestUrl);
      //parent.STATQUEST.location = newStatQuestUrl;
    }

    return true;
  }


  /*
   * Check form bevor submit;<br>
   * sets textfields VALID_FROM and VALID_TO if necessary and possible
   *
   * @param formular - form-object
   * @return true and submit or false and not submit
   */
  function submitYearReview(currentYear)
  {
    if (parent.STATRESULT)
    {
      var formular = parent.STATMASK.SELECTIONFORM;
      var selectedID = formular.TOKEN_ID.options.selectedIndex;
      var newTokenID = formular.TOKEN_ID.options[selectedID].value;
      //this.location.search='MODE=YEARREVIEW&YEAR=' + currentYear + '&TOKEN_ID=' + newTokenID;
      parent.STATRESULT.location.search='MODE=YEARREVIEW&YEAR=' + currentYear + '&TOKEN_ID=' + newTokenID;
      //parent.STATQUEST.location.search='TOKEN_ID=' + newTokenID;
    }
    formular.submit();
    return false;
  }


  /*
   * load site
   *
   * @param site url
   * @return true and submit or false and not submit
   */
  function loadSite(site)
  {
    if (parent.STATRESULT)
    {
      var formular = parent.STATMASK.SELECTIONFORM;
      var selectedID = formular.TOKEN_ID.options.selectedIndex;
      var newTokenID = formular.TOKEN_ID.options[selectedID].value;
      site += '?TOKEN_ID=' + newTokenID;
      parent.STATRESULT.location=site;
    }
    return true;
  }


  /*
   * got to anchor in the current document
   *
   * @param formular with select-object
   */
  function goToAnchor(formular)
  {
    if (formular != null && formular.QUESTIONSELECT.value != null)
    {
      //var selectedID = formular.QUESTIONSELECT.options.selectedIndex;
      //this.location.hash = formular.QUESTIONSELECT.options[selectedID].value;
      this.location = formular.QUESTIONSELECT.value;
    }
    else
    {
      if (formular.QUESTIONSELECT.options.selectedIndex == null)
      {
        return;
      }

      // avoid Netscape bug
      switch(formular.QUESTIONSELECT.options.selectedIndex)
      {
        case 0: this.location = "#Q001"; break;
        case 1: this.location = "#Q002"; break;
    //<option value="">
        case 3: this.location = "#Q003"; break;
        case 4: this.location = "#Q004"; break;
    //<option value="">
        case 6: this.location = "#Q005"; break;
        case 7: this.location = "#Q006"; break;
        case 8: this.location = "#Q007"; break;
    //<option value="">
        case 10: this.location = "#Q008"; break;
        case 11: this.location = "#Q009"; break;
        case 12: this.location = "#Q010"; break;
    //<option value="">
        case 14: this.location = "#Q011"; break;
        case 15: this.location = "#Q012"; break;
        case 16: this.location = "#Q013"; break;
        case 17: this.location = "#Q014"; break;
    //<option value="">
        case 19: this.location = "#Q015"; break;
        case 20: this.location = "#Q016"; break;
        case 21: this.location = "#Q017"; break;
        case 22: this.location = "#Q018"; break;
    //<option value="">
        case 24: this.location = "#Q019"; break;
        case 25: this.location = "#Q020"; break;
      }
    }
  }


  /*
   * sets the current date to specified textfield<br>
   * dateformat='JJJJ-MM-TT' / 'YYYY-MM-DD'
   *
   * @param element textfield-object to set
   */
  function setCurrentDate(element)
  {
    var currentDate = new Date();
    var currentYear = currentDate.getYear();
    var currentMonth = currentDate.getMonth() + 1;
    var currentDay = currentDate.getDate();

    if (currentYear < 1900)
    {
      currentYear = 1900 + currentYear;
    }

    if (currentMonth < 10)
      currentMonth = "0" + currentMonth;

    if (currentDay< 10)
      currentDay = "0" + currentDay;

    element.value = currentYear + "-" + currentMonth + "-" + currentDay;
  }


function openMailForm(itemID, itemLanguage)
{
  var searchPart = '';
  if (itemID != null)
  {
    searchPart = '?ITEMID=' + itemID;
    if (itemLanguage != null)
      searchPart += '&LANGUAGEID=' + itemLanguage;
  }
  var mailFormUrl = "../../cmd/stat-mail/index.html" + searchPart;
  var mailWindow = window.open(mailFormUrl, "MAILFORM", "width=580,height=480,toolbar=no,directories=no,status=yes,scrollbars=yes,menubar=no,resizable=yes");
  //DEBUG
  //var mailWindow = window.open(mailFormUrl, "MAILFORM", "width=580,height=480,toolbar=yes,location=yes,directories=yes,status=yes,scrollbars=yes,menubar=yes,resizable=yes");
}


function deleteLastDigit(stringValue, digit)
{
  if (stringValue == null || stringValue.length <= 0)
    return stringValue;

  if (digit)
    return stringValue.substring(0, stringValue.length - 1);
}


/**
 * Goes back in history or close window.
 *
 * depand on hidden value of WINDOWMODE in formular: 1=normal  2=new window
 */
function closeStatisticFrame(formular)
{
  if (formular.WINDOWMODE.value == 2)
    top.window.close();
  else
    top.history.back();
}

/*
 * Open a new window of browser with the statistic-rating frame and set it in front;
 * the name of the mainwindow of pirobase(R) is set to 'PBMAINWINDOW' to
 * link to the pirobase(R) mainwindow;
 *
 * @autor clorz@pironet.com
 */
function openRatingTop10Window(itemID, languageID)
{
  var param = '';
  if (itemID && languageID)
    param = '?ITEMID=' + itemID + '&LANGUAGEID=' + languageID;
  var statWindow = window.open('../../cmd/stat-rating-top10/index.html' + param, 'pageObjects',
                  'width=640,height=480,toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=no');
  if (statWindow)
  {
    if (statWindow.opener == null)
      statWindow.opener = self;
    //DEBUG
    //alert("statWindow.opener.top.name=" + statWindow.opener.top.name);
    statWindow.opener.top.name = "PBMAINWINDOW";
    statWindow.focus();
  }
}

/**
 * Open a new window of browser with the frame and set it in front.
 *
 * @param tokenID
 * @param itemType
 * @param itemID
 * @param languageID
 */
function openHits4Item(tokenID, itemType, itemID, languageID)
{
  if (navigator.userAgent.indexOf("MSIE") != -1)
  {
    // IE browser, now test for version 5 or better
    var browserVersionInfo = Array();
    browserVersionInfo = navigator.userAgent.split(";");

    IEVersion = browserVersionInfo[1].substring(6,7);
    if (IEVersion >="5")
    {
      hits4ItemFrame = window.open('../../cmd/stat-hits4item/index.html?TOKEN_ID=' + tokenID + '&ITEM_TYPE=' + itemType + '&ITEM_ID=' + itemID + '&LANGUAGE_ID=' + languageID, 'PBStat4Item', 'width=600,height=480,toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes');
    }
    else
    {
      hits4ItemFrame = window.open('../../cmd/stat-hits4item/index.html?TOKEN_ID=' + tokenID + '&ITEM_TYPE=' + itemType + '&ITEM_ID=' + itemID + '&LANGUAGE_ID=' + languageID, 'PBStat4Item', 'width=600,height=480,toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes');
    }
  }
  else
  {
    hits4ItemFrame = window.open('../../cmd/stat-hits4item/index.html?TOKEN_ID=' + tokenID + '&ITEM_TYPE=' + itemType + '&ITEM_ID=' + itemID + '&LANGUAGE_ID=' + languageID, 'PBStat4Item', 'width=600,height=480,toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes');
  }

  if (hits4ItemFrame)
    hits4ItemFrame.focus();
}

