/*
 * @(#)frames.js
 *
 *    Version: 0.99.20100301
 * Written by: Yves Kreis <mailto:yves.kreis@education.lu>
 *
 * Copyright (C) 2003-2010 by Yves Kreis
 *
 * This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Luxembourg License.
 * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/lu/ or send a letter to 
 * Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
 *
 */

/*
 *
 * This JavaScript checks if the designed frame structure is present.
 * It uses seven variables to specify
 *   - the folder the site resides in (strFolder);
 *   - the base document of the site (strBase);
 *   - the target frame (strTarget);
 *   - the document to redirect to (strRedirect);
 *   - the base document of the menu (strMenu);
 *   - the search string for the menu (strSearch);
 *
 * To change its values, use a script environment like in the following example:
 *    <html>
 *    <head>
 *      ...
 *      <script type="text/javascript">
 *      <!--
 *        var strFolder   = "/";
 *        var strBase     = "index.php";
 *        var strTarget   = "main";
 *        var strRedirect = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
 *        var strMenu     = "menu.php";
 *        var strSearch   = "menu";
 *      //-->
 *      </script>
 *      <script type="text/javascript" src="frames.js"></script>
 *      ...
 *    </head>
 *    ...
 *    </html>
 *
 */

if ("undefined" == typeof(strFolder)) {
  var strFolder = "/";
}
if ("undefined" == typeof(strBase)) {
  var strBase = "index.php";
}
if ("undefined" == typeof(strTarget)) {
  var strTarget = "main";
}
if ("undefined" == typeof(strRedirect)) {
  var strRedirect = document.location.pathname.substring(document.location.pathname.lastIndexOf("/") + 1);
}
if ("undefined" == typeof(strMenu)) {
  var strMenu = "menu.php";
}
if ("undefined" == typeof(strSearch)) {
  var strSearch = "menu";
}

var strPath = top.location.pathname.substring(0, top.location.pathname.lastIndexOf("/") + 1);
var strDocument = top.location.pathname.substring(top.location.pathname.lastIndexOf("/") + 1);

if ((top.location.protocol == 'http:' || top.location.protocol == 'https:') && (strPath != strFolder || ("" != strDocument && strDocument != strBase))) {
  strLocation = document.location.protocol + "//" + document.location.hostname + strFolder;
  if (strPath != strFolder || strRedirect != strBase) {
    strPath = document.location.pathname.substring(0, document.location.pathname.lastIndexOf("/") + 1);
    if (0 == strPath.indexOf(strFolder)) {
      strPath = strPath.substring(strFolder.length);
    }
    strLocation += strBase + "?" + strTarget + "=" + strPath + strRedirect + escape(document.location.search + document.location.hash);
  } else if (strDocument == strMenu && "" != document.location.search) {
    arrSearch = document.location.search.substring(1).split("&");
    for (intSearch = 0; intSearch < arrSearch.length; intSearch++) {
      if (0 == arrSearch[intSearch].indexOf(strSearch + "=")) {
        strLocation += strBase + "?" + arrSearch[intSearch] + escape(document.location.hash);
        break;
       }
    }
  }
  top.location = strLocation;
}