function setCookie(name, value)
{
    document.cookie= name + "=" + escape(value);
}

function getCookie(name)
{
    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 (name) {
	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 addproducttobasket(id,amount,price,name){

	var size = document.getElementById('size'+id).value;
	var color = document.getElementById('color'+id).value;

	if( size =='' ) { alert('Vælg venligst en størrelse.') }
	else if( color=='' ) { alert('Vælg venligst en farve.') }
	else { addbasket(id,amount,price,name + ', str. ' + size + ', ' + color) }

}

function addbasket(id,amount,price,name) {

	var myString = getCookie('myString');

	if (myString != null) {
		myString = getCookie('myString');
	}
	else {
	myString = 'id_0|amount_0|price_0|name_0';
	}

	var myArray
		myArray = myString.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("_");
		myAmount = myItem[1].split("_");
		myPrice = myItem[2].split("_");
		myName = myItem[3].split("_");

		if (id == myId[1] && name == myName[1]) { //Hvis produktet findes i kurven læg antal til
			var myNewAmount = 0
			myNewAmount = parseInt(myAmount[1]) + parseInt(amount);

			myString = myString.replace('id_'+myId[1]+'|amount_'+myAmount[1], 'id_'+myId[1]+'|amount_'+myNewAmount);
			
			setCookie('myString', myString);

			ItemExist = true;
			break;
		}
	}

	if (!ItemExist) {
		myString = myString +';id_'+id+'|amount_'+amount+'|price_'+price+'|name_'+name;
		setCookie('myString', myString);
	}

	myString = getCookie('myString');
	
	calculatebasket(myString);
	
	alert(amount +' stk. ' +name+ ' er tilføjet til indkøbskurven');
}

function updatebasket(id,amount,name) {

	var myString = getCookie('myString');

	var myArray
		myArray = myString.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("_");
		myAmount = myItem[1].split("_");
		myPrice = myItem[2].split("_");
		myName = myItem[3].split("_");

		if ( id == myId[1] && name == myName[1] ) {

			myNewAmount = amount;
			
				if (myNewAmount == 0) {
					myString = myString.replace(';id_'+myId[1]+'|amount_'+myAmount[1]+'|price_'+ myPrice[1] +'|name_'+myName[1], '');
					setCookie('myString', myString);
				}
				else {
					myString = myString.replace('id_'+myId[1]+'|amount_'+myAmount[1], 'id_'+myId[1]+'|amount_'+myNewAmount);
					setCookie('myString', myString);
				}

			break;
		}
	}
renderhiddenfields()
}

function calculatebasket(myString) {

	myArray = myString.split(";");

	var totalAmount, totalPrice
	totalAmount = 0;
	totalPrice = 0.00;
	myPrice = 0;
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|");
		myId = myItem[0].split("_");
		myAmount = myItem[1].split("_");
		myPrice = myItem[2].split("_");
		
		totalAmount = totalAmount + parseInt(myAmount[1]);
		totalPrice = totalPrice + (parseInt(myAmount[1]) * parseFloat(myPrice[1]));
	}
	
	if (document.getElementById('basketAmount')) {
		document.getElementById('basketAmount').innerHTML = totalAmount;
	}
	if (document.getElementById('basketPrice')) {
		document.getElementById('basketPrice').innerHTML = formatNumber(totalPrice);
	}
}

function showbasket() {

	var myString = getCookie('myString');

	if (myString != null) {
		myString = getCookie('myString');
	}
	else {
	myString = 'id_0|amount_0|price_0|name_0';
	}

	calculatebasket(myString);

}

function showfullbasket() {

	var myString = getCookie('myString');

	if (myString != null) {
		myString = getCookie('myString');
	}
	else {
	myString = 'id_0|amount_0|price_0|name_0';
	}

	myArray = myString.split(";");

	var totalAmount, price, totalPrice, moms, strOut
	price = 0;
	moms = 0;
	totalAmount = 0;
	totalPrice = 0.00;
	strOut = '';
		
	strOut = strOut + '<script>'
	strOut = strOut + 'function checkamount(id,value,name){'
	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,name); document.location=document.location}'
	strOut = strOut + '}'
	strOut = strOut + '</script>'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="100%" bgcolor="#cccccc" border="0" cellpadding="3" cellspacing="1">';
	strOut = strOut + '<tr bgcolor="#333333"><td>VARENAVN</td><td width="70" align="right">STYKPRIS</td><td width="53" align="right">ANTAL</td><td width="88" align="right">SAMLET</td></tr>';
	
	for (i = 1; i <= myArray.length-1; i++) {
		myItem = myArray[i].split("|");
		myId = myItem[0].split("_");
		myAmount = myItem[1].split("_");
		myPrice = myItem[2].split("_");
		myName = myItem[3].split("_");
		
		price = parseInt(myAmount[1]) * parseFloat(myPrice[1]);
		totalAmount = totalAmount + parseInt(myAmount[1]);
		totalPrice = totalPrice + (parseInt(myAmount[1]) * parseFloat(myPrice[1]));
		moms = totalPrice * 0.2;
		
		strOut = strOut + '<tr bgcolor="#000000"><td>' + myName[1] + '</td><td align="right">' + formatNumber(myPrice[1]) + '</td><td align="right"><input maxlength="3" style="font-size: 10px; text-align: center; width: 18px; border: 1px solid #D0D0D0" onblur="checkamount('+myId[1]+',this.value,\''+myName[1]+'\');" type="text" value="' + myAmount[1] + '"></td><td nowrap align="right">' + formatNumber(price) + '</td></tr>';
	}
	
	strOut = strOut + '</table>';
	
	var strGebyrTxt
//	strGebyrTxt = 'Ekspeditionsgebyr (kr. 10,-) og forsendelsesgebyr (kr. 15,-) vil blive lagt til din ordre.'
	
	strOut = strOut + '<table class="BreadMediumStdColor" width="100%" border="0" cellpadding="4" cellspacing="0">';
	strOut = strOut + '<tr bgcolor="#333333"><td style="font-weight: bold; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc">I ALT:</td><td style="font-weight: bold; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc" align="right">DKK ' + formatNumber(totalPrice) + '</td></tr>';
	strOut = strOut + '<tr><td style="font-weight: bold; border-left: 1px solid #cccccc; border-bottom: 1px solid #cccccc">HERAF MOMS:</td><td style="font-weight: bold; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc" align="right">DKK ' + formatNumber(moms) + '</td></tr>';
//	strOut = strOut + '<tr><td colspan="2" style="border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-bottom: 1px solid #cccccc">' + strGebyrTxt + '</td></tr>';
	strOut = strOut + '</table>';

	strOut = strOut +'<table style="padding-top: 15px;" width="100%" 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="Opdatér" onclick=" document.location=document.location">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input 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 = '<table class="BreadMediumStdColor" width="100%" border="0" cellpadding="4" cellspacing="0">';		
		strOut = strOut + '<tr><td style="border: 1px solid #cccccc;">Din indkøbskurv er tom!</td></tr>';
		strOut = strOut + '</table>';

		strOut = strOut +'<table style="padding-top: 15px;" width="100%" 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() {

	var myString = getCookie('myString');

	if (myString != null) {
		myString = getCookie('myString');
	}
	else {
	myString = 'id_0|amount_0|price_0|name_0';
	}

	myArray = myString.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("_");
		myAmount = myItem[1].split("_");
		myPrice = myItem[2].split("_");
		myName = myItem[3].split("_");
		
		index = i+'_'+myId[1];
		
		strOut = strOut + '<input type="hidden" name="doc_'+index+'" value="'+myAmount[1]+'">';
		strOut = strOut + '<input type="hidden" name="price_'+index+'" value="'+formatNumber(myPrice[1])+'">';
		strOut = strOut + '<input type="hidden" name="productname_'+index+'" value="'+myName[1]+'">';
		strOut = strOut + '<input type="hidden" name="fDocuments" value="'+index+'">';

	}

	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()
}