/*
 * @(#)secure.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.
 *
 */

/*
 *
 * Functions available in this file:
 *   secureMain(element)
 *   secureMenu(element)
 *
 * Usage examples:
 *   onclick = "return secureMain(this);"
 *   onclick = "return secureMenu(this);"
 *
 */

function escapePath(path) {
  return path.substring(0, path.indexOf('?')) + escape(path.substring(path.indexOf('?')));
}

function secure(main, menu, target) {
  if (top.location.protocol == 'http:' && (target == 'main' || target == 'menu') && top.location.hostname == 'www.glcr.lu') {
    //  7 --> http://
    main = escapePath(main.substring(7 + top.main.location.hostname.length + 1));
    
    menu = menu.replace('action=login&', '');
    menu = menu.replace('action=logout&', '');
    menu = menu.replace('&button=sent', '');
    //  7 --> http://
    // 14 --> menu.php?menu=
    menu = escapePath(menu.substring(7 + top.menu.location.hostname.length + 1 + 14));
    if ('' != menu) {
      menu = '&menu=' + menu;
    }
    
    top.location = 'https://' + top.location.hostname + '/index.php?main=' + main + menu;
    return false;
  }
  return true;
}

function secureMain(element) {
  return secure(element.href, top.menu.location.href, element.target);
}

function secureMenu(element) {
  return secure(top.main.location.href, element.href, element.target);
}