var popwin = "";
var NS = (navigator.appName == "Netscape") ? 1 : 0;
var options = "BNTTTSWFFFFFFFFFFFT";
var user;
var currentEntry = "";
var blockMenues = false;
//  0. B: british, A: american
//  1. S: small, N: normal, L: large, X: extra large
//  2. T: grammar on, F: off
//  3. T: frequency on, F: off
//  4. T: phonetics on, F: off
//  5. S: simple search, A: advanced search
//  6. W: word search, T: text search
//  7. T: simple headwords on, F: off
//  8. T: compounds on, F: off
//  9. T: phrases on, F: off
// 10. T: derived words on, F: off
// 11. T: phrasal verbs on, F: off
// 12. T: collocations on, F: off
// 13. T: definitions on, F: off
// 14. T: editorial notes on, F: off
// 15. T: examples on, F: off
// 16. T: any text on, F: off - no longer used
// 17. T: options menu on, F: off - no longer used
// 18. T: sound ikons on, F: off

var lastOperation;
var starget, sfunc, sparm1, sparm2, sparm3, sparm4;
var initialSearch = "";
var initialEntry = "";
var subFrames = null;

function service( target, func, parm1, parm2, parm3, parm4, anchor, maincont )
{ //alert( "service("+func+","+parm1+","+parm2+","+parm3+","+parm4+","+anchor+")" );
  //h = NS ? 600 : target.document.body.clientHeight;
  h = 600;
  loc = "/mc_au2/Mac2Servlet?"+
        "u="+user+
        "&o="+options+
        "&h="+h+
        "&f="+func+
        "&p1="+parm1+
        "&p2="+parm2+
        "&p3="+parm3+
        "&p4="+parm4;
  if ( anchor != "" )
    loc += "#"+anchor;
  target.location = loc;
  starget = target;
  if ( maincont )
  { sfunc = func; sparm1 = parm1; sparm2 = parm2;
    sparm3 = parm3; sparm4 = parm4; sanchor = anchor;
  }
  setFocus();
}

function redraw()
{ if ( lastOperation == "N" )
    top.contents.contents.location = "intro.htm";
  else if ( lastOperation == "I" )
    top.contents.contents.location = "welcome.htm";
  else
    service( top.contents.contents, sfunc, sparm1, sparm2, sparm3, sparm4, sanchor, true );
}

function gotoAnchor( id )
{ var i, elem, y;
  for ( i = 0; i < starget.document.anchors.length; i++ )
  { if ( starget.document.anchors[i].name == id )
    { //starget.document.anchors[i].parentElement.scrollIntoView();
      elem = starget.document.anchors[i];
      y = 0;
      while( elem != null )
      { y += elem.offsetTop;
        elem = elem.offsetParent;
      }
      if ( NS )
        y -= 20;
      starget.scrollTo( 0, y );
      break;
    }
  }
}

function setCurrentEntry( id )
{ if ( -1 != ( i = id.indexOf('-') ) &&
       -1 != ( i = id.indexOf('-',i+1) ) )
    currentEntry = id.substring(0,i);
  else
    currentEntry = id;
}

function init()
{ var query, parms, pos, nam, val;
  user = Math.round( 1000000 * Math.random() );
  initialSearch = "";
  initialEntry = "";
  query = location.search.substring( 1 );
  if ( query.length > 0 )
  { parms = query.split( '&' );
    for ( i = 0; i < parms.length; i++ )
    { pos = parms[i].indexOf( '=' );
      nam = parms[i].substring( 0, pos );
      val = parms[i].substring( pos+1 );
      if ( nam == "o" )
        options = val;
      if ( nam == "s" )
        initialSearch = unescape(val);
      if ( nam == "e" )
        initialEntry = val;
    }
  }
  //alert( "header: "+ header.location );
  lastOperation = "I";
  blockMenues = true;
  subFrames = new Array(
    new Array( "contents", "content0.htm" ),
    new Array( "promo", "promo.htm" ),
    new Array( "input", "search.htm" ),
    new Array( "line", "line.htm" ),
    new Array( "header", "header.htm" ),
    new Array( "contents", "wait.htm" ) );
  if ( options.charAt(5) == 'A' )
  { if ( options.charAt(6) == 'W'  )
      subFrames[0][1] = "content1.htm";
    else
      subFrames[0][1] = "content2.htm";
  }
  setTimeout( "loadFrames()", 10 );

  //setFocus();
}

function loadFrames()
{ var len;

  if ( subFrames == null || subFrames.length == 0 )
  { setFocus();
    return;
  }
  len = subFrames.length;
  if ( len > 0 )
  { eval(subFrames[len-1][0]).location = subFrames[len-1][1];
    subFrames.length = len-1;
  }
}

function escape1( s )
{ var s1, i;
  s1 = s;
  for ( i = 0; i < s1.length; i++ )
    if ( 256 <= s1.charCodeAt(i) )
      s1 = s1.substring( 0, i ) + s1.substring( i+1 );
  s1 = escape( s1 );
  for ( i = 0; i < s1.length; i++ )
    if ( s1.charAt(i) == '+' )
      s1 = s1.substring( 0, i ) + "%2B" + s1.substring( i+1 );
  return( s1 );
}

function validEmail(email)
{ var emailRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
  return( !emailRE.test( "a@b.dk" ) || emailRE.test(email) );
}

function setFocus()
{ //input.document.forms[0].a.select();
  //top.contents.contents.focus();
  input.document.forms[0].a.value = "";
  input.document.forms[0].a.focus();
}

function notImplemented()
{ alert( "Not implemented yet." );
  setFocus();
}

var addContents;
function addFrameLoaded()
{ popwin.contents.location = addContents;
  setFocus();
  popwin.focus();
}

function addFrameReload()
{ var loc, i;
  loc = location.protocol + "//" + location.host + location.pathname;
  i = loc.lastIndexOf( '/' );
  loc = loc.substring( 0, i+1 ) + addContents;
  popwin.contents.location = loc;
  setFocus();
  popwin.focus();
}

function addFrameCancel()
{ popwin.close();
  setFocus();
}

function studyPages()
{ if ( options.charAt(0) == 'A' )
    addContents = "usstudy/index.htm";
  else
    addContents = "ukstudy/index.htm";
  popwin = window.open(
             "addframe.htm", "AddWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(800,500);
}

function help()
{ addContents = "help/index.htm";
  popwin = window.open(
             "addframe.htm", "HelpWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(800,500);
}

function help1()
{ addContents = "help/help.htm#searchtypes";
  popwin = window.open(
             "addframe.htm", "HelpWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(800,500);
}

function editOptions()
{ addContents = "options.htm";
  popwin = window.open(
             "addframe.htm", "AddWin",
             "width=250,height=180" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(250,180);
}

function loadGifs()
{ addContents = "loadgifs.htm";
  popwin = window.open(
             "addframe.htm", "AddWin",
             "width=250,height=160" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(250,160);
}

function admStatistics()
{ addContents = "stat.htm";
  popwin = window.open(
             "addframe.htm", "HelpWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(800,600);
}

function printEntry()
{ if ( currentEntry == "" )
  { alert( "No entry currently selected." );
    setFocus();
  }
  else
  { popwin = window.open(
             "prtframe.htm", "AddWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
    popwin.resizeTo(800,500);
  }
}

function printFrameLoaded()
{ service( popwin.contents, "entry", "p"+currentEntry, "popbox", "p", "", "", false );
  setFocus();
  popwin.focus();
}

function copyEntry()
{ if ( currentEntry == "" )
  { alert( "No entry currently selected." );
    setFocus();
  }
  else
  { popwin = window.open(
             "cpyframe.htm", "AddWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
    popwin.resizeTo(800,500);
  }
}

function copyFrameLoaded()
{ service( popwin.contents, "entry", "p"+currentEntry, "popbox", "c", "", "", false );
  setFocus();
  popwin.focus();
}

function todaysWord()
{ popwin = window.open(
             "dayframe.htm", "AddWin", 
             "width=600,height=500," +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(600,500);
}

function dayframeLoaded()
{ service( popwin.contents, "today", "", "", "", "", "", false );
  popwin.focus();
}

function monthlyWord()
{ addContents = "monthly.htm";
  popwin = window.open(
             "addframe.htm", "AddWin",
             "width=800,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(800,500);
}

function easyAccess()
{ addContents = "easyacc.htm";
  popwin = window.open(
             "addframe.htm", "AddWin", 
             "width=600,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(600,500);
}

function addToFavDo( opt )
{ var s, i;
  popwin.close();
  setFocus();
  s = top.location.href;
  if ( -1 != ( i = s.indexOf('?') ) )
    s = s.substring( 0, i );
  if ( opt )
    s = s + "?o="+options;
  window.external.AddFavorite( s, top.document.title );
}

function topTwenty()
{ addContents = "top20.htm";
  popwin = window.open(
             "addframe.htm", "AddWin",
             "width=700,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(700,500);
}

function suggestWord()
{ addContents = "suggest.htm";
  popwin = window.open(
             "addframe.htm", "AddWin", 
             "width=600,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(600,500);
}

function emailFriend()
{ addContents = "mailfr.htm";
  popwin = window.open(
             "addframe.htm", "AddWin", 
             "width=600,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(600,500);
}

function medSite()
{ setFocus();
  setTimeout( "window.open( 'http://www.macmillandictionary.com', '' )", 100 );
}

function emailUs()
{ addContents = "mailus.htm";
  popwin = window.open(
             "addframe.htm", "AddWin", 
             "width=600,height=500" +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(600,500);
}

function search( expr, dir, mark, page )
{ currentEntry = "";
  if ( expr == "" )
  { lastOperation = "N";
    top.contents.contents.location = "intro.htm";
  }
  else
  { lastOperation = "M";
    service( top.contents.contents, "search", escape1(expr), dir, mark, page, "", true );
  }
}

function wordclick( word )
{ //alert( "wordclick: [" + word + "]" );
  if ( -1 != word.search("\\w") && word.length < 250 )
  { currentEntry = "";
    lastOperation = "M";
    service( top.contents.contents, "search", escape1(word), 1, 0, 1, "", true );
  }
}

function wordclick1( word )
{ input.document.forms[0].a.value = word;
  input.search( word );
}

function entry( id )
{ lastOperation = "D";
  setCurrentEntry( id );
  service( top.contents.contents, "entry", id, "main", "", "", id, true );
}

function page( id )
{ lastOperation = "D";
  currentEntry = "";
  service( top.contents.contents, "entry", id, "main", "", "", id, true );
}

function match( id, span )
{ var i, id1;
  lastOperation = "D";
  if ( id.substring(0,4) == "box-" )
  { i = id.indexOf( '-', 4 );
    id1 = id.substring( i+1 );
  }
  else
    id1 = id;
  setCurrentEntry( id1 );
  service( top.contents.contents, "match", id, span, "main", "", id1, true );
}

function singleMatch( id )
{ gotoAnchor(id);
  setCurrentEntry(id);
}

var matchBoxId;
var matchBoxSpan;
function matchBox( id, span )
{ var loc, i;
  popwin = window.open(
             "", "AddWin", 
             "width=500,height=300," +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(500,300);
  matchBoxId = id;
  matchBoxSpan = span;
  loc = location.protocol + "//" + location.host + location.pathname;
  i = loc.lastIndexOf( '/' );
  loc = loc.substring( 0, i+1 ) + "popmatch.htm";
  popwin.location = loc;
}

function matchBoxLoaded()
{ service( popwin.contents, "match", matchBoxId, matchBoxSpan, "popbox", "", matchBoxId, false );
  popwin.focus();
}

function play( id )
{ nullframe.document.open();
  nullframe.document.writeln( "<html><head></head>" );
  nullframe.document.writeln( "<body>" );
  if (NS)
    nullframe.document.writeln( "<embed src=\"" +
                                "SoundServlet?"+
                                "p="+ id + "\""+
                                " autostart=\"true\"" +
                                " hidden=\"false\">" );
  else
    nullframe.document.writeln( "<bgsound src=\"" +
                                "SoundServlet?"+
                                "p="+ id + "\" loop=\"1\">" );
  nullframe.document.writeln( "</body>" );
  nullframe.document.writeln( "</html>" );
  nullframe.document.close();

  setFocus();
}

var pictureArea;
function picture( id, area, width, height )
{ popwin = window.open(
             "", "AddWin", 
             "width=" + (width+50) + ",height=" + (height+100) +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(width+50,height+100);
  service( popwin, "entry", id, "popbox", "", "", "", false );
  pictureArea = area;
  popwin.focus();
}

function pictureLoaded()
{ var i;
  popwin.focus();
  for ( i = 0; i < popwin.document.all.length; i++ )
  { if ( popwin.document.all[i].name == pictureArea )
    { popwin.document.all[i].focus();
      break;
    }
  }
}

var textId;
function textbox( id )
{ var i, loc;
  popwin = window.open(
             "", "AddWin", 
             "width=500,height=300," +
             ",resizable=yes,menubar=no,status=no,toolbar=no,location=no" +
             ",scrollbars=no" );
  popwin.resizeTo(500,300);
  textId = id;
  // Following necessary because calling page is dynamic
  loc = location.protocol + "//" + location.host + location.pathname;
  i = loc.lastIndexOf( '/' );
  loc = loc.substring( 0, i+1 ) + "popframe.htm";
  popwin.location = loc;
}

function textboxLoaded()
{ service( popwin.contents, "entry", textId, "popbox", "", "", "", false );
  popwin.focus();
}

function setparm( nr, value )
{ options = options.substring(0,nr)+value+options.substring(nr+1);
}

function checkAdvancedOptions()
{ var i;
  if ( options.charAt(6) == 'W' )
  { for ( i = 7; i <= 12; i++ )
      if ( options.charAt(i) == 'T' )
        return( true );
    alert( "Please select one or more options in WordSearch" );
    return( false );
  }
  else
  { for ( i = 13; i <= 15; i++ )
      if ( options.charAt(i) == 'T' )
        return( true );
    alert( "Please select one or more options in TextSearch" );
    return( false );
  }
}

function resetContents()
{ if ( lastOperation == "D" )
    redraw();
  else if ( lastOperation == "I" )
  { top.contents.contents.location = "welcome.htm";
    lastOperation = "I";
  }
  else
  { top.contents.contents.location = "intro.htm";
    lastOperation = "N";
  }
/*setFocus();*/
}

function switchContents()
{ blockMenues = true;
  if ( options.charAt(5) == 'S' )
    contents.location = "content0.htm";
  else if ( options.charAt(6) == 'W'  )
    contents.location = "content1.htm";
  else if ( options.charAt(6) == 'T'  )
    contents.location = "content2.htm";
}

