'*******************************************
' ALL CODE IS (C) 2007-2008
' SIMON JONASSEN
' IF U WANT SOMETHING, MAIL ME
' SIJO@BRUUNS-HANDEL.DK
'*******************************************

Dim xmlDoc, xslDoc, VareKurv

'**********************************************
' Define XML document
'**********************************************
Public Function makeBasket()
  Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  set xsldoc = CreateObject("Microsoft.XMLDOM")
  xsldoc.load ("xsl/mystyle.xsl") 

  '**********************************************
  ' Define and Create VareKurv Element
  '**********************************************
  Set VareKurv = xmlDoc.createElement("VareKurv")
  xmlDoc.appendChild VareKurv
  Set p=xmlDoc.createProcessingInstruction("xml","version='1.0' encoding='ISO-8859-1'")
  xmlDoc.insertBefore p,xmlDoc.childNodes(0)
  Set p=xmlDoc.createProcessingInstruction("xml-stylesheet","type='text/xsl' href='../xsl/mystyle.xsl'")
  xmlDoc.insertBefore p,xmlDoc.childNodes(1)
End Function

'**********************************************
' Reload Existing Cart
'**********************************************

Public Function LoadBasket(id)
  Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  set xsldoc = CreateObject("Microsoft.XMLDOM")
  docpath="orders/" & id & ".xml"
  xsldoc.load ("xsl/mystyle.xsl") 
  xmldoc.async = false
  xmldoc.load (docpath)
  Set VareKurv = XMLDoc.documentElement 
End Function

'**********************************************
' Create New Item In Cart
'**********************************************

Public Function putincart(p1,p2,p3,p4,p5,p6)
  
  p7=p2&p4&p5
  Set Vare = xmlDoc.createElement("Vare")
  VareKurv.appendChild Vare

  Set c1 = xmlDoc.createElement("Antal")
  Set c2 = xmlDoc.createElement("VareNummer")
  Set c3 = xmlDoc.createElement("VareText")
  Set c4 = xmlDoc.createElement("Storrelse")
  Set c5 = xmlDoc.createElement("Farve")
  Set c6 = xmlDoc.createElement("Pris")
  Set c7 = xmlDoc.createElement("Trace")

  Vare.appendchild c1
  Vare.appendchild c2
  Vare.appendchild c3
  Vare.appendchild c4
  Vare.appendchild c5
  Vare.appendchild c6
  Vare.appendchild c7

 
  Set V_antal = xmldoc.createTextNode(p1) 
  Set V_nummer = xmldoc.createTextNode(p2) 
  Set V_text = xmldoc.createTextNode(p3) 
  Set V_storrelse = xmldoc.createTextNode(p4) 
  Set V_Farve = xmldoc.createTextNode(p5) 
  Set V_Pris = xmldoc.createTextNode(p6) 
  Set V_Trace = xmldoc.createTextNode(p7) 
  

  c1.appendchild V_antal
  c2.appendchild v_nummer
  c3.appendchild v_text
  c4.appendChild v_storrelse
  c5.appendChild v_farve
  c6.appendChild v_pris
  c7.appendChild v_Trace


End Function

'**********************************************
' Show The Cart
'**********************************************
Public Function showcart()
  document.getelementbyID("TheBasket").innerHTML = xmldoc.transformNode(xsldoc)
  document.getelementbyID("BaskDiv").scrolltop=document.getelementbyID("BaskDiv").scrollheight - document.getelementbyID("BaskDiv").offsetheight
end function

'**********************************************
' Check Cart
'**********************************************
Public Function CheckCart(Trace, position)
  Set objChildNodes = XMLDoc.documentElement.childNodes
  if objChildNodes.Length > 0 then
    Set Root = XMLDoc.documentElement 
    Set NodeList = Root.getElementsByTagName("Trace") 
    counter=0
    checkcart=False
    For Each Elem In NodeList 
      if Elem.firstChild.nodeValue=Trace then
        Checkcart=True
        position=counter
        exit for 
      end if
    counter=counter+1 
    Next
  end if
  Set Root=Nothing
  Set NodeList=Nothing
  Set ObjChildNodes=Nothing  
End Function

'**********************************************
' Stick it in the basket
'**********************************************
Public function inbasket(p1,p2,p3,p4,p5,p6)
  trace=p2&p4&p5 
  if checkCart(Trace,position) then
    Set Root = XMLDoc.documentElement 
    Set NodeList = Root.getElementsByTagName("Antal")   
    Set objChildNode=NodeList(position).firstChild
    if p1 = 0 then
      Root.removeChild(Root.childNodes.item(position)) 
    else
      if instr(document.location.href,"checkout.asp") > 0 then
        Nyantal=clng(p1)
      else
        NyAntal=clng(ObjChildNode.nodeValue) + clng(p1)
      end if
      if NyAntal > 0 then
        ObjChildNode.nodeValue=NyAntal

        Set NodeList = Root.getElementsByTagName("Pris")   
        Set objChildNode=NodeList(position).firstChild

        price=formatnumber(replace(p6,".",",")/p1)

        ThePrice=FormatNumber(NyAntal*Price)
        ThePrice=replace(theprice,".","")
        ThePrice=replace(theprice,",",".")
        ObjChildNode.nodeValue=ThePrice
      else
        Root.removeChild(Root.childNodes.item(position)) 
      end if 
    end if
  else
    if p1 > 0 then
      call putincart(p1,p2,p3,p4,p5,p6)
    end if 
  end if 
  if instr(document.location.href,"checkout.asp") > 0 then
    call doorder()
    window.location.reload()
  else     
    call showcart()  
  end if
end function


'**********************************************
' yoink it out of the basket
'**********************************************
Public function DelFromBasket(trace)
  if checkCart(Trace,position) then
    Set Root = XMLDoc.documentElement 
    Root.removeChild(Root.childNodes.item(position)) 
  End If

  if instr(document.location.href,"checkout.asp") > 0 then
    call doorder()
    window.location.reload()
  else     
    call showcart()  
  end if
End Function



'**********************************************
  Public function UpdateBasket(p1,p2,p3,p4,p5,p6, Trace)
'**********************************************
' Update existing item (new size or color)
'**********************************************
' p1=Antal
' p2=VareNummer
' p3=VareText
' p4=Storrelse
' p5=Farve
' p6=Pris
' p7=Trace (Varenummer+storrelse+farve)
'**********************************************
  if checkCart(Trace,position) then
    Set Root = XMLDoc.documentElement 
    Set NodeList = Root.getElementsByTagName("Antal")   

    If p4 <> "" then  
      Set NodeList = Root.getElementsByTagName("Storrelse")   
      Set objChildNode=NodeList(position).firstChild
      ObjChildNode.nodeValue=p4
    End If
        
    If p5 <> "" then
      Set NodeList = Root.getElementsByTagName("Farve")   
      Set objChildNode=NodeList(position).firstChild
      ObjChildNode.nodeValue=p5
    End If
 
    Set NodeList = Root.getElementsByTagName("Trace")   
    Set objChildNode=NodeList(position).firstChild
    ObjChildNode.nodeValue=p2&p4&p5

    window.location.reload()
  End If
  End function
