// Based on scriptaculous' $() function, found in prototype.js
function $(e){
  if(typeof e == 'string'){
    e = document.getElementById(e);
  }
  
  return e;
}

function sortPage(t, d){
  $('sort_field').value = t;
  $('sort_dir').value = d;
  
  //$('search-form').action = "";
  $('search-form').submit();
}

function jumpPage(i){
  if(i > 0){
    $('sort_page').value = i;
    
    //$('search-form').action = "";
    $('search-form').submit();
  }
}

function perPage(i){
  if(i > 0){
    $('maxperpage').value = i;
    
    //$('search-form').action = "";
    $('search-form').submit();
  }
}

function addMyPSM(id, href) {
  var F = '.action-forget-' + id;
  var R = '.action-remember-' + id;
  new Ajax.Request(href, {
    onSuccess: function(transport) {
      var f=$$(F);
      f.each( function (name, index) {
        f[index].setStyle({display:'inline'});
      });
      var r=$$(R);
      r.each( function (name, index) {
        r[index].setStyle({display:'none'});
      });
      pingMessage(ADDED);
      return false;
    }
  });
  return false;
}

function delMyPSM(id, href) {
  var F = '.action-forget-' + id;
  var R = '.action-remember-' + id;
  new Ajax.Request(href, {
    onSuccess: function(transport) {
      pingMessage(REMOVED);
      if (REMOVE_FROM_DOM_ON_FORGET) {
        var TR = jQuery(F).parent().parent();
        TR.remove();
      }
      else {
        var f=$$(F);
        f.each( function (name, index) {
          f[index].setStyle({display:'none'});
        });
        var r=$$(R);
        r.each( function (name, index) {
          r[index].setStyle({display:'inline'});
        });
      }
      return false;
    }
  });
  return false;
}

function pingMessage(M) {
  jQuery('div.downloads-tooltip').fadeOut();
  var top=getScrollHeight();
  top = top + 250;
  jQuery('div#pingMessage span.message').html(M);
  jQuery('div#pingMessage').css({top: top}).fadeIn(function(){ 
    jQuery('div#pingMessage').animate({height: 50}, 1000, function(){ 
      jQuery('div#pingMessage').fadeOut(); 
    }); 
  });
}

function getScrollHeight() {
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

function resetSearchForm(){
  $('ses').value = "false";
  
  //$('search-form').action = "";
  $('search-form').submit();
}

function exportCurrentList(type){
  $('export_type').value = type;
  
  tmp = $('search-form').action;
  $('search-form').action = "export-list.php";
  $('search-form').submit();
  $('search-form').action = tmp;
}

function postMyPSMFilter(action, id) {
  var postBody = 'ses='+Math.random();
  postBody += "&action="+action;
  postBody += "&id="+id;

  var opt = {
    method: 'get',
    postBody: postBody,
    onSuccess: function(t) {
    },
    on404: function(t) {
    },
    onFailure: function(t) {
    }
  }
  
  new Ajax.Request('tools-actions.php', opt);
}

function selected(obj){
  return $(obj).options[$(obj).options.selectedIndex].value;
}

var category,langauge,level_of_use;

function ajaxUpdate(originator, category, disease, commodity, developer, language, level_of_use){
  var postBody = 'ses='+Math.random();
  
  postBody += "&originator="+originator.id;
  
  if(category){
    postBody += '&category='+category;
  }
  
  if(disease){
    postBody += '&search_diseases='+disease;
  }
  
  if(commodity){
    postBody += '&search_commodities='+commodity;
  }
  
  if(developer){
    postBody += '&developer='+developer;
  }
  
  if(language){
    postBody += '&language='+language;
  }
  
  if(level_of_use){
    postBody += '&level_of_use='+level_of_use;
  }
  
  var fields = new Array('search_diseases', 'search_commodities', 'search_developer', 'search_language', 'search_lou');
      
  var origid = 0;
  for(i=0; i < fields.length; i++){
    if(fields[i] == originator.id){
      origid=i;
    }
  }
  
  for(j=0; j < fields.length; j++){
    if(fields[j] != originator.id && origid <= j){
      $(fields[j]).disabled=true;
    }
  }
  
  var opt = {
    method: 'post',
    postBody: postBody,
    onSuccess: function(t) {
      $("reset-button").style.display = 'inline';
      ret = t.responseText;
      eval(ret);
      for(j=0; j < fields.length; j++){
        if(fields[j] != originator.id && origid <= j){
          field = $(fields[j]);
          field.disabled=false;
          field.options.length=3;
          for(i=0; i < newItems[j+1].length; i++){
            field.options.length++;
            
            field.options[i+3].value = newItems[j+1][i];
            field.options[i+3].text = newItemsText[j+1][i];
          }
        }
      }
    },
    on404: function(t) {
      alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    onFailure: function(t) {
      alert('Error ' + t.status + ' -- ' + t.statusText);
    }
  }
  
  new Ajax.Request('ajax/tools-list.php', opt);
}