// CONSTANTS.

// BROWSER : IE, NN, NN_6
var BROWSER;
if ( document.all ) {
  BROWSER = "IE";
} else if ( document.layers ) {
  BROWSER = "NN";
} else if ( document.getElementById ) {
  BROWSER = "NN_6";

// Firefox 1.5 identifizieren  
  if (navigator.userAgent.indexOf("Firefox")!=-1) {
      BROWSER="MFF";
      var versionindex = navigator.userAgent.indexOf("Firefox")+8
      v1 = navigator.userAgent.charAt(versionindex);
      v2 = navigator.userAgent.charAt(versionindex+2);
      v3 = navigator.userAgent.charAt(versionindex+4);
      if ( v1 == 1 ) {
           if ( v2 == 5 ) {
                BROWSER="MFF15";
           }
      } else if ( v1 == 2 ) {
          BROWSER = "MFF2";
      } else if ( v1 > 2 ) {
          BROWSER="MFFN";
      }
  }
  
  if (navigator.userAgent.indexOf("SeaMonkey")!=-1) {
      BROWSER="MSM";
  }
// Konqueror wird nicht unterstützt

   if ( navigator.appName == "Konqueror" ) {
      alert ( "Dear Konqueror user,\n\n    the navigation tree is not supported yet.\nPlease use firefox, mozilla or netscape browser..." );
   }

  
} else {
  BROWSER = "UNKNOWN";
}

//        if ( top.BROWSER == "FF15" ) {
//            alert ( "\n\nDEBUGGING: FF15, please press OK ..." );
//            }



// Da der Menubaum beim Konqueror 3.4.2 nicht funktionieren, Test ob Javascript Source für MSIE funktioniert
//    fuehrt aber zu viel mehr Fehlern
//   if ( navigator.appName == "Konqueror" ) {
//        BROWSER = "IE";
//   }



// EVENTS

if ( BROWSER == "NN" )   captureEvents(Event.RESIZE);
if ( BROWSER == "NN_6" ) captureEvents(Event.RESIZE);

//set event handlers
onLoad = init;
onResize = onresize;

// BROWSER_VER : Internet-Explorer / Netscape-Navigator AppVersion:
var BROWSER_VER = Number(navigator.appVersion.substring( 0, navigator.appVersion.indexOf(" ") ));
var BROWSER_LAN = navigator.appVersion.substring( navigator.appVersion.indexOf("[")+1,navigator.appVersion.indexOf("]") );

// PUB_ROOT: absolute root of the public web
var PUB_ROOT = "http://www.hpcportal.de/";

// SEC_ROOT: absolute root of the secure web
var SEC_ROOT = "https://gate.hpcportal.de/";

// START_URL: the first url to display.
var START_URL = "hpcportal.shtml";

// STYLE_URL: url of the 'css' file to be used webwide.
var STYLE_URL = "http://www.hpcportal.de/Style/debis.css";

// HTML_START and HTML_END: default html code for the beginning and the end of the page.
var HTML_START = "<html>\n<head>\n<link rel=stylesheet type=text/css href=" + STYLE_URL + " title=debis-style>\n</head>\n";
var HTML_END = "</html>";

var MODE_PUBLIC = 0;
var MODE_SECURE = 1;
var MODE_CGI = 2;
var MODE_ABSURL = 255;


// GLOBAL VARS

// the secure_access flag
var secure_access;

// the userid var
var userid = "";

// setTimeout function vars...
var infoTimeout;
var treeTimeout;
var bodyTimeout;

// set the language arrays...
var lanArray = new Array( "eng", "ger" );
var lanStrings = new Array( "English", "Deutsch" );

// current language varriable.
var curLan = lanArray[0];

// set the initial language to the language of the browser.
if ( BROWSER_LAN == "en" ) curLan = "eng"; else if ( BROWSER_LAN == "de" ) curLan = "ger";

// dummys var for displaying dynamic HTML. (language dependant)
var dummy;

var empty_html = new Array();
empty_html["eng"] = HTML_START + HTML_END;
empty_html["ger"] = HTML_START + HTML_END;

var loading_html = new Array();
loading_html["eng"] = HTML_START + "<BODY bgcolor=#d9d9dd>\n<TABLE height=100% width=100%>\n<tr><td align=center valign=center><FONT class=message>Please wait.<BR><BR>&nbsp;&nbsp;Loading ...</FONT></td></tr>\n</TABLE>\n</BODY>" + HTML_END;
loading_html["ger"] = HTML_START + "<BODY bgcolor=#d9d9dd>\n<TABLE height=100% width=100%>\n<tr><td align=center valign=center><FONT class=message>Bitte warten.<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wird geladen ...</FONT></td></tr>\n</TABLE>\n</BODY>" + HTML_END;

var redirect_html = new Array();
redirect_html["eng"] = HTML_START + "<BODY bgcolor=#d9d9dd>\n<TABLE height=100% width=100%>\n<tr><td align=center valign=center><FONT class=message>The hpcPortal has been opened in a new window...</FONT></td></tr>\n</TABLE>\n</BODY>" + HTML_END;
redirect_html["ger"] = HTML_START + "<BODY bgcolor=#d9d9dd>\n<TABLE height=100% width=100%>\n<tr><td align=center valign=center><FONT class=message>Das hpcPortal wurde in einem neuen Fenster geöffnet...</FONT></td></tr>\n</TABLE>\n</BODY>" + HTML_END;

// root locations:
var public_root = new Array();
public_root["eng"] = PUB_ROOT + "eng/";
public_root["ger"] = PUB_ROOT + "ger/";

var secure_root = new Array();
secure_root["eng"] = SEC_ROOT + "eng/";
secure_root["ger"] = SEC_ROOT + "ger/";

var public_cgi_root = PUB_ROOT + "cgi-bin/";
var secure_cgi_root = SEC_ROOT + "cgi-bin/";


// the current mode : MODE_PUBLIC or MODE_SECURE or MODE_CGI or MODE_CGI+MODE_SECURE
var curMode = MODE_PUBLIC;

// the current page displayed in the bodyFrame.
var bodyLoc = START_URL;

var targetURL = "";

// PRELOAD FUNCTION CALLS

// get the cookied settings:
getSettings();

// determine wether we have a referrer

if ( BROWSER == "NN" )
  checkReferrer();
else if ( BROWSER == "NN_6" )
  checkReferrer();
else
  checkReferrer();
//  checkSearchString();


// FUNCTIONS

function lanExists ( lan ) {
  for ( var i = 0; i < lanArray.length; i++ )
    if ( lanArray[i] == lan ) return true;
  return false;
}


function getSettings() {
  var settingsStr = GetCookie("hpcPortal settings java");
  if ( settingsStr == null ) { // we have a new user...
    // do something to welcome the new user...
    return false;
  }

  language = settingsStr;
  if ( lanExists( language ) ) curLan = language;
}

function setSettings() {
  var today = new Date();
  var expiry = new Date( today.getTime() + (365*24*60*60) );

  SetCookie( "hpcPortal settings java", curLan , expiry, "/", ".hpcportal.de" );
}

function onresize() {
  // update the locations
  updateLocations();
}

function goRestoreSettings() {
  var url = SEC_ROOT + "restore_settings.php?language=" + curLan + "&browser=" + ( BROWSER == "NN" ? "NN" : "IE" );
  bodyFrame.location.href = url;
}

function goURLafterRestored() {
  secure_access = true;
  bodyFrame.location.href = targetURL;
}

function goURL(loc,mode,delay) {
  if ( !bodyFrame ) return;
  if ( !loc ) return;
  if ( !mode ) mode = MODE_PUBLIC;

  // set the url according to the current mode:
  var url;


  if ( mode == MODE_PUBLIC ) {
    url = public_root[curLan] + loc;
  } else if ( mode == MODE_SECURE ) {
    url = secure_root[curLan] + loc;
    if ( secure_access == false ) {
      targetURL = url;
      delay = null;
      url = public_root[curLan] + "access_denied.shtml";
    }
  } else if ( mode == MODE_CGI ) {
    url = public_cgi_root + loc + curLan;
  } else if ( mode == MODE_CGI+MODE_SECURE ) {
    url = secure_cgi_root + loc + curLan;
    if ( secure_access == false ) {
      targetURL = url;
      delay = null;
      url = public_root[curLan] + "access_denied.shtml";
    }
  } else if ( mode == MODE_ABSURL ) {
    url = loc;
  } else {
    alert( mode + " is an invalid mode!" );
    return;
  }


  // update the mode:
  curMode = mode;


  // save the current bodyFrame location for resizing and language changing
  bodyLoc = loc;


  if ( delay ) { // if a delay is wanted use a waiting message and a timeout...
    bodyFrame.location.href = "javascript: top.loading_html[top.curLan];";
    top.bodyTimeout = top.setTimeout( "bodyFrame.location.href = '" + url + "';"  , delay );
  } else
    bodyFrame.location.href = url;
}

function setLanguage( lan ) {
  if ( !lan ) return;
  if ( !lanExists(lan) ) {
    alert ( "Unknown language: " + lan );
    return;
  }

  // set the curLan var to the new index number.
  curLan = lan;

  // update the settings cookie:
  setSettings();

  // update the locations
  updateLocations();
}

function updateLocations() {
  // reset the status bar
  showStatus("");

  // display updating messages.
  infoFrame.location.href = "javascript: top.loading_html[top.curLan];";
  treeFrame.location.href = "javascript: top.loading_html[top.curLan];";

  // update the locations.
  infoTimeout = top.setTimeout( "infoFrame.location.href = '" + public_root[top.curLan] + "info.shtml';", 1000 );
  treeTimeout = top.setTimeout( "treeFrame.location.href = '" + public_root[top.curLan] + "tree.shtml';", 1000 );

  goURL( bodyLoc, curMode, 2500 );
}


function showStatus(s) {
  top.status = s;
}

function makeTopWindow(win) {
  if ( win.window == top.window ) {
    //alert ( "Wonderful ! I'm the top window." );
    return true;
  } else {
    alert ( "Phew, wheese, gasp ... I can't breathe in here!\n\nOpening new browser window..."  );
    // open a new browser window with the win location.
    var OpenWin = this.open(win.location.href, "hpcPortal", "toolbar=yes,menubar=yes,location=yes,status=yes,resizable=yes,scrollbars=no");
    // display redirected message.
    win.location.href = "javascript: redirect_html[curLan];";
    return false;
  }
}

function checkReferrer() {
  if (document.referrer) {
    if ( (document.referrer == "http://www.hpcportal.de" ) ||
       (document.referrer == "http://www.hpcportal.de/" ) ||
       (document.referrer == "http://www.hpcportal.de/index.html") ) {
      secure_access = false;
    } else if ( document.referrer.indexOf("https://gate.hpcportal.de/welcome_user.php") == 0 ) {
      secure_access = true;
      var paramString = document.referrer.substring( document.referrer.indexOf("?")+1 );
      userid = paramString.substring( paramString.indexOf("userid=") + 7 );
      userid = userid.substring( 0, userid.indexOf("&") == -1 ? userid.length : userid.indexOf("&") );
    } else if ( document.referrer.indexOf("http://www.hpcportal.de/welcome_user.php") == 0 ) {
      secure_access = false;
    } else if ( document.referrer.indexOf("http://www.hpcportal.de/cookie_test.php") == 0 ) {
      secure_access = false;
    } else { // document.referrer is none of the above
      if ( curLan == "eng" )
        alert ( "The hpcPortal only functions correctly when accessed via http://www.hpcportal.de !\n\nIf you followed a link to the hpcPortal it was not set correctly.\n\nYou will be redirected to http://www.hpcportal.de now..." );
      else
        alert ( "Das hpcPortal muss, um richtig funktionieren zu können, via http://www.hpcportal.de geladen werden!\n\nFalls Sie einem Link zum hpcPortal gefolgt sind, so wies dieser nicht auf die richtige URL.\n\nSie werden jetzt nach http://www.hpcportal.de ungeleitet..." );

      top.location.href = "http://www.hpcportal.de";
      secure_access = false;
    }
  } else { // !document.referrer
    /*
     * The document.referrer variable is not set.
     * The current browser probably doesn't support this property
     * secure_access is set to true! This causes problems for non-registered users!!!
     *
     */

    secure_access = true;

    /*
    if ( curLan == "eng" )
      alert ( "The hpcPortal only functions correctly when accessed via http://www.hpcportal.de !\n\nIf you have used a bookmark to access hpcPortal the url was not set correctly. Please change the bookmark accordingly.\n\nYou will be redirected to http://www.hpcportal.de now..." );
    else
      alert ( "Das hpcPortal muss, um richtig funktionieren zu können, via http://www.hpcportal.de geladen werden!\n\nFalls Sie einem Favoriten-Eintrag gefolgt sind, so wies dieser nicht auf die richtige URL.\nBitte passen Sie Ihre Favoriten entsprechend an.\n\nSie werden jetzt nach http://www.hpcportal.de ungeleitet..." );
    top.location.href = "http://www.hpcportal.de";
    */
  }
}

function checkSearchString() {
  var searchString = window.location.search.substring(1);
  userid = searchString.substring( searchString.indexOf("userid=") + 7 );
  userid = userid.substring( 0, userid.indexOf("&") == -1 ? userid.length : userid.indexOf("&") );
  if ( userid )
    secure_access = true;
  else secure_access = false;
}

function init() {
  // make sure we're the top window...
  if ( !makeTopWindow(self) ) return;  // cancel init because a new window was opened.
  // set the initial locations...
  infoTimeout = top.setTimeout( "infoFrame.location.href = '" + public_root[curLan] + "info.shtml';", 100 );
  treeTimeout = top.setTimeout( "treeFrame.location.href = '" + public_root[curLan] + "tree.shtml';", 500 );
  // redirect the bodyFrame to the START_URL...
  goURL( START_URL, MODE_PUBLIC, 3000 );
  setSettings();
}

