// get a specified query parameter in the URI location
function getParameter(param) {
  var uri = document.URL;


  var _ret;
  var index = uri.indexOf(param + "=");

  if (index == -1) {
    _ret = null;
  } else {
    index =  index + param.length + 1;
    var end  = uri.indexOf("&", index);
    var _ret;
    
    if (end == -1) {
      _ret = uri.substring(index);
    } else {
      _ret = uri.substring(index, end);
    }
  }
  return _ret;
}

// Activate a style sheet
function activateLink(link) {
  link.setAttribute("rel", "stylesheet"); // work around for IE6 :-/
  link.disabled = false;
}

// Desactivate a style sheet
function desactivateLink(link) {
  link.disabled = true;
}

// Given an option, activate the appropriate style sheet and set the
// default option.
function option(param) {
  var _param = getParameter(param);

  if (!document.getElementById) {
    return;
  }

  if (_param != null) {
    var _paramLink = document.getElementById(_param + "Link");
    var _paramOption = document.getElementById(_param + "Option");

    if (_paramLink != null) {
      activateLink(_paramLink);
      if (_paramOption != null) {
	_paramOption.selected = true;
      }
    }
  }
}

// Event listener for input element
// @@ Not in used.
function checkImmediate(e) {    
  var _paramCheckbox = e.currentTarget;  
  var _paramLink = document.getElementById(_paramCheckbox.getAttribute("name")
					   + "Link");

  if (_paramLink != null && _paramCheckbox != null) {
    if (_paramCheckbox.checked) {
      activateLink(_paramLink);
    } else {
      desactivateLink(_paramLink);
    }
  }
}


// Given a checkbox option, activate the appropriate style sheet and the
// default for the checkbox.
function checkbox(param) {
  var _param = getParameter(param);
  var _paramCheckbox = document.getElementById(param + "Checkbox");

  if (_param == "1") {
    var _paramLink = document.getElementById(param + "Link");
  
    if (_paramLink != null) {
      activateLink(_paramLink);
      if (_paramCheckbox != null) {
	_paramCheckbox.checked = true;
      }
    }
  }

  if (document
      && document.implementation
      && document.implementation.hasFeature("MouseEvents", "2.0")
      && _paramCheckbox != null) {
    // allow immediate changes on the document
    // @@ Not in used
    // _paramCheckbox.addEventListener("click", checkImmediate, false);
  }
}
