/*
 * Set up popup window object to hold references to all
 * popup windows.
 */
if (null == versionID) var versionID = new Array();
versionID[versionID.length] = ("Popup Script: v1.3\n");

function nl_getGELevel()
{
  var t = "/courses/"
  var f = document.location + "";
  f = f.substring(f.lastIndexOf(t)+t.length,f.lastIndexOf(t)+t.length+1);
  f = parseInt(f,10);
  if (!isNaN(f))
  {
    return f;
  }
  else
  {
    return -1;
  }
}

function NL_popupWindow(bgcolor)
{
this.win = null;
this.color = bgcolor;
}

var nl_popup = new Array();
nl_popup["text"]     = new NL_popupWindow("#CCFFCC");
nl_popup["answer"]   = new NL_popupWindow("#EEEEEE");
nl_popup["grammar"]  = new NL_popupWindow("#FFFFFF");
nl_popup["wordlist"] = new NL_popupWindow("#CCCCCC");
nl_popup["tape"]     = new NL_popupWindow("#FFCC99");

function openPopupWindowSpecURL(winArray,linkHref,linkTarget,settings)
{
  if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

  var theURL = linkHref;
  var theTarget = linkTarget;

  if (null == settings)
  {
    if(String(theTarget) == "grammar")
    {
      settings = 'scrollbars=yes,width=500,height=300,menubar=yes,resizable=yes';
    }
    else
    {
      settings = 'scrollbars=yes,width=300,height=200,menubar=yes,resizable=yes';
    }
  }

  //------ C�lculo de coordenadas --------------
  var coordX, coordY;

  if(settings.indexOf("width")>=0)
  {
    coordX = settings.substring(settings.indexOf("width=")+6,settings.indexOf("width=")+9);
  }

  coordX = (screen.width - coordX - 12);
  coordY = 0;

  settings = settings + ',left=' + coordX + ',top=' + coordY;
  //------ Fin del c�lculo de coordenadas ------

  if (null == winArray[theTarget].win) {//    alert("New window or was closed programmatically");
    winArray[theTarget].win = window.open(theURL,theTarget,settings)
  }
  else if (!winArray[theTarget].win.closed) {//    alert("Already open, load new document");
    winArray[theTarget].win.location.assign(theURL)
    winArray[theTarget].win.focus();
  }
  else {//    alert("Closed manually");
    winArray[theTarget].win = window.open(theURL,theTarget,settings)
  }
}

function openPopupWindowURL(winArray,theLink,settings)
{
  if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

  var theURL = theLink.href;
  var theTarget = theLink.target;

  openPopupWindowSpecURL(winArray,theURL,theTarget,settings);
}

function openPopupWindowSameDir(winArray,theLink,settings)
{
  if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

  var theURL = theLink.href;
  var theFile = theURL.substring(theURL.lastIndexOf("/"),theURL.length);
  var theDocument = document.location.href.toString();
  var theDirectory = theDocument.substring(0,theDocument.lastIndexOf("/"));
  theURL = theDirectory + theFile;
  var theTarget = theLink.target;

  openPopupWindowSpecURL(winArray,theURL,theTarget,settings);
}

function openPopupWindowHTML(winArrayX,winNameX,title,content,settings)
{
/*
--------------- Ejemplo ------------------------------------------------
var Titulo = "Titulo de la ventana";
var Contenido = "Contenido de la ventana.";
var Settings = "width=100,height=100,scrollbar=no,resizable=yes,menubar=no";

openPopupWindowHTML(nl_popup,'answer',Titulo,Contenido,Settings);
------------------------------------------------------------------------
*/
  var winArray = (winArrayX==null)?nl_popup:winArrayX;
  var winName = (winNameX==null)?"answer":winNameX;
  var tophtml = "<html><head><title>";
  var midhtml = "</title><style>body,table { font: bold 13px Arial,Helvetica,sane-serif }</style></head><body bgcolor="+winArray[winName].color+" text=#000000>\n<table id=\"table_id\"><tr><td>\n";
  var bothtml = "<form onsubmit='return false'><p align=\"center\"><input type=button onclick='window.close()' value='Close'></p></form>\n</td></tr></table>\n</body></html>";

  if (null == settings) settings = 'scrollbars=yes,width=300,height=400,menubar=yes,resizable=yes';

  //------ C�lculo de coordenadas --------------
  if(winName == 'answer')
  {
    var coordX, coordY, X, Y, iPosicionFinal;

    tamX = null;
    tamY = null;
    posX = -1;
    posY = -1;
    if(settings.indexOf("width")>=0)
    {
      iPosicionFinal = settings.indexOf(",",settings.indexOf("width="))
      if (iPosicionFinal < 0) iPosicionFinal = settings.length - settings.indexOf(",",settings.indexOf("width="))+6;
      tamX = settings.substring(settings.indexOf("width=")+6,iPosicionFinal);
      if (tamX == "null") tamX = null;
    }
    if(settings.indexOf("height")>=0)
    {
      iPosicionFinal = settings.indexOf(",",settings.indexOf("height="))
      if (iPosicionFinal < 0) iPosicionFinal = settings.length - settings.indexOf(",",settings.indexOf("height="))+7;
      tamY = settings.substring(settings.indexOf("height=")+7,iPosicionFinal);
      if (tamY == "null") tamY = null;
    }
    if(settings.indexOf("left")>=0)
    {
      iPosicionFinal = settings.indexOf(",",settings.indexOf("left="))
      if (iPosicionFinal < 0) iPosicionFinal = settings.length - settings.indexOf(",",settings.indexOf("left="))+5;
      posX = settings.substring(settings.indexOf("left=")+5,iPosicionFinal);
    }
    if(settings.indexOf("top")>=0)
    {
      iPosicionFinal = settings.indexOf(",",settings.indexOf("top="))
      if (iPosicionFinal < 0) iPosicionFinal = settings.length - settings.indexOf(",",settings.indexOf("top="))+4;
      posY = settings.substring(settings.indexOf("top=")+4,iPosicionFinal);
    }

    if (tamX == null || tamY == null) {

      if (document.getElementById) {

        var iframe = document.getElementById("popup_calculator");
        var doc;

        if (iframe.contentDocument) {
          doc = iframe.contentDocument;
        } else {
          //IE takes iframe in another way
          doc = frames["popup_calculator"].document;
        }

        doc.open();
        doc.write(tophtml + title + midhtml + content + bothtml);
        doc.close();

        var tamX = doc.getElementById("table_id").offsetWidth + 20;
        if (tamX > 600) tamX = 600;
        tamY = doc.getElementById("table_id").offsetHeight + 40;
      } else {
        tamX = 500;
        tamY = 500;
      }

    }

    settings = settings + ",width=" + tamX;
    settings = settings + ",height=" + tamY;

    if (posX == -1)
      settings = settings + ",left=0";
      //settings = settings + ",left=" + (screen.width-tamX-12);

    if (posY == -1)
      settings = settings + ",top=0";

  }

  //------ Fin del c�lculo de coordenadas ------

  if (navigator.appVersion.indexOf("MSIE 3") >= 0)
  {
    var win = window.open("",winName,settings);
    win.document.open();
    win.document.write(tophtml + title + midhtml + content + bothtml);
    win.document.close();
    return;
  }

//  if (winArray[winName].win == null) {//    alert("New window or was closed programmatically");
  if (!winArray[winName].win) {//    alert("New window or was closed programmatically");
    winArray[winName].win = window.open("",winName,settings)
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
    winArray[winName].win.document.close();
  }
  else if (!winArray[winName].win.closed) {//    alert("Already open, write new document");
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
    winArray[winName].win.document.close();
    if (navigator.appVersion.indexOf("MSIE 3") < 0) winArray[winName].win.focus();
  }
  else {//    alert("Closed manually");
    winArray[winName].win = window.open("",winName,settings)
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + title + midhtml + content + bothtml);
    winArray[winName].win.document.close();
  }

}

/*
 *  Desc: Insert a iframe into body. This iframe permitts to load a page
 *        with the content of a popup feedback to calculate his size
 *        before show it
 */
function insertCalc(){
  if (document.getElementById("popup_calculator") == null)
  {
	  var path = getPath() + "loading.htm";
    var eDIV = document.createElement("iframe");
    //use the setAttribute method to assign it attributes
    eDIV.setAttribute("id","popup_calculator");
    eDIV.setAttribute("src",path);
    eDIV.setAttribute("scrolling","yes");
    eDIV.setAttribute("height","0");
    eDIV.setAttribute("width","300");
    //Estilo para firefox
    eDIV.setAttribute("style","width: 300px; height: 0px; border: none");
    // append your newly created DIV element to an already existing element.
    document.body.appendChild(eDIV);
  }
}
if (document.nl_config) document.nl_config.add ("onload","insertCalc()");


function openPopupWindowFrames(winArray,leftContent,rightContent,winName,title,settings)
{
  if (null == settings) settings = 'scrollbars=yes,width=500,height=250,menubar=yes,resizable=yes';
  if (null == title) title = 'Explicaciones gramaticales';
  if (null == winName) winName = 'grammar';

  //------ C�lculo de coordenadas --------------
  var coordX, coordY;

  if(settings.indexOf("width")>=0)
  {
    coordX = settings.substring(settings.indexOf("width=")+6,settings.indexOf("width=")+9);
  }

  coordX = (screen.width - coordX - 12);
  coordY = 0;

  settings = settings + ',left=' + coordX + ',top=' + coordY;
  //------ Fin del c�lculo de coordenadas ------

  var tophtml = "<html><head><title>"+title+"</title></head>\n" +
    "<frameset cols=\"156,*\" bordercolor=\"#000000\" border=\"0\">\n<frame src=\"";
  var midhtml = "\" scrolling=\"YES\" bordercolor=\"#FFFFFF\" frameborder=\"NO\" name=\"left\">\n" +
    "<frame src=\"";
  var bothtml = "\" scrolling=\"AUTO\" frameborder=\"NO\" bordercolor=\"#FFFFFF\" name=\"right\">"+
    "</frameset></html>";

  if (navigator.appVersion.indexOf("MSIE 3") >= 0)
  {
    var win = window.open("",winName,settings);
    win.document.open();
    win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
    win.document.close();
    return;
  }

  if (winArray[winName].win == null) {//    alert("New window or was closed programmatically");
    winArray[winName].win = window.open("",winName,settings)
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
    winArray[winName].win.document.close();
  }
  else if (!winArray[winName].win.closed) {//    alert("Already open, write new document");
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
    winArray[winName].win.document.close();
    if (navigator.appVersion.indexOf("MSIE 3") < 0) winArray[winName].win.focus();
  }
  else {//    alert("Closed manually");
    winArray[winName].win = window.open("",winName,settings)
    winArray[winName].win.document.open();
    winArray[winName].win.document.write(tophtml + leftContent + midhtml + rightContent + bothtml);
    winArray[winName].win.document.close();
  }
}

function closePopupWindows(popupArray)
{
if (navigator.appVersion.indexOf("MSIE 3") >= 0) return;

if (popupArray["text"].win != null)
  if (!popupArray["text"].win.closed)
    popupArray["text"].win.close();
popupArray["text"].win = null;

if (popupArray["answer"].win != null)
//if (popupArray["answer"].win)
  if (!popupArray["answer"].win.closed)
    popupArray["answer"].win.close();
popupArray["answer"].win = null;

if (popupArray["grammar"].win != null)
  if (!popupArray["grammar"].win.closed)
    popupArray["grammar"].win.close();
popupArray["grammar"].win = null;

if (popupArray["wordlist"].win != null)
  if (!popupArray["wordlist"].win.closed)
    popupArray["wordlist"].win.close();
popupArray["wordlist"].win = null;

if (popupArray["tape"].win != null)
  if (!popupArray["tape"].win.closed)
    popupArray["tape"].win.close();
popupArray["tape"].win = null;
}

if (document.nl_config) document.nl_config.add("onunload","closePopupWindows(nl_popup);");


/*
 * Decr: get the relative path to javascript folder
 * Return: the relative path to javascript folder
 */
function getPath() {
  var currentPath = document.location + "";
  var ini = currentPath.lastIndexOf("courses");
  if (ini == -1) {
    var ini = currentPath.lastIndexOf("cursos");
    if (ini == -1)
		var ini = currentPath.lastIndexOf("archive");
    	if (ini == -1)
      		alert("Error\njsExerciseXML.js don't found javascript folder.");
  }

  var str = currentPath.substring(ini, currentPath.length-1)

  // Evaluate number of folders to courses
  var nFolders = str.split("/").length - 1;

  // Create de path to javascript folder
  var jsFolderPath = "";
  for (i = 0; i < nFolders; i++)
    jsFolderPath += "../";

  jsFolderPath += "javascript/";

  return jsFolderPath;
}

