/**
 * Translates relative url into absolute one, since mapfish-print module cannot deal with them.
 * @param {Object} url The relative url to be translated.
 * @return Returns the absolute url.
 */
function absPath(url) {
  var loc = location.href;
  loc = loc.substring(0, loc.lastIndexOf('/'));
  while (/^\.\./.test(url)) {
    loc = loc.substring(0, loc.lastIndexOf('/'));
    url = url.substring(3);
  }
  return loc + '/' + url;
}

/**
 * Searches for a button, within the given document, which has the given inner HTML.
 * @param {Object} document The document to search in.
 * @param {Object} innerHTML The button inner HTML.
 */
function getButtonByInnerHTML(document, innerHTML) {
  var retval = undefined;
  var buttons = document.getElementsByTagName('button');
  for (var ii = 0; !retval && ii < buttons.length; ii++) {
    if (buttons[ii].innerHTML == innerHTML) {
      retval = buttons[ii];
    }
  }
  return retval;
}


/**
 * Extracts from the query string the value, if any, of the parameter identified by parameterName.
 * @param {Object} queryString The query string from which the parameter value has to be extracted.
 * @param {Object} parameterName The name of the parameter which value has to be extracted.
 */
function getParameter(queryString, parameterName) {
  // Add '=' to the parameter name (i.e. parameterName=value)
  var parameterName = parameterName + '=';
  if (queryString.length > 0) {
    // Find the beginning of the string
    begin = queryString.indexOf(parameterName);
    // If the parameter name is not found, skip it, otherwise return the value
    if (begin != -1) {
      // Add the length (integer) to the beginning
      begin += parameterName.length;
      // Multiple parameters are separated by the '&' sign
      end = queryString.indexOf('&', begin);
      if (end == -1) {
        end = queryString.length
      }
      // Return the string
      return unescape(queryString.substring(begin, end));
    }
    // Return null if no parameter has been found
    return null;
  }
}
