var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);
var version="";
var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);
if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){
  version="n3";
}
else{
  version="n2";
}
function blurLink(theObject){
  if(msie4){
    theObject.blur();
  }
}

function showProductGroupArrow(index)
{
  var arrow = document.getElementById("productgroupscontent:productGroups:" + index + ":arrow");
  arrow.style.visibility = 'visible';
}

function hideProductGroupArrow(index)
{
  var arrow = document.getElementById("productgroupscontent:productGroups:" + index + ":arrow");
  arrow.style.visibility = 'hidden';
}

function showVarietyGroupArrow(index)
{
  var arrow = document.getElementById("varietygroupscontent:varietyGroups:" + index + ":arrow");
  arrow.style.visibility = 'visible';
}

function hideVarietyGroupArrow(index)
{
  var arrow = document.getElementById("varietygroupscontent:varietyGroups:" + index + ":arrow");
  arrow.style.visibility = 'hidden';
}

function switchTextarea()
{
  var area = document.getElementById("cartdetailcontent:cartdetailform:alternativeAdress");
  var changeTextarea = document.getElementById("cartdetailcontent:cartdetailform:changeTextarea");

  if(changeTextarea.checked)
    area.disabled=false;
  else
    area.disabled=true;
}

function downloadCombo1Changed()
{
  var comboValue = document.getElementById("downloadscontent:categoriesForm:dlg_1").value;
  location.href = document.getElementById(comboValue).href;
  //location.href = comboValue;
}

function downloadCombo2Changed()
{
  var comboValue = document.getElementById("downloadscontent:categoriesForm:dlg_2").value;
  location.href = document.getElementById(comboValue).href;
  //location.href = comboValue;
}

function downloadCombo3Changed()
{
  var comboValue = document.getElementById("downloadscontent:categoriesForm:dlg_3").value;
  location.href = document.getElementById(comboValue).href;
  //location.href = comboValue;
}

function downloadCombo4Changed()
{
  var comboValue = document.getElementById("downloadscontent:categoriesForm:dlg_4").value;
  location.href = document.getElementById(comboValue).href;
  //location.href = comboValue;
}

function downloadCombo5Changed()
{
  var comboValue = document.getElementById("downloadscontent:categoriesForm:dlg_5").value;
  location.href = document.getElementById(comboValue).href;
  //location.href = comboValue;
}

function naviClick(row)
{
  var id = document.getElementById("navileft:naviItems:" + row + ":naviForm:naviButton").id;
  clickButton(id);
  return false;
}

function handleReturnKey(e, id)
{
  if (!e) var e = window.event
  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;
  // process only the Enter key
  if (code == 13)
  {
    // cancel the default submit
    e.returnValue=false;
    e.cancel = true;
    // submit the form by programmatically clicking the specified button
    clickButton(id);
    return false;
  }
  return true;
}

function clickButton(Id)
{
  var fireOnThis = document.getElementById(Id);
  if (document.createEvent) //Moz.
  {
    var evObj = document.createEvent('MouseEvents');
    evObj.initEvent('click', true, false );
    fireOnThis.dispatchEvent(evObj);
  }
  /*else if (document.createEventObject) //IE, does not work...
  {
    fireOnThis.fireEvent('onclick');
  }*/
  else //IE, this works in 6 and 7...
  {
    fireOnThis.click();
  }
}

function openPopupWindow(url, name, width, height)
{
  //Remove special characters from name
  name = name.replace(/\/|\-|\./gi, "");

  //Remove whitespaces from name
  var whitespace = new RegExp("\\s","g");
  name = name.replace(whitespace,"");

  //get invisible element...
  url = document.getElementById(url).href;

  datasheetPopupWindow = window.open(encodeUrl(url),name, "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=yes, width=" + width + ", height=" + height );
  if (!datasheetPopupWindow.opener) datasheetPopupWindow.opener = self;
}

function checkAddressForm()
{
  var pfad = document.getElementById('cartdetailaddressForm:firma1');

  if (pfad.value=="") {
    alert('Bitte das Feld Firma ausfüllen!');
    pfad.focus();
    return false;
  }

  pfad = document.getElementById('cartdetailaddressForm:strasse');

  if (pfad.value=="") {
    alert('Bitte das Feld Strasse ausfüllen!');
    pfad.focus();
    return false;
  }

  pfad = document.getElementById('cartdetailaddressForm:land');

  if (pfad.value=="") {
    alert('Bitte das Feld Land ausfüllen!');
    pfad.focus();
    return false;
  }

  pfad = document.getElementById('cartdetailaddressForm:plz');

  if (pfad.value=="") {
    alert('Bitte das Feld PLZ ausfüllen!');
    pfad.focus();
    return false;
  }

  pfad = document.getElementById('cartdetailaddressForm:stadt');

  if (pfad.value=="") {
    alert('Bitte das Feld Stadt ausfüllen!');
    pfad.focus();
    return false;
  }

  return true;
}


function encodeUrl(url)
{
  if (url.indexOf("?")>0)
  {
    encodedParams = "?";
    parts = url.split("?");
    params = parts[1].split("&");
    for(i = 0; i < params.length; i++)
    {
      if (i > 0)
      {
        encodedParams += "&";
      }
      if (params[i].indexOf("=")>0) //Avoid null values
      {
        p = params[i].split("=");
        encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
      }
      else
      {
        encodedParams += params[i];
      }
    }
    url = parts[0] + encodedParams;
  }
  return url;
}

//Get all the elements starting with the given id of the given tag.
function getElementsByIdStartWith(id,tag) {
  if(!tag) tag = "*";
  var anchs =  document.getElementsByTagName(tag);
  var total_anchs = anchs.length;
  var regexp = new RegExp('^' + id);
  var id_items = new Array()

  for(var i=0;i<total_anchs;i++) {
    var this_item = anchs[i];
    if(regexp.test(this_item.id)) {
      id_items.push(this_item);
    }
  }
  return id_items;
}

//Get all the elements ending with the given id of the given tag.
function getElementsByIdEndWith(id,tag) {
  if(!tag) tag = "*";
  var anchs =  document.getElementsByTagName(tag);
  var total_anchs = anchs.length;
  var regexp = new RegExp(id + '$');
  var id_items = new Array()

  for(var i=0;i<total_anchs;i++) {
    var this_item = anchs[i];
    if(regexp.test(this_item.id)) {
      id_items.push(this_item);
    }
  }
  return id_items;
}

//Get all the elements starting with the given id and ending with the other id of the given tag.
function getElementsByIdStartWithEndWith(idStart,idEnd,tag) {
  if(!tag) tag = "*";
  var anchs =  document.getElementsByTagName(tag);
  var total_anchs = anchs.length;
  var regexp = new RegExp('^' + idStart + '.*' + idEnd + '$');
  var id_items = new Array()

  for(var i=0;i<total_anchs;i++) {
    var this_item = anchs[i];
    if(regexp.test(this_item.id)) {
      id_items.push(this_item);
    }
  }
  return id_items;
}

function getNullEscapedElementData(outerElementId)
{
  var returnText = "";
  var innerElement = document.getElementById(outerElementId).firstChild;
  if(innerElement)
  {
    var text = innerElement.data;
    if(text)
    {
      returnText = text;
    }
  }
  return returnText;
}

function trim(zeichenkette)
{
  // Erst führende, dann abschließende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zurückliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}


function escapeRegexpChars(str)
{
  var specials = new RegExp('[.*+?|()\\[\\]{}\\\\]', 'g'); // .*+?|()[]{}\
  return str.replace(specials, '');
}


function searchInString(searchPattern, textString)
{
  searchPattern = trim(searchPattern);
  searchPattern = escapeRegexpChars(searchPattern);
  var regexp = new RegExp(searchPattern,'i');
  var matchPos = textString.search(regexp);
  return (matchPos!=-1);
}

function getUrlParameter(param)
{
  HTTP_GET_VARS=new Array();
  strGET=document.location.search.substr(1,document.location.search.length);
  if(strGET!='')
  {
    gArr=strGET.split('&');
    for(i=0;i<gArr.length;++i)
    {
      v='';
      vArr=gArr[i].split('=');
      if(vArr.length>1){
        v=vArr[1];
      }
      HTTP_GET_VARS[unescape(vArr[0])]=unescape(v);
    }
  }
  if(HTTP_GET_VARS[param])
    return HTTP_GET_VARS[param];
  else
    return '';
}

function suche(lang)
{
  var searchFor = document.getElementById('navileft:searchForm:searchFor').value;
  searchFor = trim(searchFor);
  if(searchFor.length>2)
  {
    var searchWhere = document.getElementById('navileft:searchForm:searchWhere').value;
    if(searchWhere == '1')
    {
      window.location = 'search.html?redirect=' + escape('searchdbp.html?lang=' + lang + '&searchFor=' + escape(searchFor));
    }
    else
    {
      window.location = 'search.html?redirect=' + escape('searchdm.html?lang=' + lang + '&searchFor=' + escape(searchFor));
    }
  }
}

function dbpSuche()
{
  var searchFor = getUrlParameter('searchFor');
  if(searchFor.length>2)
  {
    var count = 0;
    var allVarietyTables = getElementsByIdStartWith('varietytable_','table');
    var hit = false;
    //search in varieties
    for (i=0;i<allVarietyTables.length;i++)
    {
      var varietyHit = false;
      var varietyTable = allVarietyTables[i];
      var tableId = varietyTable.id;
      var elementId = tableId.replace('varietytable_', '');
      var varietyText1 = getNullEscapedElementData('variety_text1_' + elementId);
      var varietyText2 = getNullEscapedElementData('variety_text2_' + elementId);
      var varietyText = varietyText1 + ' ' + varietyText2;
      varietyHit = searchInString(searchFor,varietyText);
      if(varietyHit)
      {
        hit = true;
        var varietyItemRows = getElementsByIdStartWith('item_row_'+elementId,'tr');
        for (j=0;j<varietyItemRows.length;j++)
        {
          var itemRow = varietyItemRows[j];
          itemRow.style.display = ''; //default for tag
          count++;
        }
        varietyTable.style.display = ''; //default for tag
      }
    }

    //search artno in items
    if(hit == false)
    {
      var artNo = searchFor;
      var itemRows = getElementsByIdStartWithEndWith('item_row_', '_' + artNo,'tr');
      for (i=0;i<itemRows.length;i++)
      {
        itemRow = itemRows[i];
        var itemRowId = itemRow.id;
        elementId = itemRowId.replace('item_row_', '');
        elementId = elementId.replace('_' + artNo, '');
        itemRow.style.display = '';
        document.getElementById('varietytable_'+elementId).style.display = '';
        var rowspanTD = document.getElementById('rowspan_varietytable_'+elementId);
        rowspanTD.rowSpan = 3; //2 standard + 1 for this row
        count++;
      }
    }

    document.getElementById('count').firstChild.data = count;
    document.getElementById("progressLabel").style.display = "none";
  }
}

function dmSuche()
{
  var searchFor = getUrlParameter('searchFor');
  if(searchFor.length>2)
  {
    var count = 0;
    var allDownloadTables = getElementsByIdStartWith('downloadtable_','table');
    var hit = false;
    //search in downloads
    for (i=0;i<allDownloadTables.length;i++)
    {
      var downloadTable = allDownloadTables[i];
      var tableId = downloadTable.id;
      var elementId = tableId.replace('downloadtable_', '');
      var downloadName = getNullEscapedElementData('download_name_' + elementId);
      hit = searchInString(searchFor,downloadName);
      if(hit)
      {
        count++;
        downloadTable.style.display = ''; //default for tag
      }
      else
      {
        var downloadDescription = getNullEscapedElementData('download_description_' + elementId);
        hit = searchInString(searchFor,downloadDescription);
        if(hit)
        {
          count++;
          downloadTable.style.display = ''; //default for tag
        }
      }
    }

    document.getElementById('count').firstChild.data = count;
    document.getElementById("progressLabel").style.display = "none";
  }
}
