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

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

var newsession = true;
var wcont = window.name.split("-!-");


session();

function session()
{
  var zeit = new Date();
  cookiesession = getCookie("session");
  if (window.name.indexOf("RITSCHER") == -1 && cookiesession.indexOf("RITSCHER") == -1)
  {
    var t0 = zeit.getTime();
    sessionid = "RITSCHER"+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("RITSCHER") != -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("RITSCHER") == -1)
    {
      sessionid = cookiesession;
      window.name = sessionid;
    }
    else { sessionid = wcont[0] }
  }
  newsession = false;
}


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


function init()
{
  HTML();
  readsession();
  Mailto();
 // warenkorb("d")
}


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 = [];
            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';
                document.getElementById('cbox'+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;
                    document.getElementById('cbox'+artikel).style.display='block';
                  }
                  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)
{
  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)
  {
    jsHttp.open('GET', '/cgi-bin/shop/setarticle.pl?sessionid='+sessionid+'&artno='+artno+'&sprache='+sprache+'&val='+cval+'&del='+del+'&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()
{
    $('.div-logo').after('<div id="miniwarenkorb"><div class="titel">Warenkorb</div><div class="in"><div id="basket"></div><div class="button"><button type="button">Zur Kasse</button></div></div>');
    $('.div-logo').after('<div id="kreditkarten" title="Akzeptierte Kreditkarten"></div>');
    $('body').append('<div id="popup"><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>')

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


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 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 getCookie(name)
{ 
  var c=document.cookie+";";
  start=c.indexOf(name)+name.length+1;
  stop=c.indexOf(";",start);
  keks=c.substring(start,stop);
  return keks
}

