// showItems() - displays shopping basket in a table

function showItems() 
{
    index = document.cookie.indexOf("TheBasket");
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    
    if (countend == -1) 
	{
	    countend = document.cookie.length;
	}

    fulllist = document.cookie.substring(countbegin, countend);
    totprice = 0;
    document.writeln('<FORM NAME="updateform">');
    document.writeln('<TABLE width="500px" class="webshop_table">');
    document.writeln('<TR class="webshop_table_header_tr"><TD align="center" width="120px">Produkt</font></TD><TD width="50px" align="center">Antal</font></TD><TD width="80px" align="center">Pris pr. stk.</TD><td width="70px" align="center">Pris ialt</font></TD><td width="30">Valuta</td><TD width="70px" align="center">Funktioner</TD></TR>');
    itemlist = 0;

    for (var i = 0; i <= fulllist.length; i++) 
	{
	if (fulllist.substring(i,i+1) == '[') 
	    {
		itemstart = i+1;
		thisitem = 1;
	    } 
	else if (fulllist.substring(i,i+1) == ']') 
	    {
		itemend = i;
		thequantity = fulllist.substring(itemstart, itemend);
		itemtotal = 0;
		itemtotal = (eval(theprice*thequantity));
		temptotal = itemtotal * 100;
		totprice = totprice + itemtotal;
		itemlist=itemlist+1;
		document.write('<tr><td>'+theitem+'</td>');
		document.write('<td align=right><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3></td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td><td>DKK</td><td><a class="link" href="javascript:removeItem('+itemlist+')">Fjern</a>&nbsp;<a class="link" href="javascript:amendItem('+itemlist+',document.updateform.quant'+itemlist+'.value)">Opdater</a></td></tr>');
	    } 
	else if (fulllist.substring(i,i+1) == '|') 
	    {
		if (thisitem==1) theitem = fulllist.substring(itemstart, i);
		if (thisitem==2) theprice = fulllist.substring(itemstart, i);
		thisitem++;
		itemstart=i+1;
	    }
	}
    document.writeln('<tr class="webshop_table_header_tr"><td colspan=3><b>Ialt at betale</b></td><td align=right>' +alterError(totprice)+'</td><td>DKK</td></tr>');
    document.writeln('</TABLE>');
    document.writeln('</FORM>');
}



function amendItem(itemno, newquant)
{
    newItemList = null;
    itemlist = 0;

    for (var i = 0; i <= fulllist.length; i++) 
	{
	if (fulllist.substring(i,i+1) == '[') 
	    {
		thisitem = 1;
		itemstart = i+1;
		fullstart = i+1;
	    } 
	else if (fulllist.substring(i,i+1) == ']') 
	    {
		itemend = i;
		itemlist=itemlist+1;
		if (itemlist != itemno) 
		    {
			newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
		    } 
		else 
		    {
			newItemList = newItemList + '['+theitem+'|'+theprice+'|'+newquant+']';
		    }
	    } 
	else if (fulllist.substring(i,i+1) == '|') 
	    {
		if (thisitem==1) theitem = fulllist.substring(itemstart, i);
		if (thisitem==2) theprice = fulllist.substring(itemstart, i);
		thisitem++;
		itemstart=i+1;
	    }
	}
    index = document.cookie.indexOf("TheBasket");
    document.cookie="TheBasket="+newItemList;
    self.location = "?area=webshop&action=basket";
}

function removeItem(itemno) 
{
    newItemList = null;
    itemlist = 0;

    for (var i = 0; i <= fulllist.length; i++) 
	{
	if (fulllist.substring(i,i+1) == '[') 
	    {
		itemstart = i+1;
	    } 
	else if (fulllist.substring(i,i+1) == ']') 
	    {
		itemend = i;
		theitem = fulllist.substring(itemstart, itemend);
		itemlist=itemlist+1;
		if (itemlist != itemno) 
		    {
			newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
		    }
	    }
	}
    index = document.cookie.indexOf("TheBasket");
    document.cookie="TheBasket="+newItemList;
    self.location = "?area=webshop&action=basket";
}

// clearBasket() - removes all items from the basket
function clearBasket() 
{
    if (confirm('Er du sikker på at du vil tømme indkøbskurven?')) 
	{
	    index = document.cookie.indexOf("TheBasket");
	    document.cookie="TheBasket=.";
	    self.location = "?area=webshop&action=basket";
	}
}




// alterError - fixes a rounding bug in Netscape 2
function alterError(value) 
{
    if (value<=0.99) 
	{
	    newPounds = '0';
	} else 
	{
	    newPounds = parseInt(value);
	}
    newPence = parseInt((value+.0008 - newPounds)* 100);
    if (eval(newPence) <= 9) newPence='0'+newPence;
    newString = newPounds + '.' + newPence;
    return (newString);
}

// buyItem - adds an item to the shooping basket
function buyItem(newItem, newPrice, newQuantity) 
{
    if (newQuantity <= 0) 
	{
	    rc = alert('Det angivne antal er ikke korrekt');
	} 
    else 
	{
	    if (confirm('Tilføj '+newQuantity+' x '+newItem+' til indkøbskurv?')) 
		{
		    index = document.cookie.indexOf("TheBasket");
		    countbegin = (document.cookie.indexOf("=", index) + 1);
		    countend = document.cookie.indexOf(";", index);
		    if (countend == -1) 
			{
			    countend = document.cookie.length;
			}
		    document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newQuantity+"]";
		}
	}
}

// resetShoppingBasket - resets to shopping basket to empty
function resetShoppingBasket() 
{
    index = document.cookie.indexOf("TheBasket");
    document.cookie="TheBasket=.";
}






function printit()
{  
    if (window.print) 
	{
	    window.print();  
	} 
    else 
	{
	    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}








function alterError(value) {
    if (value<=0.99) {
	newPounds = '0';
    } else {
	newPounds = parseInt(value);
    }
    newPence = parseInt((value+.0008 - newPounds)* 100);
    if (eval(newPence) <= 9) newPence='0'+newPence;
    newString = newPounds + '.' + newPence;
    return (newString);
}


// showItems () - creates a table of items in the basket and
// creates the start of a form which sets information for
// basket items.
function showItemsWhenFinished() 
{
    index = document.cookie.indexOf("TheBasket");
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1) 
	{
	    countend = document.cookie.length;
	}
    
    fulllist = document.cookie.substring(countbegin, countend);
    totprice = 0;
    document.writeln('<TABLE class="webshop_table">');
    document.writeln('<TR class="webshop_table_header_tr"><TD align="center">Produkt</TD><TD width="50px" align="center">Antal</TD><TD width="80px" align="center">Pris pr. stk.</TD><td width="70px" align="center">Pris ialt</TD><td align="center">Valuta</td></TR>');
    itemlist = 0;
    for (var i = 0; i <= fulllist.length; i++) 
	{
	if (fulllist.substring(i,i+1) == '[') 
	    {
		thisitem = 1;
		itemstart = i+1;
	    } 
	else if (fulllist.substring(i,i+1) == ']') 
	    {
		itemend = i;
		thequantity = fulllist.substring(itemstart, itemend);
		itemtotal = 0;
		itemtotal = (eval(theprice*thequantity));
		temptotal = itemtotal * 100;
		totprice = totprice + itemtotal;
		itemlist=itemlist+1;
		document.write('<tr class="webshop_table_tr1"><td>'+theitem+'</td>');
		document.writeln('<td align=right>'+thequantity+'</td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td><td>DKK</td></tr>');
		document.writeln('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" SIZE="40">');
		document.writeln('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'" SIZE="40">');
		document.writeln('<INPUT TYPE="hidden" NAME="price each'+itemlist+'" VALUE="'+theprice+'" SIZE="40">');
		document.writeln('<INPUT TYPE="hidden" NAME="total cost'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="40">');
	    } 
	else if (fulllist.substring(i,i+1) == '|') 
	    {
		if (thisitem==1) theitem = fulllist.substring(itemstart, i);
		if (thisitem==2) theprice = fulllist.substring(itemstart, i);
		thisitem++;
		itemstart=i+1;
	    }
	}
    
    document.writeln('<tr class="webshop_table_tr_total"><td colspan=3><b>Ialt at betale</b></td><td align=right>'+alterError(totprice)+'</td><td>DKK</td></tr>');
    document.writeln('<INPUT TYPE="hidden" NAME="Total" VALUE="'+alterError(totprice)+'" SIZE="40">');
    document.writeln('</TABLE>');
}


// showItems () - creates a table of items in the basket and
// creates the start of a form which sets information for
// basket items.
function printItems() 
{
    var output = '';
    index = document.cookie.indexOf("TheBasket");
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    
    if (countend == -1) 
	{
	    countend = document.cookie.length;
	}
    
    fulllist = document.cookie.substring(countbegin, countend);
    totprice = 0;
    //output += '<FORM action="bfinish.htm" target="_top">';
    //output += '<TABLE BORDER>';
    
    //output += 'Produkt\t\tPris pr stk\t\t\tPris ialt\n\n';
    itemlist = 0;
    
    for (var i = 0; i <= fulllist.length; i++) 
	{
	    if (fulllist.substring(i,i+1) == '[') 
		{
		    thisitem = 1;
		    itemstart = i+1;
		}
	    else if (fulllist.substring(i,i+1) == ']') 
		{
		    itemend = i;
		    thequantity = fulllist.substring(itemstart, itemend);
		    itemtotal = 0;
		    itemtotal = (eval(theprice*thequantity));
		    temptotal = itemtotal * 100;
		    totprice = totprice + itemtotal;
		    itemlist=itemlist+1;
		    //output += theitem + '\n';
		    
		    //output += thequantity+ ' ' + theitem + '\t\t\t' +theprice+ '\t\t\t' +alterError(itemtotal) + '\n';
		    output += thequantity+ ' stk. ' + theitem + ':\n';
		    output += 'Pris pr. stk.:\t\t' + theprice + '\n';
		    output += 'Pris ialt:\t\t\t' + alterError(itemtotal) + '\n\n'; 
		    //output += '<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" SIZE="40">';
		    //output += '<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'" SIZE="40">';
		    //output += '<INPUT TYPE="hidden" NAME="price each'+itemlist+'" VALUE="'+theprice+'" SIZE="40">';
		    //output += '<INPUT TYPE="hidden" NAME="total cost'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="40">';
		} 
	    else if (fulllist.substring(i,i+1) == '|') 
		{
		    if (thisitem==1) theitem = fulllist.substring(itemstart, i);
		    if (thisitem==2) theprice = fulllist.substring(itemstart, i);
		    thisitem++;
		    itemstart=i+1;
		}		
	}
    
    output += '\n\n';
    output += 'Ialt at betale:\t\t' +alterError(totprice) + ' DKK';
    
    //output += '<INPUT TYPE="hidden" NAME="Total" VALUE="'+alterError(totprice)+'" SIZE="40">';
    //output += '</TABLE>';

    return output;
}



function MM_preloadImages() 
{ //v3.0
    var d=document; 
    if(d.images)
	{ 
	    if(!d.MM_p) 
		d.MM_p=new Array();
	    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
	    for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		    { 
			d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
		    }
	}
}


function MM_findObj(n, d) 
{ //v3.0
    var p,i,x;  
    if(!d) 
	d=document; 
    
    if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
    if(!(x=d[n])&&d.all) 
	x=d.all[n]; 
    
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
	x=MM_findObj(n,d.layers[i].document); 

    return x;
}



function MM_nbGroup(event, grpName) { //v3.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  
  if (event == "init" && args.length > 2) 
      {
      if ((img = MM_findObj(args[2])) != null && !img.MM_init) 
	  {
	      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
	      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
	      nbArr[nbArr.length] = img;

	      for (i=4; i < args.length-1; i+=2) 
		  if ((img = MM_findObj(args[i])) != null) 
		      {
			  if (!img.MM_up) 
			      img.MM_up = img.src;

			  img.src = img.MM_dn = args[i+1];
			  nbArr[nbArr.length] = img;
		      }
	  }

  } 
  else if (event == "over") 
      {
	  document.MM_nbOver = nbArr = new Array();
	  for (i=1; i < args.length-1; i+=3) 
	      if ((img = MM_findObj(args[i])) != null) 
		  {      
		      if (!img.MM_up) 
			  img.MM_up = img.src;
		      
		      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : args[i+1];
		      nbArr[nbArr.length] = img;
		  }
      } 
  else if (event == "out" ) 
      {
	  for (i=0; i < document.MM_nbOver.length; i++) 
	      {
		  img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; 
	      }
	  
      } 
  else if (event == "down") 
      {
	  if ((nbArr = document[grpName]) != null)
	      for (i=0; i < nbArr.length; i++) 
		  { 
		      img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; 
		  }

	  document[grpName] = nbArr = new Array();
	  
	  for (i=2; i < args.length-1; i+=2) 
	      if ((img = MM_findObj(args[i])) != null) 
		  {
		      if (!img.MM_up) img.MM_up = img.src;
		      img.src = img.MM_dn = args[i+1];
		      nbArr[nbArr.length] = img;
		  }
      }
}

