/*
  common.js - Various JavaScript functions for TCSVM website.
*/

/*
  getRelativePath - Returns the URL of the current page minus the base.
*/
function getRelativePath() {
  return location.toString().
                  gsub($$("base")[0].readAttribute("href"), "").
                  gsub(/index\.html$/, "").
                  gsub(/#.*$/, "").
                  gsub(/\?.*$/, "");
}

/*
  insertContentBefore - Inserts content before selected elements.

  selector - the CSS selector
  content - the content to be inserted
*/
function insertContentBefore(selector, content) {
  $$(selector).each( function(e) {
    e.insert({ before: content });
  });
}

/*
  insertQuotes - Inserts quotation marks where they are not supported 
  with CSS.
*/
function insertQuotes() {
  $$("q").each( function(e) {
    e.insert({ before: "“" });
    e.insert({ after: "”" });
  });
}

/*
  parseMailtoLinks - Parses anchors with a href beginning with 
  "mailto:" and replacing strings that web crawlers can't use.
*/
function parseMailtoLinks() {
  $$("a[href^=\"mailto:\"]").each( function(e) {
    e.writeAttribute("href", e.readAttribute("href").gsub(/ *\[at\] */, "@").gsub(/ *\[dot\] */, "."));
    e.innerHTML = e.innerHTML.gsub(/ *\[at\] */, "@").gsub(/ *\[dot\] */, ".");
  });
}

/*
  removeCurrentPageLink - Removes links to the current page as well as 
  the hidden class from appropriate sidebar elements.
  
  selector - the CSS selector
*/
function removeCurrentPageLink(selector) {
  selector.scan(/[^,]+/, function(m) {
    selector = selector.gsub(m, m + " a[href=\"" + 
                                    getRelativePath() + 
                                    "\"]");
  });

  $$(selector).each( function(e) {
    e.addClassName("current").writeAttribute("href", value = null);
  });
}

/*
  removeCurrentBlogPageLink - Removes links to the current blog pages 
  as well as the hidden class from appropriate sidebar elements.
  
  selector - the CSS selector
*/
function removeCurrentBlogPageLink(selector) {
  selector.scan(/[^,]+/, function(m) {
    selector = selector.gsub(m, m + " a[href=\"" + 
                                    location.toString().gsub(/index\.html$/, "").gsub(/#.*$/, "") + 
                                    "\"]");
  });

  $$(selector).each( function(e) {
    e.addClassName("current").writeAttribute("href", value = null);
  });
}

/*
  search - Called onclick. Supports various search methods.
*/
function search() {
  searchForm = $("search_form");
  searchForm.action = "http://googlesearch.tufts.edu/search";

  searchLocation = searchForm.getInputs("radio", "search_location");

  if (searchLocation[0].checked) {
    // search this site
    searchForm.as_sitesearch.value = "www.tufts.edu/vet";
    searchForm.proxystylesheet.value = "vet_template";
  } else if (searchLocation[1].checked) {
    // search tufts.edu
    searchForm.as_sitesearch.value = "";
    searchForm.proxystylesheet.value = "tufts_staging";
  } else if (searchLocation[2].checked) {
    // search for people
    searchForm.action = "http://whitepages.tufts.edu/searchresults.cgi";
    searchForm.search.value = $("search_text").value;
  }

  searchForm.submit();

  searchLocation = null;
  searchForm = null;
}

/*
  blog_search - Called onclick. Supports various search methods for blogs.
*/
function blog_search() {
  searchForm = $("search_form");
  
  searchLocation = searchForm.getInputs("radio", "search_location");

  if (searchLocation[0].checked) {
    // search this site
    searchForm.action = $$("base")[0].readAttribute("href");
  } else if (searchLocation[1].checked) {
    // search tufts.edu/vet
    searchForm.action = "http://googlesearch.tufts.edu/search";
    searchForm.as_sitesearch.value = "vet.tufts.edu";
    searchForm.proxystylesheet.value = "vet_template";
    searchForm.q.value = $("search_text").value;
  } else if (searchLocation[2].checked) {
    // search for people
    searchForm.action = "http://whitepages.tufts.edu/searchresults.cgi";
    searchForm.search.value = $("search_text").value;
  }

  searchForm.submit();

  searchLocation = null;
  searchForm = null;
}

/**
* Returns the value of the selected radio button in the radio group, null if
* none are selected, and false if the button group doesn't exist
*
* @param {radio Object} or {radio id} el
* OR
* @param {form Object} or {form id} el
* @param {radio group name} radioGroup
*/
function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
 
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}
