function listenForEnter(id,toCall) {
  var box = document.getElementById(id);
  box.onkeyup = function (event) {
    if(!event) event = window.event;
    if(event.keyCode == 13) toCall();
  };
}

function openCommentPane(object_id) {
  window.open('http://map.butterfat.net/comments.php?object_id='+object_id,'','menubar=no,height=600,width=500,resizable=yes,toolbar=no,scrollbars=yes');
}

function confirmLink(question,link) {
  if(confirm(question)) window.location=link;
}

function htmlify(text) {
  text = replaceAll("\\","",text);
  text = replaceAll("&","&amp;",text);
  text = replaceAll("\"","&quot;",text);
  text = replaceAll("'","&#039;",text);
  text = replaceAll(">","&gt;",text);
  text = replaceAll("<","&lt;",text);
  return text;
}

function dehtmlify(text) {
  text = replaceAll("&amp","&",text);
  text = replaceAll("&quot;","\"",text);
  text = replaceAll("&#039;","'",text);
  text = replaceAll("&gt;",">",text);
  text = replaceAll("&lt;","<",text);
  return text;
}

function makeFunctionSafe(text) {
  //while (text.indexOf("\\") > -1) text = text.replace("\\","");
  text = replaceAll("\\","",text);
  text = replaceAll("'","\\'",text);
  text = replaceAll("&#039;","\\'",text);
  return text;
}

function replaceAll(search,replace,subject) {
  var indexes = new Array();
  var index;
  var lastindex=0;

  while((index=subject.indexOf(search,lastindex))!=-1) {
    indexes.push(index);
    lastindex=index+1;
  }

  var diff = search.length - replace.length;

  for(var i=0; i<indexes.length; i++) {
    index = indexes[i]-(i*diff);
    subject=subject.substring(0,index)+replace+subject.substring(index+search.length,subject.length);
  }
  
  return subject;
}

function showTab(show,max) {
  for(var i=1; i<=max; i++) {
    var tab = document.getElementById("tab"+i);
    var link = document.getElementById("link"+i);
    if(i==show) { tab.style.display = "block"; link.style.borderColor="#000000"; link.style.borderBottomColor="#FFFFFF"; link.style.borderBottomWidth="2px"; }
    else { tab.style.display = "none"; link.style.borderColor="#777777"; link.style.borderBottomColor="#000000"; link.style.borderBottomWidth="0px"; }
  }
}

function makeVisible(element) {
  if(element.currentStyle) {
    element.style['display'] = "block";
    element.style['visibility'] = "visible";
  } else {
    element.style.display = "block";
    element.style.visibility = "visible";
  }
}

function makeInvisible(element) {
  element.style.display = "none";
  element.style.visibility = "hidden";
}

function toggleVisibility(element_id) {
  var element=document.getElementById(element_id);
  // IE sucks
  if(element.currentStyle) {
    if(element.style['visibility']=="hidden" || element.style['visibility']=="") makeVisible(element);
    else makeInvisible(element);
  } else {
    if(element.style.visibility=="hidden" || element.style.visibility=="") makeVisible(element);
    else makeInvisible(element);
  }
}

function getRandomString(length) {
  var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var ran = "";
  for(i=0; i<length; i++) ran+=chars.charAt(Math.round(Math.random()*61));
  return ran;
}
