//
// Copyright (c) 2007-2010 Textensor Limited, creators of publicationslist.org
//
// For licensing info / suggestions / problems
// contact support@publicationslist.org
//
//

// The schema of bibtex in JS
var helpByArticleType = {
  "article": "An article from a journal or magazine.",
  "book" : "A book with an explicit publisher.",
  "booklet" : "A work that is printed and bound, but without a named publisher or sponsoring institution.",
  "inbook" : "A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.",
  "incollection" : "A part of a book having its own title.",
  "inproceedings" : "An article in a conference proceedings.",
  "manual" : "Technical documentation.",
  "mastersthesis" : "A Master's thesis.",
  "misc" : "For use when nothing else fits.",
  "phdthesis" : "A Ph.D. thesis.",  
  "proceedings" : "The proceedings of a conference.",
  "techreport" : "A report published by a school or other institution, usually numbered within a series.",  
  "unpublished" : "A document having an author and title, but not formally published."
};

var displayType = {
  "article": "Journal articles",
  "book" : "Books",
  "booklet" : "Booklets",
  "inbook" : "Book chapters", 
  "incollection" : "Book chapters", 
  "inproceedings" : "Conference papers",
  "manual" : "Technical manuals",
  "mastersthesis" : "Masters theses",
  "misc" : "Other",
  "phdthesis" : "PhD theses",  
  "proceedings" : "Conference proceedings", 
  "techreport" : "Technical reports", 
  "unpublished" : "Other"
};

// List of fields by type, in order of display
// everything has author, year, note, key
var commonFields = ["abstract", "note", "tags", "url", "doi"];
  
// unused fields: "eid", "annote", "archive", "key"
var fieldsByType = {
  "article" : ["author", "year",  "title", "journal", "volume", "number", "pages", "month"],
  "book" : ["author", "year", "title",  "editor", "address", "publisher", "volume", 
            "series", "edition", "isbn"],
  "booklet" : ["author", "year", "title", "howpublished", "address"],
  "inbook" : ["author", "year", "title", "chapter", "editor", "pages", "address", "publisher", "volume", "series", "edition", "isbn"],
  "incollection" : [ "author", "year", "title", "booktitle", 						
                     "editor", "pages", "organization", "address", "publisher", "isbn"],
  "inproceedings" : ["author", "year", "title", "booktitle", "editor", "pages", "organization", "address", "publisher"],
  "manual" : [ "author", "year", "title", "organization", "address", "edition"],  
  "mastersthesis" : ["author", "year", "title", "school", "address"],
  "misc" : ["author", "year", "title", "howpublished", "sectionheading"],
  "phdthesis" : ["author", "year", "title", "school", "address"],
  "proceedings" : ["title", "year", "editor", "address", "publisher", "organization"],
  "techreport" : ["author", "year", "title", "institution", "series", "number",  "address"],
  "unpublished" : ["author", "year", "title"]
};

// rot images by type *** TODO
var imagesByType = {
  "article" : "laf/r-ja.png",
  "book" : "laf/r-book.png",
  "booklet" : "laf/r-booklet.png",
  "inbook" : "laf/r-chapter.png",
  "incollection" : "laf/r-chapter.png",
  "inproceedings" : "laf/r-conf.png",
  "manual" : "laf/r-manual.png",
  "mastersthesis" : "laf/r-thesis.png",
  "misc" : "laf/r-other.png",
  "phdthesis" : "laf/r-thesis.png",
  "proceedings" : "laf/r-proceedings.png",
  "techreport" : "laf/r-report.png",
  "unpublished" : "laf/r-other.png"
};

// A style.
var plosStyle = {
  "year" : { "pre":"(", post:")"}, 
  "title" : { "pre": "&nbsp;", "post":"&nbsp;" },
  "volume" : { "post":":" },
  "number" : { "post":"." },
  "address" : { "post": ":" },
  "url" : { "pre": "Available:" },
  "booktitle" : { "pre": "In:" },
  "editor" : { "pre": "Edited by:", "post" : ". " },
  "doi" : { "pre": "doi:" },
  "sectionheading" : { "pre": "[", "post":"]" },
  "isbn" : { "pre" : "isbn:"}
};

// Form style for fields
var lengthByField = {
  "journal" : 40,
	"howpublished" : 40,
	"address" : 80,
	"author" : 79, // never share a line
	"title" : 240,
	"month" : 5,
	"year" : 5,
	"booktitle" : 80,
	"volume" : 4,
	"number" : 5,
	"series" : 40,
	"editor" : 60,
	"publisher" : 60,
	"school" : 80,
	"organization" : 80,
	"chapter" : 5,
	"edition" : 10,
	"pages" : 10,
  "institution"  : 40,
  "type" : 40,
  "isbn" : 10
	};


var helpByField = {
  "journal" : "The name of the journal",
  "howpublished" : "How something strange has been published. The first word should be capitalized.",
  "address" : "The city or address of the publisher or other type of institution.",
  "author" : "Use commas for multiple authors, e.g. Alex N. Other, Joe Bloggs, Jane Doe",
  "title" : "The title of the article",
  "month" : "The month in which the work was published or, for an unpublished work, in which it was written.",
  "year" : "e.g. 2007",
  "booktitle" : "The title of the book",
  "volume" : "The volume of a journal or multi-volume book.",
  "number" : "The number of a journal, magazine, technical report, or of a work in a series.",
  "series" : "The name of a series or set of books.",
  "editor" : "Name(s) of editor(s), separated by commas",
  "publisher" : "The publisher's name.",
  "school" : "The name of the school where a thesis was written.",
  "organization" : "The organization that sponsors a conference or that publishes a manual.",
  "chapter" : "A chapter (or section or whatever) number",
  "edition" : "The edition of a book - for example, 'Second'.",
  "pages" : "One or more page numbers or range of numbers, such as 42-111 or 7,41,73-9710",
  "institution"  : "The sponsoring institution of a technical report.",
  "type" : "The type of a technical report - for example, 'Research Note'",
  "annote" : "Annotation describing the significance of the publication."
}; 



var optionalFields = {
  "number" : 1,
  "pages" : 1,
  "organization" : 1,
  "address" : 1,
  "publisher" : 1,
  "editor" : 1,
  "series" : 1,
  "edition" : 1,
  "month" : 1,
  "isbn" : 1
  
};


var hideIfCollapsed = {
  "abstract" : 1,
  "note" : 1,
  "tags" : 1
};

function getDisplayName(field, atype) {
  var dname = displayNameByField[field];

  if (atype=='inproceedings' && field=='booktitle') { return "Proceedings title"; }
  else if (atype=='incollection' && field=='title') { return "Chapter title"; }
  else return dname;
}

function isOptional(field, atype) {
  if (field=='volume' && (atype=='book' || atype=='inbook')) { return true; }
  return optionalFields[field];
}


var displayNameByField = {
  "journal" : "Journal",
	"howpublished" : "How Published",
	"address" : "Address",
	"volume" : "Volume",
	"author" : "Authors",
	"title" : "Title",
	"month" : "Month",
	"year" : "Year",
	"number" : "Number",
	"series" : "Series",
	"chapter" : "Chapter",
	"editor" : "Edited by",
	"publisher" : "Publisher",
	"school" : "School",
    "booktitle" : "Book Title",
	"organization" : "Organization",
	"edition" : "Edition",
	"pages" : "Pages",
	"type" : "Type of publication",
	"institution"  : "Institution",
  "isbn" : "ISBN"
};

function findLinkEnd(str, pos) {
  var end = str.length;
  var found = false;
  for (var i=pos; !found && i<str.length; i++) {
    var ch = str.charAt(i);
    if (ch==' ' || ch=="\n" || ch=="\r" || ch=="\t") {
      end = i;
      found = true;
    }
  }
  return end;
}

// Linkify plain text
function linkify(txt) {  
  
  var ret = "";
  do {
    var pos = txt.indexOf("http://", 0);
    if (pos != -1) {
      var epos = findLinkEnd(txt, pos);
      var link = txt.substr(pos, epos - pos);
      ret += txt.substr(0, pos);
      ret += " <a href='"+link+"'>"+link+"</a> ";
      txt = txt.substr(epos);
    }
    else {
      ret += txt;
    }
  } while (pos != -1);

  return ret;
  
}

function getDisplayType(atype) {
  if (displayType[atype]) { return displayType[atype]; }
  return "Misc";
}

function trim(str) {
  if (!str) { return ""; }
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function splitTags(tags) {
  var commasplit =  tags.split(",");
  if (tags.indexOf(";") != -1) {
    var semisplit =  tags.split(";");
    if (semisplit.length > commasplit.length) { return semisplit; }
  }
  return commasplit;
}



// Is str a valid surname?
function validSurname(str) {
  if (str.length <=1) { return false; }  
  var ich = str.substr(0,1);  
  //  alert("ICH is "+ich);
  if (ich == ich.toUpperCase()) {
	if (str.length > 3) { 
	  return true; 
	}
	if (str != str.toUpperCase()) {
	  return true;
	}
  }  
  return false;  
}

function validInitials(str) {
  if (str == "") { return true; }
  var init = str.substr(0,1);
  var sameupper = (str == str.toUpperCase());
  var samelower = (str == str.toLowerCase());
  if (sameupper || samelower) {
	if (str.length < 4) {
	  return true;
	}
  }
  return false;
}

function appendSpace(ostr, nstr) {
  if (nstr == "") { return ostr; }
  if (ostr != "") { return ostr + " "+ nstr; }
  return nstr;
}

function prependSpace(ostr, nstr) {
  if (nstr == "") { return ostr; }
  if (ostr != "") { return nstr + " "+ ostr; }
  return nstr;
}


// Convert author name to obj : first: middle: last
function newSplitAuthor(prevauthor, author) {
  author = author.replace(/\./g, ' ');
  
  var words = author.split(" ");

  // Remove empty words and brackets
  var words2 = new Array();
  var ignore = 0;
  for (var i=0; i<words.length; i++) {
	var w = trim(words[i]);
	//	alert("word "+w);
	if (w != "") {
	  if (w.indexOf("(") != -1) {
		ignore ++;
	  }
	  else if (w.indexOf(")") != -1) {
		ignore --;
	  }
	  else if (ignore <= 0) {
		words2[words2.length] = w;
	  }
	}
  }

  words = words2;
  var num = words.length;
  //  alert("words "+words);

  if (num == 0) { return false; } // or "" "" "" ? 
  
  var first = "";
  var middle = "";
  var last = "";
  
  if (words.length > 0) { last = words[words.length -1]; }
  var lastvalid = validSurname(last);
  var lastvalidinitial = validInitials(last);

  var allinitials = true;
  for (var i=0; i<num; i++) {
	if (!validInitials(words[i])) {
	  allinitials = false;
	}
  }

  if (num == 1) {
	if (lastvalid || prevauthor == false || !lastvalidinitial || prevauthor.first != prevauthor.first.toLowerCase()) {
	  var ret = {"first" : first, "middle" : middle, "last" : last };
	  return ret;
	}
	else {
	  prevauthor["first"] = prependSpace(prevauthor["first"], last);
	  prevauthor["extended"] = true;
	  return prevauthor;
	}
  }
  else {
	first = words[0];
	firstvalid = validSurname(first);
	if (firstvalid && !lastvalid) {
	  // we're looking at Howell F W - do a swap.
	  var sncount = 1;
	  for (var n = 1; n < num; n++) {
		if (validSurname(words[n])) {
		  sncount = n + 1;
		}
		else {
		  break;
		}
	  }
	  var newwords = new Array();
	  for (var n = sncount; n < num; n++) {
		newwords[newwords.length] = words[n];
	  }
	  for (var n=0; n<sncount; n++) {
		newwords[newwords.length] = words[n];
	  }
	  words = newwords;
	  first = words[0];
	  last = words[num-1];	  
	}
	else if (allinitials && prevauthor != false && prevauthor.first == prevauthor.first.toLowerCase()) {
	  var extra = words[0];
	  for (var n=1; n<num; n++) {
		extra = appendSpace(extra, words[n]);
	  }
	  prevauthor["first"] = prependSpace(prevauthor["first"], extra);
	  prevauthor["extended"] = true;
	  return prevauthor;
	}
  }

  // OK, now just regular...

  if (words.length > 1) { 
	first = words[0]; 
  } 
  if (words.length > 2) { 
    var midstart = 1;
    var midend = words.length - 2;
    
    var mid = words[words.length-2]; 
    if (mid == "Dalla" || mid == "De" || mid == "Do" || mid == "Der" || mid == "La" || mid == "Le" || mid == "Van" || mid == "Von" || mid == mid.toLowerCase()) {
      last = mid+" "+last;
      midend --;
    }

    // Check for van der ...
    if (words.length > 3) {
      var mid = words[words.length-3];
      if (mid == "De" || mid == "Van" || mid == "Von" || mid == mid.toLowerCase()) {
        last = mid+" "+last;
        midend --;
      }      
    }
    
    if (midstart <= midend) {
      for (var i=midstart; i<=midend; i++) {
		if (middle != "") { middle += " "; }
		middle += words[i];
      }
    }
  }

  // Check for 1st name all caps and no middle
  if (validInitials(first) && middle == "" && first == first.toUpperCase()) {
	var rest = first;
	var init = first.substr(0,1);
	first = init;
	do {
	  var rest = rest.substr(1);
	  if (rest != "") {
		init = rest.substr(0,1);
		if (init) {
		  middle = appendSpace(middle, init);
		}
	  }
	  
	} while (rest);
  }
  
  var ret = {"first" : first, "middle" : middle, "last" : last };
  return ret;
}


// Convert author name to obj : first: middle: last
function authorNameToObj(str) {
  return newSplitAuthor(false, str);
}

 // Convert an author string to an array of objs
function authorStringToArray(str) {
  str = str.replace(/[\&\;]/g, ",");
  str = str.replace(/[\:\.]/g, " "); // do allow - sign
  str = str.replace(/\ and\ /g, ",");
  //  alert(str);
  var list = splitTags(str); 
  var ret = new Array();

  var prevauthor = false;

  for (var j=0; j<list.length; j++) {
    var name = trim(list[j]);
	if (name) {
	  var nextauthor = newSplitAuthor(prevauthor, name);
	  if (typeof(nextauthor.extended) != "undefined") {
		ret[ret.length -1] = nextauthor;
		prevauthor = false;
	  }
	  else if (nextauthor) {
		ret[ret.length] = nextauthor;
		prevauthor = nextauthor;
	  }
	}
  }
  return ret;
}


function removeChildren(el) {
  if (el) {
    while (el.firstChild) {
      el.removeChild(el.firstChild);
    }
  }
}

function divInnerHtml(el, txt) {
  removeChildren(el);
  var sp = document.createElement("div");
  sp.innerHTML = txt;
  el.appendChild(sp);
}

function spanInnerHtml(el, txt) {
  if (el) {
	removeChildren(el);
	var sp = document.createElement("span");
	sp.innerHTML = txt;
	el.appendChild(sp);
  }
}

// Handle toggle refs without interfering with selection.
var dragged = false;

function refDown() {
  dragged = false;
}
function refDrag() {
   dragged = true;
}
var idx = 0;
function toggleIdx() {
  toggleRef(idx);
}
function refUp(i) {
  if (!dragged) {
    idx = i;
    // Do it in timeout thread or get selection problems.
    setTimeout(toggleIdx, 10);
  }
}


// Note: a is URI encoded
function visitAuthor(a) {
  //  alert("got "+a);
  if (!dragged) {
    refDrag(); // cancel toggle...
    parent.location = rootpath+"php/authors.php?a="+a;
  }
}

// Convert authors to index links.
function linkifyAuthors(authors) {
  var ary = authorStringToArray(authors);
  var ret = "";
  for (var i=0; i<ary.length; i++) {
    var f = ary[i]["first"];
    var m = ary[i]["middle"];
    var l = ary[i]["last"];
    var link = l;
    if (m!="") { link = m+"_"+link; }
    if (f!="") { link = f+"_"+link; }
    var cma = "";
    if (i<ary.length-1) { cma = ", "; }
	var enclink = encodeURIComponent(link);
	enclink = enclink.replace(/\'/g, "%27"); // ' 
    ret += "<a class='authorlink' title='Open author index' href='javascript:visitAuthor(\""+enclink+"\")'>"+f+" "+m+" "+l+"</a>"+cma; // "</span><span class='author'>";
  }
  return ret;
}

// Fix a URL entered without http://
function httpPrefix(url) {
  if (url) {
	var init = url.substr(0,4);
	if (init != "http" && init != "ftp:") {
	  return "http://"+url;
	}
  }
  return url;
}

// Variable for tag show / hide.
var tagshowidx = 1;

// Convert a ref object to HTML.
function refToHtml(atype, ref, expanded, userid, rootpath, toggleIdx) {
  if (!atype) atype="article";
  var style = plosStyle;
  var txt = "";
  var fields = fieldsByType[atype];
  var first = true;

  //  txt += ".";


  // Do the common fields.
  txt += "<div class='fulltextlinks'>\n"; // style='float:right; padding-left : 12px; width: 64px;'>";

  if (ref["urllink"]) {
	txt += " <a class='htmllink' title='View HTML version' href='"+httpPrefix(ref.urllink)+"'><img src='"+rootpath+"page-sml.png'/></a>";
  }

  if (ref["pdflink"]) {
	txt += " <a class='pdflink' title='View PDF version' href='"+httpPrefix(ref.pdflink)+"'><img src='"+rootpath+"pdf-sml.png'/></a>";
  }
  var attachmenturl = "";

  if (ref["attachment"]) {    
    var ext = ref.attachment.lastIndexOf(".");
    var str = "";
	var tp = ""; 
    if (ext!=-1) { tp = ref.attachment.substr(ext+1); }
	
	str += tp;
	if (tp == "pdf") {
	  str = "<img src='"+rootpath+"pdf-sml.png'/>";
	}
	else {
	  str = "[" + str +"]";
	}

	txt += " <a class='pdflink' title='View local version' href='";
	
	if (ref.attachment.substring(0,4)=="http") {
	  attachmenturl = ref.attachment;
	}
	else {
	  attachmenturl = rootpath+"data/"+userid+"/"+ref.attachment;
	}
	txt += attachmenturl;
	txt += "'>"+str+"</a>";
  }
  
  if (ref["doi"]) {
    var href = ref.doi;
    if (ref.doi.substr(0,4)!="http") {
      href = "http://dx.doi.org/"+ref.doi;
    }
    txt += " &nbsp;<br/><a target='_blank' class='doilink' title='DOI: "+ref.doi+"' href='"+href+"'>DOI</a>&nbsp;";
  }

  if (ref["pubmed"]) {
    var href = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pubmed&cmd=Retrieve&dopt=AbstractPlus&list_uids="+ref.pubmed;

    txt += " &nbsp;<br/><a target='_blank' class='pubmedlink' title='PubMed ID : "+ref.pubmed+"' href='"+href+"'>PMID</a>&nbsp;";
  }
  if (ref["repostatus"]=="accepted") {
    txt += " &nbsp;<br/><a target='repository' class='repolink' title='View Repository Copy' " +
      "href='"+ref.repourl+"'>Repository</a>&nbsp;";
  }
  txt += "</div>";


  // Is there a thumbnail?
  if (ref._thumb) {
    if (typeof(nothumbs)!="undefined" && nothumbs) {} else {
    txt += "<div class='thumbdiv'>"; 
    if (ref.attachment) {
      txt += " <a target='_blank' class='pdflink' title='View local version' href='"+attachmenturl+"'>";
    }


    var thumburl = ref._thumb;
    if (thumburl.substring(0,4) != "http") {
      // full refid can be user-123
      var refnum = "" + ref.refid; if (refnum.indexOf("-")!==-1) { refnum = refnum.substr(refnum.lastIndexOf("-")+1); }

      thumburl = rootpath+"data/"+userid+"/ref-"+refnum+"/"+ref._thumb;
    }
    txt += "<img width='80px' class='thumbimg' src='"+thumburl+"'/>";
    if (ref.attachment) {
      txt += "</a>";
    }
    txt += "</div>";
    }
  }


  /*
  if (toggleIdx != -1) {
    txt += "<span class='refbody' onmousedown='refDown()' onmousemove='refDrag()' onmouseup='refUp("+toggleIdx+")'>";
  }
  else {
  */
  txt += "<span class='refbody'>";
  //}
  for (var j=0; j<fields.length; j++) {
	var fn = fields[j];

	if (ref[fn]) {
	  if (!first) {
	    txt += " "; 
	  }
	  	 
	  var entry = trim(ref[fn]);
	  
	  if (entry) {
		if (fn=="author") {
		  entry = linkifyAuthors(entry);
		}
		var pre = "";
		var post = " ";
		if (style[fn]) { 
		  if (style[fn]["pre"]) { pre = style[fn].pre; }
		  if (style[fn]["post"]) { post = style[fn].post; }
		}
		// work-around for IE6 bug
		if (fn != "author") {// && document.all && !window.XMLHttpRequest) { 
		  // Special case for IE6
		  if (fn=="year" || fn=="month" || fn=="institution" || fn=="organization" || fn=="publisher" || fn=="series" || fn=="pages" || fn=="address" || fn=="number") { pre = " "+pre; } 
		  var ocl = " onclick='toggleRef("+toggleIdx+")'";
		  if (fn == "title") {
		    var words = entry.split(" ");
		    txt +=  "<a name='x'"+ocl+" class='ax "+fn+"'>"+pre+"</a>";
		    for (var k=0; k<words.length; k++) {
		      txt += "<a name='x'"+ocl+" class='ax "+fn+"'>"+words[k]+"</a> ";
		    }
		    txt +=  "<a name='x'"+ocl+" class='ax "+fn+"'>"+post+"</a>";
		  }
		  else {
		    txt += "<a name='x' class='ax "+fn+"'>"+pre+entry+post+"</a> ";
		  }
		}
		else {
		  txt += entry+" "; // "<a name='x' class='"+fn+"'>"+pre+entry+post+"</a> ";
		}
		//	if (document.all) { txt += "&nbsp;"; }
		first = false;
	  }
	}
  }

  txt += "&nbsp; </span>"; // end of the onclick area. nbsp needed or IE mucks up display


  if (expanded) {
	if (ref["tags"]) {
	  var list = splitTags(ref["tags"]);
	  txt += " <div class='tags'>";
	  var maxtags = 3;

	  for (var k=0; k<list.length; k++) {
		var tag = trim(list[k]);
		var cma = ""; if (k<list.length-1) { cma = ",&nbsp;&nbsp; "; }

		if (k == maxtags && list.length > maxtags) {
		  txt += "<a class='atag layout' id='sata"+tagshowidx+"' href='javascript:showAllTags("+tagshowidx+")'><i>(all "+list.length+" tags)</i></a> <span style='display:none' class='layout' id='sats"+tagshowidx+"'>";
		  tagshowidx ++;
		}

		// if (k!=0) { txt += ",&nbsp;&nbsp; "; }
		txt += " <span><a class='atag layout' href='javascript:showTag(\""+tag+"\")'>"+tag+"</a>"+cma+"</span>";
	  }

	  if (list.length > maxtags) {
		txt += "</span>";
	  }
	  txt += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
	}
	
    if (ref["abstract"]) {
      txt += " <div class='abstract layout'>Abstract: "+ref["abstract"]+"</div>"; // *** need to htmlentities...
    }
    
    if (ref["note"]) {
      txt += " <div class='note layout'>Notes: "+linkify(ref["note"])+"</div>"; // *** need to htmlentities...
    }


    if (ref.repostatus || ref.repocollections) {
      txt += " <div class='note layout'>";
      if (ref["repostatus"] == "harvested") {
        txt += "This publication has been harvested by the repository and is being checked. ";
      }
      else if (ref["repostatus"] == "accepted") {
        txt += "This publication is available from the repository: ";

	if (ref["repoid"]) {
	  txt += "<a class='repolink' target='repository' href='"+ref.repourl+"'>Repository entry: "+ref.repoid+"</a> <br/>";
	}
      }
      else if (ref["repostatus"] == "rejected") {
        txt += "This publication was not accepted by the repository: ";
      }
      else if (ref["repostatus"] == "error") {
        txt += "There was a harvesting error for this item.";
      }
      else if (ref["repocollections"]!="" 
	       && ref["repocollections"]!="-"
	       && ref["attachment"]!="") {
        txt += "This publication is available for automated harvesting by the repository";
      }
	       
      
      txt += "</div>";
    }    
    
  }

  txt += "<div style='clear:right'></div>";


  return txt;
}

function showAllTags(idx) {
  var ael = document.getElementById('sata'+idx);
  var sel = document.getElementById('sats'+idx);
  if (ael) {
	ael.style.display = "none";
  }
  if (sel) {
	sel.style.display = "inline";
  }
}

function forceRepaintCB() {
  var el = document.getElementById("publistdiv");
  if (false && el) {
	document.body.removeChild(el);
	//	  alert("Removing bug");
  }
  else if (el) {	  
	var div = document.createElement("span");
	div.id = "iebug";
	div.innerHTML = "&nbsp;";
	el.style.display = "none";
	el.style.display = "block";
	//	alert("Adding pl  bug");
	el.appendChild(div);
  }
}

function forceRepaint() {
  if (document.all) {
	setTimeout(forceRepaintCB, 100);	
  }
}
