
function form_validator(f) {
	var error="";
	if(f.query.value=="") {
		error="Du måste ange ett sökord!";
		setBorder(f.query,1);
	} else {
		setBorder(f.query,0);
	}
	if(error) {
	alert(''+error+"\n");
	return false;
	} else {
	return true;
	}

}

function setBorder(t,b) {
	if(b) {
		t.style.borderColor='#ce0000';
		t.style.borderStyle='solid';
		t.style.borderWidth='1px';
	} else {
		t.style.borderColor='';
		t.style.borderStyle='';
		t.style.borderWidth='';
	}
}



	function alterError(value) {
		if (value<=0.99) {
			newDollar = '0';
		} else {
			newDollar = parseInt(value);
		}
		newCent = parseInt((value+.0008 - newDollar)* 100);
		if (eval(newCent) <= 9) newCent='0'+newCent;
		newString = newDollar + '.' + newCent;
		return (newString);
	}

        function addItem(newItem, newPrice, newQuantity,  newNumber, newOption, newOption2, newWeight) {
		if (newQuantity >= 1000) {
			rc = alert('Felaktigt antal!');
		} else {
                if (newQuantity >= 1) {
			if (!confirm('Vill du placera '+newQuantity+' st '+newItem+' i din varukorg?'))
			{
			return
			}

                        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+"|"+newNumber+"|"+newOption+"|"+newOption2+"|"+newWeight+"|"+newQuantity+"]"+";path=/";
						
			}
		}
loopCodes();
document.getElementById('shoptotal').innerHTML=showItems();
	}



	// 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);
		subtotal = 0;
		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;
				subtotal = subtotal + itemtotal;
				itemlist=itemlist+1;


			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) theprice = fulllist.substring(itemstart, i);
				if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
				if (thisitem==4) theoption = fulllist.substring(itemstart, i);
				if (thisitem==5) theoption2 = fulllist.substring(itemstart, i);
				if (thisitem==6) theweight = fulllist.substring(itemstart, i);
				thisitem++;
				itemstart=i+1;
			}
		}

		//document.writeln('<FONT SIZE="2" FACE="Verdana, Arial, Helvetica, sans-serif">Totalt&nbsp;SEK: <B>'+alterError(subtotal)+'</B></FONT>');
		return '<FONT SIZE="2" FACE="Verdana, Arial, Helvetica, sans-serif">Totalt&nbsp;SEK: <B>'+alterError(subtotal)+'</B></FONT>';

	}

		function updateItem(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+'|'+thenumber+'|'+theweight+'|'+newquant+']';
				}
			} else if (fulllist.substring(i,i+1) == '|') {
				if (thisitem==1) theitem = fulllist.substring(itemstart, i);
				if (thisitem==2) theprice = fulllist.substring(itemstart, i);
				if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
				if (thisitem==4) theweight = fulllist.substring(itemstart, i);
				thisitem++;
				itemstart=i+1;
			}
		}
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket="+newItemList+"; path=/";

document.getElementById('shoptotal').innerHTML=showItems();


	}

function ifnocreate() {
		var index = document.cookie.indexOf("TheBasket");
//alert('-' + index + '-');
if (index==-1) {
resetShoppingBasket();
}
}

	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+"; path=/";;
document.getElementById('shoptotal').innerHTML=showItems();

	}

	function clearBasket() {
		if (confirm('  T&ouml;m   ')) {
			index = document.cookie.indexOf("TheBasket");
			document.cookie="TheBasket=."+"; path=/";;


		}
document.getElementById('shoptotal').innerHTML=showItems();
	}



	function resetShoppingBasket() {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=."+"; path=/";
document.getElementById('shoptotal').innerHTML=showItems();
	}









function updateItemPrice(itemno, newprice) {
	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);


	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;
thequantity = fulllist.substring(itemstart, itemend);

				if (theoption != itemno) {
					newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
				} else {

					newItemList = newItemList +'['+theitem+'|'+newprice+'|'+theoption+'|'+theoption2+'|'+thenumber+'|'+theweight+'|'+thequantity+']';

			}
		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			if (thisitem==3) theoption = fulllist.substring(itemstart, i);
			if (thisitem==4) theoption2 = fulllist.substring(itemstart, i);
			if (thisitem==5) thenumber = fulllist.substring(itemstart, i);
			if (thisitem==6) theweight = fulllist.substring(itemstart, i);
			if (thisitem==7) thequant = fulllist.substring(itemstart, i);
			thisitem++;
			itemstart=i+1;
		}
	}
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket="+newItemList+"; path=/";
		document.getElementById('shoptotal').innerHTML=showItems();
	
}



function addCodes(code_no, kcode, kprice) {
ifnocreate();
	index2 = document.cookie.indexOf("kcodes");
	countbegin = (document.cookie.indexOf("=", index2) + 1);
	countend = document.cookie.indexOf(";", index2);
	if (countend == -1) {
	countend = document.cookie.length;
	}
	document.cookie="kcodes="+document.cookie.substring(countbegin, countend)+"["+code_no+"|"+kcode+"|"+kprice+"|]"+"; path=/";
}


	function loopCodes() {

			index3 = document.cookie.indexOf("kcodes");
			countbegin = (document.cookie.indexOf("=", index3) + 1);
	        	countend = document.cookie.indexOf(";", index3);
	        	if (countend == -1) {
	            		countend = document.cookie.length;
	        	}
			fulllist2 = document.cookie.substring(countbegin, countend);

		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist2.length; i++) {

			if (fulllist2.substring(i,i+1) == '[') {
				thisitem = 1;
				itemstart = i+1;
				fullstart = i+1;
			} else if (fulllist2.substring(i,i+1) == ']') {
				itemend = i;
				itemlist=itemlist+1;
				updateItemPrice(theitem,theprice);
			} else if (fulllist2.substring(i,i+1) == '|') {

				if (thisitem==1) theitem = fulllist2.substring(itemstart, i);
				if (thisitem==2) thecode = fulllist2.substring(itemstart, i);
				if (thisitem==3) theprice = fulllist2.substring(itemstart, i);
				thisitem++;
				itemstart=i+1;
			}
		}

document.getElementById('shoptotal').innerHTML=showItems();

	}






function AddCodePrice(code_no, kprice) {
var b=document.getElementById('codeprice'+code_no);
if(b!=null) {
b.innerHTML=kprice;
}

var a=document.getElementById('oldprice'+code_no);
if(a!=null) {
a.style.color="#000000";
a.style.textDecoration="line-through";


}



}




	function itemListCodePrices() {

			index3 = document.cookie.indexOf("kcodes");
			countbegin = (document.cookie.indexOf("=", index3) + 1);
	        	countend = document.cookie.indexOf(";", index3);
	        	if (countend == -1) {
	            		countend = document.cookie.length;
	        	}
			fulllist2 = document.cookie.substring(countbegin, countend);

		newItemList = null;
		itemlist = 0;
		for (var i = 0; i <= fulllist2.length; i++) {

			if (fulllist2.substring(i,i+1) == '[') {
				thisitem = 1;
				itemstart = i+1;
				fullstart = i+1;
			} else if (fulllist2.substring(i,i+1) == ']') {
				itemend = i;
				itemlist=itemlist+1;
				AddCodePrice(theitem,theprice);
			} else if (fulllist2.substring(i,i+1) == '|') {

				if (thisitem==1) theitem = fulllist2.substring(itemstart, i);
				if (thisitem==2) thecode = fulllist2.substring(itemstart, i);
				if (thisitem==3) theprice = fulllist2.substring(itemstart, i);
				thisitem++;
				itemstart=i+1;
			}
		}

	}
		
