/* ********** ********** ********** AJAX Functionality ********** ********** ********** */
var ajax;

function GetAjaxObject() {
  var xmlHttp = null;

  try { xmlHttp = new XMLHttpRequest();
  } catch (e) { try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  } return xmlHttp;
}

function genericAppendContent() {
  if(ajax.readyState == 4) {
    if(ajax.status == 200) {
      document.getElementById("message").innerHTML = "</p>executed!</p>";
      document.getElementById("content").innerHTML += ajax.responseText;
      setTimeout("cleanMessage()", 2500);
    } else {
      document.getElementById("message").innerHTML = "</p>ERROR: It was impossible to get the server response</p>";
      setTimeout("cleanMessage()", 2500);
    }
  }
}

function genericAddContent() {
  if(ajax.readyState == 4) {
    if(ajax.status == 200) {
      document.getElementById("message").innerHTML = "</p>executed!</p>";
      document.getElementById("content").innerHTML = ajax.responseText;
      setTimeout("cleanMessage()", 2500);
    } else {
      document.getElementById("message").innerHTML = "</p>ERROR: It was impossible to get the server response</p>";
      setTimeout("cleanMessage()", 2500);
    }
  }
}

function genericSender(params, message, add) {
  ajax = GetAjaxObject();

  if(ajax == null) {
    document.getElementById("message").innerHTML = "<p>Your browser does not support AJAX!</p>";
    setTimeout("cleanMessage()", 2500);
  } else {
    document.getElementById("message").innerHTML = message;
    ajax.open("POST", "http://p-bodydb.sjcg.es/AjaxResponse.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    if(add) {
      ajax.onreadystatechange = genericAddContent;
    } else {
      ajax.onreadystatechange = genericAppendContent;
    }
    ajax.send(params);
  }
}
/* ********** ********** ********** ********** ********** ********** ********** ********** */

/* ********** ********** ********** ********** ********** ********** ********** ********** */

function manageMenu(layer) {

  if(document.getElementById(layer).style.visibility == 'hidden') {
    document.getElementById(layer).style.height = 'auto';
    document.getElementById(layer).style.visibility = 'visible';
    document.getElementById(layer).style.marginTop = '-15px';
  }
  else {
    document.getElementById(layer).style.height = '0px';
    document.getElementById(layer).style.visibility = 'hidden';
    document.getElementById(layer).style.marginTop = '0px';
  }
}

function closeMenu(layer) {
  if(document.getElementById(layer).style.visibility != 'hidden') {
    document.getElementById(layer).style.height = '0px';
    document.getElementById(layer).style.visibility = 'hidden';
    document.getElementById(layer).style.marginTop = '0px';
  }
}

function resetEntry(user, pasd) {
  document.getElementById(user).value = "";
  document.getElementById(pasd).value = "";
  document.getElementById(user).focus();
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */
function cleanMessage() {
  document.getElementById("message").innerHTML = '';
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */
function checkSubmission(event, formNumber) {

  if(event.keyCode == 13) {
    switch(formNumber) {
      case "1":
        search();
        break; 
      case "2":
        validateUser();
        break;
      case "3":
        addAction();
        break;
    }
  } else if(event.keyCode == 27) {
    switch(formNumber) {
      case "2":
        manageMenu('login');
        resetEntry('user', 'passwd');
        break;
    }
  }
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

/* ********** ********** ********** Users Management ********** ********** ********** */

function checkLogin() {
  if(ajax.readyState == 4) {
    if(ajax.status == 200) {
        if(ajax.responseText != '0') {
          document.getElementById("message").innerHTML = "<p>Welcome to PbodyDB</p>";
          document.getElementById("toplinks").innerHTML = ajax.responseText;
        } else {
          document.getElementById("message").innerHTML = "<p>ERROR: User or/and pass not valid</p>";
        }
    } else {
      document.getElementById("message").innerHTML = "</p>ERROR: It was impossible to get the server response</p>";
    }
    setTimeout("cleanMessage()", 2500);
  }
  showMain();
}


function validateUser() {

  ajax = GetAjaxObject();
  if(ajax == null) {
    document.getElementById("message").innerHTML = "<p>Your browser does not support AJAX!</p>";
    setTimeout("cleanMessage()", 2500);
  } else {
    manageMenu('login');
    closeMenu('AdvSearch');

    var params = "u=" +  document.getElementById("user").value + "&p=" + document.getElementById("passwd").value;
    resetEntry('user', 'passwd');
    document.getElementById("message").innerHTML = "<p>Checking ...</p>";

    ajax.open("POST", "http://p-bodydb.sjcg.es/AjaxResponse.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.onreadystatechange=checkLogin;
    ajax.send(params);
  }
}

function logout(){

  ajax = GetAjaxObject();
  if(ajax == null) {
    document.getElementById("message").innerHTML = "<p>Your browser does not support AJAX!</p>";
    setTimeout("cleanMessage()", 2500);
  } else {
    closeMenu('AdvSearch');

    var params = "destroySession=True";
    resetEntry('user', 'passwd');
    document.getElementById("message").innerHTML  = "<p>See you ...</p>";
    document.getElementById("toplinks").innerHTML = "you are logged as <b>anonymous user</b> | <a onClick = \"javascript:manageMenu('login');\">login</a>";
    
    setTimeout("cleanMessage()", 2500);

    ajax.open("POST", "http://p-bodydb.sjcg.es/AjaxResponse.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", params.length);
    ajax.setRequestHeader("Connection", "close");
    ajax.send(params);
  }
  showMain();
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

/* ********** ********** ********** Group-Class Management ********** ********** ********** */

function getClass() {
  if(ajax.readyState == 4) {
    if(ajax.status == 200) {
        cleanMessage();
        document.getElementById("Class").innerHTML = ajax.responseText;
    } else {
      document.getElementById("message").innerHTML = "</p>ERROR: It was impossible to get the server response</p>";
      setTimeout("cleanMessage()", 2500);
    }
  }
}

function selectClass(index) {

  if(index == 0) {
    var txt = '<select disabled id="Class2Group" style="width: 300px;"><option value=\"0\" selected>All Classes</option></selected>';
    document.getElementById("Class").innerHTML  = txt;
  } 
  else {
    ajax = GetAjaxObject();
    if(ajax == null) {
      document.getElementById("message").innerHTML = "<p>Your browser does not support AJAX!</p>";
      setTimeout("cleanMessage()", 2500);
    } else {
      var params = "gr=" + index;

      document.getElementById("message").innerHTML = "<p>Loading Classes ...</p>";
      ajax.open("POST", "http://p-bodydb.sjcg.es/AjaxResponse.php", true);
      ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      ajax.setRequestHeader("Content-length", params.length);
      ajax.setRequestHeader("Connection", "close");
      ajax.onreadystatechange = getClass;
      ajax.send(params);
    }
  }
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */


/* ********** ********** ********** ********** ********** ********** ********** ********** */

function search() {

  closeMenu('login');
  closeMenu('AdvSearch');

  var params = "pr=" + document.getElementById("protein").value + "&gr=" + document.getElementById("Group").value;
  params += "&cl=" + document.getElementById("Class2Group").value;
  genericSender(params, "<p>Searching...</p>", true);
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

function displayEntry(entry) {

  closeMenu('login');
  closeMenu('AdvSearch');

  var params = "ent=" + entry;
  genericSender(params, "<p>Recovering entry...</p>", true);
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

function addAction() {

  closeMenu('login');
  closeMenu('AdvSearch');
  closeMenu('newAction');

  var params = "act=" + document.getElementById("newAct").value;
  genericSender(params, "<p>Adding action...</p>", true);
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

function displayAct(action) {

  closeMenu('login');
  closeMenu('AdvSearch');

  var params = "getAct=" + action;
  genericSender(params, "", true);
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */

function showFAQ() {

  var txt  = "<h2>FAQ - Frequently Asked Questions</h2><p><b>How can I perform a simple search?</b></p><p>Either the protein ";
      txt += "name (e.g. DcpS) or Uniprot ID (e.g. Q96C86) is enough.</p><p><b>Which are the options available in the Advanced ";
      txt += "section?</b></p><p>Results can be restricted by specifying P body Group, P body Class, or by simply providing a ";
      txt += "fasta sequence in order to execute a blast search. This option is still under development. Sorry for the inconvenience.";
      txt += " When being functional, the threshold will be available for regulation.</p><p><b>Why is the threshold useful?</b></p><p>";
      txt += "The expectation value (E) threshold is a statistical measure of the number of expected matches in a random database.";
      txt += "The lower the e-value, the more likely the match is to be significant. E-values between 0.1 and 10 are generally dubious,";
      txt += " and over 10 are unlikely to have biological significance. In all cases, those matches need to be verified manually.";
      txt += "You may need to increase the E threshold if you have a very short query sequence, to detect very weak similarities,";
      txt += " or similarities in a short region.</p><p><b>How can I register?</b></p><p>Free registration is not possible at the moment.";
      txt += " Please <a href = 'mailto:scapella@crg.es'>contact</a> for further information.</p>";

  document.getElementById("content").innerHTML = txt;
}

function showMain() {

  var txt  = "<h2>General information</h2><img src=\"./images/pbodies.jpg\" class=\"photo\" /><p>Post-transcriptional ";
      txt += "processes have a central role in the regulation of eukaryotic gene expression. Although it has been known ";
      txt += "for a long time that these processes are functionally linked, often by the use of common protein factors, ";
      txt += "it has only recently become apparent that many of these processes are also physically connected. Indeed, ";
      txt += "proteins that are involved in mRNA degradation, translational repression, mRNA surveillance and RNA-mediated ";
      txt += "gene silencing, together with their mRNA targets, colocalize within discrete cytoplasmic domains known as <b>";
      txt += "P bodies</b>. The available evidence indicates that P bodies are sites where mRNAs that are not being translated ";
      txt += "accumulate, the information carried by associated proteins and regulatory RNAs is integrated, and their fate &#8212; ";
      txt += "either translation, silencing or decay &#8212; is decided.</p><p><small><b>Eulalio, A., Behm-Ansmant, I. and Izaurralde, ";
      txt += "E.</b> (2007). P bodies: at the crossroads of post-transcriptional pathways. <i>Nat. Rev. Mol. Cell Biol.</i> <b>8";
      txt += "</b>, 9-22.</small></p>";

  document.getElementById("content").innerHTML = txt;
}

/* ********** ********** ********** ********** ********** ********** ********** ********** */