/*
 * BioModels Database
 *
 * This is the code of BioModels Database Version 3.
 * Copyright (C)  BioModels.net 2007-2012
 *
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * BioModels.net
 * EMBL - European Bioinformatics Institute,
 * Wellcome Trust Genome Campus
 * CB10 1SD,
 * Hinxton
 * UK
 */

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function doAjaxRequest(url, htmlTableId) {
  var biomodelsXmlHttp;
  var processingHtmlTableId;
  processingHtmlTableId=htmlTableId;
  biomodelsXmlHttp=GetXmlHttpObject();
  if (biomodelsXmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    // use traditional jsp
    return false;
  }
  biomodelsXmlHttp.onreadystatechange=function(){
    if (biomodelsXmlHttp.readyState==4) {
      var currentTable=document.getElementById(processingHtmlTableId);
      currentTable.innerHTML=biomodelsXmlHttp.responseText;
    }
  }
  biomodelsXmlHttp.open("GET",url,true);
  biomodelsXmlHttp.send(null);
  return false;
}

function set_resource_input_helper_by_resource_id(form)
{
 var value = form.rid.options[form.rid.selectedIndex].value;
 if (value.match("MIR:00000002")) {        // ChEBI
  form.rac.value = "CHEBI:";
 } else if (value.match("MIR:00000003")) { // Ensembl
  form.rac.value = "ENS";
 } else if (value.match("MIR:00000004")) { // EC code
  form.rac.value = "";
 } else if (value.match("MIR:00000010")) { // IntAct
  form.rac.value = "EBI-";
 } else if (value.match("MIR:00000011")) { // InterPro
  form.rac.value = "IPR";
 } else if (value.match("MIR:00000013")) { // KEGG Compound
  form.rac.value = "C";
 } else if (value.match("MIR:00000014")) { // KEGG Reaction
  form.rac.value = "R";
 } else if (value.match("MIR:00000017")) { // PIRSF
  form.rac.value = "PIRSF";
 } else if (value.match("MIR:00000022")) { // Gene Ontology
  form.rac.value = "GO:";
 } else if (value.match("MIR:00000067")) { // FMA
  form.rac.value = "FMA:";
 } else if (value.match("MIR:00000111")) { // Brenda Tissue Ontology
  form.rac.value = "BTO:";
 } else if (value.match("MIR:00000110")) { // Cell Type Ontology
  form.rac.value = "CL:";
 } else if (value.match("MIR:00000043")) { // IPI
  form.rac.value = "IPI";
 } else if (value.match("MIR:00000056")) { // Protein Modification Ontology
  form.rac.value = "MOD:";
 } else if (value.match("MIR:00000018")) { // Reactome
  form.rac.value = "REACT_";
 } else if (value.match("MIR:00000234")) { // Cell Cycle Ontology
  form.rac.value = "CCO:";
 } else if (value.match("MIR:00000112")) { // PATO
  form.rac.value = "PATO:";
 } else if (value.match("MIR:00000233")) { // Human Disease Ontology
  form.rac.value = "DOID:";
 } else {
  form.rac.value = "";
 }
 form.rac.focus();
};

function validate_resource_input_by_resource_id(form)
{
 var resource_id = form.rid.options[form.rid.selectedIndex].value;
 var input = form.rac.value;
 if (input.length == 0) {
  alert("Resource identifier is required.");
  return false;
 } 
 if (resource_id.match("MIR:00000001")) {
  var regex = /^\d+$/
  if (!regex.test(input)) {
   alert("Invalid BIND identifier respect to pattern:\n^\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000002")) {
  var regex = /^CHEBI:\d+$/
  if (!regex.test(input)) {
   alert("Invalid ChEBI identifier respect to pattern:\n^CHEBI:\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000003")) {
  var regex = /^ENS[A-Z]*[FPTG]\d{11}$/
  if (!regex.test(input)) {
   alert("Invalid Ensembl identifier respect to pattern:\n^ENS[A-Z]*[FPTG]\\d{11}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000004")) {
  var regex = /^\d+|\d+\.(-|\d+)|\d+\.\d+\.(-|\d+)|\d+\.\d+\.\d+\.(-|\d+)$/
  if (!regex.test(input)) {
   alert("Invalid EC code identifier respect to pattern:\n^\\d+|\\d+\\.(-|\\d+)|\\d+\\.\\d+\\.(-|\\d+)|\\d+\\.\\d+\\.\\d+\\.(-|\\d+)$");
   return false;
  }
 } else if (resource_id.match("MIR:00000005")) {
  var regex = /^([A-N,R-Z][0-9][A-Z][A-Z, 0-9][A-Z, 0-9][0-9])|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])$/
  if (!regex.test(input)) {
   alert("Invalid UniProt identifier respect to pattern:\n^([A-N,R-Z][0-9][A-Z][A-Z, 0-9][A-Z, 0-9][0-9])|([O,P,Q][0-9][A-Z, 0-9][A-Z, 0-9][A-Z, 0-9][0-9])$");
   return false;
  }
 } else if (resource_id.match("MIR:00000006")) {
  var regex = /^\d+$/
  if (!regex.test(input)) {
   alert("Invalid Taxonomy identifier respect to pattern:\n^\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000007")) {
  var regex = /^(BIOMD|MODEL)\d{10}$/
  if (!regex.test(input)) {
   alert("Invalid BioModels Database model identifier respect to pattern:\n^(BIOMD|MODEL)\\d{10}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000009")) {
  var regex = /^[A-Z]\d+(\.[-\d+])?$/
  if (!regex.test(input)) {
   alert("Invalid ICD10 identifier respect to pattern:\n^[A-Z]\\d+(\\.[-\\d+])?$");
   return false;
  }
 } else if (resource_id.match("MIR:00000010")) {
  var regex = /^EBI\-[0-9]+$/
  if (!regex.test(input)) {
   alert("Invalid IntAct identifier respect to pattern:\n^EBI\\-[0-9]+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000011")) {
  var regex = /^IPR\d{6}$/
  if (!regex.test(input)) {
   alert("Invalid InterPro identifier respect to pattern:\n^IPR\\d{6}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000012")) {
  var regex = /^\w{2,3}\d{5}$/
  if (!regex.test(input)) {
   alert("Invalid KEGG Pathway identifier respect to pattern:\n^\w{2,3}\d{5}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000013")) {
  var regex = /^C\d+$/
  if (!regex.test(input)) {
   alert("Invalid KEGG Compound identifier respect to pattern:\n^C\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000014")) {
  var regex = /^R\d+$/
  if (!regex.test(input)) {
   alert("Invalid KEGG Reaction identifier respect to pattern:\n^R\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000016")) {
  var regex = /^[*#+%^]?\d{6}$/
  if (!regex.test(input)) {
   alert("Invalid OMIM identifier respect to pattern:\n^[*#+%^]?\\d{6}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000017")) {
  var regex = /^PIRSF\d{6}$/
  if (!regex.test(input)) {
   alert("Invalid PIRSF identifier respect to pattern:\n^PIRSF\d{6}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000018")) {
  var regex = /^\d+$/
  var regex1 = /^REACT_\d+(\.\d+)?$/
  if (!regex.test(input) 
	&& !regex1.test(input)) {
   alert("Invalid Reactome identifier respect to pattern:\n^REACT_\\d+(\\.\\d+)?$");
   return false;
  }
 } else if (resource_id.match("MIR:00000021")) {
  var regex = /^[0-9A-Za-z]+:\d+:\d{1,5}(\.\d)?$/
  if (!regex.test(input)) {
   alert("Invalid CluSTr identifier respect to pattern:\n^[0-9A-Za-z]+:\\d+:\\d{1,5}(\\.\\d)?$");
   return false;
  }
 } else if (resource_id.match("MIR:00000022")) {
  var regex = /^GO:\d{7}$/
  if (!regex.test(input)) {
   alert("Invalid Gene Ontology identifier respect to pattern:\n^GO:\\d{7}$");
   return false;
  }
 } else if (resource_id.match("MIR:00000034")) {
  var regex = /^\d+$/
  if (!regex.test(input)) {
   alert("Invalid PubChem-compound identifier respect to pattern:\n^\\d+$");
   return false;
  }
 } else if (resource_id.match("MIR:00000067")) {
  var regex = /^FMA:\d+$/
  if (!regex.test(input)) {
   alert("Invalid FMA identifier respect to pattern:\n^FMA:\\d+$");
   return false;
  }
 }
 return true;
};

function validate_sbo_id(sbo_id)
{
 if (sbo_id.length == 0) {
  alert("Please input SBO Term ID.");
  return false;
 }
 var regex = new RegExp("^SBO:\\d{7}$");
 if (!regex.test(sbo_id)) {
  alert("Invalid SBO Term identifier respect to pattern:\n^SBO:\\d{7}$");
  return false;
 }
 return true;
}

/*
Retrieves the website URL of a resource
*/
function go_to_resource_by_datatype_id(form)
{
  /*
  Depends on jQuery (1.7.1)

  this returns the website URL of a resource, but what is really needed is *a* URL for the data collection

  var collection_id = form.rnm.options[form.rnm.selectedIndex].value;
  // sends the info to the server
  var request = $.ajax({
    url: "getResourceUrl",
    data: {'cid': collection_id},
    type: 'GET'
  });
  
  // success (from the Ajax point of view)
  request.done(function(output) {
    open_window(output);
  });
  
  // failure
  request.fail(function(output) {
    alert("Unable to retrieve the requested URL!");
  });
  */

 if (datatype_id.match("MIR:00000001")) {
  open_window("http://www.bind.ca/");
 } else if (datatype_id.match("MIR:00000002")) {
  open_window("http://www.ebi.ac.uk/chebi/");
 } else if (datatype_id.match("MIR:00000003")) {
  open_window("http://www.ensembl.org/");
 } else if (datatype_id.match("MIR:00000004")) {
  open_window("http://www.ebi.ac.uk/IntEnz/");
 } else if (datatype_id.match("MIR:00000005")) {
  open_window("http://www.uniprot.org/");
 } else if (datatype_id.match("MIR:00000006")) {
  open_window("http://www.uniprot.org/taxonomy/");
 } else if (datatype_id.match("MIR:00000007")) {
  open_window("http://www.ebi.ac.uk/biomodels/");
 } else if (datatype_id.match("MIR:00000009")) {
  open_window("http://www3.who.int/icd/vol1htm2003/fr-icd.htm");
 } else if (datatype_id.match("MIR:00000010")) {
  open_window("http://www.ebi.ac.uk/intact");
 } else if (datatype_id.match("MIR:00000011")) {
  open_window("http://www.ebi.ac.uk/interpro/");
 } else if (datatype_id.match("MIR:00000012")) {
  open_window("http://www.genome.jp/kegg/pathway.html");
 } else if (datatype_id.match("MIR:00000013")) {
  open_window("http://www.genome.jp/kegg/ligand.html");
 } else if (datatype_id.match("MIR:00000014")) {
  open_window("http://www.genome.jp/kegg/ligand.html");
 } else if (datatype_id.match("MIR:00000016")) {
  open_window("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=OMIM");
 } else if (datatype_id.match("MIR:00000017")) {
  open_window("http://pir.georgetown.edu/pirsf/");
 } else if (datatype_id.match("MIR:00000018")) {
  open_window("http://www.reactome.org/");
 } else if (datatype_id.match("MIR:00000021")) {
  open_window("http://www.ebi.ac.uk/clustr/");
 } else if (datatype_id.match("MIR:00000022")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=GO");
 } else if (datatype_id.match("MIR:00000034")) {
  open_window("http://pubchem.ncbi.nlm.nih.gov/");
 } else if (datatype_id.match("MIR:00000067")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=FMA");
 } else if (datatype_id.match("MIR:00000110")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=CL");
 } else if (datatype_id.match("MIR:00000111")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=BTO");
 } else if (datatype_id.match("MIR:00000112")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=PATO");
 } else if (datatype_id.match("MIR:00000233")) {
  open_window("http://disease-ontology.org/");
 } else if (datatype_id.match("MIR:00000234")) {
  open_window("http://www.cellcycleontology.org/");
 }
};

function go_to_resource_by_datatype_id(select)
{
 var datatype_id = select.options[select.selectedIndex].value;
 if (datatype_id.match("Publication")) {
  open_window("http://www.ebi.ac.uk/citexplore/");
 } else if (datatype_id.match("MIR:00000001")) {
  open_window("http://www.bind.ca/");
 } else if (datatype_id.match("MIR:00000002")) {
  open_window("http://www.ebi.ac.uk/chebi/");
 } else if (datatype_id.match("MIR:00000003")) {
  open_window("http://www.ensembl.org/");
 } else if (datatype_id.match("MIR:00000004")) {
  open_window("http://www.ebi.ac.uk/IntEnz/");
 } else if (datatype_id.match("MIR:00000005")) {
  open_window("http://www.uniprot.org/");
 } else if (datatype_id.match("MIR:00000006")) {
  open_window("http://www.uniprot.org/taxonomy/");
 } else if (datatype_id.match("MIR:00000007")) {
  open_window("http://www.ebi.ac.uk/biomodels");
 } else if (datatype_id.match("MIR:00000009")) {
  open_window("http://www3.who.int/icd/vol1htm2003/fr-icd.htm");
 } else if (datatype_id.match("MIR:00000010")) {
  open_window("http://www.ebi.ac.uk/intact");
 } else if (datatype_id.match("MIR:00000011")) {
  open_window("http://www.ebi.ac.uk/interpro/");
 } else if (datatype_id.match("MIR:00000012")) {
  open_window("http://www.genome.jp/kegg/pathway.html");
 } else if (datatype_id.match("MIR:00000013")) {
  open_window("http://www.genome.jp/kegg/ligand.html");
 } else if (datatype_id.match("MIR:00000014")) {
  open_window("http://www.genome.jp/kegg/ligand.html");
 } else if (datatype_id.match("MIR:00000016")) {
  open_window("http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=OMIM");
 } else if (datatype_id.match("MIR:00000017")) {
  open_window("http://pir.georgetown.edu/pirsf/");
 } else if (datatype_id.match("MIR:00000018")) {
  open_window("http://www.reactome.org/");
 } else if (datatype_id.match("MIR:00000021")) {
  open_window("http://www.ebi.ac.uk/clustr/");
 } else if (datatype_id.match("MIR:00000022")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=GO");
 } else if (datatype_id.match("MIR:00000034")) {
  open_window("http://pubchem.ncbi.nlm.nih.gov/");
 } else if (datatype_id.match("MIR:00000067")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=FMA");
 } else if (datatype_id.match("MIR:00000110")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=CL");
 } else if (datatype_id.match("MIR:00000111")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=BTO");
 } else if (datatype_id.match("MIR:00000112")) {
  open_window("http://www.ebi.ac.uk/ontology-lookup/browse.do?ontName=PATO");
 } else if (datatype_id.match("MIR:00000233")) {
  open_window("http://disease-ontology.org/");
 } else if (datatype_id.match("MIR:00000234")) {
  open_window("http://www.cellcycleontology.org/");
 }
};

function validate_modelmonth(form)
{
 if (form.mid.value.length == 0) {
  alert("Please give the related model(s) ID.");
  form.mid.focus();
  return false;
 }
 if (form.ttl.value.length == 0) {
  alert("Please give the title.");
  form.ttl.focus();
  return false;
 }
 if (form.ath.value.length == 0) {
  alert("Please give the authors.");
  form.ath.focus();
  return false;
 }
 if (form.lnk.value.length == 0) {
  alert("Please give the link of the article.");
  form.lnk.focus()
  return false;
 }
 return true;
};

function toggleVisibleById(objId, previousClassName) {
  var obj=document.getElementById(objId);
  if (obj.className == previousClassName) {
    obj.className = "hidden";
  } else {
    obj.className = previousClassName;
  }
}

function toggleGraph(obj) {
  if (obj.title=="Collapse") {
    obj.src="http://www.ebi.ac.uk/biomodels/icons/plus.gif";
    obj.title="Expand";
  } else {
    obj.src="http://www.ebi.ac.uk/biomodels/icons/minus.gif";
    obj.title="Collapse";
  }
}

function highlightRow(metaId) {
  if (highlightedObjId!="") {
    var row1=document.getElementById(highlightedObjId);
    if (row1 != null) {
      row1.className = "";
    }
  }

  var row2=document.getElementById(metaId);
  if (row2 != null) {
    row2.className = "highlighted_border";
    highlightedObjId = metaId;
  }
}

