// INIT SETTINGS -----------------------------------------

var name = 'egolive.shopcookie';
var myString = getShopCookie();

function addproducttobasket(me){
	
	var name = me.name.value;
	var quantity = me.quantity.value;
	var price = me.price.value;
	var size = me.size.value;
	var color = me.color.value;
	var url = me.url.value;
	
	if(color==''){alert('Vælg venligst en farve.')}
	else if(size==''){alert('Vælg venligst en størrelse.')}
	else{
		name = name + ', ' + color.split("_")[1] + ', ' + size.split("_")[1]
		itemid = size.split("_")[0]
		addbasket(itemid,name,quantity,price,url);
	}
}

function setshopreturnurl(){
	var objCookieMdl = new CookieModule();
	var url = document.location;
	objCookieMdl.setValueByCookieAndKeyAndValue('User','ShopReturnUrl',url );
}

function getshopreturnurl(){
	var objCookieMdl = new CookieModule();
	return objCookieMdl.getValueByCookieAndKey('User','ShopReturnUrl');
}

function setShopCookie(value)
{
    document.cookie = name + "=" + escape(value);
}

function getShopCookie()
{

    var dc = document.cookie.split('; ');
    var prefix = name + "=";
    var initstring = '';
    
    for (i = 0; i <= dc.length-1; i++) {
		if(dc[i].indexOf( prefix ) != -1) {
			initstring = unescape(dc[i].replace(name+'=',''));
			break;
		}
    }
    
	return initstring;
 
}

function deleteShopCookie(){
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getShopCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function adddeliverycost(value){

	var ItemExist = false;
	var productid = 0;
	var name = 'Fragt';
	var quantity = 1;
	var price = value;
	var url = '';

	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];

			//Hvis produktet findes i kurven læg antal til
			if (productid == myId)
			{
				myString = myString.replace(myArray[i],'id€'+productid+'|€|name€'+name+'|€|quantity€'+quantity+'|€|price€'+price+'|€|url€'+url);
				setShopCookie(myString);
				ItemExist = true;
				break;
			}
		}
	
	}

	if (!ItemExist) {

		myString += '<split>id€'+productid+'|€|name€'+name+'|€|quantity€'+quantity+'|€|price€'+price+'|€|url€'+url;
		setShopCookie(myString);
	}


}

function addbasket(productid,name,quantity,price,url) {

	var ItemExist = false;

	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];

			//Hvis produktet findes i kurven læg antal til
			if (productid == myId && myName == name)
			{
			
				newquantity = parseInt(myQuantity) + parseInt(quantity);
				myString = myString.replace(myArray[i],'id€'+productid+'|€|name€'+name+'|€|quantity€'+newquantity+'|€|price€'+price+'|€|url€'+url);
				setShopCookie(myString);
				ItemExist = true;
				break;
			}
		}
	
	}

	if (!ItemExist) {

		myString += '<split>id€'+productid+'|€|name€'+name+'|€|quantity€'+quantity+'|€|price€'+price+'|€|url€'+url;
		setShopCookie(myString);
	}

	alert('Produktet er lagt i din kurv.');

//	addbasketalert();
	
	showbasket();
}

function updatebasket(productid,quantity,name) {

	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];

			if (productid==myId && myName==name) {
				newquantity = quantity;
			
				if (newquantity == 0) {
					myString = myString.replace('<split>'+myArray[i],'');
					setShopCookie(myString);
				}
				else {
					myString = myString.replace(myArray[i],'id€'+productid+'|€|name€'+myName+'|€|quantity€'+newquantity+'|€|price€'+myPrice+'|€|url€'+myUrl);
					setShopCookie(myString);
				}

				break;
			}
			
		}
	
	}
	
}

function showbasket() {

	var totalQuantity = 0;
	var totalPrice = 0.00;
	var myPrice = 0;
	var Product = '';
	
	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];
			
			if(myId>0){ //tager ikke fragten med

				totalQuantity += parseInt(myQuantity);
				subPrice = parseInt(myQuantity) * parseFloat(myPrice)
				totalPrice += (parseInt(myQuantity) * parseFloat(myPrice));
			}
		
		}
	
	}	

	if (totalPrice == 0) {
		Product += '<div style="padding-left:13px;padding-top:56px;">';
		Product += '  <div style="width:61px;" class="left">0 varer</div>';
		Product += '  <div class="left">DKK 0,00</div>';
		Product += '  <div class="clear"><img src="/files/system/gfx/1x1px.gif" alt="" /></div>';
		Product += '</div>';
	}
	else {
		Product += '<div style="padding-left:13px;padding-top:56px;">';
		Product += '  <div style="width:61px;" class="left">' + totalQuantity + ' varer</div>';
		Product += '  <div class="left">DKK ' + formatNumber(totalPrice) + '</div>';
		Product += '  <div class="clear"><img src="/files/system/gfx/1x1px.gif" alt="" /></div>';
		Product += '</div>';
	}

	if (document.getElementById('productsinbasket')) {
		document.getElementById('productsinbasket').innerHTML = Product;
	}		

}

function showfullbasket()
{
	var BasketHeaderColor = '#ffffff';
	var ItemColor = '#ffffff';
	var subTotalColor = '#ffffff';
	var TotalColor = '#ffffff';
	var BorderColor = '#cccccc';
	var ItemNameWidth = '55%';
	var UnitPriceWidth = '15%';
	var AmountWidth = '15%';
	var TotalPriceWidth = '15%';
	var totalQuantity, price, totalPrice, moms, strOut
	
	price = 0;
	moms = 0;
	totalQuantity = 0;
	totalPrice = 0.00;
	strOut = '';
	
	var TableWidth = "444";
		
	strOut = strOut + '<script type="text/javascript">';
	strOut = strOut + 'function checkamount(id,value,name){';
	strOut = strOut + 'if(isNaN(value)){alert("Quantity is not a number"); document.location=document.location; return false;}';
	strOut = strOut + '  if(value.length>1 && value.charAt(0)==0){alert("Quantity is not a number"); document.location=document.location; return false;}';
	strOut = strOut + '	 else{updatebasket(id,value,name); document.location=document.location; return false;}';
	strOut = strOut + '}';
	strOut = strOut + '</script>';
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';
	strOut = strOut + '<tr height="26" bgcolor="'+BasketHeaderColor+'">';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+ItemNameWidth+'"><b>Varenavn</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+UnitPriceWidth+'" align="right"><b>Enhedspris</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+AmountWidth+'" align="right"><b>Antal</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+TotalPriceWidth+'" align="right"><b>Total</b></td>';
	strOut = strOut + '</tr>';

	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];
			
			if(myId>0){ //tager ikke fragten med

				price = parseInt(myQuantity) * parseFloat(myPrice);
				totalQuantity += parseInt(myQuantity);
				totalPrice += (parseInt(myQuantity) * parseFloat(myPrice));
				moms = totalPrice * 0.2;
		
				strOut = strOut + '<tr height="26" bgcolor="'+ItemColor+'"><td><a href="' + myUrl + '">' + myName + '</a></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,\''+myName+'\');" type="text" value="' + myQuantity + '"></td><td nowrap align="right">' + formatNumber(price) + '</td></tr>';
			}
		}
	
	}	
	
	strOut = strOut + '</table>';
	
	strOut += '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';
	strOut += '<tr height="22" bgcolor="'+subTotalColor+'"><td style="font-weight: bold; border-top: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">TOTAL</td><td style="font-weight: bold; border-top: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(totalPrice) + '</td></tr>';
//	strOut += '<tr height="22" 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-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(moms) + '</td></tr>';
	strOut += '</table><br/>';
	
	strOut += '<div style="width: '+TableWidth+'px">';
	strOut += '<div class="left"><input class="WebshopButton"  onclick="document.location=getshopreturnurl()" type="button" value="Køb mere"></div>';
	strOut += '<div class="right"><input class="WebshopButton" onclick="document.fmOrderForm.submit()" type="button" value="Næste trin"></div>';
	strOut += '<div class="right" style="padding-right: 25px"><input class="WebshopButton" type="button" value="Opdater"></div>';
	strOut += '<div class="right" style="padding-right: 25px"><input class="WebshopButton" onclick="deleteShopCookie();document.location=document.location" type="button" value="Tøm kurv"></div>';
	strOut += '</div>';
	

	if (totalQuantity == 0)
	{
		strOut = '';
		strOut += '<h1>Din kurv er tom.</h1>';
		strOut += '<div style="margin-top: 10px"><input class="shopbutton" onMouseOver="this.style.cursor=\'pointer\';" onfocus="this.blur()" onclick="document.location=getshopreturnurl()" type="button" value="Fortsæt køb"></div>';

		document.write (strOut);
	}
	else {	
		document.write (strOut);
	}
	
}

function showacceptfullbasket()
{
	var BasketHeaderColor = '#ffffff';
	var ItemColor = '#ffffff';
	var subTotalColor = '#ffffff';
	var TotalColor = '#ffffff';
	var BorderColor = '#cccccc';
	var ItemNameWidth = '55%';
	var UnitPriceWidth = '15%';
	var AmountWidth = '15%';
	var TotalPriceWidth = '15%';
	
	var totalQuantity, price, totalPrice, moms, strOut
	price = 0;
	moms = 0;
	totalQuantity = 0;
	totalPrice = 0.00;
	strOut = '';
	
	var TableWidth = "420";
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';
	strOut = strOut + '<tr height="26" bgcolor="'+BasketHeaderColor+'">';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+ItemNameWidth+'"><b>Varenavn</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+UnitPriceWidth+'" align="right"><b>Enhedspris</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+AmountWidth+'" align="right"><b>Antal</b></td>';
	strOut = strOut + '<td style="border-bottom: 1px solid '+BorderColor+'" width="'+TotalPriceWidth+'" align="right"><b>Total</b></td>';
	strOut = strOut + '</tr>';

	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];

			price = parseInt(myQuantity) * parseFloat(myPrice);
			totalQuantity += parseInt(myQuantity);
			totalPrice += (parseInt(myQuantity) * parseFloat(myPrice));
			moms = totalPrice * 0.2;
		
			if (myUrl!=''){
				strOut = strOut + '<tr height="26" bgcolor="'+ItemColor+'"><td><a href="' + myUrl + '">' + myName + '</a></td><td align="right">' + formatNumber(myPrice) + '</td><td align="right">' + myQuantity + '</td><td nowrap align="right">' + formatNumber(price) + '</td></tr>';
			} else {
				strOut = strOut + '<tr height="26" bgcolor="'+ItemColor+'"><td>' + myName + '</td><td align="right">' + formatNumber(myPrice) + '</td><td align="right">' + myQuantity + '</td><td nowrap align="right">' + formatNumber(price) + '</td></tr>';
			}

		}
	
	}	
	
	strOut = strOut + '</table>';
	
	strOut += '<table class="BreadMediumStdColor" width="'+TableWidth+'" border="0" cellpadding="0" cellspacing="0">';
	strOut += '<tr height="22" bgcolor="'+subTotalColor+'"><td style="font-weight: bold; border-top: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'">TOTAL</td><td style="font-weight: bold; border-top: 1px solid '+BorderColor+'; border-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(totalPrice) + '</td></tr>';
//	strOut += '<tr height="22" 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-bottom: 1px solid '+BorderColor+'" align="right">DKK ' + formatNumber(moms) + '</td></tr>';
	strOut += '</table><br/>';
	

	document.write (strOut);
	
}

function renderhiddenfields()
{

	var strOut = '';
	
	if (myString != '') {

		var myArray = myString.split("<split>");

		for (i = 1; i < myArray.length; i++) {

			myItem = myArray[i].split("|€|");
			
			myId = myItem[0].split("€")[1];
			myName = myItem[1].split("€")[1];
			myQuantity = myItem[2].split("€")[1];
			myPrice = myItem[3].split("€")[1];
			myUrl = myItem[4].split("€")[1];
			
			index = myId

			strOut += '<input type="hidden" name="items" value="'+index+'">';
			strOut += '<input type="hidden" name="name_'+index+'" value="'+myName+'">';
			strOut += '<input type="hidden" name="quantity_'+index+'" value="'+myQuantity+'">';
			strOut += '<input type="hidden" name="unitprice_'+index+'" value="'+myPrice+'">';
			strOut += '<input type="hidden" name="url_'+index+'" value="'+myUrl+'">';

		}
	
	}		

	document.write (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()
}