/** ACO Global Javascripts **/

function clean(field)
{
	if (field.defaultValue == field.value){
		field.value = "";
	}
}

/** Function to show and hide a DIV **/
function showHide(id) {
	var state = document.getElementById(id).style.display;
	var div = document.getElementById(id);
	if (state == 'block') {
		div.style.display = 'none';
	} else {
		div.style.display = 'block';
    }
}

/** Show and hide a row **/
function displayRow(id) {
	var row = document.getElementById(id);
	if (row.className=="show") row.className="hide";
	else row.className="show";
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};



/** Show one row and hide all others **/
function displayOneRow(id, link) {

 var row = document.getElementById(id); 
 if (!row) {
    id = id.replace(/\./g, "");
    row = document.getElementById(id);
    }
 if (row.className=="show") {
	row.className="hide";
 }
 else{
	// Hide all the tr with class show
	var trlist = getElementsByClassName("show");
//	var trlist = document.getElementsByClassName('show');
	for ( var i = 0; i < trlist.length; i++ ) {
		trlist[i].className = "hide";
  	}
	// Show the selected tr
	//var row = document.getElementById(id);
	row.className="show";

	// Remove class red from all the links
//	var reda = document.getElementsByClassName('red');
	var reda = getElementsByClassName("red");
	for ( var i = 0; i < reda.length; i++ ) {
		reda[i].className = "";
 	}

  	// Add class red to the selected link
	link = link.replace(/\./g, "");
	var alink = document.getElementById(link);
	if (alink) {
		alink.className="red";
	}
  }
}


/** Tab Set Definition - Start **/
var tabLinks = new Array();
var contentDivs = new Array();

function init() {


  // Grab the tab links and content divs from the page
  var tabListItems = document.getElementById('tabs').childNodes;
  for ( var i = 0; i < tabListItems.length; i++ ) {
    if ( tabListItems[i].nodeName == "LI" ) {
      var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
      var id = getHash( tabLink.getAttribute('href') );
      tabLinks[id] = tabLink;
      // Check if Tab is really there
	  if (document.getElementById( id )){
		contentDivs[id] = document.getElementById( id );
	  }
    }
  }

  // Assign onclick events to the tab links, and
  // highlight the first tab
  var i = 0;

  for ( var id in tabLinks ) {
    tabLinks[id].onclick = showTab;
    tabLinks[id].onfocus = function() { this.blur() };
    if ( i == 0 ) tabLinks[id].className = 'selected';
    i++;
  }

  // Hide all content divs except the first
  var i = 0;

  for ( var id in contentDivs ) {
    if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
    i++;
  }

 //Go to Zubehor Tab
 if ( document.location.hash  && document.location.hash.substring(1) == "tab_Zubehoer"){
 	 showTab( document.location.hash.substring(1) );
 }
 else{
	    // If a articule number was supplied in the page URL,
	    //open the div with the article number from the url
		if(document.location.toString().indexOf("?") > -1){
			var alt = new String(document.location);
			var index = alt.indexOf("?");
			var iLen = 10;
			if (alt.indexOf("#", index+1) > -1) {
    			    var iEnd = alt.indexOf("#", index+1);
    			    iLen = iEnd-index-1;
    			}
			var number = alt.substr( index + 1, iLen );
			displayOneRow(number, 'AA' + number);
			//displayOneRow(number, number);
		}
	}

}

 function showTab( selectedId ) {
	if ( typeof selectedId != 'string' ) var selectedId = getHash( this.getAttribute('href') );

  // Highlight the selected tab, and dim all others.
  // Also show the selected content div, and hide all others.
  for ( var id in contentDivs ) {
    if ( id == selectedId ) {
      tabLinks[id].className = 'selected';
      contentDivs[id].className = 'tabContent';
    } else {
      tabLinks[id].className = '';
      contentDivs[id].className = 'tabContent hide';
    }
  }

  // Stop the browser following the link
  return false;
}

function getFirstChildWithTagName( element, tagName ) {
  for ( var i = 0; i < element.childNodes.length; i++ ) {
    if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
  }
}

function getHash( url ) {
  var hashPos = url.lastIndexOf ( '#' );
  return url.substring( hashPos + 1 );
}
/** Tab Set Definition - End **/

function tab(link){
  var tabListItems = document.getElementById('tabs').childNodes;
    for ( var i = 0; i < tabListItems.length; i++ ) {
    if ( tabListItems[i].nodeName == "LI" ) {
      var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
      var id = getHash( tabLink.getAttribute('href') );
      tabLinks[id] = tabLink;
      contentDivs[id] = document.getElementById( id );
	  if (tabLinks[id].className == 'selected')
		window.open(link+'/(tab)/'+id,'_blank');
    }
  }
}
