 /**
  Copyright (c) 2009, Ralf Hertsch
  Contact me: hertsch(at)berlin.de, http://phpManufaktur.de

  This module is free software. You can redistribute it and/or modify it
  under the terms of the GNU General Public License  - version 2 or later,
  as published by the Free Software Foundation: http://www.gnu.org/licenses/gpl.html.

  This module 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.
  
  $Id: seo_tool.js 9 2009-12-07 08:49:18Z ralf $
  
**/

/**
  * Read Params of the URL into Variables
  * 
  * @return NULL
  */
function getParams() {
  var thisURL = document.URL;
  if (thisURL.indexOf("?") == -1) { return null; }
  var paramStr = thisURL.substr((thisURL.indexOf("?")+1));
  var paramSplit;
  var endPos;
  var paramName;
  var paramValue;
  // loop through the params
  while (paramStr != "") {
  paramSplit = paramStr.indexOf("=");
  if (paramSplit == -1) { break; }
  endPos = paramStr.indexOf("&");
  if (endPos < 0) { endPos = 500000; }
  paramName = paramStr.substr(0,paramSplit);
  paramValue = paramStr.substring(paramSplit+1,endPos);
  // create variable and set value
  eval(paramName + " = \"" + paramValue + "\"");
  paramStr = paramStr.substr(endPos+1);
  }
  return null;
} // getParams()

/**
  * Check if the cookie SEO is set and return the value or NULL
  *
  * @return INT 
  */
function getCookie() {
  var keks = document.cookie;
  // startposition of seo entry...
   var posName = keks.indexOf("; seo=");
   if (posName == -1) {
    // perhaps first entry in cookie list?
    if (keks.indexOf("seo=") == 0) posName = 0;
    // no luck, break
    else return null;
   }
   // search start and endposition
   var valueStart = keks.indexOf("=", posName)+1;
   var valueEnd = keks.indexOf(";", posName+1);
   if (valueEnd == -1) valueEnd = keks.length;
   // get value and return
   var val = keks.substring(valueStart, valueEnd);
   return unescape(val);
} // getCookie()

/**
  * Check if the param or an cookie SEO is set and
  * display the infobox.
  *
  * @return STR InfoBox
  */
function showPageInfo() {
 getParams();
 if ((typeof(seo) !== 'undefined') && (navigator.cookieEnabled == true)) {
   // param seo is set
   if (seo == 1) {
   // set cookie
   document.cookie = "seo=1; path=/;";
   }
   else {
   // delete cookie
   var exp = new Date(); 
   exp.setTime (exp.getTime() - 1); 
   document.cookie = "seo=0; path=/; expires=" + exp.toGMTString()+";";
   }
 }
 else if (typeof(seo) == 'undefined') {
   seo = 0;
 }

 if (navigator.cookieEnabled == true) {
   seo = getCookie();
   if (seo == null) seo = 0;
 }

 if (seo == 1) {
   // show informations about website title, description and keywords
   var title = document.title;
   var description = "";
   var keywords = "";
   meta = document.getElementsByTagName("meta");
   if (typeof(meta.description) !== 'undefined') description = meta.description.content;
   if (typeof(meta.keywords) !== 'undefined') keywords = meta.keywords.content;
   titleCount = title.split(" ");
   descriptionCount = description.split(" ");
   keywordsCount = keywords.split(",");
   document.write('<div style="margin:10px;padding:10px;border:1px solid #000;"><p><b>Seitentitel:</b><br /><i>'+title+'</i><br /><span style="font-size:smaller;">W&ouml;rter: <b>'+titleCount.length+'</b> L&auml;nge: <b>'+title.length+'</b></span></p>'+
   '<p><b>Kurzbeschreibung:</b><br /><i>'+description+'</i><br /><span style="font-size:smaller">W&ouml;rter: <b>'+descriptionCount.length+'</b> L&auml;nge: <b>'+description.length+'</b></span></p>'+  
   '<p><b>Schl&uuml;sselw&ouml;rter:</b><br /><i>'+keywords+'</i><br /><span style="font-size:smaller">W&ouml;rter: <b>'+keywordsCount.length+'</b> L&auml;nge: <b>'+keywords.length+'</b></span></p>'+
   '</div>');
 }
} // showPageInfo()
