/*
 * Shows the given message using alert if condition is false.
 */
function assert(cond, msg) {
  if (!(typeof(cond) == "boolean")) {
    alert("Function assert(cond,msg) must be called with a boolean for cond.");
  }
  if (!(typeof(msg) == "string")) {
    alert("Function assert(cond,msg) must be called with a string for msg.");
  }
  
  if (!cond) {
    alert(msg);
  }
}


/*
 * Returns a random integer between 0 (including) and m (excluding).
 */
function randomInt(m) {
  assert(typeof(m) == "number", "Function randomInt(m) must be called with a number for m.");
  assert(m > 0, "Function randomInt(m) must not be called mit m = " + m + " <= 0.");
  var result = -1;
  while (result < 0 || result >= m) { // Given no rounding errors, this loop is executed once only...
    result = Math.floor(Math.random() * m);
  }
  return result;
}


/*
 * This function returns a XML HTTP request object for the current browser.
 */
function createXmlHttpRequest() {
  var request = false;
  try {
    request = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (err1) {
    try {
      request = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (err2) {
	    try {
	      request = new XMLHttpRequest();
	    } catch (err3) {
	      request = false;
	    }
    }
  }
  return request;
}


/*
 * Emulates a XML HTTP request by adding a script node to the DOM tree. Some browser require this for cross-site requests.
 * This technique is called AJAST or Ajast (Asynchronous JavaScript and Script Tags).
 */
function emulateXmlHttpRequest(url) {
  assert(typeof(url) == "string", "Function emulateXmlHttpRequest(url) must be called with a string for url.");
  var scriptId = null;
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", url);
  script.setAttribute("id", "script_id");
  scriptId = document.getElementById("script_id");
  if (scriptId) {
    document.getElementsByTagName("head")[0].removeChild(scriptId);
  }
  document.getElementsByTagName("head")[0].appendChild(script);
}


/*
 * This function toggles the publications' abstracts.
 */
function togglePublicationAbstract(bibtexkeyWeb) {
  assert(typeof(bibtexkeyWeb) == "string", "Function togglePublicationAbstract(bibtexkeyWeb) must be called with a string for bibtexkeyWeb.");
  var trAbstract = document.getElementById(bibtexkeyWeb + "_abstract");
  var aAbstractLink = document.getElementById(bibtexkeyWeb + "_abstract-link");
  if (trAbstract.style.display == "none") {
    trAbstract.style.display = "";
    aAbstractLink.innerHTML = "[&minus;] Abstract";
  } else {
    trAbstract.style.display = "none";
    aAbstractLink.innerHTML = "[+] Abstract";
  }
}


/*
 * This function toggles the publications' BibTeX.
 */
function togglePublicationBibTeX(bibtexkeyWeb) {
  assert(typeof(bibtexkeyWeb) == "string", "Function togglePublicationBibTeX(bibtexkeyWeb) must be called with a string for bibtexkeyWeb.");
  var trBibtex = document.getElementById(bibtexkeyWeb + "_bibtex");
  var aBibtexLink = document.getElementById(bibtexkeyWeb + "_bibtex-link");
  if (trBibtex.style.display == "none") {
    trBibtex.style.display = "";
    aBibtexLink.innerHTML = "[&minus;] BibTeX";
  } else {
    trBibtex.style.display = "none";
    aBibtexLink.innerHTML = "[+] BibTeX";
  }
}


/*
 * These funcation provide mappings from month numbers to month names or abbreviations and vice-versa.
 */     
// var MONTH_NAMES = new Array();
// MONTH_NAMES.push(new Array("January"  , "january"  , "Jan", "JAN", "jan"));
// MONTH_NAMES.push(new Array("February" , "february" , "Feb", "FEB", "feb"));
// MONTH_NAMES.push(new Array("March"    , "march"    , "Mar", "MAR", "mar"));
// MONTH_NAMES.push(new Array("April"    , "april"    , "Apr", "APR", "apr"));
// MONTH_NAMES.push(new Array("May"      , "may"      , "May", "MAY", "may"));
// MONTH_NAMES.push(new Array("June"     , "june"     , "Jun", "JUN", "jun"));
// MONTH_NAMES.push(new Array("July"     , "july"     , "Jul", "JUL", "jul"));
// MONTH_NAMES.push(new Array("August"   , "august"   , "Aug", "AUG", "aug"));
// MONTH_NAMES.push(new Array("September", "september", "Sep", "SEP", "sep"));
// MONTH_NAMES.push(new Array("October"  , "october"  , "Oct", "OCT", "oct"));
// MONTH_NAMES.push(new Array("November" , "november" , "Nov", "NOV", "nov"));
// MONTH_NAMES.push(new Array("December" , "december" , "Dec", "DEC", "dec"));

// function monthNameToNumber(name) {
  // assert(typeof(name) == "string", "Function monthNameToNumber(name) must be called with a string for name.");
  // assert(MONTH_NAMES.length == 12, "Length of MONTH_NAMES is not 12.");
  // for (var i = 0; i < MONTH_NAMES.length; i++) {
    // var names = MONTH_NAMES[i];
    // for (var j = 0; j < names.length; j++) {
      // if (name == names[j]) {
        // return i + 1; // Return number between 1 and 12, not 0 and 11.
      // }
    // }
  // }
  // return -1;
// }

