function advSearch(mode){
  if($('query').value){
    if(mode != 'simple'){
      o = $('include-categories');
      
      if(o.options.length > 0 && o.options[0].value != "0"){
        for(i=0; i < o.options.length; i++){
          o.options[i].selected=true;
        }
      }
    $('advanced-search-mode').value = 'full'; // mode;
    }
    else {
      $('advanced-search-mode').value = 'simple'; // mode;
    }
    
    
    $('advanced-search-form').submit();
    return true;
  }
  else{
    //new Effect.Shake('query-block');
    new Effect.Morph('query-block', { style: 'background: #c00; color: #fff', duration: 0.2});
    setTimeout("new Effect.Morph('query-block', { style: 'background: #b4c8e7; color: #3d3d3d', duration: 0.2})", 500);
    $('query').focus();
    return false;
  }
}

function jumpPage(i){
  if(i > 0){
    $('sort_page').value = i;
    
    advSearch($('advanced-search-mode').value);
  }
}
var s;
function advSearchAddCat(s){
  o = $('exclude-categories');
  d = $('include-categories');
  
  sel = (s) ? s : o.options.selectedIndex;
  
  if(sel == sel/1 && o.options.length > 0){
    try {
      v = o.options[sel].value;
      n = o.options[sel].text;
      
      d.options.length += 1;
      d.options[d.options.length-1].value = v;
      d.options[d.options.length-1].text = n;
      
      newO = new Array();
      for(i=0; i < o.options.length; i++){
        if(o.options[i].value != v){
          newO[newO.length] = new Array(o.options[i].value, o.options[i].text);
        }
      }
    
      o.options.length = newO.length;
      for(i=0; i < newO.length; i++){
        o.options[i].value = newO[i][0];
        o.options[i].text  = newO[i][1];
      }
      
      newD = new Array();
      for(i=0; i < d.options.length; i++){
        if(d.options[i].value != 0){
          newD[newD.length] = new Array(d.options[i].value, d.options[i].text);
        }
      }
    
      d.options.length = newD.length;
      for(i=0; i < newD.length; i++){
        d.options[i].value = newD[i][0];
        d.options[i].text  = newD[i][1];
      }
      
      o.options.selectedIndex = 0;
      d.options.selectedIndex = 0;
    } catch(e) {
      // nothing, requested entry does not appear to exist
    }
  }
}

function advSearchDelCat(){
  o = $('include-categories');
  d = $('exclude-categories');
  
  if(o.options.selectedIndex == o.options.selectedIndex/1 && o.options[o.options.selectedIndex].value != "0" && o.options.length > 0){
    v = o.options[o.options.selectedIndex].value;
    n = o.options[o.options.selectedIndex].text;
    
    d.options.length += 1;
    d.options[d.options.length-1].value = v;
    d.options[d.options.length-1].text = n;
    
    newO = new Array();
    for(i=0; i < o.options.length; i++){
      if(o.options[i].value != v){
        newO[newO.length] = new Array(o.options[i].value, o.options[i].text);
      }
    }
  
    o.options.length = newO.length;
    for(i=0; i < newO.length; i++){
      o.options[i].value = newO[i][0];
      o.options[i].text  = newO[i][1];
    }
    
    if(o.options.length == 0 || (o.options.length == 1 && o.options[0].value == "")){
      o.options.length = 1;
      o.options[0].value = 0;
      o.options[0].text  = 'Any PSM Area';
    }
    
    o.options.selectedIndex = 0;
    d.options.selectedIndex = 0;
  }
}

function advSearchRestoreCats(){
  if(arguments.length > 0){
    o = $('exclude-categories');
    
    for(restI=0; restI < arguments.length; restI++){
      for(restJ=0; restJ < o.options.length; restJ++){
        if(o.options[restJ].value == arguments[restI]){
          advSearchAddCat(""+restJ);
        }
      }
    }
  }
}

function advSearchToggle(){
  Effect.toggle('fullSearch', 'slide');
  Effect.toggle('btnSimpleSearch', 'appear');
  Effect.toggle('link-more', 'blind');
  Effect.toggle('link-less', 'blind');
}