/*Arch Functions*/
var g_recommended;
var g_url = "/cgi-bin/forms/arch/results.pl";

function getResults(search, recommended) {
  var params = search.toQueryString();
  if (recommended) {
    params += "&recommended=" + recommended;
  }
  if (params) {
    var myAjax = new Ajax.Updater(
      'rssresults',
      g_url, 
      {
        method: 'post', 
        parameters: params,
        onComplete: function () { toggleDisplay(); handleEmptyRecommended(recommended); displayPersistentDiv();}
      }
    );
  }
  hidePersistentForm();
  toggleDisplay();
}

function handleEmptyRecommended (recommended) {
  if (recommended) {
    var result_size_span = $("result_size");
    if (result_size_span) {
      var result_size = result_size_span.innerHTML;
      if (result_size) {
        if (result_size <= 0) {
          if (g_search) {
            var use_attributes = g_search.getUseAttributes();
            if (use_attributes) {
              for (i=0; i<use_attributes.length; i++) {
                if (use_attributes[i]) {
                  if (use_attributes[i].value && use_attributes[i].value == "29") {
                    if (use_attributes[i].term) {
                      var recommendedRegExp = /\%20Recommended$/;
                      use_attributes[i].term = use_attributes[i].term.replace(recommendedRegExp, "");
                    }
                  }
                }
              }
              g_search.setUseAttributes(use_attributes);
              getResults(g_search, false)
            }
          }
        }
      }
    }
  }
}

function toggleDisplay() {
  var thinking = $("thinking");
  if(thinking.style.display == "none") {
    thinking.style.display = "block";
  } else {
    thinking.style.display = "none";
  }
}

function getBaseLink () {
  var baselink = window.location.href;
  var querypoint = baselink.indexOf("?");
  if (querypoint != -1) {
    baselink = baselink.substr(0,querypoint);
  }
  return baselink;
}

function getPersistentLink () {
  var baselink = getBaseLink();
  var plink = ""; 
  if (baselink) {
    plink = baselink;
    if (g_search) {
      var qs = "";
      qs = g_search.toPersistentQueryString();
      plink = plink.concat("?", qs);
    }
  }
  return plink;
}

function displayPersistentLink () {
  setPersistent();
  showPersistentForm();
}

function setPersistent () {
  var persistent = $("persistent");
  if (persistent) {
    persistent.value = getPersistentLink();
  }
}

function displayPersistentDiv () {
  var persistentDiv = $("persistentDiv");
  if (persistentDiv) {
    if(persistentDiv.style.display == "none") {
      persistentDiv.style.display = "block";
    }
  }
  setPersistent();
}

function togglePersistentForm () {
  var persistentForm = $("persistentForm");
  if (persistentForm) {
    if(persistentForm.style.display == "none") {
      showPersistentForm();
    } else {
      hidePersistentForm();
    }
  }
}

function showPersistentForm () {
  var persistentForm = $("persistentForm");
  if (persistentForm) {
    persistentForm.style.display = "block";
  } 
}

function hidePersistentForm () {
  var persistentForm = $("persistentForm");
  if (persistentForm) {
    persistentForm.style.display = "none";
  } 
}

var g_search = new ArchSearch();

function getArchSearch () {
  if (!g_search) {
    g_search = new ArchSearch();
  }
  return g_search;
}

function setArchSearch (search) {
  if (!search) {
    search = new ArchSearch();
  }
  g_search = search;
}

function updateArchSearch (start, direction) {
  var search = getArchSearch();
  search.setStart(start);
  search.setDirection(direction);
  return search;
}

function handleQS() {
  var b_search = false;
  var query = $Q();
  if (query) {
    if (query.subject || query.keywords) {
      if (query.subject) {
        if ($("use_term1")) {
          $("use_term1").value = query.subject;
          b_search = true;
        }
      } else if(query.keywords) {
        if ($("use_term1")) {
          $("use_term1").value = query.keywords;
          b_search = true;
        }
      }
    }
    if (query.format) {
      if ($("use_term2")) {
        $("use_term2").value = query.format;
        b_search = true;
      }
    }
    if (query.use_term1) {
      if ($("use_term1")) {
        $("use_term1").value = query.use_term1;
        b_search = true;
      }
    }
    if (query.use_term2) {
      if ($("use_term2")) {
        $("use_term2").value = query.use_term2;
        b_search = true;
      }
    }
    if (query.recommended) {
      g_recommended = eval(query.recommended);
    }
  }
  return b_search;
}

/*Arch BobCatSearch Class*/
ArchSearch.prototype = new BobCatSearch();
ArchSearch.prototype.constructor=ArchSearch;
function ArchSearch(){
  this.start;
  this.max;
  this.direction;
  this.syntax = "xml";
}

ArchSearch.prototype.setStart = function (start) {
  this.start = start;
}

ArchSearch.prototype.getStart = function () {
  return this.start;
}

ArchSearch.prototype.setMax = function (max) {
  this.max = max;
}

ArchSearch.prototype.getMax = function () {
  return this.max;
}

ArchSearch.prototype.setDirection = function (direction) {
  this.direction = direction;
}

ArchSearch.prototype.getDirection = function () {
  return this.direction;
}

ArchSearch.prototype.setSyntax = function (syntax) {
  this.syntax = syntax;
}

ArchSearch.prototype.getSyntax = function () {
  return this.syntax;
}

ArchSearch.prototype.toQueryString = function () {
  var qs = "";
  if (this.getUseAttributes()) {
    var use_attributes = this.getUseAttributes();
    if (use_attributes) {
      var length = use_attributes.length;
      if (length) {
        for (var i=1; i <= length; i++) {
          var use_attribute = use_attributes[i-1];
          if (use_attribute) {
            if (use_attribute.getValue()) {
              qs += "use_attribute" + i + "=" + use_attribute.getValue() + "&";
              if (use_attribute.getTerm()) {
                qs += "use_term" + i + "=" + use_attribute.getTerm() + "&";
              }
              if (use_attribute.getLabel()) {
                qs += "label" + i + "=" + use_attribute.getLabel() + "&";
              }
            }
          }
        }
      }
    }
  }
  if (this.getStructureAttribute()) {
    qs += "structure_attribute=" + this.getStructureAttribute() + "&";
  }
  if (this.getStart()) {
    qs += "start=" + this.getStart() + "&";
  }
  if (this.getMax()) {
    qs += "max=" + this.getMax() + "&";
  }
  if (this.getDirection()) {
    qs += "direction=" + this.getDirection() + "&";
  }
  return qs;
}

ArchSearch.prototype.toPersistentQueryString = function () {
  var qs = "";
  var subject;
  var keywords;
  var format;
  var recommended;
  var recommendedRegExp = /\%20Recommended$/;
  if (this.getUseAttributes()) {
    var use_attributes = this.getUseAttributes();
    if (use_attributes) {
      var length = use_attributes.length;
      if (length) {
        for (var i=0; i <= length; i++) {
          var use_attribute = use_attributes[i];
          if (use_attribute) {
            var value = use_attribute.value;
            if (value) {
              switch (value) {
                case "29" :
                  subject = use_attribute.term;
                  if (recommendedRegExp.test(subject)) {
                    subject = subject.replace(recommendedRegExp, "");
                    recommended = "true";
                  } else {
                    recommended = "false";
                  }
                  break;
                case "1016" :
                  keywords = use_attribute.term;
                  break;
                case "1031" :
                  format = use_attribute.term;
                  break;
                case "1014" :
                  format = use_attribute.term;
                  break;
                case "2029" :
                  format = use_attribute.term;
              }
            }
          }
        }
      }
    }
  }
  if (subject || keywords) {
    if (subject) {
      qs = qs.concat("subject=", subject, "&");
    } else if (keywords) {
      qs = qs.concat("keywords=", keywords, "&");
    }
  }
  if (format) {
    qs = qs.concat("format=", format, "&");
  }
  if (recommended) {
    qs = qs.concat("recommended=", recommended, "&");
  }
  return qs;
}

/*Arch Use Attribute Class: extends Use Attribute*/
ArchUseAttribute.prototype = new UseAttribute();
ArchUseAttribute.prototype.constructor=ArchUseAttribute;
function ArchUseAttribute(){
  this.label
}

ArchUseAttribute.prototype.setLabel = function (label) {
  this.label = label;
}

ArchUseAttribute.prototype.getLabel = function () {
  return this.label;
}
