function callSmallWindow(page_name,intern_name,width,height){
  window.open(page_name, intern_name, config='height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=yes, resizable=yes');
}

function callSmallWindow2(page_name,intern_name, big) { 
  if (big == 0) window.open(page_name,intern_name, config='height=220, width=300, toolbar=no, menubar=no,scrollbars=yes, resizable=yes, directories=no,status=no');
  else if(big == 2) window.open(page_name,intern_name, config='height=500, width=800, toolbar=no, menubar=no,scrollbars=yes, resizable=yes, directories=no,status=no');
  else window.open(page_name,intern_name, config='height=500, width=400, toolbar=no, menubar=no,scrollbars=yes, resizable=yes, directories=no,status=no');
}

function call_small_window(page_name,intern_name) { 
  window.open(page_name,intern_name, config='height=150, width=300, toolbar=no, menubar=no,scrollbars=yes, resizable=yes, directories=no,status=no');
}

function ControleAndSendCode(form){
  var val = new String(form.PDBlist.value);
  if(val == ''){
    alert("You have to specify PDB codes");
  } else if (val.indexOf(";")!=-1 || val.indexOf('"')!=-1 || val.indexOf("'")!=-1 || val.indexOf("=")!=-1) {
    alert("Invalid characters in text ( ;\"=' )");
  } else {
    callSmallWindow("annotations.php","Results","250",'200');
    form.target ="Results";
    form.method ="POST";
    form.action ="annotations.php";
    form.submit();   
  }
}

function ControleAndSendName(form){
  var val = new String(form.PDBtext.value);
  if(val == '' ) {
    alert("You have to specify a text to search");
  } else if (val.indexOf(";")!=-1 || val.indexOf('"')!=-1 || val.indexOf("'")!=-1 || val.indexOf("=")!=-1) {
    alert("Invalid characters in text ( ;\"=' )");
  } else {
    callSmallWindow("annotations.php","Results","450",'200');
    form.target ="Results";
    form.method ="POST";
    form.action ="annotations.php";
    form.submit();   
  }
}

function ControleAndSendFilters(targetCode){
  var id = ''
  if (targetCode == '') {
    if ((document.targetCode.l1.value!='' && document.targetCode.l0.value=='') || (document.targetCode.l2.value!='' && document.targetCode.l1.value=='') || (document.targetCode.l3.value!='' && document.targetCode.l2.value=='') || (document.targetCode.l4.value!='' && document.targetCode.l3.value=='')) {
      alert("If you specify a level you have to specify all its parent levels too")
      return;
    } else if (document.targetCode.l0.value=='') {
      alert("You have to specify at least one level")
      return;
    } else {
      var l0 = document.targetCode.l0.value
      var l1 = document.targetCode.l1.value
      var l2 = document.targetCode.l2.value
      var l3 = document.targetCode.l3.value
      var l4 = document.targetCode.l4.value

        if (l1.length==3 && l1.charAt(0)=='0') l1 = l1.substring(1,3);
        if (l1.length==2 && l1.charAt(0)=='0') l1 = l1.substring(1,2);
        if (l2.length==3 && l2.charAt(0)=='0') l2 = l2.substring(1,3);
        if (l2.length==2 && l2.charAt(0)=='0') l2 = l2.substring(1,2);
        if (l3.length==3 && l3.charAt(0)=='0') l3 = l3.substring(1,3);
        if (l3.length==2 && l3.charAt(0)=='0') l3 = l3.substring(1,2);
        if (l4.length==3 && l4.charAt(0)=='0') l4 = l4.substring(1,3);
        if (l4.length==2 && l4.charAt(0)=='0') l4 = l4.substring(1,2);

      id = l0
      if (l1 != 0 || (l1 == 0 && l0 == 'NR')) { // in the case of NR, l1 can be '0'
	id = id + "." + l1
	if (l2 != 0) {
	  id = id + "." + l2
	  if (l3 != 0) {
	    id = id + "." + l3
	    if (l4 != 0) {
	      id = id + "." + l4
	    }
	  }
	}
      }
    }
    if (id=='NR.') id='NR';
    } else id = targetCode;

    var item="";
    for (var i=0; i < document.newForm.covTypeSelect.length; i++)
    {
      if (document.newForm.covTypeSelect[i].checked)
      {
        item=document.newForm.covTypeSelect[i].value;

      }
    }

  if (item=='structures') var loc = "http://cgl.imim.es/fcp/fcp.php?struct=1&compactId=" + id;
  else var loc = "http://cgl.imim.es/fcp/fcp.php?struct=0&compactId=" + id;

  if (document.filtersForm.year.value != 'all') loc = loc + "&year=" + document.filtersForm.yearOperator.value + document.filtersForm.year.value
  if (document.filtersForm.resolution.value != 'all') loc = loc + "&resolution=" + document.filtersForm.resOperator.value + document.filtersForm.resolution.value
  if (document.filtersForm.population.value != 'all') loc = loc + "&population=" + document.filtersForm.popOperator.value + document.filtersForm.population.value
  if (document.filtersForm.bias.value != 'all') loc = loc + "&bias=" + document.filtersForm.biasOperator.value + document.filtersForm.bias.value
  if (document.filtersForm.coverage.value != 'all') loc = loc + "&coverage=" + document.filtersForm.covOperator.value + document.filtersForm.coverage.value

  var filterString = ''
  var otherOption = false
  var allOption = false
  var chosen = document.filtersForm.specie.options
  
  for(i=0; i<chosen.length; i++) {
    if (chosen[i].selected) {
      if (chosen[i].value == 'all') allOption = true
      else otherOption = true
      if (allOption && otherOption) {
	alert("You cannot choose 'all' at the same time that any other option in Species filter")
	i = chosen.length
	filterString = 'error'
      } else filterString = filterString + chosen[i].value + ','
    }
  }

  if (filterString == 'error') loc = 'error'
  else if (filterString != 'all,all,' && filterString != 'all,' && filterString != '') loc = loc + '&specie=' + filterString

  if (loc != 'error') window.parent.location = loc;

}

function ShowNew(form) {
  
  callSmallWindow("annotations.php","newStructures","750","850");  
  form.target ="newStructures";
  form.method ="POST";
  form.action ="annotations.php";
  form.submit(); 

}

function showAndHideBoxes(box1, tab1, box2, tab2) {
  boxToShow = document.getElementById(box1);
  boxToShow.style.display = '';

  tabToOn = document.getElementById(tab1);
  tabToOn.style.background = "#DDDDDD url(images/orangetab_400.jpg) no-repeat";

  boxToHide = document.getElementById(box2);
  boxToHide.style.display = 'none';

  tabToOff = document.getElementById(tab2);
  tabToOff.style.background = "#DDDDDD url(images/darkorangetab_400.jpg) no-repeat";

}

function unsetFilters() {  
  var item = document.getElementById('year');
  item.value = 'all';
//  alert(item.value);
  item = document.getElementById('resolution');
  item.value = 'all';
//  alert(item.value);
  item = document.getElementById('specie');
  item.value = 'all';
//  alert(iem.value);
  item = document.getElementById('population');
  item.value = 'all';
//  alert(item.value);
  item = document.getElementById('coverage');
  item.value = 'all';
//  alert(item.value);
  item = document.getElementById('bias');
  item.value = 'all';
//  alert(item.value);

}

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function exportToCSV() 
{
  document.getElementById("Image10").blur();
  if (document.getElementById('sourcedate')!=null)
  { 
   document.form1.table.value =document.getElementById('sourcedate').innerHTML;
   document.form1.submit();
  }
}
