// Cookie innehåll  [artnr|artgrp|description|price|size?|number]
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(newArtnr,newArtgrp,newDescript, newPrice, newQuantity,newSize)
{
//alert(newArtgrp+newDescript);
//	alert(newQuantity);
//	newSize="XL";
//alert(newSize);
	if (newQuantity <= 0)
	{
		rc = alert('Minst en vara !');
	}
	else
	{
		if (confirm('Vill du lägga '+newQuantity+' stycken\n'+newArtnr +'  -  ' +newDescript+' '+newSize+'\n i varukorgen ?'))
		{
			index = document.cookie.indexOf("TheBasket");
			countbegin = (document.cookie.indexOf("=", index) + 1);
			countend = document.cookie.indexOf(";", index);
			if (countend == -1)
			{
				countend = document.cookie.length;
			}
// Cookie innehåll  [artnr|artgrp|description|price|size?|number]			
			document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newArtnr+"|"+newArtgrp+"|"+newDescript+"|"+newPrice+"|"+newSize+"|"+newQuantity+"]";
		}
	}
}

//


	function showItems2(frakt) {
thisitem=0;
		index = document.cookie.indexOf("TheBasket");
		countbegin = (document.cookie.indexOf("=", index) + 1);
		//alert(countbegin);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {

			      		countend = document.cookie.length;
        	}
//							alert(index);
//		alert(countend+'-'+countbegin+'-'+index);

					if(index!==-1 && countend-countbegin>1){

		fulllist = document.cookie.substring(countbegin, countend);
//alert(fulllist);
		totprice = 0;
		document.writeln('<FORM NAME="updateform">');
		document.writeln('<table width="900" border="1">');
		document.writeln('<tr><th colspan="7" align=left><h4>Innehållet i varukorgen<br/></h4></th></tr>');		   
		document.writeln('<tr><th width=400 align="left">Artikel - Beskrivning</th><th align="left">Storlek</th><th align="left">Antal</th><!--th align="left">Stl/Färg</th--><th align="right">Pris/st.</th><th>Radpris<th align="center" colspan=2>Åtgärd</th></tr>');
		itemlist = 0;
//document.write('<h1>'+document.cookie+'</h1>');
		for (var i = 0; i <= fulllist.length; i++) {
//alert(i+'-'+fulllist.length);
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
//				alert('hittad1');
				thisitem = 1;
			} else if (fulllist.substring(i,i+1) == ']') {
//							alert('hittad2');
				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><th align="left" valign="middle">'+theitem+' - '+thedesc+'</th>');
//  			document.write('<td  align=left><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3></td><!--th align="left"--><INPUT TYPE=text NAME="group'+itemlist+'" VALUE="99999999" SIZE=8><INPUT TYPE=text NAME="size'+itemlist+'" VALUE="'+theSize+'" SIZE=8><!--/th--><th align=right>'+theprice+'</th><th align=right>'+itemtotal+'</th><th><a href="javascript:removeItem('+itemlist+')">Ta bort<!--input  type=button value="Ta boooort"--></a>&nbsp;<br/><hr/><a href="javascript:amendItem('+itemlist+', '+itemlist+'document.updateform.group'+itemlist+'.value,, '+itemlist+'document.updateform.quant'+itemlist+'.value, document.updateform.size'+itemlist+'.value)">Ändra antal</a></th></tr>');
				document.write('<th align=left  height="40" valign="middle">');
				
				if(theSize==''){
				document.write('<INPUT TYPE=hidden NAME="size'+itemlist+'" VALUE="'+theSize+'" SIZE=1></td><td>');
				}
				else{
				document.write('<INPUT TYPE=text NAME="size'+itemlist+'" VALUE="'+theSize+'" SIZE=1></td><td>');
				}

				document.write('<INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=1>');

				document.write('<INPUT TYPE=hidden NAME="group'+itemlist+'" VALUE="'+thegroup+'" SIZE=6>');

				document.write('<INPUT TYPE=hidden NAME="desc'+itemlist+'" VALUE="'+thedesc+'" SIZE=24>');
				document.write('<INPUT TYPE=hidden NAME="price'+itemlist+'" VALUE="'+theprice+'" SIZE=12></th>');
				document.write('<th align=right  valign="middle">'+theprice+'</th><th align=right  valign="middle">'+itemtotal+'</th>');
				document.write('<th valign="middle" align="center"><a href="javascript:removeItem('+itemlist+')">Ta bort <img src="images/no.gif" valign=middle  border="0" width="23" height="23"  alt="Ta bort"></a></th><th align=center  valign="middle">');
				document.write('<a href="javascript:amendItem('+itemlist+',');
								document.write('document.updateform.group'+itemlist+'.value,');
								document.write('document.updateform.desc'+itemlist+'.value,');
								document.write('document.updateform.price'+itemlist+'.value,');
								document.write('document.updateform.size'+itemlist+'.value,');
								document.write('document.updateform.quant'+itemlist+'.value)">Ändra antal/storl <img src="images/yes.gif"  valign=middle border="0" width="23" height="23" alt="Ändra antal-storlek"></a></th></tr>');

//																						 					 																																																	 												 																																												 																																			 																							 		                                      
//			} else if (fulllist.substring(i,i+1) == '|'  ) {
			} else if (fulllist.substring(i,i+1) == '|'  && thisitem>0) {			
			
//alert('thisitem=');
// Cookie innehåll  [artnr|artgrp|description|price|size?|number]
			
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) thegroup = fulllist.substring(itemstart, i);
				if (thisitem==3) thedesc = fulllist.substring(itemstart, i);
				if (thisitem==4) theprice = fulllist.substring(itemstart, i);
				if (thisitem==5) theSize = fulllist.substring(itemstart, i);	
				if (thisitem==6) newquant = fulllist.substring(itemstart, i);									
				thisitem++;
				itemstart=i+1;
				
				

// newArtnr+", "+newItem+"|"+newPrice+"|"+newSize+"|"+newQuantity+"]";				
				
				
			}
		}
		totprice=parseFloat(totprice)+parseFloat(frakt);
		document.writeln('<tr border=0 height=10><th colspan=6 bgcolor=#666666></th></tr>');
		document.writeln('<tr><td colspan=3><b>Totalt pris (inklusive frakt '+frakt+' SEK, och moms)</b></td><th align=right colspan="1">'+totprice+'</th><th align=center>SEK</th></tr>');
		document.writeln('<tr valign=middle><td colspan="5" align="center"><form><input type="button" name="clear" value="Töm varukorgen" onClick="clearBasket()"></form></td>');
		document.writeln('<td colspan=1 align=left><a href="bbuy.php?menutime=3">Till kassan <img src="images/arrowgray.gif" border=0 width="15" height="15" alt="">');
		document.writeln('<img src="images/cash1.gif"  border="0" valign=middle width="30" height="30" alt="Beställ"></a></td></tr>');
		document.writeln('</TABLE>');
		document.writeln('</FORM>');
}


else{


//alert("tom");
document.writeln('<font color=000000><br/>Din varukorg är tom.<br/><br/></font>');
}

	}
	
// Cookie innehåll  [artnr|artgrp|description|price|size?|number]	
function amendItem(itemno,newgroup, newdesc, newprice, newsize,newquant ) {
//	function amendItem(itemno,newgroup, newsize,newquant ) {
//alert('nu');
		newItemList = null;
		itemlist = 0;

//		confirm(itemno)
//		confirm(newgroup)
//		confirm(newdesc)
//		confirm(newprice)
//		confirm(newsize)
//		confirm(newquant)


		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)+']';
//					alert('xxx'+					newItemList);

				} else {


//function amendItem(itemno,newgroup, newdesc, newprice, newsize,newquant ) {
					newItemList = newItemList + '['+theitem+'|'+thegroup+'|'+thedesc+'|'+theprice+'|'+newsize+'|'+newquant+']';



				}

			} else if (fulllist.substring(i,i+1) == '|') {

// Cookie innehåll  [artnr|artgrp|description|price|size?|number]


				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) thegroup = fulllist.substring(itemstart, i);
				if (thisitem==3) thedesc = fulllist.substring(itemstart, i);
				if (thisitem==4) theprice = fulllist.substring(itemstart, i);
				if (thisitem==5) theSize = fulllist.substring(itemstart, i);				
				if (thisitem==6) newquant = fulllist.substring(itemstart, i);				
				thisitem++;

				itemstart=i+1;

			}

		}

		index = document.cookie.indexOf("TheBasket");

		document.cookie="TheBasket="+newItemList;

		self.location = "bbasket.php";



	}



	function removeItem(itemno) {
//	alert('nej');

		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 = "bbasket.php";

	}



	// clearBasket() - removes all items from the basket

	function clearBasket() {

		if (confirm('Är du säker på att du vill tömma korgen ?')) {

			index = document.cookie.indexOf("TheBasket");

			document.cookie="TheBasket=.";

			self.location = "bbasket.php";

		}

	}

	

	











//

	//Kolla data som matas in

	function Formchek(inData){
	

	var antiKorg = alterError(totprice);

	var email=inData.xemail.value;
	if (antiKorg == 0)
	  {
	  	alert("Din korg är tom !! ??");
	  	return (false);
	  }
if(document.getElementById('ok').checked) {
} else {
alert('Du måste godkänna villkoren');
	  	return (false);
}




	  if (email != "" && email.indexOf("@")<1 || inData.xemail.value=="" ){

	   alert("Fyll i en korrekt e-mail adress ! Antingen saknas adress eller saknas ett '@'-tecken i ("+email+")");

	   inData.xemail.focus();

	   return (false);

	   }





	// Name

	if (inData.namn.value == "" )
	  {
	  	alert("Var vänlig och fyll i ditt namn !");
	  	inData.namn.focus();
	  	return (false);
	  }
	// Street
		if (inData.gatan.value == "" )
	  {
	  	alert("Var vänlig och fyll i din gatuadress !");
	  	inData.gatan.focus();
	  	return (false);
	  }
	// City
		if (inData.stadort.value == "" )
	  {
	  	alert("Var vänlig och fyll i stad/ort !");
	  	inData.stadort.focus();
	  	return (false);
	  }
	//ZIP
		if (inData.postnr.value == "" )
	  {
	  	alert("Var vänlig och fyll i postnummer !");
	  	inData.postnr.focus();
	  	return (false);
	  }
	//Country	
			if (inData.land.value == "")
	  {
	  	alert("Var vänlig och fyll i land !");
	  	inData.land.focus();
	  	return (false);
	  }

	  if (inData.telephone.value == "")

	  {

	   	alert("Telefonnumer saknas, fyll i ett nummer som vi kan nå dig på vid behov. ");

	   	inData.telephone.focus();

	   	return (false);

	  }

	// Postnummer-koll

	var blank=inData.postnr.value.lastIndexOf(" ");

        skapatal=Number(inData.postnr.value);

        var langd=inData.postnr.value.length;

	//  alert(langd)



          if (isNaN(skapatal))

          {

          alert("Inget postnummer !!")

	  inData.postnr.focus();

	  return (false);

	  }





	  if (skapatal <10000 || skapatal >99999)

	  {

          skapatal= Number.NaN

          alert("Felaktigt postnummer !!")

	  inData.postnr.focus();

	  return (false);

	  }





	//



	  return (true);

	  }

	// Slut inData koll



	// 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);

	}



	// showItems () - creates a table of items in the basket and

	// creates the start of a form which sets information for

	// basket items.
	function showItems(frakt) {
//	alert(frakt);
		index = document.cookie.indexOf("TheBasket");
		countbegin = (document.cookie.indexOf("[", index) + 0);
        	countend = document.cookie.indexOf(";", index);
        	if (countend == -1) {
            		countend = document.cookie.length;
        	}
		fulllist = document.cookie.substring(countbegin, countend);
		totprice = 0;
		document.writeln('<div class="orderform">\n<form method="post" ACTION="order.php" onSubmit="return Formchek(this)"  name="inData">\n');
	  document.writeln("<div class='orderdate'>Datum: "+Date()+"</div>\n");
		document.writeln('<div class="orderhead">\n<table><tr><th width=500>Artikel</th><th width=100>Storl./Färg</th--><th width=100>Antal</th><th width=100>Pris/styck</th><th width=100>Totalt</th></tr></table></div>\n');
//		document.writeln('<div class="orderhead"><tr><th>Artikel</th><th>Storl./Färg</th><th>Antal</th><th>Pris/styck</th><th>Totalt</th></tr></div>');
		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('<div class="pos1">'+theitem+'-'+thedesc+'</div>');
				document.writeln('<div class="pos">'+theSize+'</div><div class="pos">'+thequantity+'</div><div class="pos">'+theprice+'</div><div class="pos">'+alterError(itemtotal)+'</div><br/><br/>');
				document.writeln('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'" SIZE="40">\n');
				document.writeln('<INPUT TYPE="hidden" NAME="size'+itemlist+'" VALUE="'+thesize+'" SIZE="40">\n');
				document.writeln('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'" SIZE="40">\n');
				document.writeln('<INPUT TYPE="hidden" NAME="price each'+itemlist+'" VALUE="'+theprice+'" SIZE="40">\n');
				document.writeln('<INPUT TYPE="hidden" NAME="total cost'+itemlist+'" VALUE="'+alterError(itemtotal)+'" SIZE="40">\n');
				}
			else if (fulllist.substring(i,i+1) == '|')
 
				{
//				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
//				if (thisitem==4) theprice = fulllist.substring(itemstart, i);
				if (thisitem==3) thesize = fulllist.substring(itemstart, i);
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) thegroup = fulllist.substring(itemstart, i);
				if (thisitem==3) thedesc = fulllist.substring(itemstart, i);
				if (thisitem==4) theprice = fulllist.substring(itemstart, i);
				if (thisitem==5) theSize = fulllist.substring(itemstart, i);	
				if (thisitem==6) newquant = fulllist.substring(itemstart, i);													
				thisitem++;
				itemstart=i+1;
				}
		}
		totprice=parseFloat(totprice);
//				totprice=parseFloat(frakt);
				totprice=parseFloat(totprice)+parseFloat(frakt);
//		alert(totprice);
		document.writeln('<div class="posr"><b>Totalt inkl. frakt och moms </b>: '+alterError(totprice)+'</div>\n');
//				document.writeln('<div class="posr"><b>Frakt tillkommer med moms</b>: '+frakt+' SEK</div>\n');
		document.writeln('<INPUT TYPE="hidden" NAME="Goods Total" VALUE="'+alterError(totprice)+'" SIZE="40">\n');
	}
//	
		function showBasketSelect() {
//					alert(document.cookie);
var x=document.getElementById("bask");
basketstring="<form	name='showbasket'><select class='basket'><option>Varukorg</option></select></form>";
document.getElementById('bask').innerHTML ="<form	name='showbasket'><select class='basket'><option value='aaa'>"+document.cookie+"</option></select></form>";
basketstring="<form	name='showbasket'><select class='basket' onChange=Javascript:GotoURL(this.options[this.selectedIndex].value);><option>Varukorg</option><option value=index.php>Artikel - Pris/st</option>";
		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);
		for (var i = 0; i <= fulllist.length; i++) {
			if (fulllist.substring(i,i+1) == '[') {
				itemstart = i+1;
				artstart=itemstart;
// Group 2 digits !
		artend = (fulllist.indexOf("|",itemstart)+3);
				thisitem = 1;
				therow= fulllist.substring(artstart, artend);
				thepartno= therow.substring(0, (therow.length-3));
				thegroup= therow.substring((therow.length-2), (therow.length));
			} else if (fulllist.substring(i,i+1) == ']') {
				itemend = i;
				thequantity = fulllist.substring(itemstart, itemend);
				itemtotal = 0;
				itemtotal = (eval(theprice*thequantity));
				temptotal = itemtotal * 100;
				basketstring=basketstring+"<option value='bitem.php?&artno="+thepartno+"&agroup="+theartgrp+"'>";
				basketstring=basketstring+theitem+"---"+theprice+"     SEK/st";
				basketstring=basketstring+"</option>";
			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				
								if (thisitem==2) theartgrp = fulllist.substring(itemstart, i);
				if (thisitem==4) theprice = fulllist.substring(itemstart, i);
				if (thisitem==3) theSize = fulllist.substring(itemstart, i);
				thisitem++;
				itemstart=i+1;
			}
		}
basketstring=basketstring+"</select>";
document.getElementById('bask').innerHTML =basketstring;
}
//

function GotoURL(url){
//alert(url);
window.location.href = url;
}

function openDetails2(w,h,name,text){
//alert(h);
//alert(h);
document.getElementById('xx').innerHTML ="<br><br><img id='pic' name='"+name+"'border='1' alt='x' src='"+name+"'><br/><br/><br/><img id='pic' border='0' alt='x' width=90 height=19 src='images/close.gif'>";
//alert(document.getElementById('xx').innerHTML);
//document.getElementById('pic').innerHTML;
//alert(document.images[name].width);
//alert(document.images[name].height);
//scale(document.images[name].width,document.images[name].height,name);
//alert(document.images[0].width;
//alert('höjd:'+document.documentElement.clientHeight);
//alert('bredd:'+document.documentElement.clientWidth);
document.getElementById('xx').style.color = "#00ff00";
document.getElementById('yy').innerHTML ="<br/><b>Beskrivning:</b><br/><p>"+text+"</p>";
ywidth=1020;
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer"){
document.getElementById('xx').style.width=1020+'px';
}
else{
//alert('hej');
document.getElementById('xx').style.width=1020+'px';
}
h=parseFloat(h);
document.getElementById('xx').style.top=0+'px';
h=h+100;
document.getElementById('xx').style.height=h+'px';
document.getElementById('xx').style.left=(document.body.clientWidth-ywidth)/2+'px';
//alert(document.getElementById('xx').style.left);
//
document.getElementById('yy').style.color = "#ffffff";
document.getElementById('yy').style.width=1010+'px';
h=parseFloat(h);
document.getElementById('yy').style.padding=5+'px';
document.getElementById('yy').style.top=h+'px';
//document.getElementById('yy').style.left=(document.body.clientWidth-ywidth)/2+'px';
document.getElementById('yy').style.left=document.getElementById('xx').style.left;
//alert(document.getElementById('yy').style.left);
//alert('xx:'+xx.style.left);
//alert('yy:'+yy.style.left);

document.getElementById('yy').style.color = "#ffffff";
document.getElementById('yy').style.height=(document.documentElement.clientHeight-h-10)+'px';
//alert(document.getElementById('yy').style.height);
document.getElementById('yy').style.opacity = 1;
//alert(ywidth);
}
//function scale(xwidth,yheight,name){
// Zooma fram bilden
//alert(xwidth);
//alert(name);

//}
function closeDetails2(name,text){
document.getElementById('xx').innerHTML ="<b></b>";
//xx.style.color = "000000";
//alert('1');
document.getElementById('xx').style.width=0+'px';
document.getElementById('xx').style.height=0+'px';
//alert('2');
document.getElementById('yy').innerHTML ="<b></b>";
document.getElementById('yy').style.opacity = 0;
document.getElementById('yy').style.width=0+'px';
document.getElementById('yy').style.height=0+'px';
}
