// INIT SETTINGS -----------------------------------------

var name = 'egolive.cookie'
var myString = getCookie();

// DETTE ER INDSAT 22-09-08
if(myString){
	if (myString != '') {
		myString = getCookie();
	}
	else {
		myString = 'id=0|#|amount=0|#|price=0|#|name=0';
		setCookie(myString);
	}
} else {
	myString = 'id=0|#|amount=0|#|price=0|#|name=0';
	setCookie(myString);
}
// HERTIL

function setCookie(value)
{
    document.cookie= name + "=" + escape(value);
}

function getCookie()
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie () {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	showbasket();
}

function addbasket(id,amount,price,name) {

	var myArray
		myArray = myString.split("<split>");

	var i,myItem,myId, myAmount, myPrice, myName, ItemExist

	ItemExist = false;

	for (i = 0; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];

		//Hvis produktet findes i kurven læg antal til
		if (id == myId)
		{
			var myNewAmount = 0
			myNewAmount = parseInt(myAmount) + parseInt(amount);

			myString = myString.replace('id='+myId+'|#|amount='+myAmount, 'id='+myId+'|#|amount='+myNewAmount);
			
			setCookie(myString);

			ItemExist = true;
			break;
		}
	}

	if (!ItemExist) {
		myString = myString +'<split>id='+id+'|#|amount='+amount+'|#|price='+price+'|#|name='+name;
		setCookie(myString);
	}

	myString = getCookie();
	
	calculatebasket(myString);
	
	alert(amount +' stk. ' +name+ ' er tilføjet til indkøbskurven');
}

function updatebasket(id,amount) {

	var myArray
		myArray = myString.split("<split>");

	var i,myItem,myId, myAmount, myName, ItemExist

	ItemExist = false;	

	for (i = 0; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];

		if (id == myId) {

			myNewAmount = amount;
			
				if (myNewAmount == 0) {
					myString = myString.replace('<split>id='+myId+'|#|amount='+myAmount+'|#|price='+ myPrice +'|#|name='+myName, '');
					setCookie(myString);
				}
				else {
					myString = myString.replace('id='+myId+'|#|amount='+myAmount, 'id='+myId+'|#|amount='+myNewAmount);
					setCookie(myString);
				}

			break;
		}
	}
renderhiddenfields()
}

function calculatebasket(myString) {

	myArray = myString.split("<split>");

	var totalAmount, totalPrice, Product
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	Product = '';
	
	for (i = 1; i <= myArray.length-1; i++)
	{
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
		totalAmount = totalAmount + parseInt(myAmount);
		subPrice = parseInt(myAmount) * parseFloat(myPrice)
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		
		Product = Product + '<tr>'
		Product = Product + '  <td valign="top" nowrap style="border-bottom: 1px solid #ffffff; border-left: 1px solid #ffffff; border-right: 1px solid #ffffff">'+myAmount+' stk.</td>'
		Product = Product + '  <td style="border-bottom: 1px solid #ffffff; padding-left: 5px">'+myName+'</td>'
		Product = Product + '  <td valign="top" nowrap align="right" style="border-bottom: 1px solid #ffffff; padding-right: 10px">DKK '+formatNumber(subPrice)+'</td>'
		Product = Product + '</tr>'
	}

	if (Product=='') {
		Product = '<div class="BreadMediumStdColor" style="margin-left:10px;width:193px;padding-top:5px;padding-bottom:5px;"><i>Indkøbskurven er tom!</i></div>' 
	}
	else {
		Product = '<table class="BreadMediumStdColor" width="100%" cellpadding="0" cellspacing="0">'+Product+'</table>'
	}

	if (document.getElementById('Products')) {
		document.getElementById('Products').innerHTML = Product;
	}		

	if (document.getElementById('basketAmount')) {
		document.getElementById('basketAmount').innerHTML = totalAmount;
	}
	if (document.getElementById('basketPrice')) {
		document.getElementById('basketPrice').innerHTML = 'DKK ' + formatNumber(totalPrice);
	}
}

function showbasket()
{
	calculatebasket(myString);
}

function showfullbasket()
{
	var BasketHeaderColor = '#ffffff';
	var ItemColor = '#ffffff';
	var subTotalColor = '#ffffff';
	var TotalColor = '#ffffff';
	var BorderColor = '#757474';
	var ItemNameWidth = '31%';
	var UnitPriceWidth = '23%';
	var AmountWidth = '23%';
	var TotalPriceWidth = '23%';
	
	myArray = myString.split("<split>");

	var totalAmount, price, totalPrice, moms, strOut
	price = 0;
	moms = 0;
	totalAmount = 0;
	totalPrice = 0.00;
	strOut = '';
	
	var TableWidth = 444
		
	strOut = strOut + '<script type="text/javascript">'
	strOut = strOut + 'function checkamount(id,value){'
	strOut = strOut + 'if(isNaN(value)){alert("Ugyldigt tal"); document.location=document.location; return false;}'
	strOut = strOut + '  if(value.length>1 && value.charAt(0)==0){alert("Ugyldigt tal"); document.location=document.location; return false;}'
	strOut = strOut + '	 else{updatebasket(id,value); document.location=document.location}'
	strOut = strOut + '}'
	strOut = strOut + '</script>'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="2" cellspacing="1">';
	strOut = strOut + '<tr bgcolor="'+BasketHeaderColor+'"><td width="'+ItemNameWidth+'"><b>VARENAVN</b></td><td width="'+UnitPriceWidth+'" align="right"><b>STYKPRIS</b></td><td width="'+AmountWidth+'" align="right"><b>ANTAL</b></td><td width="'+TotalPriceWidth+'" align="right"><b>SAMLET</b></td></tr>';
	
	for (i = 1; i <= myArray.length-1; i++)
	{
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
		price = parseInt(myAmount) * parseFloat(myPrice);
		totalAmount = totalAmount + parseInt(myAmount);
		totalPrice = totalPrice + (parseInt(myAmount) * parseFloat(myPrice));
		moms = totalPrice * 0.2;
		
		strOut = strOut + '<tr bgcolor="'+ItemColor+'"><td>' + myName + '</td><td align="right">' + formatNumber(myPrice) + '</td><td align="right"><input maxlength="4" style="font-size: 10px; text-align: center; width: 32px; border: 1px solid #D0D0D0" onblur="checkamount('+myId+',this.value);" type="text" value="' + myAmount + '"></td><td nowrap align="right">' + formatNumber(price) + '</td></tr>';
	}
	
	strOut = strOut + '</table>';
	
	var strGebyrTxt
	strGebyrTxt = 'Bemærk! Gratis ekspedition ved køb over DKK 500,00. Såfremt totalbeløbet på din ordre er under DKK 500,00 vil der blive tillagt et ekspeditionsgebyr på kr. 60,00.'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="5" cellspacing="0">';
	strOut = strOut + '<tr bgcolor="'+subTotalColor+'"><td style="font-weight: bold; border-left: 0px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">TOTAL</td><td style="font-weight: bold; border-right: 0px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(totalPrice) + '</td></tr>';
	strOut = strOut + '<tr bgcolor='+TotalColor+'><td style="font-weight: bold; border-left: 0px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">HERAF MOMS</td><td style="font-weight: bold; border-right: 0px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(moms) + '</td></tr>';
	strOut = strOut + '<tr><td colspan="2" style="border-left: 0px solid '+BorderColor+'; border-right: 0px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">' + strGebyrTxt + '</td></tr>';
	strOut = strOut + '</table>';

	strOut = strOut +'<table style="padding-top: 15px;" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';		
	strOut = strOut + '<tr><td align="right"><input class="WebshopButton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="history.back()" type="button" value="Tilbage">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="WebshopButton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" type="button" value="Opdater" onclick=" document.location=document.location">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input name=""btnNext"" class="WebshopButton" onfocus="this.blur()" onMouseOver="this.style.cursor=\'pointer\';" onclick="renderhiddenfields();fmOrderForm.submit()" type="button" value="Fortsæt"></td></tr>';
	strOut = strOut + '</table>';
	
	if (totalAmount == 0)
	{
		strOut = '';
		strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="2" cellspacing="0">';
		strOut = strOut + '  <tr><td style="border: 1px solid #cccccc;">Indkøbskurven er tom</td></tr>';
		strOut = strOut + '</table>';
		strOut = strOut + '<table style="padding-top: 15px;" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';		
		strOut = strOut + '  <tr><td align="right"><input class="WebshopButton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="history.back()" type="button" value="Tilbage"></td></tr>';
		strOut = strOut + '</table>';

		document.write (strOut);
	}
	else {	
		document.write (strOut);
	}
	
}

function renderhiddenfields()
{
	myArray = myString.split("<split>");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	strOut = '';	

	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|#|");
		myId = myItem[0].split("=")[1];
		myAmount = myItem[1].split("=")[1];
		myPrice = myItem[2].split("=")[1];
		myName = myItem[3].split("=")[1];
		
	strOut = strOut + '<input type="hidden" name="doc_'+myId+'" value="'+myAmount+'">';
	strOut = strOut + '<input type="hidden" name="price_'+myId+'" value="'+formatNumber(myPrice)+'">';
	strOut = strOut + '<input type="hidden" name="fDocuments" value="'+myId+'">';

	}

	document.getElementById('HiddenFields').innerHTML = strOut;
}

function formatNumber(Number)
{

//	Uses NumberFormat.js file
//	For more options visit http://www.mredkj.com/javascript/numberFormatPage2.html (STT)

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(Number);
	num.setPlaces('2', false);
	num.setCurrencyValue('');
	num.setCurrency(true);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, '.', ',');
	
	return num.toFormatted()
}