function ajaxRequest(url) {
  new Ajax.Request( url, { method: 'post'} );
}

function showForm(url,citation_type, new_spa) {
  var request_url = url+'?citation_type='+document.getElementsByName(citation_type)[0].value

  if (new_spa) {
    request_url += "&new_spa=true"
  }
  
  new Ajax.Updater('citation_type_container', request_url, {
    method:     'post',
    onFailure:  function() {Element.classNames('notice').add('failure')},
    onComplete: function() {new Effect.Highlight('citation_type_container', {duration: 0.25})}
  });
}

function showForm2(url, citation_class_field) {
  var request_url = url+'?citation_class=' + document.getElementsByName(citation_class_field)[0].value

  new Ajax.Updater('citation_type_container', request_url, {
    method:     'post',
    onFailure:  function() {Element.classNames('notice').add('failure')},
    onComplete: function() {new Effect.Highlight('citation_type_container', {duration: 0.25})}
  });
}

var OFF_COLOR = "";
var ON_COLOR = "#ffccff";

/* highlight the claim selected */
function selectClaim(claimNumber, checkbox, containerElement, valuesElement, listViewElement) {
  var claimSelected = checkbox.checked;
  // set the style to ON if selected or OFF if deselected
  containerElement.style.backgroundColor = claimSelected?ON_COLOR:OFF_COLOR;

  // get saved claims array from hidden value field
  var savedClaims = getSavedClaimsArray(valuesElement);

  // add to list if selected and not already in the list, add it
  if ( claimSelected ) {
    if(savedClaims.indexOf(claimNumber) < 0) {
      savedClaims[savedClaims.length] = claimNumber;
    }
  } else {
    savedClaims = savedClaims.without(claimNumber);
  }

  setHiddenClaimValues(savedClaims, valuesElement, listViewElement);
}

function getSavedClaimsArray(valuesElement){
  var claimsList = valuesElement.value;
  return (claimsList.length > 0)?claimsList.split(','):new Array();
}

function setHiddenClaimValues(claimsArr, valuesElement, listViewElement) {
  if ( !claimsArr.empty ) {
    var claim_str = claimsArr.sort().join(',');
    valuesElement.value = claim_str;
    listViewElement.innerHTML = claim_str;
  }
}

function getClaimDivElement(claimNumber) {
  return $("claim_item_" + claimNumber);
}

function getClaimCheckboxElement(claimNumber) {
  return $("claim_number_" + claimNumber);
}

// find claim ids that are selected ( in the claims_values ) and highlight them
function highlightSelectedClaims() {
  var claims = getSavedClaimsArray($('claim_values'));
  for (var index = 0, len = claims.length; index < len; ++index) {
    var claimId = claims[index];

    var checkboxElement = getClaimCheckboxElement(claimId);
    // if the elements exist on this page
    if ( checkboxElement ) {
      checkboxElement.checked = true;
      getClaimDivElement(claimId).style.backgroundColor = ON_COLOR;
    }
  }
}
