
function setActiveStyleSheet(title) {
   var i, links;
   links = document.getElementsByTagName("link");
   for(i=0; i < links.length; i++) {
      if (links[i].getAttribute("rel").indexOf("stylesheet") != -1 && links[i].getAttribute("title")) {
         if (links[i].getAttribute("title") == title) {
            links[i].disabled = false;
         } else {
            links[i].disabled = true;
         }
      }
   }
}

function getActiveStyleSheet() {
   var i, links;
   links = document.getElementsByTagName("link");
   for(i=0; i < links.length; i++) {
      if (links[i].getAttribute("rel").indexOf("stylesheet") != -1 &&
          links[i].getAttribute("title") &&
          !links[i].disabled) {
         return links[i].getAttribute("title");
      }
   }
   return "";
}

function toggleStyleSheet() {
  
  try {
  	var navigationFrame = window.top.frames["eAISNavigationBase"].frames["eAISNavigation"];
  } catch (e) {
  	// Do nothing, we are not in a frameset
  }
  // Get document's base, to compare with other hrefs
  var base = getBase( location.href );
	
  if (getActiveStyleSheet() == "Show Amendments Changes") {
    setActiveStyleSheet("Hide Amendments Changes");
    unpadHrefs(window); // Change in the current window
    if (navigationFrame)
    	unpadHrefs(navigationFrame);
  } else {
    setActiveStyleSheet("Show Amendments Changes");
    padHrefs(window, base);
    if (navigationFrame) 
    	padHrefs(navigationFrame, base);
  }
}


// ============= Menu highlighting ============
function menu_on(el) {
//  if (alreadyActivated(el)) return;
//  el.style.backgroundColor = '#aaa';
}

function menu_off(el) {
//  if (alreadyActivated(el)) return;
//  el.style.backgroundColor = 'transparent';
}

function alreadyActivated(el) {
  if (el.id.indexOf("menu-amdt-") != -1) {
    var activesheet = getActiveStyleSheet();
    var status = el.id.substr(10); // position of Hide and Show in the id
    return (activesheet.indexOf(status) != -1); // return true if the clicked stylesheet is already activated
  }
}

// ============ Show amendments persistence ==============

/** value appended to links which is checked for to display amdts by default*/
SHOW_AMDT = "amdt=show";

/** 
 * Adds at the end of all internal links SHOW_AMDT 
 * @param win Window which contains the links to be updated
 */
function padHrefs(win, base) {
	var elems = win.document.getElementsByTagName("A");
	for ( i=0; i < elems.length; i++ ) {
		// See if they are relative = contain the base
		if ( elems[i].href.indexOf( base ) != -1 || elems[i].target == "_self") {
			if (elems[i].search.search(SHOW_AMDT) != -1) //Skip if it already exists
				continue;
			// Remember the hash, because we are redoing the url by hand
			if (elems[i].search == "") { 
				elems[i].search = "?" + SHOW_AMDT;
			} else {
				elems[i].search = elems[i].search +"&"+ SHOW_AMDT;
			}
			remakeUrl(elems[i]); // Fix IE bug
		}
	}
}

/**
 * Same as above but without the base checking
 * Warning: this will pad links that point externally. Make
 * sure there are no such links in the window from which you
 * call this function
 */
function padHrefsNoBase(win) {
	var elems = win.document.getElementsByTagName("A");
	for ( i=0; i < elems.length; i++ ) {
		if (elems[i].search.search(SHOW_AMDT) != -1) //Skip if it already exists
					continue;
		if (elems[i].search == "") { 
			elems[i].search = "?" + SHOW_AMDT;
		} else {
			elems[i].search = elems[i].search +"&"+ SHOW_AMDT;
		}
		remakeUrl(elems[i]); // Fix IE bug
	}
}

/** 
 * Remove at the end of all internal links SHOW_AMDT, if it exists 
 * @param win Window in which to do the unpadding
 */
function unpadHrefs(win) {
	// go through all hrefs and see if they are padded.
	var elems = win.document.getElementsByTagName("A");
	for ( i=0; i < elems.length; i++ ) {
		if ( elems[i] == null ) return;
			
		var attr = getAttributes( elems[i].search );
		var result = new String();	
		for ( j=0; j<attr.length; j++) {
			if (attr[j] != SHOW_AMDT) 
				result = addAttribute( result, attr[j] );
		}
		
		// IE Bug. IE sometimes forgets the hash when we update the search
		var temp = elems[i].hash.split('#')[1];
		elems[i].search = result;
		if (temp != null)
			elems[i].hash = temp;
			
		remakeUrl(elems[i]); // Fix IE bug
	}
	
}

/** Checks if we should show or not the amendments 
 *  return Boolean True means we should show the amendments
 */
function isAmendmentDisplayed() {
	attr = getAttributes( location.search );
	for ( i=0; i<attr.length; i++) {
		if (attr[i] == SHOW_AMDT){
			return true;
		}
	}
	return false;
}

/**
 * Toggle on amendment on all links in the calling window
 */
function toggleAmendment() {
	if (isAmendmentDisplayed()) {
		// Base is the the URL of the content window
		//var base = getBase(window.top.frames["eAISContent"].document.location.href);
		//padHrefs(window, base);
		padHrefsNoBase(window);
	}
}


/** Get the attributes in an array from a string starting with a ?*/
function getAttributes( fromHere ) {
	attributes = fromHere.split("?")[1]; //Get the string after the ?
	if ( attributes == null ) {
		return "";
	} else return attributes.split("&"); //If there are several attributes, split them
}
/**
 *  Adds an attribute to the list,; separated by &
 * list: String containing attributes.
 * attribute: String containing the attribute in attr=value form
 */
function addAttribute( list, attribute ) {
	separator = "&";
	if (list="") {
		separator = "";
	}
	return list + separator + attribute;
}

/** Strips the end of an URL, to get a kind of base */
function getBase( href ) {
	// find position of last /
	last = href.lastIndexOf("/");
	// Basename is from 0 to that
	if (last == -1) return "";
	return href.substring(0,last);
}

/**
 * Function to bypass IE braindamage search / hash ordering
 * It reorders properly the href part with search and anchor.
 * @param a location which has search, href and hash attributes
 */
function remakeUrl(loc) {

	var browserName = navigator.appName;
	if (browserName == "Microsoft Internet Explorer") {
	
		// Strip search part
		var temp = loc.href.split('?')[0];
		// Strip Anchor part
		temp = temp.split('#')[0];
		// Append search
		temp = temp + loc.search;
		// Append anchor
		temp = temp + loc.hash;
		
		loc.href = temp;
	}
}





