window.cart = new shoppingcart()

function shoppingcart()    {
    this.autor = "frank@scooter-attack.com"
    this.id = "innercart"
    this.session = ""
    this.language = "d"
    this.currency = "EUR"
    this.altcurrency = (this.currency=="DM") ? "EUR" : "DM"
    this.cookieCart = new Array()
    this.Artikel = new Array()
    this.Artikel['nr'] = new Array()
    this.Artikel['bez'] = new Array()
    this.Artikel['mwst'] = new Array()
    this.Artikel['bpreis'] = new Array()
    this.Artikel['preis'] = new Array()
    this.Artikel['katbpreis'] = new Array()
    this.Artikel['katpreis'] = new Array()
    this.Artikel['gew'] = new Array()
    this.Artikel['anzahl'] = new Array()
    this.Artikel['lager'] = new Array()
    this.Artikel['preisfix'] = new Array()
    this.index = 1
    this.add = add_item
    this.wipe = clear_all
    this.update = update_anzahl
    this.getSum = getSum
    this.getCart = getHTMLCart
    this.getCartBig = getHTMLCartBigNew
    this.getCartBigContent = getHTMLCartBig
    this.getItems = getHTMLItems
    this.refresh = refresh_all
    this.checkArtikel = checkArtikel
    this.del = deleteArtikel
}

function refresh_all() {
    var loc = location.href;
    var pos = loc.lastIndexOf("/");
    var skript = loc.substring(pos+1,loc.length);
//    if (skript=="cart.php") {
        var text = this.getCartBig();
        if (document.layers) {
            var mylayer = top.main.document.layers[this.id].document
            mylayer.open();
            mylayer.write(text);
            mylayer.close();
        }
        else if (document.all) top.main.document.all[this.id].innerHTML = text;
        else if (document.getElementById) {
            var mylayer = top.main.document.getElementById(this.id);
            mylayer.innerHTML = text;
        }
//    }
//    else {
//        var url = loc.substr(0,pos+1)
//        location.href = url+"cart.php?s="+this.session;
//    }
}

function add_item(nr,bez,preis,mwst,gew,anzahl,lager,preisfix,sidekick)    {
    var i = this.checkArtikel(nr);
    if (i>0) {
        this.Artikel['anzahl'][i]++;
        this.cookieCart[i] = nr+":"+this.Artikel['anzahl'][i]+";"+sidekick;
    } else {
        var jpreis = parseFloat(preis)
        this.Artikel['nr'][this.index] = nr
        this.Artikel['bez'][this.index] = bez
        gew = parseFloat(gew)
        this.Artikel['gew'][this.index] = (isNaN(gew)) ? 0 : gew;
        this.Artikel['mwst'][this.index] = mwst;
        if (preisfix<2) this.Artikel['bpreis'][this.index] = jpreis*(1-top.OnlineRabatt)
        else this.Artikel['bpreis'][this.index] = jpreis
        this.Artikel['katbpreis'][this.index] = jpreis
        jpreis = jpreis*(1+(mwst/100))
        this.Artikel['katpreis'][this.index] = jpreis;
        if (preisfix<2) this.Artikel['preis'][this.index] = jpreis*(1-top.OnlineRabatt)
        else this.Artikel['preis'][this.index] = jpreis
        this.Artikel['lager'][this.index] = lager;
        this.Artikel['anzahl'][this.index] = anzahl
        this.Artikel['preisfix'][this.index] = preisfix
        this.cookieCart[this.index] = nr+":"+anzahl+";"+sidekick;
        this.index++
    }
    cValue = this.cookieCart.join(",");
    window.document.cookie = "cookiecart=" + escape(cValue);
}

function checkArtikel(artnr) {
    //bei Entdrosseln nicht zussammenfassen
    if (artnr==top.ArtEntdrosseln) return 0;
    for (i=this.Artikel['anzahl'].length;i>0;--i) {
        if (this.Artikel['nr'][i]==artnr) {
            return i;
        }
    }
    return 0;
}

function deleteArtikel(index) {
         this.Artikel['anzahl'][index] = 0
         this.cookieCart[index] = ""

         cValue = this.cookieCart.join(",");
         window.document.cookie = "cookiecart=" + escape(cValue);

         this.refresh()
         return true
}

function clear_all()    {
    this.Artikel = new Array()
    this.Artikel['nr'] = new Array()
    this.Artikel['bez'] = new Array()
    this.Artikel['mwst'] = new Array()
    this.Artikel['bpreis'] = new Array()
    this.Artikel['preis'] = new Array()
    this.Artikel['katbpreis'] = new Array()
    this.Artikel['katpreis'] = new Array()
    this.Artikel['gew'] = new Array()
    this.Artikel['anzahl'] = new Array()
    this.Artikel['lager'] = new Array()
    this.Artikel['preisfix'] = new Array()
    this.index = 1
    window.document.cookie = "cookiecart=";
}

function update_anzahl(index,anzahl) {
    var newanzahl = parseInt(anzahl)
    if (document.layers)
        var fld = eval("top.main.document."+this.id+".document.frmcart.anzahl"+index)
    else
        var fld = eval("top.main.document.frmcart.anzahl"+index)
    if (isNaN(newanzahl))    {
        alert("Bitte Zahlen eingeben !!")
        fld.value=this.Artikel['anzahl'][index]
        fld.focus()
        return false;
    }    else {
            this.Artikel['anzahl'][index] = newanzahl
            fld.blur()
            this.refresh()
    }
    return true;
}

function getSum() {
    var sum=0
    for (i in this.Artikel['anzahl'])
        sum += this.Artikel['preis'][i] * this.Artikel['anzahl'][i]
    return sum
}

function getHTMLCart() {
       var txt = "<TABLE WIDTH=164 BORDER=0 CELLPADDING=1 CELLSPACING=1>"
    for (i=this.Artikel['anzahl'].length;i>0;--i) {
        if (this.Artikel['anzahl'][i]>0) {
            txt += "<TR><TD align=right valign=top><FONT face=\"Tahoma, Arial, Helvetica\" COLOR=\"#000000\" size=-3 class=\"xlt\">"+this.Artikel['anzahl'][i]+"&nbsp;</FONT></TD>"
            txt += "<TD><FONT face=\"Tahoma, Arial, Helvetica\" COLOR=\"#000000\" size=-3 class=\"xlt\">"+unescape(this.Artikel['bez'][i])+"</FONT></TD>"
            txt += "<TD valign=top align=right><FONT face=\"Tahoma, Arial, Helvetica\" COLOR=\"#000000\" size=-3 class=\"xlt\">"+formatArtikelPrice(this.Artikel['preis'][i],this.currency,this.language,this.Artikel['nr'][i])+"</FONT></TD></TR>"
        }
    }
       txt += "<TR><TD colspan=3 align=right><FONT face=\"Arial, Helvetica\" COLOR=\"#000000\" size=-2 style=\"FONT-SIZE: 13px; FONT-FAMILY: Arial, Helvetica; FONT-WEIGHT: bold; COLOR: black;\">"
       txt += "<BR><B>_________________</B><BR><IMG SRC=\"../img/arrow2.gif\" width=8 height=8 hspace=2>"
    if (this.language=="d")
           txt += "SUMME: <B>"
    else
           txt += "SUBTOTAL: <B>"

    var Sum = this.getSum();
       txt += formatPrice(Sum,this.currency) +"</B></FONT>"
       txt += "</TD></TR></TABLE>"

    return txt
}

function getHTMLCartBigNew() {
  var cartcontent = this.getCartBigContent();
  return top.main.carthead+cartcontent+top.main.cartfoot;
}

function getHTMLCartBig() {
    var evnt = "onChange"
    if (navigator.appVersion.substr(2,1) == "0")
        evnt =     "onBlur"

    fnt = "<FONT class=lt"
    fntsmall = "<FONT class=xltgrey"

    var txt = "<TABLE border=0 cellPadding=1 cellSpacing=0 width=\"100%\">"
    txt += "<TR bgColor=\"#000000\">"

    if (this.language=="d") {
        txtdelete = "Artikel entfernen"
        txtempty = "<br><blockquote><FONT class=md face=\"Verdana,Arial,Helvetica\"><b>Dein Warenkorb ist leer...<br><br></b></FONT></blockquote>"
        txt += "<TD width=24>&nbsp;</TD>"
        txt += "<TD width=40 align=center>"+fnt+" color=\"#ffffff\"><B>STK.</B></FONT></TD>"
        txt += "<TD>"+fnt+" color=\"#ffffff\"><B>ARTIKELNR</B></FONT></TD>"
        txt += "<TD>"+fnt+" color=\"#ffffff\"><B>BEZEICHNUNG</B></FONT></TD>"
        txt += "<TD align=center>"+fnt+" color=\"#ffffff\"><B>LAGER</B></FONT></TD>"
        txt += "<TD align=right>"+fnt+" color=\"#ffffff\"><B>ONLINEPREIS</B></FONT></TD>"
        txt += "<TD align=right>"+fnt+" color=\"#ffffff\"><B>SUMME</B></FONT></TD>"
        txt += "<TD>&nbsp;</TD>"
        txt += "</TR>"
     } else {
        txtdelete = "Delete Article"
        txtempty = "<br><blockquote><FONT class=md face=\"Verdana,Arial,Helvetica\"><b>your cart is empty...<br><br></b></FONT></blockquote>"
        txt += "<TD width=24>&nbsp;</TD>"
        txt += "<TD width=40 align=center>"+fnt+" color=\"#ffffff\"><B>QTY.</B></FONT></TD>"
        txt += "<TD>"+fnt+" color=\"#ffffff\"><B>ARTICLE</B></FONT></TD>"
        txt += "<TD>"+fnt+" color=\"#ffffff\"><B>DESCRIPTION</B></FONT></TD>"
        txt += "<TD align=center>"+fnt+" color=\"#ffffff\"><B>STOCK</B></FONT></TD>"
        txt += "<TD align=right>"+fnt+" color=\"#ffffff\"><B>ONLINEPRICE</B></FONT></TD>"
        txt += "<TD align=right>"+fnt+" color=\"#ffffff\"><B>SUM</B></FONT></TD>"
        txt += "<TD>&nbsp;</TD>"
        txt += "</TR>"
    }

    gespreis = 0
    gespreiskat = 0
    counter = 0
    gesgew = VerpackungGew
    cartempty = true

    for (i=this.Artikel['anzahl'].length;i>0;--i) {
        if (this.Artikel['anzahl'][i]>0) {
            cartempty = false
            artnr = unescape(this.Artikel['nr'][i])
            bez = unescape(this.Artikel['bez'][i])
            anzahl = this.Artikel['anzahl'][i]
            lager = this.Artikel['lager'][i]
            gew = anzahl*this.Artikel['gew'][i]
            preis = this.Artikel['preis'][i]
            summe = anzahl*preis
            gespreis += summe
            gesgew += gew
            gespreiskat += this.Artikel['katpreis'][i] * anzahl

 //           txt += "<TR bgColor=\""+color+"\">"
            txt += "<TR>"
            txt += "<TD align=center><a href=\"javascript:void(0)\" onClick=\"window.top.cart.del("+i+");return false;\"><IMG SRC=\"/img/bt_icon_delete.gif\" width=20 height=21 border=0 alt=\""+txtdelete+"\"></a></td>"
            txt += '<TD align=center>'+fnt+'><input type=text class=bginput size=2 maxlength=4 name="anzahl'+i+'" value="'+anzahl+'" '+evnt+'="window.top.cart.update('+i+',this.value);return false;" onkeydown="return keys();"></FONT></TD>'
            txt += '<TD nowrap>'+fnt+'><a href="../shop/artikel.php?s='+this.session+'&artnr='+escape(artnr)+'">'+artnr+'</a></FONT></TD>'
            txt += "<TD>"+fnt+">"+bez+"</FONT></TD>"
            txt += "<TD align=center>"+fnt+">"+lager+"</FONT></TD>"
            txt += "<TD align=right>"+fnt+">"+formatArtikelPrice(preis,this.currency,this.language,artnr)+"</FONT></TD>"
            txt += "<TD align=right>"+fnt+">"+formatArtikelPrice(summe,this.currency,this.language,artnr)+"</FONT></TD>"
            txt += "<TD>&nbsp;</TD>"
            txt += "</TR>"
            txt += "<TR>"
            txt += "<td colspan=8><IMG SRC=\"/img/black.gif\" width=100% height=1></td>"
            txt += "</TR>"

            txt += '<input type=hidden name="artzahl'+counter+'" value="'+anzahl+'">'
            txt += '<input type=hidden name="artnr'+counter+'" value="'+artnr+'">'
            txt += '<input type=hidden name="artbez'+counter+'" value="'+bez+'">'
            txt += '<input type=hidden name="artpreis'+counter+'" value="'+this.Artikel['preis'][i]+'">'
            txt += '<input type=hidden name="artbpreis'+counter+'" value="'+this.Artikel['bpreis'][i]+'">'
            txt += '<input type=hidden name="artmwst'+counter+'" value="'+this.Artikel['mwst'][i]+'">'
            txt += '<input type=hidden name="artpreisfix'+counter+'" value="'+this.Artikel['preisfix'][i]+'">'
            counter++
        }
    }
    if (cartempty) return txtempty

    txt += "<TR>"
    txt += "<TD colspan=8 align=right><br>"
    txt += "<TABLE width=\"100%\" BORDER=0 cellpadding=4 cellspacing=2 background=\"/img/bg_warenkorb.gif\">"

    if (this.language=="d") {
        txt += "<TR><TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">GESAMTGEWICHT (INKL. VERP.) CA.: </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatGew(gesgew)+"&nbsp;kg</FONT></TD></TR>"
        txt += "<TR><TD valign=top align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">GESAMTSUMME: </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatPrice(gespreis,this.currency)+"</FONT></TD></TR>"
        txt += "<TR><TD valign=top align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">ERSPARNIS GEGENÜBER KATALOGPREIS: </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatPrice(gespreiskat-gespreis,this.currency)+"</FONT></TD></TR>"
    } else {
        txt += "<TR><TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">TOTAL WEIGHT (INCL. PACKAGE) APRX. </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatGew(gesgew)+"&nbsp;kg</FONT></TD></TR>"
        txt += "<TR><TD valign=top align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">SUBTOTAL: </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatPrice(gespreis,this.currency)+"</FONT></TD></TR>"
        txt += "<TR><TD valign=top align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\">SAVED: </FONT></TD>"
        txt += "<TD align=right><FONT class=nm size=-1 face=\"Verdana,Arial,Helvetica\"><b>"+formatPrice(gespreiskat-gespreis,this.currency)+"</FONT></TD></TR>"
    }
    txt += "</TR>"
    txt += "<TR>"
    txt += "<TD colspan=2 align=right><br><input name=submit type=image SRC=\"/img_"+this.language+"/bt_senden.gif\" width=60 height=15 border=0><br><br></td>"
    txt += "</TR>"
    txt += "</TABLE>"
    txt += "</TD>"
    txt += "</TR></TABLE>"

    txt += '<input type=hidden name="gew" value="'+formatGew(gesgew)+'">'
    txt += '<input type=hidden name="summe" value="'+gespreis+'">'
    txt += '<input type=hidden name="artcounter" value="'+counter+'">'

    return txt
}

function getHTMLItems() {
    var counter = 0
    var txt = ""
    for (i=this.Artikel['anzahl'].length;i>0;--i) {
        if (this.Artikel['anzahl'][i]>0) {
            artnr = unescape(this.Artikel['nr'][i])
            anzahl = this.Artikel['anzahl'][i]
            preis = this.Artikel['brpreis'][i]
            mwst = this.Artikel['mwst'][i]

            bez = unescape(this.Artikel['bez'][i])
            txt += '<input type=hidden name="artzahl'+counter+'" value="'+anzahl+'">'
            txt += '<input type=hidden name="artnr'+counter+'" value="'+artnr+'">'
            txt += '<input type=hidden name="artbez'+counter+'" value="'+bez+'">'
            txt += '<input type=hidden name="artpreis'+counter+'" value="'+preis+'">'
            txt += '<input type=hidden name="artmwst'+counter+'" value="'+mwst+'">'
            counter++
        }
    }
    txt += '<input type=hidden name="artcounter" value="'+(counter)+'">'
    return txt
}

function formatArtikelPrice(num,currency,language,artnr) {
    var thePrice;
    if (num==0 && artnr!='KAT') {
        if (language=="d")
            thePrice = (currency=="DM") ? "a. A." : "auf Anfrage"
        else
            thePrice = "ask us";
    }
    else
        thePrice = formatPrice(num,currency);

    return thePrice
}

function formatPrice(num,currency) {
    if (currency=="DM") num=num*Eurokurs;
    num = Math.round(num*100)/100
    string=String(num);
    if (string.indexOf('.') == -1)
        string=string+'.00';    //kein nachkomma
    var seperation = string.length - string.indexOf('.')
    if (seperation >3) //mehr als 2 stellen nachkomma
        string=string.substring(0,string.length-seperation+3);
    else if (seperation == 2)
        string=string+'0'; //nur ein nachkomma
    if (currency=="DM")
        string += "&nbsp;DM"
    else
        string = "&euro;" + string
    return (string);
}

function formatGew(x) {
    x = Math.round(x*10)/10
    str = String(x)
    if (str.charAt(0)=='.') str = "0" + str
    return str
}
