/* Copyright Scientec Internet Applications + Media GmbH - www.scientec.de */

/****** sofort ******/

window.onerror=catchError;
var newsession = true;
var wcont = window.name.split("-!-");
document.write('<style>#M .artikel .check{display:inline}</style>')



session();

function session()
{
  var zeit = new Date();
  cookiesession = getCookie("session");
  if (window.name.indexOf("HUESKEN") == -1 && cookiesession.indexOf("HUESKEN") == -1)
  {
    var t0 = zeit.getTime();
    sessionid = "HUESKEN"+t0;
    window.name = sessionid;
    var t1 = new Date(t0 + 1000*60*60*6);
    document.cookie = "session="+sessionid+"; expires="+t1.toGMTString()+"; path=/";
  }
  else
  {
    if(cookiesession.indexOf("HUESKEN") != -1)
    {
      var t0 = zeit.getTime();
      var t1 = new Date(t0 + 1000*60*60*6);
      document.cookie = "session="+cookiesession+"; expires="+t1.toGMTString()+"; path=/";
    }
    if (window.name.indexOf("HUESKEN") == -1)
    {
      sessionid = cookiesession;
      window.name = sessionid;
    }
    else { sessionid = wcont[0] }
  }
  newsession = false;
}


/****** nach domready ******/


function init()
{
  SPRACHE=$("html")[0].lang;
  HTML();
  readsession();
  Mailto();
  Links();
  searchStringHighlight();
  if(window.FORM_beding)document.getElementById("Suchbegriff").value=FORM_beding;
  //sitecloud()
}


function readsession()
{
  var zeit = new Date();
  if (!newsession)
  {
    var jsHttp = false;

    /*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest) function XMLHttpRequest() { return new ActiveXObject('Microsoft.XMLHTTP')}@end @*/        

    if (typeof XMLHttpRequest != 'undefined'){jsHttp = new XMLHttpRequest()}
    if (jsHttp)
    {
      jsHttp.open('GET', '/sessions/'+sessionid+'.js?'+zeit.getTime(), true);
      jsHttp.onreadystatechange = function ()
      {
        if (jsHttp.readyState == 4)
        {
          if(jsHttp.status == 200)
          {
            basketverkauf = [];
            basketanzahl = [];
            baskettext = [];
            verkaufszahl = 0;

            for(var c = 0; c < document.getElementsByTagName("input").length; c++)
            {
              if(document.getElementsByTagName("input")[c].id.indexOf("bbox") != -1 && document.getElementsByTagName("input")[c].checked == true)
              {
                var cid = document.getElementsByTagName("input")[c].id.substr(4);
                document.getElementById('bbox'+cid).checked = false;
                document.getElementById('abox'+cid).style.display = '';
                document.getElementById('bbox'+cid).style.display = 'none';
              }
            }
            eval(jsHttp.responseText);
            {
              for (var artikel in basketverkauf)
              {
                if (basketverkauf[artikel] > 0)
                {
                  verkaufszahl++;
                }
                if (document.getElementById('bbox'+artikel))
                {
                  if (basketverkauf[artikel] > 0)
                  {
                    document.getElementById('abox'+artikel).style.display='none';
                    document.getElementById('bbox'+artikel).style.display='inline';
                    document.getElementById('bbox'+artikel).checked = true;
                  }
                  else
                  {
                    basket[artikel] = '';
                  }
                }
              }
            }
            if (document.getElementById('basket'))
            {
              document.getElementById('basket').innerHTML = minibaskethtml;
            }
          }
          if (document.getElementById('miniwarenkorb'))
          {
             document.getElementById('miniwarenkorb').style.display = (window.verkaufszahl)? "block" : "none" 
          }
        }
      }
      jsHttp.send(null);
    }
  }
}


function setarticle(obj,artno,sprache,cval,cbasket,del,anzahl,text)
{
  if (text && text != undefined)
  {
    text = encodeURIComponent(text.replace(/\r/g,"").replace(/\n/g,"-!!-"));
  }
  else
  {
    text = "";
  }
  var zeit = new Date();
  if (obj) { name1=obj.name }
  if (obj.tagName == "INPUT"){ cval = ''}
  var jsHttp = false;

  /*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest) function XMLHttpRequest() { return new ActiveXObject('Microsoft.XMLHTTP')}@end @*/        

  if (typeof XMLHttpRequest != 'undefined'){jsHttp = new XMLHttpRequest()}
  if (jsHttp)
  {
    if(anzahl==0) del = "del";
    if(!anzahl) anzahl = 1;
    jsHttp.open('GET', '/cgi-bin/shop/setarticle.pl?sessionid='+sessionid+'&artno='+artno+'&sprache='+sprache+'&val='+cval+'&del='+del+'&anzahl='+anzahl+'&text='+text+'&timestamp='+zeit.getTime(), true);
    jsHttp.onreadystatechange = function ()
    {
      if (jsHttp.readyState == 4)
      {
        eval(jsHttp.responseText);
        readsession();
        if (cbasket == "1")
        {
          document.getElementById('popup-inhalt').getElementsByTagName('iframe')[0].src='/cgi-bin/shop/warenkorb.pl?sessionid='+sessionid+'&sprache='+sprache;
        }
      }
    }
    jsHttp.send(null);
  }
}

function HTML()
{
  if(SPRACHE == "en")
  {
    $('#suche').after('<div id="miniwarenkorb" class="block"><div class="titel">Selected Items</div><div class="in"><div id="basket"></div><div class="button"><button type="button">Buy now</button></div></div></div>');
    mwst0 = "<a href='/agb_e.html?a#endpreis'>Price</a>";
//    mwst7 = "Price including 7 % VAT";
//    mwst19 = "Price including 19 % VAT";
    mwst7 = "<a href='/agb_e.html?a#endpreis'>Price</a> including VAT";
    mwst19 = "<a href='/agb_e.html?a#endpreis'>Price</a> including VAT";
    mwst = ", excl. <a href='/versandkosten_e.html'>shipment</a>";
    $('body').append('<div id="popup"><div class="abdeckung"></div><div id="popup-titel"><div id="popup-titel-text">Order</div><div id="popup-titel-nav"><a href="#">Back to shop</a></div></div><div id="popup-inhalt"><iframe src="/onTEAM/layout/leer.html" frameborder="0" id="popup-iframe" allowtransparency="1"></iframe></div><div id="popup-hintergrund"></div></div>')
    $("#pfad").before('<div id="print" title="Print this page printerfriendly"></div>')
    $('#miniwarenkorb').bind('click', function() { warenkorb('e')});
  }
  else
  {
    $('#suche').after('<div id="miniwarenkorb" class="block"><div class="titel">Warenkorb</div><div class="in"><div id="basket"></div><div class="button"><button type="button">Zur Kasse</button></div></div>');
    mwst0 = "<a href='/agb.html?a#endpreis'>Preis</a> ";
//    mwst7 = "Preis inkl. 7 % Mehrwertsteuer, ";
//    mwst19 = "Preis inkl. 19 % Mehrwertsteuer";
    mwst7 = "<a href='/agb.html?a#endpreis'>Preis</a> inklusive Mehrwertsteuer, ";
    mwst19 = "<a href='/agb.html?a#endpreis'>Preis</a> inklusive Mehrwertsteuer, ";
    mwst = " exklusive <a href='/versandkosten.html'>Versandkosten</a>";
    $('body').append('<div id="popup"><div class="abdeckung"></div><div id="popup-titel"><div id="popup-titel-text">Bestellvorgang</div><div id="popup-titel-nav"><a href="#">Zum Shop</a></div></div><div id="popup-inhalt"><iframe src="/onTEAM/layout/leer.html" frameborder="0" id="popup-iframe" allowtransparency="1"></iframe></div><div id="popup-hintergrund"></div></div>')
    $("#pfad").before('<div id="print" title="Diese Seite druckerfreundlich drucken"></div>')
    $('#miniwarenkorb').bind('click', function() { warenkorb('d')});
  }

  $('#print').bind('click', function() { self.print() });
  $('#popup-titel-nav a').bind('click', function() { popup('off'); return false });
  $('#popup-hintergrund').bind('click', function() { popup('off') });

  if(document.title.indexOf("Fehler/Error")!=-1){$('#home').before('<div id="e404">Seite nicht gefunden<br>Page not found</div>')}

  $('.mwst').each(function(){
    if($(this).html()==7)$(this).html(mwst7+mwst);
    else if($(this).html()==0)$(this).html(mwst0+mwst);
    else if($(this).html()==19)$(this).html(mwst19+mwst);
    $(this).css("display","block")
  });

  $('#copyright').before($('#seiten').clone());
  $('#miniwarenkorb').after('<div id="cc"></div>');
  $('#R').append('<div id="ssl"><a href="http://www.instantssl.com" rel="nofollow" title="Instant SSL Certificate Secure Site" target="_blank"></a></div>')


}


function warenkorb(sprache)
{
  popup("on");
  var zeit = new Date();
  document.getElementById('popup-inhalt').getElementsByTagName('iframe')[0].src='/cgi-bin/shop/warenkorb.pl?sessionid='+sessionid+'&sprache='+sprache+'&timestamp='+zeit.getTime();
}


function validateForm() {
 var send = 1;
 if (send == 1){if (document.contact.name.value == ""){SPRACHE=="de" ? alert("Bitte tragen Sie Ihren Namen ein.") : alert("Please enter your name."); send = 0; document.contact.name.focus();}}
 if (send == 1){if (document.contact.nachricht.value == ""){SPRACHE=="de" ? alert("Bitte tragen Sie Ihre Nachricht ein.") : alert("Please enter your message."); send = 0; document.contact.nachricht.focus();}}
 if (send==1){return true}
 else {return false}
}


function popup(modus)
{
	if(modus=="on")
	{
        document.getElementById("popup-iframe").src="/onTEAM/layout/leer.html";
        document.getElementsByTagName("body")[0].style.overflow = "hidden";
        document.getElementById("popup").style.visibility = "visible";
        document.getElementById("popup").style.left = "0";
	}
	else if(modus=="off")
	{
        document.getElementById("popup-iframe").src="/onTEAM/layout/leer.html";
        if(window.location.href.indexOf("https:") != -1)
        {
          window.location.href = window.location.href.replace("https:","http:").replace(window.location.search,"");
        }
        else
        {
          readsession();
          document.getElementsByTagName("body")[0].style.overflow = "auto";
          document.getElementById("popup").style.visibility = "hidden";
          document.getElementById("popup").style.left = "-200%";
	  }
	}
}


/*Allgemeine Funktionen*/

function Mailto()
{
	$('em.em').each(function(){
		var EM = $(this).text().replace(/~at~/i,"@");
		$(this).html('<a href="mailto:'+EM+'">'+EM+'</a>');
	});
}

function Links()
{
	$('#nav a[href='+document.location.pathname+']').addClass(function(){ return "on" });
	if($('#pfad').get(0))
	{
		$('#nav a').addClass(function(){
 			if($('#pfad').html().indexOf($(this).attr('href'))!=-1) return "aktiv" 
 			else return  
		});
	}
}


function highlightWord(node,word) {
  if (node.hasChildNodes){
    var hi_cn;
    for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++){
      highlightWord(node.childNodes[hi_cn],word);
    }
  }
  if (node.nodeType == 3 && node.parentNode.tagName!="TEXTAREA") {
    tempNodeVal = node.nodeValue.toLowerCase();
    tempWordVal = word.toLowerCase();
    if (tempNodeVal.indexOf(tempWordVal) != -1) {
      pn = node.parentNode;
      if (pn.className != "searchword") {
        nv = node.nodeValue;
        ni = tempNodeVal.indexOf(tempWordVal);
        before = document.createTextNode(nv.substr(0,ni));
        docWordVal = nv.substr(ni,word.length);
        after = document.createTextNode(nv.substr(ni+word.length));
        hiwordtext = document.createTextNode(docWordVal);
        hiword = document.createElement("span");
        hiword.className = "searchword";
        hiword.title = "Suchtreffer";
        hiword.appendChild(hiwordtext);
        pn.insertBefore(before,node);
        pn.insertBefore(hiword,node);
        pn.insertBefore(after,node);
        pn.removeChild(node);
      }
    }
  }
}


function searchStringHighlight() {
  if (!document.createElement || !document.getElementsByTagName || !document.getElementById("M")) return;
  ref = document.referrer;
//ref = "http://www.google.de/?hl=de&source=hp&q=Berlin+Orden&btnG=Google-Suche&meta=&aq=f&oq=Berlin";
  if (ref.indexOf('?') == -1 || ref.indexOf('google') == -1) return;// || ref.indexOf('search') == -1
  qs = ref.substr(ref.indexOf('?')+1);
  qsa = qs.split('&');
  for (i=0;i<qsa.length;i++) {  
    qsip = qsa[i].split('='); 
    if (qsip.length == 1) continue;
    if (qsip[0] == 'q' || qsip[0] == 'as_q') { /* q= for Google, p= for Yahoo*/

      words = unescape(qsip[1].replace(/\+/g,' ')).replace(/\"/g,'').split(/\s+/);
      highlightedWords="";
      for (var w=0;w<words.length;w++) {
        if(words[w].length>3) {
          highlightWord(document.getElementById("M"),words[w]);
          highlightedWords+=words[w]+" ";
        }
      }
    }
  }
  if(highlightedWords!="" && document.getElementById("Suchbegriff").value=="")
  {
    document.getElementById("Suchbegriff").value+=highlightedWords;
    document.getElementById("R").innerHTML="<div id=gsearch></div>"+document.getElementById("R").innerHTML;

    if(document.getElementById("gsearch"))
    {
      document.getElementById("R").innerHTML+='<iframe src="/onTEAM/layout/gsearch.html" id="searchIFR" name="searchIFR"></iframe>';
      
      searchIFR.onload=function()
      {
        if(document.getElementById("searchIFR").contentWindow.document.getElementById("beding"))
        {
          document.getElementById("searchIFR").contentWindow.document.getElementById("beding").value = highlightedWords;
          if(document.getElementsByTagName("html")[0].lang == "en")
          {
            document.getElementById("searchIFR").contentWindow.document.getElementById("language").value = "e";
          }
          else
          {
            document.getElementById("searchIFR").contentWindow.document.getElementById("language").value = "d";
          }
          document.getElementById("searchIFR").contentWindow.document.getElementById("search").submit();
        }
      }
    }
  }
}

function catchError(Nachricht,Datei,Zeile){
  fehler="";
  fehler+="-!-Fehlermeldung="+escape(Nachricht);
  fehler+="-!-Datei="+escape(Datei);
  fehler+="-!-Zeile="+escape(Zeile);
  fehler+="-!-Agent="+escape(navigator.userAgent);
}


function sitecloud()
{
  var zeit = new Date();
  var statimg = new Image();
  var logdata="";

  logdata+="&S="+window.name;
  logdata+="&U="+document.location.href;
  logdata+="&T="+zeit.getTime();
  logdata+=(window.fehler)?"&F="+fehler:"";

  logdata+=(document.referrer!="")?(document.referrer.indexOf(window.location.host)==-1)?"&R="+document.referrer:"&R=i/"+document.referrer:"&R=0";
  statimg.src = encodeURI("http://huesken.sitecloud.de/?"+logdata);
}

function getCookie(name)
{ 
  var c=document.cookie+";";
  start=c.indexOf(name)+name.length+1;
  stop=c.indexOf(";",start);
  keks=c.substring(start,stop);
  return keks
}
