﻿this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 20;
		yOffset = -250;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";
		var tekst = this.t.replace(/-br-/g,"<br />");		
		$("body").append("<p id='tooltip'>"+ tekst +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function showMSG(tekst) {											  	
		$("body").append("<p id='vareikurv'>"+ tekst +"</p>");
		$("#vareikurv").fadeIn("fast").delay(1500).fadeOut('slow'); 
};



	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);
	};




// starting the script on page load
$(document).ready(function(){
	tooltip();
});

	// 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;
		document.write('<div id="thaBasket"><form id="previewBasket" action="_SELF" method="post"><fieldset><select name="item"><option value="kurv">Kurv</option>');
		itemlist = 0;
		thisitem = 1;
		itemstart = 1;
		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;
				subtotal = subtotal + itemtotal;
				itemlist=itemlist+1;
				document.write('<option value="'+theitem+'">'+thequantity+' x '+theitem+'</option>');

			} 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);
				thisitem++;
				itemstart=i+1;
			}
		}
		document.write('</select></fieldset></form><div><a href="index.asp?content=kurv" title="Klik her for at se indkøbskurven">I alt kr: '+alterError(subtotal)+' ex. moms</a></div></div>');
	};

	

	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)+']';
				}
			}
		}
	};

	function addItem(newItem, newPrice, newQuantity,  newNumber, newOption, newAntal, newPrisen2) {
		if (newQuantity >= 100000) {
		rc = alert('Forkert antal!');
		} else {
			if (newQuantity >= 1) {
		
				//location = "index.asp?"+window.location.search.substring(1);
				setTimeout(function() {location = "index.asp?"+window.location.search.substring(1);} , 2500); // delays 2.5 sec
				index = document.cookie.indexOf("TheBasket");
				countbegin = (document.cookie.indexOf("=", index) + 1);
				countend = document.cookie.indexOf(";", index);
				if (countend == -1) {
					countend = document.cookie.length;
				}
				
				if (newQuantity >= newAntal) {
					newPrice=newPrisen2;
					document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newOption+"|"+newAntal+"|"+newPrisen2+"|"+newQuantity+"]";
				} else {
					document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newOption+"|"+newAntal+"|"+newPrisen2+"|"+newQuantity+"]";
				}
				showMSG("Tilføjer : " + newQuantity + " x " + newItem + " til varekurv.");
			}
		}
	};

	function resetShoppingBasket() {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
	};
	
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
} 

function defaultText(thefield){
	if (thefield.value=="")
	thefield.value = thefield.defaultValue
} 
