function detectBrowser ()
{
   var element= null;
   navigator.OS= '';
   navigator.version= 0;
   navigator.org= '';
   navigator.family= '';

   // Plateform detection...
   var platform;
   if (typeof(window.navigator.platform) != 'undefined')
   {
      platform = window.navigator.platform.toLowerCase();
      if (platform.indexOf('win') != -1)
         navigator.OS = 'win';
      else if (platform.indexOf('mac') != -1)
         navigator.OS = 'mac';
      else if (platform.indexOf('unix') != -1 || platform.indexOf('linux') != -1 || platform.indexOf('sun') != -1)
         navigator.OS = 'nix';
   }

   // Browser family, organization and version...
   var i = 0;
   var ua = window.navigator.userAgent.toLowerCase();
   
   if (ua.indexOf('opera') != -1)
   {
      i = ua.indexOf('opera');
      navigator.family= 'opera';
      navigator.org= 'opera';
      navigator.version= parseFloat('0' + ua.substr(i + 6), 10);
   }
   else if ((i = ua.indexOf('msie')) != -1)
   {
      navigator.org= 'microsoft';
      navigator.version= parseFloat('0' + ua.substr(i + 5), 10);
      
      if (navigator.version < 4)
         navigator.family= 'ie3';
      else if (navigator.version < 5)
         navigator.family= 'ie4';
      else if (navigator.version < 6)
         navigator.family= 'ie5';
      else
         navigator.family= 'ie6'
   }
   else if (typeof(window.controllers) != 'undefined' && typeof(window.locationbar) != 'undefined')
   {
      i = ua.lastIndexOf('/')
      navigator.version= parseFloat('0' + ua.substr(i + 1), 10);
      navigator.family= 'gecko';

      if (ua.indexOf('netscape') != -1)
         navigator.org= 'netscape';
      else if (ua.indexOf('compuserve') != -1)
         navigator.org= 'compuserve';
      else
         navigator.org= 'mozilla';
   }
   else if ((ua.indexOf('mozilla') !=-1) && (ua.indexOf('spoofer')==-1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera')==-1)&& (ua.indexOf('webtv')==-1) && (ua.indexOf('hotjava')==-1))
   {
       var is_major= parseFloat(navigator.appVersion);
    
      if (is_major < 4)
         navigator.version= is_major;
      else
      {
         i= ua.lastIndexOf('/')
         navigator.version= parseFloat('0' + ua.substr(i + 1), 10);
      }
      navigator.org= 'netscape';
      navigator.family= 'nn' + parseInt(navigator.appVersion);
   }
   else if ((i = ua.indexOf('aol')) != -1 )
   {
      // aol
      navigator.family   = 'aol';
      navigator.org      = 'aol';
      navigator.version   = parseFloat('0' + ua.substr(i + 4), 10);
   }

   // DOM and CSS support...
   navigator.DOMCORE1= (typeof(document.getElementsByTagName) != 'undefined' && typeof(document.createElement) != 'undefined');
   navigator.DOMHTML= (navigator.DOMCORE1 && typeof(document.getElementById) != 'undefined');
   navigator.DOMCORE2= (navigator.DOMHTML && typeof(document.createElementNS) != 'undefined');
   navigator.DOMCSS1= ( (navigator.family == 'gecko') || (navigator.family == 'ie4') );

   navigator.DOMCSS2  = false;
   if (navigator.DOMCORE1)
   {
      element= document.createElement('p');
      if (typeof(element) != 'undefined')
        navigator.DOMCSS2 = (typeof(element.style) == 'object');
   }

   navigator.DOMEVENTS   = (typeof(document.createEvent) != 'undefined');
}


function popFrame ()
{
   popTargetFrame (self);
}

function popTargetFrame (target)
{
  if(target != top)
    replaceURL(target.location.href.toString());
}

function replaceURL (url)
{
  top.location.replace(url.toString());
}


function showAll (srcObj)
{
   var output= "Object: ";
   for(var i in srcObj)
      output+=  i + ' = ' + srcObj[i] + '   ';
   alert(output);
}


var  mainTitle= "--==[ Portails sur Might and Magic ]==--";
var  mainUrl= "http://www.portailsmm.com/index.html";
var  baseUrl= "http://www.portailsmm.com/";
var  portalSubject= "Might and Magic - Portail";
var  COPYRIGHT_HTML= "<div class=\"Center Copyright\">Copyright &copy; 1997-2008, Soft Labs</div>";

function setBookmark ()
{
  if (navigator.family.indexOf('ie') != -1  && navigator.version >= 4)
    window.external.AddFavorite(mainUrl, mainTitle); 
  else
    alert ("Fonction non disponible!\n\nThis function is not available!");
}

function setStartPage (target)
{
  if (navigator.family.indexOf('ie') != -1  && navigator.version >= 4)
  {
    target.style.behavior='url(#default#homepage)';
    target.setHomePage(mainUrl);
  }
  else
    alert ("Fonction non disponible!\n\nThis function is not available!");
}

function rnd (max)
{
  return Math.round(Math.random() * max) % max;
}

function launch ( newURL, newName, newFeatures, orgName ) 
{
  var remote = open(newURL, newName, newFeatures);
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;
  return remote;
}

function showPage ( pageUrl )
{
  var newwin= "pcRemote";
  var features= "alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=0,z-lock=0";
  var thiswin= "APage";
  launch (pageUrl, newwin, features, thiswin);
}

function sendMel (name, server, attributes)
{
  var mailURL= "mailto:" + name + "@" + server;
  if (attributes)
    mailURL+= "?" + attributes;
  replaceURL(mailURL);
}

function toPageTop ( url )
{
  newURL= url;
  if (url.indexOf("#", 0) != -1)
    newURL= url.substring (0, url.indexOf("#", 0));
  newURL+= "#sommet";
  replaceURL(newURL);
}

function outHandler ( img )
{
  return img.off.src;
}

function overHandler ( img )
{
  return img.on.src;
}

var myImgRemote= null;
var myMapRemote= null;
var myRedacRemote= null;

function showImage ( imgURI, width, height, title, css )
{
  var pageUrl= "";
  var newwin= "ImageView";
  var features= "scrollbars=0,height=" + height + ",width=" + width;
  features= features + ",alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=0,location=0,menubar=0,resizable=0,status=0,titlebar=0,toolbar=0,z-lock=0";
  var thiswin= "mainWin";

  if (myImgRemote != null && !myImgRemote.closed)
    myImgRemote.close();
  myImgRemote= launch (pageUrl, newwin, features, thiswin);

  htmlCode= "<html><head><title>--==[ " + title + " ]==--</title><link rel=\"STYLESHEET\" href=\"" + css + "\" type=\"text/css\" />";
  htmlCode+= "<body>";
  htmlCode+= "<div class=\"ImageView\"><img src=\"" + imgURI + "\" alt=\"" + title + "\" width=\"" + width + "\" height=\"" + height + "\" /></div>";
  htmlCode+= "</body></html>";

  myImgRemote.document.write (htmlCode); 

  if (navigator.version > 4)
    myImgRemote.moveTo(0, 0);
}

function showRedaction ( english )
{
  if (myRedacRemote == null || myRedacRemote.closed)
  {
    var pageUrl= "";
    var newwin= "RedacView";
    var features= "scrollbars=1,height=480,width=640";
    features= features + ",alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=0,location=0,menubar=0,resizable=1,status=0,titlebar=0,toolbar=0,z-lock=0";
    var thiswin= "mainWin";

    myRedacRemote= launch (pageUrl, newwin, features, thiswin);

    if (navigator.version > 4)
      myRedacRemote.moveTo(0, 0);
  }

  myRedacRemote.document.close();
  myRedacRemote.document.open();

  title= "Ravenshore Times - La R&eacute;daction";
  if (english)
    title= "Ravenshore Times - The Redaction";

  explanetion= "Au premi&egrave;re &eacute;tage de la Taverne \"Kessel's Kantina\".";
  if (english)
    explanetion= "At the second level of the Inn \"Kessel's Kantina\"."

  htmlCode= "<html><head><title>--==[ " + title + " ]==--</title><link rel=\"STYLESHEET\" href=\"common.css\" type=\"text/css\" />";
  htmlCode+= "<body class=\"Center\"><h1>" + title + "<br \>&nbsp;</h1>";
  htmlCode+= "<div class=\"ImageView\" id = \"mapImage\"><img src=\"pics/RavenshoreTimes.gif\" alt=\"" + title + "\" /><br />";
  htmlCode+= explanetion + "<br />&nbsp;</div>" + COPYRIGHT_HTML;
  htmlCode+= "</div></body></html>";

  myRedacRemote.document.write (htmlCode); 
}

function showMap ( imgURI, title, css )
{
  if (myMapRemote == null || myMapRemote.closed)
  {
    var pageUrl= "";
    var newwin= "ImageView";
    var features= "scrollbars=1,height=480,width=640";
    features= features + ",alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=0,location=0,menubar=0,resizable=1,status=0,titlebar=0,toolbar=0,z-lock=0";
    var thiswin= "mainWin";

    myMapRemote= launch (pageUrl, newwin, features, thiswin);

    if (navigator.version > 4)
      myMapRemote.moveTo(0, 0);
  }

  myMapRemote.document.close();
  myMapRemote.document.open();

  var imageURL= null;
  if (imgURI.indexOf("http", 0) == -1)
    imageURL= baseUrl + imgURI;
  else
    imageURL= imgURI;

  htmlCode= "<html><head><title>--==[ " + title + " ]==--</title><link rel=\"STYLESHEET\" href=\"" + css + "\" type=\"text/css\" />";
  htmlCode+= "<body><div class=\"Center\"><h1>" + title + "<br />&nbsp;</h1>";
  htmlCode+= "<div class=\"ImageView\" id = \"mapImage\"><img src=\"" + imgURI + "\" alt=\"" + title + "\" /><br />";
  htmlCode+= "<a href=\"" + imageURL + "\">" + imageURL + "</a><br />&nbsp;</div>" + COPYRIGHT_HTML;
  htmlCode+= "</div></body></html>";

  myMapRemote.document.write (htmlCode); 
}

function buildRegionMap ( region, ext )
{
  path = "pics";
  imgObj= new Object;
  imgObj.on= new Image ();
  imgObj.on.src= path + "/worldmap_" + region + "_on." + ext;
  imgObj.off= new Image ();
  imgObj.off.src= path + "/worldmap_" + region + "_off." + ext;
  return imgObj;
} // END buildRegionMap ()


function setRandomBackground ()
{
  var idx= rnd(3);
  var bgPath= "url(pics/bg" + idx + ".jpg)";
  document.body.style.backgroundImage= bgPath.toString();
}

popFrame ();

function init  ()
{
  detectBrowser ();
}


function levelToXP (level)
{
  return 500 * level * (level - 1);
}

function XPToLevel (xp)
{
  return Math.floor(Math.sqrt(0.25 + xp * 0.002) + .5);
}

function testMMLevelForm (entry) 
{
  if (entry.value != null && entry.value.length != 0) 
  {
    var toto= entry.value * 1;
    entry.value= isFinite(toto) ? entry.value : "";
  }
   computeMMLevelForm (entry.form);
}

function computeMMLevelForm (form) 
{
  if (form.level.value == null || form.level.value.length == 0) 
  {
    form.level.value= 0;
  }
  if (form.experience.value == null || form.experience.value.length == 0) 
  {
    form.experience.value= 0;
  }

  var i= form.level.value;
  var j= form.experience.value;
  form.experienceNeed.value= levelToXP(i);
  form.bestLevel.value= XPToLevel(j);
}

function resetMMLevelForm (form) 
{
  form.level.value="";
  form.experienceNeed.value="";
  form.experience.value="";
  form.bestLevel.value="";
  i="";
  j="";
}

function ICRAValidator (target)
{
 showPage ("http://www.icra.org/cgi-bin/test.cgi?lang=FR&amp;url=" + target);
}
